Skip to content

Commit

Permalink
Only allow secrets in config in POST deployments
Browse files Browse the repository at this point in the history
  • Loading branch information
dotNomad committed Sep 14, 2024
1 parent 9867a4e commit cf59b50
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
9 changes: 9 additions & 0 deletions internal/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ type Config struct {
Connect *Connect `toml:"connect,omitempty" json:"connect,omitempty"`
}

func (c *Config) HasSecret(secret string) bool {
for _, s := range c.Secrets {
if s == secret {
return true
}
}
return false
}

type Environment = map[string]string

type Python struct {
Expand Down
2 changes: 2 additions & 0 deletions internal/services/api/post_deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ func (s *PostDeploymentHandlerFuncSuite) TestPostDeploymentHandlerFuncStateErr()
handler := PostDeploymentHandlerFunc(s.cwd, log, nil, events.NewNullEmitter())
handler(rec, req)
s.Equal(http.StatusBadRequest, rec.Result().StatusCode)
body, _ := io.ReadAll(rec.Body)
s.Contains(string(body), "test error from state factory")
}

func (s *PostDeploymentHandlerFuncSuite) TestPostDeploymentHandlerFuncWrongServer() {
Expand Down
7 changes: 7 additions & 0 deletions internal/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ func New(path util.AbsolutePath, accountName, configName, targetName string, sav
}
}

// Check that the secrets passed are in the config
for secret := range secrets {
if !cfg.HasSecret(secret) {
return nil, fmt.Errorf("secret '%s' is not in the configuration", secret)
}
}

return &State{
Dir: path,
AccountName: accountName,
Expand Down
27 changes: 26 additions & 1 deletion internal/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,15 @@ func (s *StateSuite) makeConfiguration(name string) *config.Config {
return cfg
}

func (s *StateSuite) makeConfigurationWithSecrets(name string, secrets []string) *config.Config {
path := config.GetConfigPath(s.cwd, name)
cfg := s.makeConfiguration(name)
cfg.Secrets = secrets
err := cfg.WriteFile(path)
s.NoError(err)
return cfg
}

func (s *StateSuite) TestNew() {
accts := &accounts.MockAccountList{}
acct := accounts.Account{}
Expand Down Expand Up @@ -405,7 +414,7 @@ func (s *StateSuite) TestNewWithSecrets() {
accts := &accounts.MockAccountList{}
acct := accounts.Account{}
accts.On("GetAllAccounts").Return([]accounts.Account{acct}, nil)
s.makeConfiguration("default")
s.makeConfigurationWithSecrets("default", []string{"API_KEY", "DB_PASSWORD"})

secrets := map[string]string{
"API_KEY": "secret123",
Expand All @@ -418,6 +427,22 @@ func (s *StateSuite) TestNewWithSecrets() {
s.Equal(secrets, state.Secrets)
}

func (s *StateSuite) TestNewWithInvalidSecret() {
accts := &accounts.MockAccountList{}
acct := accounts.Account{}
accts.On("GetAllAccounts").Return([]accounts.Account{acct}, nil)
s.makeConfiguration("default")

secrets := map[string]string{
"INVALID_SECRET": "secret123",
}

state, err := New(s.cwd, "", "", "", "", accts, secrets)
s.NotNil(err)
s.ErrorContains(err, "secret 'INVALID_SECRET' is not in the configuration")
s.Nil(state)
}

func (s *StateSuite) TestGetDefaultAccountNone() {
actual, err := getDefaultAccount([]accounts.Account{})
s.Nil(actual)
Expand Down

0 comments on commit cf59b50

Please sign in to comment.