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 a call to MkdirAll for the credentials directory path. #381

Merged
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
17 changes: 15 additions & 2 deletions pkg/porter/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ func (g *CredentialOptions) validateCredName(args []string) error {
return nil
}

// GenerateCredentials builds a new credential set based on the given options. This can be either
// a silent build, based on the opts.Silent flag, or interactive using a survey. Returns an
// error if unable to generate credentials
func (p *Porter) GenerateCredentials(opts CredentialOptions) error {

//TODO make this work for either porter.yaml OR a bundle
Expand Down Expand Up @@ -168,13 +171,23 @@ func (p *Porter) GenerateCredentials(opts CredentialOptions) error {
fmt.Fprintf(p.Out, "%v", string(data))
return nil
}

credentialsDir, err := p.Config.GetCredentialsDir()
if err != nil {
return errors.Wrap(err, "unable to get credentials directory")
}
// Make the credentials path if it doesn't exist. MkdirAll does nothing if it already exists
// Readable, writable only by the user
err = p.Config.FileSystem.MkdirAll(credentialsDir, 0700)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't handling the error, can you add that quick?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeahhhh i have that in a commit i noticed it wasn't there after i pusheddddd

if err != nil {
return errors.Wrap(err, "unable to create credentials directory")
}
dest, err := p.Config.GetCredentialPath(genOpts.Name)
if err != nil {
return errors.Wrap(err, "unable to determine credentials directory")
return errors.Wrap(err, "unable to determine credentials path")
}

fmt.Fprintf(p.Out, "Saving credential to %s\n", dest)

err = p.Context.FileSystem.WriteFile(dest, data, 0600)
if err != nil {
return errors.Wrapf(err, "couldn't write credential file %s", dest)
Expand Down
70 changes: 70 additions & 0 deletions pkg/porter/credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
printer "github.com/deislabs/porter/pkg/printer"

"github.com/deislabs/duffle/pkg/bundle"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -231,3 +232,72 @@ good-creds now`},
})
}
}

func TestGenerateNoCredentialDirectory(t *testing.T) {
p := NewTestPorter(t)
p.TestConfig.SetupPorterHome()
p.CNAB = &TestCNABProvider{}

opts := CredentialOptions{
Silent: true,
}
opts.Name = "name"

//Check if the credentials directory exists in the FS. It shouldn't.
credDir, err := p.Config.GetCredentialsDir()
require.NoError(t, err, "should have been able to get credentials directory path")
vdice marked this conversation as resolved.
Show resolved Hide resolved
credDirExists, err := p.Porter.Context.FileSystem.DirExists(credDir)
require.NoError(t, err, "shouldn't have failed on dir exists")
require.False(t, credDirExists, "there should not have been a credential directory for this test")

//Now generate the credentials. After completion, the directory should now exist. It should be
//created if it does not exit
err = p.GenerateCredentials(opts)
assert.NoError(t, err, "credential generation should have been successful")
credDirExists, err = p.Porter.Context.FileSystem.DirExists(credDir)
assert.NoError(t, err, "shouldn't have gotten an error checking credential directory after generate")
assert.True(t, credDirExists, "should have been a credential directory after the generation")

//Verify that the credential was actually created.
path, err := p.Porter.Config.GetCredentialPath("name")
assert.NoError(t, err, "couldn't get credential path")
credFileExists, err := p.Porter.Context.FileSystem.Exists(path)
assert.True(t, credFileExists, "expected the file %s to exist", path)
assert.NoError(t, err, "should have been able to check if get credential path exists")
}

func TestGenerateCredentialDirectoryExists(t *testing.T) {
p := NewTestPorter(t)
p.TestConfig.SetupPorterHome()
p.CNAB = &TestCNABProvider{}

opts := CredentialOptions{
Silent: true,
}
opts.Name = "name"

//Create the credentials directory
credDir, err := p.Config.GetCredentialsDir()
require.NoError(t, err, "should have been able to get credentials directory path")
err = p.Config.FileSystem.MkdirAll(credDir, 0600)
require.NoError(t, err, "should have been able to make directory path")

//Verify the directory does in fact, exist.
credDirExists, err := p.Porter.Context.FileSystem.DirExists(credDir)
require.NoError(t, err, "shouldn't have failed on dir exists")
require.True(t, credDirExists, "there should have been a credential directory for this test")

//Generate the credential now. The directory does exist, so there should be no error.
err = p.GenerateCredentials(opts)
assert.NoError(t, err, "credential generation should have been successful")
credDirExists, err = p.Porter.Context.FileSystem.DirExists(credDir)
assert.NoError(t, err, "shouldn't have gotten an error checking credential directory after generate")
assert.True(t, credDirExists, "should have been a credential directory after the generation")

//Verify we wrote the credential file.
path, err := p.Porter.Config.GetCredentialPath("name")
assert.NoError(t, err, "couldn't get credential path")
credFileExists, err := p.Porter.Context.FileSystem.Exists(path)
assert.True(t, credFileExists, "expected the file %s to exist", path)
assert.NoError(t, err, "should have been able to check if get credential path exists")
}