Skip to content

Commit 991f3ab

Browse files
committed
Fix authorization
Fixed authorization by setting keychain for the pack client. This is required since recently we set pull policy to "always". Signed-off-by: Matej Vašek <mvasek@redhat.com>
1 parent d5dc82c commit 991f3ab

File tree

1 file changed

+56
-23
lines changed

1 file changed

+56
-23
lines changed

hack/update-builder.go

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,12 @@ func buildBuilderImage(ctx context.Context, variant, arch string) (string, error
102102
}
103103
newBuilderImage := "ghcr.io/knative/builder-jammy-" + variant
104104
newBuilderImageTagged := newBuilderImage + ":" + *release.Name + "-" + arch
105-
dockerUser := "gh-action"
106-
dockerPassword := os.Getenv("GITHUB_TOKEN")
107105

108106
ref, err := name.ParseReference(newBuilderImageTagged)
109107
if err != nil {
110108
return "", fmt.Errorf("cannot parse reference to builder target: %w", err)
111109
}
112-
desc, err := remote.Head(ref, remote.WithAuth(auth{dockerUser, dockerPassword}))
110+
desc, err := remote.Head(ref, remote.WithAuthFromKeychain(DefaultKeychain))
113111
if err == nil {
114112
fmt.Fprintln(os.Stderr, "The image has been already built.")
115113
return newBuilderImage + "@" + desc.Digest.String(), nil
@@ -131,8 +129,7 @@ func buildBuilderImage(ctx context.Context, variant, arch string) (string, error
131129
return "", fmt.Errorf("cannot patch java buildpacks: %w", err)
132130
}
133131
addGoAndRustBuildpacks(&builderConfig)
134-
135-
packClient, err := pack.NewClient()
132+
packClient, err := pack.NewClient(pack.WithKeychain(ghKeychain{}))
136133
if err != nil {
137134
return "", fmt.Errorf("cannot create pack client: %w", err)
138135
}
@@ -168,21 +165,17 @@ func buildBuilderImage(ctx context.Context, variant, arch string) (string, error
168165
return "", fmt.Errorf("cannot create docker client")
169166
}
170167

171-
authConfig := registry.AuthConfig{
172-
Username: dockerUser,
173-
Password: dockerPassword,
174-
}
175-
bs, err := json.Marshal(&authConfig)
176-
if err != nil {
177-
return "", fmt.Errorf("cannot marshal credentials: %w", err)
178-
}
179-
imagePushOptions := image.PushOptions{
180-
All: false,
181-
RegistryAuth: base64.StdEncoding.EncodeToString(bs),
182-
}
168+
pushImage := func(img string) (string, error) {
169+
regAuth, err := dockerDaemonAuthStr(img)
170+
if err != nil {
171+
return "", fmt.Errorf("cannot get credentials: %w", err)
172+
}
173+
imagePushOptions := image.PushOptions{
174+
All: false,
175+
RegistryAuth: regAuth,
176+
}
183177

184-
pushImage := func(image string) (string, error) {
185-
rc, err := dockerClient.ImagePush(ctx, image, imagePushOptions)
178+
rc, err := dockerClient.ImagePush(ctx, img, imagePushOptions)
186179
if err != nil {
187180
return "", fmt.Errorf("cannot initialize image push: %w", err)
188181
}
@@ -265,10 +258,7 @@ func buildBuilderImageMultiArch(ctx context.Context, variant string) error {
265258
}
266259

267260
remoteOpts := []remote.Option{
268-
remote.WithAuth(authn.FromConfig(authn.AuthConfig{
269-
Username: "gh-action",
270-
Password: os.Getenv("GITHUB_TOKEN"),
271-
})),
261+
remote.WithAuthFromKeychain(DefaultKeychain),
272262
}
273263

274264
idx := mutate.IndexMediaType(empty.Index, types.DockerManifestList)
@@ -745,3 +735,46 @@ func newGHClient(ctx context.Context) *github.Client {
745735
AccessToken: os.Getenv("GITHUB_TOKEN"),
746736
})))
747737
}
738+
739+
var DefaultKeychain = authn.NewMultiKeychain(ghKeychain{}, authn.DefaultKeychain)
740+
741+
type ghKeychain struct{}
742+
743+
func (g ghKeychain) Resolve(resource authn.Resource) (authn.Authenticator, error) {
744+
if resource.RegistryStr() != "ghcr.io" {
745+
return authn.Anonymous, nil
746+
}
747+
return &authn.Basic{
748+
Username: "gh-action",
749+
Password: os.Getenv("GITHUB_TOKEN"),
750+
}, nil
751+
}
752+
753+
func dockerDaemonAuthStr(img string) (string, error) {
754+
ref, err := name.ParseReference(img)
755+
if err != nil {
756+
return "", err
757+
}
758+
759+
a, err := DefaultKeychain.Resolve(ref.Context())
760+
if err != nil {
761+
return "", err
762+
}
763+
764+
ac, err := a.Authorization()
765+
if err != nil {
766+
return "", err
767+
}
768+
769+
authConfig := registry.AuthConfig{
770+
Username: ac.Username,
771+
Password: ac.Password,
772+
}
773+
774+
bs, err := json.Marshal(&authConfig)
775+
if err != nil {
776+
return "", err
777+
}
778+
779+
return base64.StdEncoding.EncodeToString(bs), nil
780+
}

0 commit comments

Comments
 (0)