Skip to content

Commit

Permalink
more
Browse files Browse the repository at this point in the history
  • Loading branch information
veochen-octopus committed Nov 14, 2023
1 parent 6b43dda commit e7cf08d
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 11 deletions.
4 changes: 4 additions & 0 deletions pkg/accounts/account_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ type AccountResource struct {
TenantTags []string `json:"TenantTags,omitempty"`
Token *core.SensitiveValue `json:"Token,omitempty"`
Username string `json:"Username,omitempty"`
Audience string `json:"Audience,omitempty"`
DeploymentSubjectKeys []string `json:"DeploymentSubjectKeys,omitempty"`
HealthCheckSubjectKeys []string `json:"HealthCheckSubjectKeys,omitempty"`
AccountTestSubjectKeys []string `json:"AccountTestSubjectKeys,omitempty"`

resources.Resource
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/accounts/account_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ func TestAccountServiceUpdateWithEmptyAccount(t *testing.T) {
require.Error(t, err)
require.Nil(t, account)

account, err = service.Update(&AzureOIDCAccount{})
require.Error(t, err)
require.Nil(t, account)

account, err = service.Update(&AzureSubscriptionAccount{})
require.Error(t, err)
require.Nil(t, account)
Expand Down
25 changes: 25 additions & 0 deletions pkg/accounts/account_utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ func ToAccount(accountResource *AccountResource) (IAccount, error) {
azureServicePrincipalAccount.AzureEnvironment = accountResource.AzureEnvironment
azureServicePrincipalAccount.ResourceManagerEndpoint = accountResource.ResourceManagerEndpoint
account = azureServicePrincipalAccount
case AccountTypeAzureOIDC:
azureOIDCAccount, err := NewAzureOIDCAccount(accountResource.GetName(), *accountResource.SubscriptionID, *accountResource.TenantID, *accountResource.ApplicationID)
if err != nil {
return nil, err
}
azureOIDCAccount.AuthenticationEndpoint = accountResource.AuthenticationEndpoint
azureOIDCAccount.AzureEnvironment = accountResource.AzureEnvironment
azureOIDCAccount.ResourceManagerEndpoint = accountResource.ResourceManagerEndpoint
azureOIDCAccount.Audience = accountResource.Audience
azureOIDCAccount.DeploymentSubjectKeys = accountResource.DeploymentSubjectKeys
azureOIDCAccount.AccountTestSubjectKeys = accountResource.AccountTestSubjectKeys
azureOIDCAccount.HealthCheckSubjectKeys = accountResource.HealthCheckSubjectKeys
account = azureOIDCAccount
case AccountTypeAzureSubscription:
azureSubscriptionAccount, err := NewAzureSubscriptionAccount(accountResource.GetName(), *accountResource.SubscriptionID)
if err != nil {
Expand Down Expand Up @@ -122,6 +135,18 @@ func ToAccountResource(account IAccount) (*AccountResource, error) {
accountResource.ResourceManagerEndpoint = azureServicePrincipalAccount.ResourceManagerEndpoint
accountResource.SubscriptionID = azureServicePrincipalAccount.SubscriptionID
accountResource.TenantID = azureServicePrincipalAccount.TenantID
case AccountTypeAzureOIDC:
azureOIDCAccount := account.(*AzureOIDCAccount)
accountResource.ApplicationID = azureOIDCAccount.ApplicationID
accountResource.AuthenticationEndpoint = azureOIDCAccount.AuthenticationEndpoint
accountResource.AzureEnvironment = azureOIDCAccount.AzureEnvironment
accountResource.ResourceManagerEndpoint = azureOIDCAccount.ResourceManagerEndpoint
accountResource.SubscriptionID = azureOIDCAccount.SubscriptionID
accountResource.TenantID = azureOIDCAccount.TenantID
accountResource.Audience = azureOIDCAccount.Audience
accountResource.DeploymentSubjectKeys = azureOIDCAccount.DeploymentSubjectKeys
accountResource.AccountTestSubjectKeys = azureOIDCAccount.AccountTestSubjectKeys
accountResource.HealthCheckSubjectKeys = azureOIDCAccount.HealthCheckSubjectKeys
case AccountTypeAzureSubscription:
azureSubscriptionAccount := account.(*AzureSubscriptionAccount)
accountResource.AzureEnvironment = azureSubscriptionAccount.AzureEnvironment
Expand Down
14 changes: 7 additions & 7 deletions pkg/accounts/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,20 @@ func (a *Accounts) UnmarshalJSON(b []byte) error {
return err
}
a.Items = append(a.Items, azureServicePrincipalAccount)
case AccountTypeAzureSubscription:
var azureSubscriptionAccount *AzureSubscriptionAccount
err := json.Unmarshal(*account, &azureSubscriptionAccount)
if err != nil {
return err
}
a.Items = append(a.Items, azureSubscriptionAccount)
case AccountTypeAzureOIDC:
var azureOIDCAccount *AzureOIDCAccount
err := json.Unmarshal(*account, &azureOIDCAccount)
if err != nil {
return err
}
a.Items = append(a.Items, azureOIDCAccount)
case AccountTypeAzureSubscription:
var azureSubscriptionAccount *AzureSubscriptionAccount
err := json.Unmarshal(*account, &azureSubscriptionAccount)
if err != nil {
return err
}
a.Items = append(a.Items, azureSubscriptionAccount)
case AccountTypeGoogleCloudPlatformAccount:
var googleCloudAccount *GoogleCloudPlatformAccount
err := json.Unmarshal(*account, &googleCloudAccount)
Expand Down
7 changes: 4 additions & 3 deletions pkg/accounts/azure/azurewebapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package azure

import (
"fmt"
"strings"

"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/accounts"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/constants"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/services/api"
"strings"
)

type AzureWebApp struct {
Expand All @@ -22,7 +23,7 @@ type AzureWebAppSlot struct {
ResourceGroup string `json:"ResourceGroup,omitempty"`
}

func GetWebSites(client client.Client, account *accounts.AzureServicePrincipalAccount) ([]*AzureWebApp, error) {
func GetWebSites(client client.Client, account accounts.IAccount) ([]*AzureWebApp, error) {
path := account.GetLinks()[constants.LinkWebSites]
if path == "" {
return nil, fmt.Errorf("cannot get websites for account '%s' (%s)", account.GetName(), account.GetID())
Expand All @@ -38,7 +39,7 @@ func GetWebSites(client client.Client, account *accounts.AzureServicePrincipalAc
return items, nil
}

func GetWebSiteSlots(client client.Client, spAccount *accounts.AzureServicePrincipalAccount, app *AzureWebApp) ([]*AzureWebAppSlot, error) {
func GetWebSiteSlots(client client.Client, spAccount accounts.IAccount, app *AzureWebApp) ([]*AzureWebAppSlot, error) {
path := spAccount.GetLinks()[constants.LinkWebSiteSlots]
if path == "" {
return nil, fmt.Errorf("cannot get websites for account '%s' (%s)", spAccount.GetName(), spAccount.GetID())
Expand Down
2 changes: 1 addition & 1 deletion pkg/accounts/azure_oidc_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type AzureOIDCAccount struct {
account
}

// NewAzureOIDCAccount creates and initializes an Azure service principal account.
// NewAzureOIDCAccount creates and initializes an Azure OIDC account.
func NewAzureOIDCAccount(name string, subscriptionID uuid.UUID, tenantID uuid.UUID, applicationID uuid.UUID) (*AzureOIDCAccount, error) {
if internal.IsEmpty(name) {
return nil, internal.CreateRequiredParameterIsEmptyOrNilError("name")
Expand Down
2 changes: 2 additions & 0 deletions pkg/accounts/is_nil.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ func IsNil(i interface{}) bool {
return v == nil
case *AzureServicePrincipalAccount:
return v == nil
case *AzureOIDCAccount:
return v == nil
case *AzureSubscriptionAccount:
return v == nil
case *GoogleCloudPlatformAccount:
Expand Down
41 changes: 41 additions & 0 deletions test/e2e/account_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,43 @@ func CreateTestAzureServicePrincipalAccount(t *testing.T, client *client.Client)
return createdAccount
}

func CreateTestAzureOIDCAccount(t *testing.T, client *client.Client) accounts.IAccount {
if client == nil {
client = getOctopusClient()
}
require.NotNil(t, client)

applicationID := uuid.New()
azureEnvironment := getRandomAzureEnvironment()
name := internal.GetRandomName()
subscriptionID := uuid.New()
tenantID := uuid.New()

account, err := accounts.NewAzureOIDCAccount(name, subscriptionID, tenantID, applicationID)

require.NotNil(t, account)
require.NoError(t, err)
require.NoError(t, account.Validate())

// set Azure environment fields
if !internal.IsEmpty(azureEnvironment.Name) {
account.AzureEnvironment = azureEnvironment.Name
account.AuthenticationEndpoint = azureEnvironment.AuthenticationEndpoint
account.ResourceManagerEndpoint = azureEnvironment.ResourceManagerEndpoint
}

require.NoError(t, account.Validate())

createdAccount, err := client.Accounts.Add(account)
require.NoError(t, err)
require.NotNil(t, createdAccount)
require.NotEmpty(t, createdAccount.GetID())
require.Equal(t, accounts.AccountTypeAzureServicePrincipal, createdAccount.GetAccountType())
require.Equal(t, name, createdAccount.GetName())

return createdAccount
}

func CreateTestAzureSubscriptionAccount(t *testing.T, client *client.Client) accounts.IAccount {
if client == nil {
client = getOctopusClient()
Expand Down Expand Up @@ -316,6 +353,10 @@ func TestAccountServiceAddGetDelete(t *testing.T) {
ValidateAccount(t, azureServicePrincipalAccount)
defer DeleteTestAccount(t, client, azureServicePrincipalAccount)

azureOIDCAccount := CreateTestAzureOIDCAccount(t, client)
ValidateAccount(t, azureOIDCAccount)
defer DeleteTestAccount(t, client, azureOIDCAccount)

azureSubscriptionAccount := CreateTestAzureSubscriptionAccount(t, client)
ValidateAccount(t, azureSubscriptionAccount)
defer DeleteTestAccount(t, client, azureSubscriptionAccount)
Expand Down

0 comments on commit e7cf08d

Please sign in to comment.