Skip to content

Commit

Permalink
Fix serialization of secret entities (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
micahlmartin authored May 20, 2022
1 parent 1562bc2 commit 40241e9
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 7 deletions.
32 changes: 32 additions & 0 deletions harness/nextgen/api_secrets_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
19 changes: 19 additions & 0 deletions harness/nextgen/client_test.go
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 8 additions & 0 deletions harness/nextgen/client_utils.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion harness/nextgen/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
23 changes: 23 additions & 0 deletions harness/nextgen/enum_secret_spec_types.go
Original file line number Diff line number Diff line change
@@ -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)
}
20 changes: 20 additions & 0 deletions harness/nextgen/enum_secret_text_value_types.go
Original file line number Diff line number Diff line change
@@ -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)
}
23 changes: 23 additions & 0 deletions harness/nextgen/enum_secret_types.go
Original file line number Diff line number Diff line change
@@ -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)
}
12 changes: 9 additions & 3 deletions harness/nextgen/model_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:"-"`
}
62 changes: 62 additions & 0 deletions harness/nextgen/model_secret_serializer.go
Original file line number Diff line number Diff line change
@@ -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))
}
6 changes: 3 additions & 3 deletions harness/nextgen/model_secret_text_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

0 comments on commit 40241e9

Please sign in to comment.