Skip to content

Commit

Permalink
fix: Create directory for file and credential_process commands (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
patricksanders committed Jun 22, 2021
1 parent 560405f commit 4a235a7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
6 changes: 6 additions & 0 deletions cmd/credential_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,17 @@ func writeConfigFile(roles []string, destination string) error {
ini.PrettyEqual = true

if util.FileExists(destination) {
// There's an existing config file, so we'll load it in and update the existing contents
configINI, err = ini.Load(destination)
if err != nil {
return err
}
} else {
// Config file doesn't exist yet. Create it with the same perms as awscli
err = util.CreateFile(destination, 0700, 0600)
if err != nil {
return err
}
configINI = ini.Empty()
}

Expand Down
8 changes: 7 additions & 1 deletion cmd/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,17 @@ func writeCredentialsFile(credentials *creds.AwsCredentials, profile, filename s
ini.PrettyEqual = true

if util.FileExists(filename) {
// There's an existing credentials file, so we'll load it in and update the existing contents
credentialsINI, err = ini.Load(filename)
if err != nil {
return err
}
} else {
// Credentials file doesn't exist yet. Create it with the same perms as awscli
err = util.CreateFile(filename, 0700, 0600)
if err != nil {
return err
}
credentialsINI = ini.Empty()
}

Expand All @@ -186,5 +192,5 @@ func writeCredentialsFile(credentials *creds.AwsCredentials, profile, filename s
return err
}

return nil
return err
}
24 changes: 24 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ package util

import (
"fmt"
"io/fs"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"

Expand Down Expand Up @@ -98,6 +100,28 @@ func FileExists(path string) bool {
return err == nil
}

func createEmptyFile(filename string, filemode fs.FileMode) error {
d := []byte("")
err := ioutil.WriteFile(filename, d, filemode)
return err
}

// CreateFile safely creates an empty file and any missing directory structure.
// The directories will have directoryPerm filemode. The file will have filePerm filemode.
func CreateFile(filename string, directoryPerm, filePerm fs.FileMode) error {
var err error

// Make sure the directory exists, using the same perms that awscli uses (0600)
dir := filepath.Dir(filename)
err = os.MkdirAll(dir, directoryPerm)
if err != nil {
return err
}

err = createEmptyFile(filename, filePerm)
return err
}

// WriteError writes a status code and plaintext error to the provided http.ResponseWriter.
// The error is written as plaintext so AWS SDKs will display it inline with an error message.
func WriteError(w http.ResponseWriter, message string, status int) {
Expand Down

0 comments on commit 4a235a7

Please sign in to comment.