-
Notifications
You must be signed in to change notification settings - Fork 25
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
feat: Add gomod git auth #509
base: main
Are you sure you want to change the base?
Changes from all commits
241ae1e
b9f76f4
e1f339c
c057ad0
5546b9e
ed649d4
ae49155
f989f8f
a6fe9fe
c82a6e0
5991ee0
a19ba73
dbbc366
51f3e8d
c7d7610
161629d
d66c8cd
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 |
---|---|---|
@@ -1,14 +1,20 @@ | ||
package dalec | ||
|
||
import ( | ||
"bytes" | ||
_ "embed" | ||
"fmt" | ||
"path/filepath" | ||
"sort" | ||
|
||
"github.com/moby/buildkit/client/llb" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
const ( | ||
gomodCacheDir = "/go/pkg/mod" | ||
gomodCacheDir = "/go/pkg/mod" | ||
gitConfigMountpoint = "/dev/shm/git" | ||
scriptRelativePath = "/go_mod_download.sh" | ||
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. Can you give this variable a better name? |
||
) | ||
|
||
func (s *Source) isGomod() bool { | ||
|
@@ -32,7 +38,12 @@ func (s *Spec) HasGomods() bool { | |
|
||
func withGomod(g *SourceGenerator, srcSt, worker llb.State, opts ...llb.ConstraintsOpt) func(llb.State) llb.State { | ||
return func(in llb.State) llb.State { | ||
workDir := "/work/src" | ||
const ( | ||
fourKB = 4096 | ||
workDir = "/work/src" | ||
scriptMountPoint = "/tmp/mnt" | ||
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. Let's give this a better path to make sure it's not overwriting anything in the builder image (just in case). |
||
) | ||
|
||
joinedWorkDir := filepath.Join(workDir, g.Subpath) | ||
srcMount := llb.AddMount(workDir, srcSt) | ||
|
||
|
@@ -41,10 +52,17 @@ func withGomod(g *SourceGenerator, srcSt, worker llb.State, opts ...llb.Constrai | |
paths = []string{"."} | ||
} | ||
|
||
sort.Strings(paths) | ||
script := g.gitconfigGeneratorScript() | ||
scriptPath := filepath.Join(scriptMountPoint, scriptRelativePath) | ||
|
||
for _, path := range paths { | ||
in = worker.Run( | ||
ShArgs("go mod download"), | ||
ShArgs(scriptPath), | ||
llb.AddEnv("GOPATH", "/go"), | ||
g.withGomodSecretsAndSockets(), | ||
llb.AddMount(scriptMountPoint, script), | ||
llb.AddMount(gitConfigMountpoint, llb.Scratch(), llb.Tmpfs(llb.TmpfsSize(fourKB))), // to house the gitconfig, which has secrets | ||
llb.Dir(filepath.Join(joinedWorkDir, path)), | ||
srcMount, | ||
WithConstraints(opts...), | ||
|
@@ -54,6 +72,101 @@ func withGomod(g *SourceGenerator, srcSt, worker llb.State, opts ...llb.Constrai | |
} | ||
} | ||
|
||
func (g *SourceGenerator) gitconfigGeneratorScript() llb.State { | ||
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. Have you considered utilizing |
||
var ( | ||
script bytes.Buffer | ||
noop = func() {} | ||
|
||
createPreamble = func() { | ||
fmt.Fprintln(&script, `set -eu`) | ||
fmt.Fprintf(&script, `ln -sf %s/.gitconfig "${HOME}/.gitconfig"`, gitConfigMountpoint) | ||
script.WriteRune('\n') | ||
} | ||
) | ||
|
||
fmt.Fprintln(&script, `#!/usr/bin/env sh`) | ||
|
||
for host, auth := range g.Gomod.Auth { | ||
// Only do this the first time through the loop | ||
createPreamble() | ||
createPreamble = noop | ||
|
||
var headerArg string | ||
if auth.Header != "" { | ||
headerArg = fmt.Sprintf(`Authorization: ${%s}`, auth.Header) | ||
} | ||
|
||
if auth.Token != "" && headerArg == "" { | ||
line := fmt.Sprintf(`tkn="$(echo -n "x-access-token:${%s}" | base64)"`, auth.Token) | ||
fmt.Fprintln(&script, line) | ||
|
||
headerArg = `Authorization: basic ${tkn}` | ||
} | ||
|
||
if headerArg != "" { | ||
fmt.Fprintf(&script, `git config --global http."https://%s".extraheader "%s"`, host, headerArg) | ||
script.WriteRune('\n') | ||
continue | ||
} | ||
|
||
username := "git" | ||
if auth.SSH != nil { | ||
if auth.SSH.Username != "" { | ||
username = auth.SSH.Username | ||
} | ||
|
||
fmt.Fprintf(&script, `git config --global "url.ssh://%[1]s@%[2]s/.insteadOf" https://%[2]s/`, username, host) | ||
script.WriteRune('\n') | ||
} | ||
} | ||
|
||
fmt.Fprintln(&script, "go mod download") | ||
return llb.Scratch().File(llb.Mkfile(scriptRelativePath, 0o755, script.Bytes())) | ||
} | ||
|
||
func (g *SourceGenerator) withGomodSecretsAndSockets() llb.RunOption { | ||
return runOptionFunc(func(ei *llb.ExecInfo) { | ||
if g.Gomod == nil { | ||
return | ||
} | ||
|
||
var ( | ||
setenv = func() { | ||
llb.AddEnv( | ||
"GIT_SSH_COMMAND", | ||
`ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no`, | ||
).SetRunOption(ei) | ||
} | ||
|
||
noop = func() {} | ||
) | ||
|
||
secrets := make(map[string]struct{}, len(g.Gomod.Auth)) | ||
for _, auth := range g.Gomod.Auth { | ||
if auth.Token != "" { | ||
secrets[auth.Token] = struct{}{} | ||
continue | ||
} | ||
|
||
if auth.Header != "" { | ||
secrets[auth.Header] = struct{}{} | ||
continue | ||
} | ||
|
||
if auth.SSH != nil && auth.SSH.ID != "" { | ||
llb.AddSSHSocket(llb.SSHID(auth.SSH.ID)).SetRunOption(ei) | ||
|
||
setenv() | ||
setenv = noop | ||
} | ||
} | ||
|
||
for secret := range secrets { | ||
secretToEnv(secret).SetRunOption(ei) | ||
} | ||
}) | ||
} | ||
|
||
func (s *Spec) gomodSources() map[string]Source { | ||
sources := map[string]Source{} | ||
for name, src := range s.Sources { | ||
|
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.
Looks like this is unused?