-
Notifications
You must be signed in to change notification settings - Fork 37
Add support for SSH known hosts in git sources #729
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 3 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 | ||||
---|---|---|---|---|---|---|
|
@@ -34,6 +34,11 @@ type GitAuth struct { | |||||
// Note: This should not have the *actual* secret value, just the name of | ||||||
// the secret which was specified as a build secret. | ||||||
SSH string `yaml:"ssh,omitempty" json:"ssh,omitempty"` | ||||||
// KnownHosts is the SSH known hosts data to use for host key verification. | ||||||
// This should be the actual known hosts content (can be expanded from build args). | ||||||
// When provided, SSH connections will verify the host key against this data. | ||||||
// When not provided, BuildKit will use TOFU (Trust On First Use). | ||||||
KnownHosts string `yaml:"knownHosts,omitempty" json:"knownHosts,omitempty"` | ||||||
} | ||||||
|
||||||
type GomodGitAuth struct { | ||||||
|
@@ -81,6 +86,10 @@ func (a *GitAuth) SetGitOption(gi *llb.GitInfo) { | |||||
if a.SSH != "" { | ||||||
gi.MountSSHSock = a.SSH | ||||||
} | ||||||
|
||||||
if a.KnownHosts != "" { | ||||||
gi.KnownSSHHosts = a.KnownHosts | ||||||
} | ||||||
} | ||||||
|
||||||
func (src *SourceGit) IsDir() bool { | ||||||
|
@@ -156,7 +165,17 @@ func (src *SourceGit) processBuildArgs(lex *shell.Lex, args map[string]string, a | |||||
if err != nil { | ||||||
errs = append(errs, err) | ||||||
} | ||||||
if len(errs) > 1 { | ||||||
|
||||||
// Process KnownHosts in Auth if present | ||||||
if src.Auth.KnownHosts != "" { | ||||||
updated, err = expandArgs(lex, src.Auth.KnownHosts, args, allowArg) | ||||||
src.Auth.KnownHosts = updated | ||||||
if err != nil { | ||||||
errs = append(errs, err) | ||||||
} | ||||||
} | ||||||
|
||||||
if len(errs) > 0 { | ||||||
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. The condition was changed from
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
return fmt.Errorf("failed to process build args for git source: %w", stderrors.Join(errs...)) | ||||||
} | ||||||
return nil | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,6 +95,42 @@ git auth section (shown below with default values): | |
Note: These are secret names which are used to reference the secrets provided | ||
by the client, not the actual secret values. | ||
|
||
#### SSH Known Hosts | ||
|
||
For SSH-based git URLs, you can specify known SSH host keys to improve security | ||
and avoid Trust-On-First-Use (TOFU) behavior. When known hosts are provided, | ||
the SSH connection will verify the host key against the specified keys and fail | ||
if they don't match. | ||
|
||
```yaml | ||
someSource1: | ||
git: | ||
url: [email protected]:myOrg/myRepo.git | ||
commit: 1234567890abcdef | ||
auth: | ||
knownHosts: | | ||
github.com ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC7vbqbLJofwIHMHnSVPP0k+aLU6X5OtN6a1r9K4kS... | ||
github.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl | ||
``` | ||
|
||
You can also use build arguments to dynamically provide known hosts: | ||
|
||
```yaml | ||
someSource1: | ||
git: | ||
url: [email protected]:myOrg/myRepo.git | ||
commit: 1234567890abcdef | ||
auth: | ||
knownHosts: ${KNOWN_HOSTS} | ||
``` | ||
|
||
Then build with: `docker build --build-arg KNOWN_HOSTS="$(cat ~/.ssh/known_hosts)" ...` | ||
|
||
When known hosts are not specified, BuildKit will use Trust-On-First-Use (TOFU) | ||
behavior, where it will connect to the SSH server, retrieve the host key, and | ||
use that for the connection. Specifying known hosts avoids this extra connection | ||
and provides better security by ensuring you're connecting to the expected server. | ||
|
||
### HTTP | ||
|
||
HTTP sources fetch a file from an HTTP URL. The HTTP source type is considered to be a "file" source. | ||
|
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.
I wouldn't consider
KnownHosts
as auth.Perthaps stick this dirrectly into
SourceGit
asSSHKnownHosts
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.
@copilot ^
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.
You're absolutely right! I've moved the
KnownHosts
field fromGitAuth
toSourceGit
asSSHKnownHosts
. This makes much more sense since known hosts are about host verification rather than authentication.The changes include:
KnownHosts
→SSHKnownHosts
inSourceGit
structbaseState()
to use BuildKit'sllb.KnownSSHHosts()
option directlyprocessBuildArgs()
to handle the new field locationAll tests pass with this refactoring. Thanks for the great feedback! (commit 2a458b3)