Skip to content

Commit

Permalink
Try escaping, that's a cool trick
Browse files Browse the repository at this point in the history
I couldn’t find a go shell escaping lib to do this for me so I just did spaces.
  • Loading branch information
keithduncan committed Aug 31, 2021
1 parent 867eadf commit 8766f51
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions s3secrets-helper/secrets/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,20 +205,29 @@ func handleGitCredentials(conf Config, results <-chan getResult) error {
continue
}
log.Printf("Adding git-credentials in %s/%s as a credential helper", r.bucket, r.key)
helpers = append(helpers, fmt.Sprintf(
"credential.helper=%s %s %s",
conf.GitCredentialHelper, r.bucket, r.key,
))

// Replace spaces ' ' in the helper path with an escaped space '\ '
escapedCredentialHelper := strings.ReplaceAll(conf.GitCredentialHelper, " ", "\\ ")

helper := fmt.Sprintf("credential.helper=%s %s %s", escapedCredentialHelper, r.bucket, r.key)

helpers = append(helpers, helper)
}
if len(helpers) == 0 {
return nil
}

// Build an environment variable for interpretation by a shell
var singleQuotedHelpers []string
for helper := range helpers {
for _, helper := range helpers {
// Escape any escape sequences, the shell will interpret the first level
// of escaping.

// Replace backslash '\' with double backslash '\\'
helper = strings.ReplaceAll(helper, "\\", "\\\\")

singleQuotedHelpers = append(singleQuotedHelpers, "'" + helper + "'")
}

env := "GIT_CONFIG_PARAMETERS=\"" + strings.Join(singleQuotedHelpers, " ") + "\"\n"

if _, err := io.WriteString(conf.EnvSink, env); err != nil {
Expand Down

0 comments on commit 8766f51

Please sign in to comment.