Skip to content

Commit

Permalink
Various bug fixes (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdsmithit authored Apr 5, 2023
1 parent 60ec9db commit 8b897ee
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 20 deletions.
14 changes: 9 additions & 5 deletions src/certificateHandler/certificateHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ import (
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"path/filepath"
"os"

"github.com/dfds/roles-anywhere-helper/fileHandler"
)

func CreatePemFileFromPemBlock(pemData *pem.Block, directory string, fileName string) error {

fileOut, err := fileHandler.CreateFile(filepath.Join(directory, fileName))
fileOut, err := fileHandler.CreateFile(directory, fileName)

if err != nil {
return err
if err != os.ErrExist {
return err
}
}

defer fileOut.Close()
Expand All @@ -30,10 +32,12 @@ func CreatePemFileFromPemBlock(pemData *pem.Block, directory string, fileName st

func CreatePemFileFromString(pemData string, directory string, fileName string) error {

fileOut, err := fileHandler.CreateFile(filepath.Join(directory, fileName))
fileOut, err := fileHandler.CreateFile(directory, fileName)

if err != nil {
return err
if err != os.ErrExist {
return err
}
}

defer fileOut.Close()
Expand Down
6 changes: 3 additions & 3 deletions src/cmd/setup-all.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ func setupAllCmdRun(cmd *cobra.Command, args []string) {
profileArn, _ := cmd.Flags().GetString(flags.ProfileArn)
roleArn, _ := cmd.Flags().GetString(flags.RoleArn)
profileNameRoles, _ := cmd.Flags().GetString(flags.ProfileNameRolesAnywhere)
rolesAnywhereRegion, _ := cmd.Flags().GetString(flags.RegionNameRolesAnywhereDesc)
pcaRegion, _ := cmd.Flags().GetString(flags.RegionNameAcmPcaDesc)
acmRegion, _ := cmd.Flags().GetString(flags.RegionNameAcmDesc)
rolesAnywhereRegion, _ := cmd.Flags().GetString(flags.RolesAnywhereRegion)
pcaRegion, _ := cmd.Flags().GetString(flags.PcaRegion)
acmRegion, _ := cmd.Flags().GetString(flags.AcmRegion)

acmAccessKey, _ := cmd.Flags().GetString(flags.AccessKeyAcm)
acmSecretAccessKey, _ := cmd.Flags().GetString(flags.SecretAccessKeyAcm)
Expand Down
12 changes: 6 additions & 6 deletions src/credentialService/credentialService.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package credentialService
import (
"fmt"
"os"
"path/filepath"
"path"

"github.com/dfds/roles-anywhere-helper/executableFinder"
"github.com/dfds/roles-anywhere-helper/fileHandler"
Expand All @@ -15,7 +15,7 @@ import (
const awsSignHelpName = "aws_signing_helper"
const awsCredDir = "/.aws/credentials"

var CredentialsFilePath = GetCredentialsFilePath()
var CredentialsFilePath = path.Join(GetDefaultCredentialsFilePath(), awsCredDir)

type CredentialsFileTemplate struct {
CredentialProcess string `ini:"credential_process,omitempty"`
Expand All @@ -28,8 +28,8 @@ func Configure(profileName string, certificatePath string, privateKeyPath string
err := executableFinder.CommandExists(awsSignHelpName)
Check(err)

file, err := fileHandler.CreateFile(GetCredentialsFilePath())
file, err := fileHandler.CreateFile(GetDefaultCredentialsFilePath(), awsCredDir)

if err != nil {
fmt.Println("Credential file already exists:", err)
} else {
Expand All @@ -42,10 +42,10 @@ func Configure(profileName string, certificatePath string, privateKeyPath string
fmt.Printf("Profile %s set", profileName)
}

func GetCredentialsFilePath() string {
func GetDefaultCredentialsFilePath() string {
homeDir, err := os.UserHomeDir()
Check(err)
return filepath.Join(homeDir, awsCredDir)
return homeDir
}

func ProcessCredentialProcessTemplate(certificatePath string, privateKeyPath string, trustAnchorArn string, profileArn string, roleArn string, region string) CredentialsFileTemplate {
Expand Down
4 changes: 2 additions & 2 deletions src/credentialService/credentialService_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ func TestGetCredentialsFilePath(t *testing.T) {
if err != nil {
t.Fatal("No access to 'HOME' directory: ", err)
}
expected := filepath.Join(home, ".aws/credentials")
actual := GetCredentialsFilePath()
expected := home
actual := GetDefaultCredentialsFilePath()
assert.Equal(t, expected, actual)
}

Expand Down
13 changes: 9 additions & 4 deletions src/fileHandler/fileHandler.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package fileHandler

import (
"fmt"
"os"
"path/filepath"
)

func CreateFile(filePath string) (*os.File, error) {
func CreateFile(filePath string, fileName string) (*os.File, error) {
if _, err := os.Stat(filePath); os.IsNotExist(err) {
os.MkdirAll(filePath, 0755)
}
absoluteFilePath := filepath.Join(filePath, fileName)

_, e := os.Stat(filePath)
_, e := os.Stat(absoluteFilePath)

if e == nil {
return nil, os.ErrExist
fmt.Printf("File Path already exisits.... Overwriting %s", absoluteFilePath)
return os.Open(absoluteFilePath)
}
return os.Create(filePath)

return os.Create(absoluteFilePath)
}

0 comments on commit 8b897ee

Please sign in to comment.