Skip to content

Commit

Permalink
Add schemaVersion to credentialSet (#229)
Browse files Browse the repository at this point in the history
* 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 <me@carolynvanslyck.com>

* Fix comment

Signed-off-by: Carolyn Van Slyck <me@carolynvanslyck.com>

* 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 <me@carolynvanslyck.com>
  • Loading branch information
carolynvs authored Sep 30, 2020
1 parent f661954 commit 4276c70
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
29 changes: 29 additions & 0 deletions credentials/credentialset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
27 changes: 27 additions & 0 deletions credentials/credentialset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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")
}
1 change: 1 addition & 0 deletions credentials/testdata/staging-unix.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
name: staging
schemaVersion: "1.0.0-DRAFT+b6c701f"
credentials:
- name: read_file
source:
Expand Down
1 change: 1 addition & 0 deletions credentials/testdata/staging-windows.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
name: staging
schemaVersion: "1.0.0-DRAFT+b6c701f"
credentials:
- name: read_file
source:
Expand Down

0 comments on commit 4276c70

Please sign in to comment.