-
Notifications
You must be signed in to change notification settings - Fork 353
Add SSH config and env var #3938
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -838,6 +838,23 @@ func (e *Executor) defaultCheckoutPhase(ctx context.Context) (retErr error) { | |||||||||||||
| addRepositoryHostToSSHKnownHosts(ctx, e.shell, e.Repository) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| sshKeyPath, cleanupSSHKey, err := e.prepareGitSSHKey() | ||||||||||||||
| if err != nil { | ||||||||||||||
| return fmt.Errorf("preparing git ssh key: %w", err) | ||||||||||||||
| } | ||||||||||||||
| if cleanupSSHKey != nil { | ||||||||||||||
| defer func() { | ||||||||||||||
| if cleanupErr := cleanupSSHKey(); cleanupErr != nil { | ||||||||||||||
| cleanupErr = fmt.Errorf("cleaning up git ssh key %q: %w", sshKeyPath, cleanupErr) | ||||||||||||||
| if err == nil { | ||||||||||||||
| err = cleanupErr | ||||||||||||||
| } else { | ||||||||||||||
| err = errors.Join(err, cleanupErr) | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+849
to
+853
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
| } | ||||||||||||||
| }() | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| var mirrorDir string | ||||||||||||||
|
|
||||||||||||||
| // If we can, get a mirror of the git repository to use for reference later | ||||||||||||||
|
|
@@ -1065,6 +1082,77 @@ func (e *Executor) defaultCheckoutPhase(ctx context.Context) (retErr error) { | |||||||||||||
| return nil | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // prepareGitSSHKey materialises the configured GitSSHKey into a private | ||||||||||||||
| // directory next to the build checkout and points GIT_SSH_COMMAND at it for | ||||||||||||||
| // the duration of the checkout phase. It returns the path to the key file, a | ||||||||||||||
| // cleanup function that removes the key and restores any previous | ||||||||||||||
| // GIT_SSH_COMMAND, and an error. If no key is configured both the path and | ||||||||||||||
| // cleanup are zero. | ||||||||||||||
| // | ||||||||||||||
| // Only the default checkout phase invokes this; custom checkout hooks must | ||||||||||||||
| // arrange their own credentials. | ||||||||||||||
| func (e *Executor) prepareGitSSHKey() (_ string, _ func() error, retErr error) { | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naming the return args has clear readability and documentation upsides over using
Suggested change
|
||||||||||||||
| if e.GitSSHKey == "" { | ||||||||||||||
| return "", nil, nil | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| checkoutPath, exists := e.shell.Env.Get("BUILDKITE_BUILD_CHECKOUT_PATH") | ||||||||||||||
| if !exists || checkoutPath == "" { | ||||||||||||||
| return "", nil, errors.New("BUILDKITE_BUILD_CHECKOUT_PATH is not set") | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| parentDir := filepath.Dir(checkoutPath) | ||||||||||||||
| if err := os.MkdirAll(parentDir, 0o700); err != nil { | ||||||||||||||
| return "", nil, fmt.Errorf("creating ssh key parent directory %q: %w", parentDir, err) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| pattern := ".buildkite-ssh-key-" | ||||||||||||||
| if e.PipelineSlug != "" { | ||||||||||||||
| pattern += badCharsRE.ReplaceAllString(e.PipelineSlug, "-") + "-" | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // os.MkdirTemp creates the directory with mode 0o700 on Unix, giving the | ||||||||||||||
| // key file inside an additional layer of protection from sibling builds. | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sibling builds would have to be running under a different uid to be prevented from accessing inside a dir with mode 0o700, right? |
||||||||||||||
| sshKeyDir, err := os.MkdirTemp(parentDir, pattern) | ||||||||||||||
| if err != nil { | ||||||||||||||
| return "", nil, fmt.Errorf("creating ssh key directory: %w", err) | ||||||||||||||
| } | ||||||||||||||
| defer func() { | ||||||||||||||
| if retErr != nil { | ||||||||||||||
| _ = os.RemoveAll(sshKeyDir) | ||||||||||||||
| } | ||||||||||||||
| }() | ||||||||||||||
|
|
||||||||||||||
| sshKeyPath := filepath.Join(sshKeyDir, "id") | ||||||||||||||
| // Most SSH key parsers require a trailing newline; tolerate either form | ||||||||||||||
| // of input and always write a single one. | ||||||||||||||
| keyBytes := []byte(strings.TrimRight(e.GitSSHKey, "\n") + "\n") | ||||||||||||||
| if err := os.WriteFile(sshKeyPath, keyBytes, 0o600); err != nil { | ||||||||||||||
| return "", nil, fmt.Errorf("writing ssh key file: %w", err) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| previousGitSSHCommand, hadPreviousGitSSHCommand := e.shell.Env.Get("GIT_SSH_COMMAND") | ||||||||||||||
| e.shell.Env.Set("GIT_SSH_COMMAND", gitSSHCommandForKeyFile(sshKeyPath)) | ||||||||||||||
|
|
||||||||||||||
| cleanup := func() error { | ||||||||||||||
| if hadPreviousGitSSHCommand { | ||||||||||||||
| e.shell.Env.Set("GIT_SSH_COMMAND", previousGitSSHCommand) | ||||||||||||||
| } else { | ||||||||||||||
| e.shell.Env.Remove("GIT_SSH_COMMAND") | ||||||||||||||
| } | ||||||||||||||
| if err := os.RemoveAll(sshKeyDir); err != nil && !os.IsNotExist(err) { | ||||||||||||||
| return err | ||||||||||||||
| } | ||||||||||||||
| return nil | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| return sshKeyPath, cleanup, nil | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| func gitSSHCommandForKeyFile(path string) string { | ||||||||||||||
| return fmt.Sprintf("ssh -i %s -o IdentitiesOnly=yes", shellwords.Quote(path)) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // gitFetchWithFallback run git fetch for refspecs, when it fails on recoverable reason, it will retry fetching | ||||||||||||||
| // all heads and refs. | ||||||||||||||
| func gitFetchWithFallback(ctx context.Context, shell *shell.Shell, gitFetchFlags string, refspecs ...string) error { | ||||||||||||||
|
|
||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't really a change, it's just a result of using
gofumptfor my formatting rather thangofmt, I'm happy to default to the latter, though