Skip to content

Commit

Permalink
Add base64 encoding co Create and change Read fucntion to get value with
Browse files Browse the repository at this point in the history
decode.

Add base64 encoding for values in Update
  • Loading branch information
anvial committed Apr 11, 2024
1 parent 1ff451b commit 2aa33c5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 17 deletions.
26 changes: 23 additions & 3 deletions internal/juju/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package juju

import (
"encoding/base64"
"errors"
"fmt"

Expand Down Expand Up @@ -90,7 +91,14 @@ func (c *secretsClient) CreateSecret(input *CreateSecretInput) (CreateSecretOutp
defer func() { _ = conn.Close() }()

secretAPIClient := c.getSecretAPIClient(conn)
secretId, err := secretAPIClient.CreateSecret(input.Name, input.Info, input.Value)

// Encode the secret values as base64
encodedValue := make(map[string]string, len(input.Value))
for k, v := range input.Value {
encodedValue[k] = base64.StdEncoding.EncodeToString([]byte(v))
}

secretId, err := secretAPIClient.CreateSecret(input.Name, input.Info, encodedValue)
if err != nil {
return CreateSecretOutput{}, typedError(err)
}
Expand Down Expand Up @@ -135,10 +143,16 @@ func (c *secretsClient) ReadSecret(input *ReadSecretInput) (ReadSecretOutput, er
return ReadSecretOutput{}, errors.New(results[0].Error)
}

// Decode the secret values from base64
decodedValue, err := results[0].Value.Values()
if err != nil {
return ReadSecretOutput{}, err
}

return ReadSecretOutput{
SecretId: results[0].Metadata.URI.String(),
Name: results[0].Metadata.Label,
Value: results[0].Value.EncodedValues(),
Value: decodedValue,
Info: results[0].Metadata.Description,
}, nil
}
Expand Down Expand Up @@ -167,7 +181,13 @@ func (c *secretsClient) UpdateSecret(input *UpdateSecretInput) error {
}
var value map[string]string
if input.Value != nil {
value = *input.Value
// Encode the secret values as base64
encodedValue := make(map[string]string, len(*input.Value))
for k, v := range *input.Value {
encodedValue[k] = base64.StdEncoding.EncodeToString([]byte(v))
}

value = encodedValue
} else {
value = map[string]string{}
}
Expand Down
37 changes: 23 additions & 14 deletions internal/juju/secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,18 @@ func (s *SecretSuite) TestCreateSecret() {
ctlr := s.setupMocks(s.T())
defer ctlr.Finish()

decodedValue := map[string]string{"key": "value"}
encodedValue := map[string]string{"key": base64.StdEncoding.EncodeToString([]byte("value"))}

s.mockSecretClient.EXPECT().CreateSecret(
"test-secret", "test info", map[string]string{"key": "value"},
"test-secret", "test info", encodedValue,
).Return("secret-id", nil).AnyTimes()

client := s.getSecretsClient()
output, err := client.CreateSecret(&CreateSecretInput{
ModelName: s.testModelName,
Name: "test-secret",
Value: map[string]string{"key": "value"},
Value: decodedValue,
Info: "test info",
})
s.Require().NoError(err)
Expand All @@ -71,15 +74,18 @@ func (s *SecretSuite) TestCreateSecretError() {

errBoom := errors.New("boom")

decodedValue := map[string]string{"key": "value"}
encodedValue := map[string]string{"key": base64.StdEncoding.EncodeToString([]byte("value"))}

s.mockSecretClient.EXPECT().CreateSecret(
"test-secret", "test info", map[string]string{"key": "value"},
"test-secret", "test info", encodedValue,
).Return("", errBoom).AnyTimes()

client := s.getSecretsClient()
output, err := client.CreateSecret(&CreateSecretInput{
ModelName: s.testModelName,
Name: "test-secret",
Value: map[string]string{"key": "value"},
Value: decodedValue,
Info: "test info",
})
s.Require().Error(err)
Expand Down Expand Up @@ -130,6 +136,7 @@ func (s *SecretSuite) TestReadSecret() {
s.Require().NoError(err)

s.Assert().NotNil(output)
s.Assert().Equal("value", output.Value["key"])
}

func (s *SecretSuite) TestReadSecretError() {
Expand Down Expand Up @@ -171,21 +178,22 @@ func (s *SecretSuite) TestUpdateSecretWithRenaming() {
secretInfo := "secret info"
autoPrune := true

// update secret with value2
value2 := base64.StdEncoding.EncodeToString([]byte("value2"))
decodedValue := map[string]string{"key": "value"}
encodedValue := map[string]string{"key": base64.StdEncoding.EncodeToString([]byte("value"))}

secretURI, err := coresecrets.ParseURI(secretId)
s.Require().NoError(err)

s.mockSecretClient.EXPECT().UpdateSecret(
secretURI, "", &autoPrune, newSecretName, "secret info", map[string]string{"key": value2},
secretURI, "", &autoPrune, newSecretName, "secret info", encodedValue,
).Return(nil).AnyTimes()

client := s.getSecretsClient()
err = client.UpdateSecret(&UpdateSecretInput{
SecretId: secretId,
ModelName: s.testModelName,
Name: &newSecretName,
Value: &map[string]string{"key": value2},
Value: &decodedValue,
AutoPrune: &autoPrune,
Info: &secretInfo,
})
Expand All @@ -203,7 +211,7 @@ func (s *SecretSuite) TestUpdateSecretWithRenaming() {
Revision: 1,
},
},
Value: coresecrets.NewSecretValue(map[string]string{"key": value2}),
Value: coresecrets.NewSecretValue(encodedValue),
Error: "",
},
}, nil).Times(1)
Expand All @@ -226,20 +234,21 @@ func (s *SecretSuite) TestUpdateSecret() {
secretInfo := "secret info"
autoPrune := true

// update secret with value2
value2 := base64.StdEncoding.EncodeToString([]byte("value2"))
decodedValue := map[string]string{"key": "value"}
encodedValue := map[string]string{"key": base64.StdEncoding.EncodeToString([]byte("value"))}

secretURI, err := coresecrets.ParseURI(secretId)
s.Require().NoError(err)

s.mockSecretClient.EXPECT().UpdateSecret(
secretURI, "", &autoPrune, "", "secret info", map[string]string{"key": value2},
secretURI, "", &autoPrune, "", secretInfo, encodedValue,
).Return(nil).AnyTimes()

client := s.getSecretsClient()
err = client.UpdateSecret(&UpdateSecretInput{
SecretId: secretId,
ModelName: s.testModelName,
Value: &map[string]string{"key": value2},
Value: &decodedValue,
AutoPrune: &autoPrune,
Info: &secretInfo,
})
Expand All @@ -258,7 +267,7 @@ func (s *SecretSuite) TestUpdateSecret() {
Revision: 1,
},
},
Value: coresecrets.NewSecretValue(map[string]string{"key": value2}),
Value: coresecrets.NewSecretValue(encodedValue),
Error: "",
},
}, nil).Times(1)
Expand Down

0 comments on commit 2aa33c5

Please sign in to comment.