Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add schemaVersion to credentialSet #229

Merged
merged 3 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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