diff --git a/harness/nextgen/api_secrets_test.go b/harness/nextgen/api_secrets_test.go new file mode 100644 index 00000000..b2763413 --- /dev/null +++ b/harness/nextgen/api_secrets_test.go @@ -0,0 +1,32 @@ +package nextgen + +import ( + "fmt" + "testing" + + "github.com/harness/harness-go-sdk/harness/utils" + "github.com/stretchr/testify/require" +) + +func TestCreateSecret(t *testing.T) { + c, ctx := getClientWithContext() + + id := fmt.Sprintf("%s_%s", t.Name(), utils.RandStringBytes(5)) + + secret := &Secret{ + Type_: SecretTypes.SecretText, + Name: id, + Identifier: id, + Text: &SecretTextSpec{ + Type_: SecretSpecTypes.Text, + ValueType: SecretTextValueTypes.Inline, + Value: "test", + SecretManagerIdentifier: "harnessSecretManager", + }, + } + + resp, _, err := c.SecretsApi.PostSecret(ctx, SecretRequestWrapper{Secret: secret}, c.AccountId, &SecretsApiPostSecretOpts{}) + require.NoError(t, err) + require.NotNil(t, resp.Data.Secret) + require.Equal(t, secret.Name, resp.Data.Secret.Name) +} diff --git a/harness/nextgen/client_test.go b/harness/nextgen/client_test.go new file mode 100644 index 00000000..acbb4304 --- /dev/null +++ b/harness/nextgen/client_test.go @@ -0,0 +1,19 @@ +package nextgen + +import ( + "context" + "sync" +) + +var configureClient sync.Once +var client *APIClient + +func getClientWithContext() (*APIClient, context.Context) { + configureClient.Do(func() { + cfg := NewConfiguration() + client = NewAPIClient(cfg) + }) + + ctx := context.WithValue(context.Background(), ContextAPIKey, APIKey{Key: client.ApiKey}) + return client, ctx +} diff --git a/harness/nextgen/client_utils.go b/harness/nextgen/client_utils.go new file mode 100644 index 00000000..0b43076c --- /dev/null +++ b/harness/nextgen/client_utils.go @@ -0,0 +1,8 @@ +package nextgen + +import "context" + +func (c *APIClient) WithAuthContext(ctx context.Context) (*APIClient, context.Context) { + authCtx := context.WithValue(ctx, ContextAPIKey, APIKey{Key: c.ApiKey}) + return c, authCtx +} diff --git a/harness/nextgen/configuration.go b/harness/nextgen/configuration.go index a24241f6..1cee6510 100644 --- a/harness/nextgen/configuration.go +++ b/harness/nextgen/configuration.go @@ -77,7 +77,7 @@ func NewConfiguration() *Configuration { cfg := &Configuration{ AccountId: helpers.EnvVars.AccountId.Get(), - ApiKey: helpers.EnvVars.ApiKey.Get(), + ApiKey: helpers.EnvVars.PlatformApiKey.Get(), BasePath: helpers.EnvVars.Endpoint.GetWithDefault(utils.BaseUrl), DefaultHeader: make(map[string]string), HTTPClient: utils.GetDefaultHttpClient(logger), diff --git a/harness/nextgen/enum_secret_spec_types.go b/harness/nextgen/enum_secret_spec_types.go new file mode 100644 index 00000000..29aff987 --- /dev/null +++ b/harness/nextgen/enum_secret_spec_types.go @@ -0,0 +1,23 @@ +package nextgen + +type SecretSpecType string + +var SecretSpecTypes = struct { + File SecretSpecType + SSHKey SecretSpecType + Text SecretSpecType +}{ + File: "SecretFileSpe", + SSHKey: "SSHKeySpec", + Text: "SecretTextSpec", +} + +var SecretSpecTypeValues = []string{ + SecretSpecTypes.File.String(), + SecretSpecTypes.SSHKey.String(), + SecretSpecTypes.Text.String(), +} + +func (e SecretSpecType) String() string { + return string(e) +} diff --git a/harness/nextgen/enum_secret_text_value_types.go b/harness/nextgen/enum_secret_text_value_types.go new file mode 100644 index 00000000..c41d6f51 --- /dev/null +++ b/harness/nextgen/enum_secret_text_value_types.go @@ -0,0 +1,20 @@ +package nextgen + +type SecretTextValueType string + +var SecretTextValueTypes = struct { + Inline SecretTextValueType + Reference SecretTextValueType +}{ + Inline: "Inline", + Reference: "Reference", +} + +var SecretTextValueTypeValues = []string{ + SecretTextValueTypes.Inline.String(), + SecretTextValueTypes.Reference.String(), +} + +func (e SecretTextValueType) String() string { + return string(e) +} diff --git a/harness/nextgen/enum_secret_types.go b/harness/nextgen/enum_secret_types.go new file mode 100644 index 00000000..fd64e535 --- /dev/null +++ b/harness/nextgen/enum_secret_types.go @@ -0,0 +1,23 @@ +package nextgen + +type SecretType string + +var SecretTypes = struct { + SecretFile SecretType + SecretText SecretType + SSHKey SecretType +}{ + SecretFile: "SecretFile", + SecretText: "SecretText", + SSHKey: "SSHKey", +} + +var SecretTypeValues = []string{ + SecretTypes.SecretFile.String(), + SecretTypes.SecretText.String(), + SecretTypes.SSHKey.String(), +} + +func (e SecretType) String() string { + return string(e) +} diff --git a/harness/nextgen/model_secret.go b/harness/nextgen/model_secret.go index d0032040..b16bef88 100644 --- a/harness/nextgen/model_secret.go +++ b/harness/nextgen/model_secret.go @@ -9,10 +9,12 @@ */ package nextgen +import "encoding/json" + // This is details of the secret entity defined in Harness. type Secret struct { // This specifies the type of secret - Type_ string `json:"type"` + Type_ SecretType `json:"type"` // Name of the Secret Name string `json:"name"` // Identifier of the Secret @@ -24,6 +26,10 @@ type Secret struct { // Tags Tags map[string]string `json:"tags,omitempty"` // Description of the Secret - Description string `json:"description,omitempty"` - Spec *SecretSpec `json:"spec"` + Description string `json:"description,omitempty"` + Spec json.RawMessage `json:"spec"` + + File *SecretFileSpe `json:"-"` + Text *SecretTextSpec `json:"-"` + SSHKey *SshKeySpec `json:"-"` } diff --git a/harness/nextgen/model_secret_serializer.go b/harness/nextgen/model_secret_serializer.go new file mode 100644 index 00000000..ffc35f5a --- /dev/null +++ b/harness/nextgen/model_secret_serializer.go @@ -0,0 +1,62 @@ +package nextgen + +import ( + "encoding/json" + "fmt" +) + +func (a *Secret) UnmarshalJSON(data []byte) error { + + type Alias Secret + + aux := &struct { + *Alias + }{ + Alias: (*Alias)(a), + } + + err := json.Unmarshal(data, &aux) + if err != nil { + return err + } + + switch a.Type_ { + case SecretTypes.SecretFile: + err = json.Unmarshal(aux.Spec, &a.File) + case SecretTypes.SSHKey: + err = json.Unmarshal(aux.Spec, &a.SSHKey) + case SecretTypes.SecretText: + err = json.Unmarshal(aux.Spec, &a.Text) + default: + panic(fmt.Sprintf("unknown secret type %s", a.Type_)) + } + + return err +} + +func (a *Secret) MarshalJSON() ([]byte, error) { + type Alias Secret + + var spec []byte + var err error + + switch a.Type_ { + case SecretTypes.SecretFile: + spec, err = json.Marshal(a.File) + case SecretTypes.SSHKey: + // spec, err = json.Marshal(a.AssumeIamRole) + // noop + case SecretTypes.SecretText: + spec, err = json.Marshal(a.Text) + default: + panic(fmt.Sprintf("unknown secret type %s", a.Type_)) + } + + if err != nil { + return nil, err + } + + a.Spec = json.RawMessage(spec) + + return json.Marshal((*Alias)(a)) +} diff --git a/harness/nextgen/model_secret_text_spec.go b/harness/nextgen/model_secret_text_spec.go index 504dd71b..9cdb81ef 100644 --- a/harness/nextgen/model_secret_text_spec.go +++ b/harness/nextgen/model_secret_text_spec.go @@ -11,12 +11,12 @@ package nextgen // This has details of encrypted text secret. type SecretTextSpec struct { - ErrorMessageForInvalidYaml string `json:"errorMessageForInvalidYaml,omitempty"` - Type_ string `json:"type"` + ErrorMessageForInvalidYaml string `json:"errorMessageForInvalidYaml,omitempty"` + Type_ SecretSpecType `json:"type"` // Identifier of the Secret Manager used to manage the secret. SecretManagerIdentifier string `json:"secretManagerIdentifier"` // This has details to specify if the secret value is inline or referenced. - ValueType string `json:"valueType"` + ValueType SecretTextValueType `json:"valueType"` // Value of the Secret Value string `json:"value,omitempty"` }