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

Made credentials required by default by adding UnmarshalYAML method to CredentialDefinition type #657

Merged
merged 3 commits into from
Sep 30, 2019
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
19 changes: 19 additions & 0 deletions pkg/config/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ func (pd *ParameterDefinition) DeepCopy() *ParameterDefinition {
return &p2
}

// CredentialDefinition represents the structure or fields of a credential parameter
type CredentialDefinition struct {
Name string `yaml:"name"`
Description string `yaml:"description,omitempty"`
Expand All @@ -159,6 +160,24 @@ type CredentialDefinition struct {
Location `yaml:",inline"`
}

func (cd *CredentialDefinition) UnmarshalYAML(unmarshal func(interface{}) error) error {
type rawCreds CredentialDefinition
rawCred := rawCreds{
Name: cd.Name,
Description: cd.Description,
Required: true,
Location: cd.Location,
}

if err := unmarshal(&rawCred); err != nil {
return err
}

*cd = CredentialDefinition(rawCred)

return nil
}

// TODO: use cnab-go's bundle.Location instead, once yaml tags have been added
// Location represents a Parameter or Credential location in an InvocationImage
type Location struct {
Expand Down
15 changes: 15 additions & 0 deletions pkg/config/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ func TestMixinDeclaration_UnmarshalYAML_Invalid(t *testing.T) {
assert.Contains(t, err.Error(), "mixin declaration contained more than one mixin")
}

func TestCredentialsDefinition_UnmarshalYAML(t *testing.T) {
assertAllCredentialsRequired := func(t *testing.T, creds []CredentialDefinition) {
for _, cred := range creds {
assert.EqualValuesf(t, true, cred.Required, "Credential: %s should be required", cred.Name)
}
}
t.Run("all credentials in the generated manifest file are required", func(t *testing.T) {
c := NewTestConfig(t)
c.TestContext.AddTestFile("testdata/with-credentials.yaml", Name)
m, err := c.ReadManifest(Name)
require.NoError(t, err)
assertAllCredentialsRequired(t, m.Credentials)
})
}

func TestMixinDeclaration_MarshalYAML(t *testing.T) {
m := struct {
Mixins []MixinDeclaration
Expand Down
40 changes: 40 additions & 0 deletions pkg/config/testdata/with-credentials.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
mixins:
- exec

name: hello
description: "An example Porter configuration"
version: 0.1.0
invocationImage: porter-hello:latest

credentials:
- name: SUBSCRIPTION_ID
env: AZURE_SUBSCRIPTION_ID
- name: TENANT_ID
env: AZURE_TENANT_ID
- name: CLIENT_ID
env: AZURE_CLIENT_ID
- name: CLIENT_SECRET
env: AZURE_CLIENT_SECRET
- name: kubeconfig
path: /root/.kube/config

install:
- exec:
description: "Say Hello"
command: bash
flags:
c: echo Hello World

status:
- exec:
description: "Get World Status"
command: bash
flags:
c: echo The world is on fire

uninstall:
- exec:
description: "Say Goodbye"
command: bash
flags:
c: echo Goodbye World