Skip to content

Commit

Permalink
Add constructor for CredentialSet
Browse files Browse the repository at this point in the history
* Ensure that SchemaVersion is set on new CredentialSets.
* Try a new pattern for the default schema that doesn't involve checking
for errors parsing the spec constant. We test the spec constant in our
tests and people can rely on us having set it properly.

Signed-off-by: Carolyn Van Slyck <me@carolynvanslyck.com>
  • Loading branch information
carolynvs committed Aug 20, 2020
1 parent a4a6a5b commit d6bf372
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 17 deletions.
33 changes: 22 additions & 11 deletions credentials/credentialset.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@ import (
"github.com/cnabio/cnab-go/valuesource"
)

// CNABSpecVersion represents the CNAB Spec version of the Credentials
// that this library implements
// This value is prefixed with e.g. `cnab-credentials-` so isn't itself valid semver.
var CNABSpecVersion string = "cnab-credentialsets-1.0.0-DRAFT-b6c701f"
const (
// DefaultSchemaVersion is the default SchemaVersion value
// set on new CredentialSet instances, and is the semver portion
// of CNABSpecVersion.
DefaultSchemaVersion = schema.Version("1.0.0-DRAFT+b6c701f")

// CNABSpecVersion represents the CNAB Spec version of the Credentials
// that this library implements
// This value is prefixed with e.g. `cnab-credentials-` so isn't itself valid semver.
CNABSpecVersion string = "cnab-credentialsets-" + string(DefaultSchemaVersion)
)

// CredentialSet represents a collection of credentials
type CredentialSet struct {
Expand All @@ -32,14 +39,18 @@ type CredentialSet struct {
Credentials []valuesource.Strategy `json:"credentials" yaml:"credentials"`
}

// GetDefaultSchemaVersion returns the default semver CNAB schema version of the CredentialSet
// that this library implements
func GetDefaultSchemaVersion() (schema.Version, error) {
ver, err := schema.GetSemver(CNABSpecVersion)
if err != nil {
return "", err
// NewCredentialSet creates a new CredentialSet with the required fields initialized.
func NewCredentialSet(name string, creds ...valuesource.Strategy) CredentialSet {
now := time.Now()
cs := CredentialSet{
SchemaVersion: DefaultSchemaVersion,
Name: name,
Created: now,
Modified: now,
Credentials: creds,
}
return ver, nil

return cs
}

// Load a CredentialSet from a file at a given path.
Expand Down
30 changes: 26 additions & 4 deletions credentials/credentialset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/cnabio/cnab-go/schema"
"github.com/cnabio/cnab-go/secrets/host"
"github.com/cnabio/cnab-go/valuesource"
)

func TestCredentialSet_ResolveCredentials(t *testing.T) {
Expand All @@ -27,10 +29,6 @@ func TestCredentialSet_ResolveCredentials(t *testing.T) {
credset, err := Load(fmt.Sprintf("testdata/staging-%s.yaml", goos))
is.NoError(err)

version, err := GetDefaultSchemaVersion()
require.NoError(t, err, "GetDefaultSchemaVersion failed")
is.Equal(version, credset.SchemaVersion)

h := &host.SecretStore{}
results, err := credset.ResolveCredentials(h)
if err != nil {
Expand All @@ -55,3 +53,27 @@ func TestCredentialSet_ResolveCredentials(t *testing.T) {
is.Equal(tt.expect, strings.TrimSpace(dest))
}
}

func TestCNABSpecVersion(t *testing.T) {
version, err := schema.GetSemver(CNABSpecVersion)
require.NoError(t, err)
assert.Equal(t, DefaultSchemaVersion, version)
}

func TestNewCredentialSet(t *testing.T) {
cs := NewCredentialSet("mycreds",
valuesource.Strategy{
Name: "password",
Source: valuesource.Source{
Key: "env",
Value: "MY_PASSWORD",
},
})

assert.Equal(t, "mycreds", cs.Name, "Name was not set")
assert.NotEmpty(t, cs.Created, "Created was not set")
assert.NotEmpty(t, cs.Modified, "Modified was not set")
assert.Equal(t, cs.Created, cs.Modified, "Created and Modified should have the same timestamp")
assert.Equal(t, DefaultSchemaVersion, cs.SchemaVersion, "SchemaVersion was not set")
assert.Len(t, cs.Credentials, 1, "Credentials should be initialized with 1 value")
}
2 changes: 1 addition & 1 deletion credentials/testdata/staging-unix.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: staging
schemaVersion: "1.0.0-DRAFT-b6c701f"
schemaVersion: "1.0.0-DRAFT+b6c701f"
credentials:
- name: read_file
source:
Expand Down
2 changes: 1 addition & 1 deletion credentials/testdata/staging-windows.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: staging
schemaVersion: "1.0.0-DRAFT-b6c701f"
schemaVersion: "1.0.0-DRAFT+b6c701f"
credentials:
- name: read_file
source:
Expand Down

0 comments on commit d6bf372

Please sign in to comment.