Skip to content

Commit aa2aaaa

Browse files
author
Prince Rachit Sinha
committed
[image-builder-bob] Use separate auth for target and base
1 parent 2bc93c2 commit aa2aaaa

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

components/image-builder-bob/cmd/proxy.go

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import (
1919

2020
var proxyOpts struct {
2121
BaseRef, TargetRef string
22-
Auth string
23-
AdditionalAuth string
22+
BaseAuth string
23+
TargetAuth string
2424
}
2525

2626
// proxyCmd represents the build command
@@ -31,15 +31,28 @@ var proxyCmd = &cobra.Command{
3131
log.Init("bob", "", true, os.Getenv("SUPERVISOR_DEBUG_ENABLE") == "true")
3232
log := log.WithField("command", "proxy")
3333

34-
authP, err := proxy.NewAuthorizerFromDockerEnvVar(proxyOpts.Auth)
34+
// Base refers to the user's base image. We prefer user given auth
35+
// for base ref
36+
authBase, err := proxy.NewAuthorizerFromEnvVar(proxyOpts.BaseAuth)
3537
if err != nil {
36-
log.WithError(err).WithField("auth", proxyOpts.Auth).Fatal("cannot unmarshal auth")
38+
log.WithError(err).WithField("auth", proxyOpts.BaseAuth).Fatal("cannot unmarshal authBase")
3739
}
38-
authA, err := proxy.NewAuthorizerFromEnvVar(proxyOpts.AdditionalAuth)
40+
// Target refers to the target registry where we want to upload the built image.
41+
// We prefer existing configuration for target auth
42+
authTarget, err := proxy.NewAuthorizerFromDockerEnvVar(proxyOpts.TargetAuth)
3943
if err != nil {
40-
log.WithError(err).WithField("auth", proxyOpts.Auth).Fatal("cannot unmarshal auth")
44+
log.WithError(err).WithField("auth", proxyOpts.TargetAuth).Fatal("cannot unmarshal authTarget")
45+
}
46+
// fallback: Add missing auth to authTarget from authBase
47+
authTarget = authTarget.AddIfNotExists(authBase)
48+
49+
// Just reuse authBase as authTarget if authTarget has not been supplied
50+
if authBase == nil {
51+
authBase = authTarget
52+
} else {
53+
// fallback: Add missing auth to authBase from authTarget
54+
authBase = authBase.AddIfNotExists(authTarget)
4155
}
42-
authP = authP.AddIfNotExists(authA)
4356

4457
baseref, err := reference.ParseNormalizedNamed(proxyOpts.BaseRef)
4558
if err != nil {
@@ -58,19 +71,22 @@ var proxyCmd = &cobra.Command{
5871
targettag = r.Tag()
5972
}
6073

61-
auth := func() docker.Authorizer { return docker.NewDockerAuthorizer(docker.WithAuthCreds(authP.Authorize)) }
74+
authB := func() docker.Authorizer { return docker.NewDockerAuthorizer(docker.WithAuthCreds(authBase.Authorize)) }
75+
authT := func() docker.Authorizer {
76+
return docker.NewDockerAuthorizer(docker.WithAuthCreds(authTarget.Authorize))
77+
}
6278
prx, err := proxy.NewProxy(&url.URL{Host: "localhost:8080", Scheme: "http"}, map[string]proxy.Repo{
6379
"base": {
6480
Host: reference.Domain(baseref),
6581
Repo: reference.Path(baseref),
6682
Tag: basetag,
67-
Auth: auth,
83+
Auth: authB,
6884
},
6985
"target": {
7086
Host: reference.Domain(targetref),
7187
Repo: reference.Path(targetref),
7288
Tag: targettag,
73-
Auth: auth,
89+
Auth: authT,
7490
},
7591
})
7692
if err != nil {
@@ -92,6 +108,6 @@ func init() {
92108
// These env vars start with `WORKSPACEKIT_` so that they aren't passed on to ring2
93109
proxyCmd.Flags().StringVar(&proxyOpts.BaseRef, "base-ref", os.Getenv("WORKSPACEKIT_BOBPROXY_BASEREF"), "ref of the base image")
94110
proxyCmd.Flags().StringVar(&proxyOpts.TargetRef, "target-ref", os.Getenv("WORKSPACEKIT_BOBPROXY_TARGETREF"), "ref of the target image")
95-
proxyCmd.Flags().StringVar(&proxyOpts.Auth, "auth", os.Getenv("WORKSPACEKIT_BOBPROXY_AUTH"), "authentication to use")
96-
proxyCmd.Flags().StringVar(&proxyOpts.AdditionalAuth, "additional-auth", os.Getenv("WORKSPACEKIT_BOBPROXY_ADDITIONALAUTH"), "additional authentication to use")
111+
proxyCmd.Flags().StringVar(&proxyOpts.BaseAuth, "base-auth", os.Getenv("WORKSPACEKIT_BOBPROXY_AUTH"), "authentication to use for base ref")
112+
proxyCmd.Flags().StringVar(&proxyOpts.TargetAuth, "target-auth", os.Getenv("WORKSPACEKIT_BOBPROXY_TARGETAUTH"), "authentication to use for target ref")
97113
}

components/image-builder-mk3/pkg/orchestrator/orchestrator.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,9 @@ func (o *Orchestrator) Build(req *protocol.BuildRequest, resp protocol.ImageBuil
330330
bobBaseref += ":latest"
331331
}
332332
wsref, err := reference.ParseNamed(wsrefstr)
333-
var additionalAuth []byte
333+
var baseRefAuth []byte
334334
if err == nil {
335-
additionalAuth, err = json.Marshal(reqauth.GetImageBuildAuthFor([]string{
335+
baseRefAuth, err = json.Marshal(reqauth.GetImageBuildAuthFor([]string{
336336
reference.Domain(wsref),
337337
}))
338338
if err != nil {
@@ -374,15 +374,15 @@ func (o *Orchestrator) Build(req *protocol.BuildRequest, resp protocol.ImageBuil
374374
{Name: "WORKSPACEKIT_BOBPROXY_BASEREF", Value: baseref},
375375
{Name: "WORKSPACEKIT_BOBPROXY_TARGETREF", Value: wsrefstr},
376376
{
377-
Name: "WORKSPACEKIT_BOBPROXY_AUTH",
377+
Name: "WORKSPACEKIT_BOBPROXY_TARGETAUTH",
378378
Secret: &wsmanapi.EnvironmentVariable_SecretKeyRef{
379379
SecretName: o.Config.PullSecret,
380380
Key: ".dockerconfigjson",
381381
},
382382
},
383383
{
384-
Name: "WORKSPACEKIT_BOBPROXY_ADDITIONALAUTH",
385-
Value: string(additionalAuth),
384+
Name: "WORKSPACEKIT_BOBPROXY_AUTH",
385+
Value: string(baseRefAuth),
386386
},
387387
{Name: "SUPERVISOR_DEBUG_ENABLE", Value: fmt.Sprintf("%v", log.Log.Logger.IsLevelEnabled(logrus.DebugLevel))},
388388
},

0 commit comments

Comments
 (0)