From 4276c703586251cb06c753aa730456de72672946 Mon Sep 17 00:00:00 2001 From: Carolyn Van Slyck Date: Wed, 30 Sep 2020 12:25:06 -0500 Subject: [PATCH] Add schemaVersion to credentialSet (#229) * Add schemaVersion to credentialSet Add a schemaVersion field to credentialSet so that we can identify the spec version that the credential set adheres to. This was very useful for claims, we use it for data migrations when the claim spec changes, and it is best to have it _before_ we need to change the spec. Worst case we never change the spec and we all win. Signed-off-by: Carolyn Van Slyck * Fix comment Signed-off-by: Carolyn Van Slyck * Add constructor for CredentialSet * 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 --- credentials/credentialset.go | 29 +++++++++++++++++++++++ credentials/credentialset_test.go | 27 +++++++++++++++++++++ credentials/testdata/staging-unix.yaml | 1 + credentials/testdata/staging-windows.yaml | 1 + 4 files changed, 58 insertions(+) diff --git a/credentials/credentialset.go b/credentials/credentialset.go index 05a33425..1ddbb322 100644 --- a/credentials/credentialset.go +++ b/credentials/credentialset.go @@ -8,12 +8,27 @@ import ( "gopkg.in/yaml.v2" "github.com/cnabio/cnab-go/bundle" + "github.com/cnabio/cnab-go/schema" "github.com/cnabio/cnab-go/secrets" "github.com/cnabio/cnab-go/valuesource" ) +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 { + // SchemaVersion is the version of the credential-set schema. + SchemaVersion schema.Version `json:"schemaVersion" yaml:"schemaVersion"` // Name is the name of the credentialset. Name string `json:"name" yaml:"name"` // Created timestamp of the credentialset. @@ -24,6 +39,20 @@ type CredentialSet struct { Credentials []valuesource.Strategy `json:"credentials" yaml:"credentials"` } +// 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 cs +} + // Load a CredentialSet from a file at a given path. // // It does not load the individual credentials. diff --git a/credentials/credentialset_test.go b/credentials/credentialset_test.go index 9ebf055a..83c43e9d 100644 --- a/credentials/credentialset_test.go +++ b/credentials/credentialset_test.go @@ -8,8 +8,11 @@ import ( "testing" "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) { @@ -50,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") +} diff --git a/credentials/testdata/staging-unix.yaml b/credentials/testdata/staging-unix.yaml index dc8efb5e..4486a52e 100644 --- a/credentials/testdata/staging-unix.yaml +++ b/credentials/testdata/staging-unix.yaml @@ -1,4 +1,5 @@ name: staging +schemaVersion: "1.0.0-DRAFT+b6c701f" credentials: - name: read_file source: diff --git a/credentials/testdata/staging-windows.yaml b/credentials/testdata/staging-windows.yaml index 670f6edd..6b8022ad 100644 --- a/credentials/testdata/staging-windows.yaml +++ b/credentials/testdata/staging-windows.yaml @@ -1,4 +1,5 @@ name: staging +schemaVersion: "1.0.0-DRAFT+b6c701f" credentials: - name: read_file source: