-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add generic oidc account (#288)
* feat: Add generic oidc account * chore: Add generic oidc account to account type * chore: remove subject keys which are not required * fix: add AccountTypeGenericOIDCAccount to account resource creation
- Loading branch information
1 parent
d53c52f
commit 94ae598
Showing
7 changed files
with
134 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package accounts | ||
|
||
import ( | ||
"github.com/OctopusDeploy/go-octopusdeploy/v2/internal" | ||
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/validation" | ||
"github.com/go-playground/validator/v10" | ||
"github.com/go-playground/validator/v10/non-standard/validators" | ||
) | ||
|
||
// GenericOIDCAccount represents a Generic OIDC account. | ||
type GenericOIDCAccount struct { | ||
Audience string `json:"Audience,omitempty"` | ||
DeploymentSubjectKeys []string `json:"DeploymentSubjectKeys,omitempty" validate:"omitempty,dive,oneof=space environment project tenant runbook account type'"` | ||
|
||
account | ||
} | ||
|
||
// NewGenericOIDCAccount creates and initializes a Generic OIDC account. | ||
func NewGenericOIDCAccount(name string) (*GenericOIDCAccount, error) { | ||
if internal.IsEmpty(name) { | ||
return nil, internal.CreateRequiredParameterIsEmptyOrNilError("name") | ||
} | ||
|
||
account := GenericOIDCAccount{ | ||
account: *newAccount(name, AccountTypeGenericOIDCAccount), | ||
} | ||
|
||
// validate to ensure that all expectations are met | ||
err := account.Validate() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &account, nil | ||
} | ||
|
||
// Validate checks the state of this account and returns an error if invalid. | ||
func (a *GenericOIDCAccount) Validate() error { | ||
v := validator.New() | ||
err := v.RegisterValidation("notblank", validators.NotBlank) | ||
if err != nil { | ||
return err | ||
} | ||
err = v.RegisterValidation("notall", validation.NotAll) | ||
if err != nil { | ||
return err | ||
} | ||
return v.Struct(a) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package accounts | ||
|
||
import ( | ||
"github.com/OctopusDeploy/go-octopusdeploy/v2/internal" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestGenericOIDCAccount(t *testing.T) { | ||
name := internal.GetRandomName() | ||
audience := "api://default" | ||
deploymentSubjectKeys := []string{"space", "project", "tenant", "environment"} | ||
invalidDeploymentSubjectKeys := []string{"space", "target"} | ||
|
||
testCases := []struct { | ||
TestName string | ||
IsError bool | ||
Name string | ||
Audience string | ||
DeploymentSubjectKeys []string | ||
}{ | ||
{"Valid", false, name, audience, deploymentSubjectKeys}, | ||
{"EmptyName", true, "", audience, deploymentSubjectKeys}, | ||
{"NilSubjectKeys", false, name, "", nil}, | ||
{"InvalidDeploymentSubjectKeys", true, name, "", invalidDeploymentSubjectKeys}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.TestName, func(t *testing.T) { | ||
genericOIDCAccount := &GenericOIDCAccount{ | ||
Audience: tc.Audience, | ||
DeploymentSubjectKeys: tc.DeploymentSubjectKeys, | ||
} | ||
genericOIDCAccount.AccountType = AccountTypeGenericOIDCAccount | ||
genericOIDCAccount.Name = tc.Name | ||
if tc.IsError { | ||
require.Error(t, genericOIDCAccount.Validate()) | ||
} else { | ||
require.NoError(t, genericOIDCAccount.Validate()) | ||
|
||
require.Equal(t, AccountTypeGenericOIDCAccount, genericOIDCAccount.GetAccountType()) | ||
require.Equal(t, tc.Name, genericOIDCAccount.GetName()) | ||
} | ||
genericOIDCAccount.SetName(tc.Name) | ||
if tc.IsError { | ||
require.Error(t, genericOIDCAccount.Validate()) | ||
} else { | ||
require.NoError(t, genericOIDCAccount.Validate()) | ||
require.Equal(t, tc.Name, genericOIDCAccount.GetName()) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestGenericOIDCAccountNew(t *testing.T) { | ||
name := internal.GetRandomName() | ||
|
||
account, err := NewGenericOIDCAccount(name) | ||
|
||
require.NotNil(t, account) | ||
require.NoError(t, err) | ||
require.NoError(t, account.Validate()) | ||
} |