Skip to content

Commit

Permalink
Create add and remove secret config functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dotNomad committed Sep 26, 2024
1 parent c5faed5 commit 9019fbf
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 39 deletions.
21 changes: 21 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,24 @@ func (cfg *Config) WriteFile(path util.AbsolutePath) error {
defer f.Close()
return cfg.Write(f)
}

func (cfg *Config) AddSecret(secret string) error {
// Check if the secret already exists before adding
for _, s := range cfg.Secrets {
if s == secret {
return nil // Secret already exists, no need to add
}
}
cfg.Secrets = append(cfg.Secrets, secret)
return nil
}

func (cfg *Config) RemoveSecret(secret string) error {
for i, s := range cfg.Secrets {
if s == secret {
cfg.Secrets = append(cfg.Secrets[:i], cfg.Secrets[i+1:]...)
break
}
}
return nil
}
41 changes: 41 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,44 @@ func (s *ConfigSuite) TestReadComments() {

s.Equal([]string{" These are comments.", " They will be preserved."}, cfg.Comments)
}

func (s *ConfigSuite) TestApplySecretActionAdd() {
cfg := New()
cfg.Secrets = []string{}
err := cfg.AddSecret("secret1")
s.NoError(err)
s.Equal([]string{"secret1"}, cfg.Secrets)
}

func (s *ConfigSuite) TestApplySecretActionAddWithExistingSecrets() {
cfg := New()
cfg.Secrets = []string{"existingSecret1", "existingSecret2"}
err := cfg.AddSecret("newSecret")
s.NoError(err)
s.Equal([]string{"existingSecret1", "existingSecret2", "newSecret"}, cfg.Secrets)
}

func (s *ConfigSuite) TestApplySecretActionAddNoDuplicates() {
cfg := New()
cfg.Secrets = []string{"existingSecret1", "existingSecret2"}

err := cfg.AddSecret("existingSecret1")
s.NoError(err)
s.Equal([]string{"existingSecret1", "existingSecret2"}, cfg.Secrets)
}

func (s *ConfigSuite) TestApplySecretActionRemove() {
cfg := New()
cfg.Secrets = []string{"secret1", "secret2"}
err := cfg.RemoveSecret("secret1")
s.NoError(err)
s.Equal([]string{"secret2"}, cfg.Secrets)
}

func (s *ConfigSuite) TestApplySecretActionRemoveFromEmptySecrets() {
cfg := New()
cfg.Secrets = []string{}
err := cfg.RemoveSecret("nonexistentSecret")
s.NoError(err)
s.Equal([]string{}, cfg.Secrets)
}
16 changes: 2 additions & 14 deletions internal/services/api/post_config_secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,12 @@ type postConfigSecretsRequest struct {
func applySecretAction(cfg *config.Config, action string, secret string) error {
switch action {
case secretActionAdd:
// Check if the secret already exists before adding
for _, s := range cfg.Secrets {
if s == secret {
return nil // Secret already exists, no need to add
}
}
cfg.Secrets = append(cfg.Secrets, secret)
return cfg.AddSecret(secret)
case secretActionRemove:
for i, s := range cfg.Secrets {
if s == secret {
cfg.Secrets = append(cfg.Secrets[:i], cfg.Secrets[i+1:]...)
break
}
}
return cfg.RemoveSecret(secret)
default:
return fmt.Errorf("unknown action: %s", action)
}
return nil
}

func PostConfigSecretsHandlerFunc(base util.AbsolutePath, log logging.Logger) http.HandlerFunc {
Expand Down
25 changes: 0 additions & 25 deletions internal/services/api/post_config_secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,6 @@ func (s *ApplySecretActionSuite) TestApplySecretActionAdd() {
s.Equal([]string{"secret1"}, cfg.Secrets)
}

func (s *ApplySecretActionSuite) TestApplySecretActionAddWithExistingSecrets() {
cfg := config.New()
cfg.Secrets = []string{"existingSecret1", "existingSecret2"}
err := applySecretAction(cfg, secretActionAdd, "newSecret")
s.NoError(err)
s.Equal([]string{"existingSecret1", "existingSecret2", "newSecret"}, cfg.Secrets)
}

func (s *ApplySecretActionSuite) TestApplySecretActionAddNoDuplicates() {
cfg := config.New()
cfg.Secrets = []string{"existingSecret1", "existingSecret2"}

err := applySecretAction(cfg, secretActionAdd, "existingSecret1")
s.NoError(err)
s.Equal([]string{"existingSecret1", "existingSecret2"}, cfg.Secrets)
}

func (s *ApplySecretActionSuite) TestApplySecretActionRemove() {
cfg := config.New()
cfg.Secrets = []string{"secret1", "secret2"}
Expand All @@ -59,14 +42,6 @@ func (s *ApplySecretActionSuite) TestApplySecretActionRemove() {
s.Equal([]string{"secret2"}, cfg.Secrets)
}

func (s *ApplySecretActionSuite) TestApplySecretActionRemoveFromEmptySecrets() {
cfg := config.New()
cfg.Secrets = []string{}
err := applySecretAction(cfg, secretActionRemove, "nonexistentSecret")
s.NoError(err)
s.Equal([]string{}, cfg.Secrets)
}

func (s *ApplySecretActionSuite) TestApplySecretActionUnknownAction() {
cfg := config.New()
err := applySecretAction(cfg, "invalidAction", "someSecret")
Expand Down

0 comments on commit 9019fbf

Please sign in to comment.