Skip to content

Commit

Permalink
feat: Better validation
Browse files Browse the repository at this point in the history
  • Loading branch information
soerenschneider committed Oct 12, 2021
1 parent 2e3ecb7 commit 06fec46
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 1 deletion.
13 changes: 12 additions & 1 deletion internal/config/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"errors"
"fmt"
"github.com/rs/zerolog/log"
"net/url"
"strings"
Expand All @@ -23,6 +24,7 @@ func (conf *VaultConfig) IsTokenIncreaseEnabled() bool {

func (conf *VaultConfig) Print() {
log.Info().Msgf("VaultAddr=%s", conf.VaultAddr)
log.Info().Msgf("PathPrefix=%s", conf.PathPrefix)
if len(conf.RoleId) > 0 {
log.Info().Msgf("VaultRoleId=%s", conf.RoleId)
}
Expand All @@ -38,7 +40,6 @@ func (conf *VaultConfig) Print() {
if conf.TokenIncreaseInterval > 0 {
log.Info().Msgf("TokenIncreaseInterval=%d", conf.TokenIncreaseInterval)
}
// TODO: Check pathPrefix
}

func DefaultVaultConfig() VaultConfig {
Expand All @@ -57,6 +58,16 @@ func (conf *VaultConfig) Validate() error {
if len(conf.VaultAddr) == 0 {
return errors.New("no Vault address defined")
}
addr, err := url.ParseRequestURI(conf.VaultAddr)
if err != nil || addr.Scheme == "" || addr.Host == "" || addr.Port() == "" {
return errors.New("can not parse supplied vault addr as url")
}

for _, prefix := range []string{"/", "secret/"} {
if strings.HasPrefix(conf.PathPrefix, prefix) {
return fmt.Errorf("vault path prefix must not start with %s", prefix)
}
}

validRoleIdCredentials := len(conf.SecretId) > 0 && len(conf.RoleId) > 0
if !validRoleIdCredentials && len(conf.VaultToken) == 0 {
Expand Down
101 changes: 101 additions & 0 deletions internal/config/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package config

import "testing"

func TestVaultConfig_Validate(t *testing.T) {
type fields struct {
VaultToken string
VaultAddr string
SecretId string
RoleId string
TokenIncreaseSeconds int
TokenIncreaseInterval int
PathPrefix string
}
tests := []struct {
name string
fields fields
wantErr bool
}{
{
name: "valid config - token",
fields: fields{
VaultToken: "s.asd83hrfhasfjsda",
VaultAddr: "https://my-vault-instance:443",
TokenIncreaseSeconds: 0,
TokenIncreaseInterval: 0,
PathPrefix: "production",
},
},
{
name: "valid config - approle",
fields: fields{
VaultAddr: "https://my-vault-instance:443",
SecretId: "super-secret",
RoleId: "my-role",
TokenIncreaseSeconds: 0,
TokenIncreaseInterval: 0,
PathPrefix: "dev-v002",
},
},
{
name: "invalid config - missing protocol",
fields: fields{
VaultToken: "s.asd83hrfhasfjsda",
VaultAddr: "my-vault-instance:443",
TokenIncreaseSeconds: 0,
TokenIncreaseInterval: 0,
PathPrefix: "production",
},
wantErr: true,
},
{
name: "invalid config - missing port",
fields: fields{
VaultToken: "s.asd83hrfhasfjsda",
VaultAddr: "http://my-vault-instance",
TokenIncreaseSeconds: 0,
TokenIncreaseInterval: 0,
PathPrefix: "production",
},
wantErr: true,
},
{
name: "invalid config - invalid path prefix",
fields: fields{
VaultToken: "s.asd83hrfhasfjsda",
VaultAddr: "http://my-vault-instance:443",
TokenIncreaseSeconds: 0,
TokenIncreaseInterval: 0,
PathPrefix: "/production",
},
wantErr: true,
},
{
name: "invalid config - no auth methods",
fields: fields{
VaultAddr: "http://my-vault-instance:443",
TokenIncreaseSeconds: 0,
TokenIncreaseInterval: 0,
PathPrefix: "production",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
conf := &VaultConfig{
VaultToken: tt.fields.VaultToken,
VaultAddr: tt.fields.VaultAddr,
SecretId: tt.fields.SecretId,
RoleId: tt.fields.RoleId,
TokenIncreaseSeconds: tt.fields.TokenIncreaseSeconds,
TokenIncreaseInterval: tt.fields.TokenIncreaseInterval,
PathPrefix: tt.fields.PathPrefix,
}
if err := conf.Validate(); (err != nil) != tt.wantErr {
t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

0 comments on commit 06fec46

Please sign in to comment.