Skip to content

Commit

Permalink
Merge branch 'optional-really-means-optional'
Browse files Browse the repository at this point in the history
  • Loading branch information
glyn committed Aug 14, 2019
2 parents 1ef7d96 + ed19eee commit fc81dbd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
14 changes: 7 additions & 7 deletions credentials/credentialset.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (s Set) Expand(b *bundle.Bundle, stateless bool) (env, files map[string]str
for name, val := range b.Credentials {
src, ok := s[name]
if !ok {
if stateless {
if stateless || !val.Required {
continue
}
err = fmt.Errorf("credential %q is missing from the user-supplied credentials", name)
Expand Down Expand Up @@ -77,15 +77,15 @@ func Load(path string) (*CredentialSet, error) {

// Validate compares the given credentials with the spec.
//
// This will result in an error only if:
// - a parameter in the spec is not present in the given set
// - a parameter in the given set does not match the format required by the spec
// This will result in an error only when the following conditions are true:
// - a credential in the spec is not present in the given set
// - the credential is required
//
// It is allowed for spec to specify both an env var and a file. In such case, if
// the givn set provides either, it will be considered valid.
// the given set provides either, it will be considered valid.
func Validate(given Set, spec map[string]bundle.Credential) error {
for name := range spec {
if !isValidCred(given, name) {
for name, cred := range spec {
if !isValidCred(given, name) && cred.Required {
return fmt.Errorf("bundle requires credential for %s", name)
}
}
Expand Down
21 changes: 20 additions & 1 deletion credentials/credentialset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,15 @@ func TestCredentialSet_Merge(t *testing.T) {

}

func TestCredentialSetMissingCred(t *testing.T) {
func TestCredentialSetMissingRequiredCred(t *testing.T) {
b := &bundle.Bundle{
Name: "knapsack",
Credentials: map[string]bundle.Credential{
"first": {
Location: bundle.Location{
EnvironmentVariable: "FIRST_VAR",
},
Required: true,
},
},
}
Expand All @@ -134,3 +135,21 @@ func TestCredentialSetMissingCred(t *testing.T) {
_, _, err = cs.Expand(b, true)
assert.NoError(t, err)
}

func TestCredentialSetMissingOptionalCred(t *testing.T) {
b := &bundle.Bundle{
Name: "knapsack",
Credentials: map[string]bundle.Credential{
"first": {
Location: bundle.Location{
EnvironmentVariable: "FIRST_VAR",
},
},
},
}
cs := Set{}
_, _, err := cs.Expand(b, false)
assert.NoError(t, err)
_, _, err = cs.Expand(b, true)
assert.NoError(t, err)
}

0 comments on commit fc81dbd

Please sign in to comment.