-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix serialization of secret entities (#123)
- Loading branch information
1 parent
1562bc2
commit 40241e9
Showing
10 changed files
with
200 additions
and
7 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
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) | ||
} |
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,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 | ||
} |
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,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 | ||
} |
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,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) | ||
} |
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,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) | ||
} |
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,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) | ||
} |
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,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)) | ||
} |
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