Skip to content

Commit

Permalink
Resolves containers#13629 Add RegistryAuthHeader to manifest push
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Montleon <jmontleo@redhat.com>
  • Loading branch information
jason authored and jmontleon committed Mar 25, 2022
1 parent a416fd6 commit f8fa9b5
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
11 changes: 9 additions & 2 deletions pkg/bindings/manifests/manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (

"github.com/blang/semver"
"github.com/containers/image/v5/manifest"
imageTypes "github.com/containers/image/v5/types"
"github.com/containers/podman/v4/pkg/api/handlers"
"github.com/containers/podman/v4/pkg/auth"
"github.com/containers/podman/v4/pkg/bindings"
"github.com/containers/podman/v4/pkg/bindings/images"
"github.com/containers/podman/v4/version"
Expand Down Expand Up @@ -179,6 +181,11 @@ func Push(ctx context.Context, name, destination string, options *images.PushOpt
return "", err
}

header, err := auth.MakeXRegistryAuthHeader(&imageTypes.SystemContext{AuthFilePath: options.GetAuthfile()}, options.GetUsername(), options.GetPassword())
if err != nil {
return "", err
}

params, err := options.ToParams()
if err != nil {
return "", err
Expand All @@ -192,11 +199,11 @@ func Push(ctx context.Context, name, destination string, options *images.PushOpt

var response *bindings.APIResponse
if bindings.ServiceVersion(ctx).GTE(semver.MustParse("4.0.0")) {
response, err = conn.DoRequest(ctx, nil, http.MethodPost, "/manifests/%s/registry/%s", params, nil, name, destination)
response, err = conn.DoRequest(ctx, nil, http.MethodPost, "/manifests/%s/registry/%s", params, header, name, destination)
} else {
params.Set("image", name)
params.Set("destination", destination)
response, err = conn.DoRequest(ctx, nil, http.MethodPost, "/manifests/%s/push", params, nil, name)
response, err = conn.DoRequest(ctx, nil, http.MethodPost, "/manifests/%s/push", params, header, name)
}
if err != nil {
return "", err
Expand Down
49 changes: 49 additions & 0 deletions test/e2e/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,55 @@ var _ = Describe("Podman manifest", func() {
))
})

It("authenticated push", func() {
if podmanTest.Host.Arch == "ppc64le" {
Skip("No registry image for ppc64le")
}
session := podmanTest.Podman([]string{"manifest", "create", "foo"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
session = podmanTest.Podman([]string{"pull", ALPINE})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))

lock := GetPortLock("5000")
defer lock.Unlock()

session = podmanTest.Podman([]string{"run", "-d", "-p", "5000:5000", "--name", "registry", "-e", "REGISTRY_AUTH=htpasswd", "-e",
"REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm", "-e", "REGISTRY_AUTH_HTPASSWD_PATH=/htpasswd",
"--entrypoint", "/bin/sh", registry, "-c",
"htpasswd -Bbc /htpasswd podmantest test && /entrypoint.sh /etc/docker/registry/config.yml"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))

if !WaitContainerReady(podmanTest, "registry", "listening on", 20, 1) {
Skip("Cannot start docker registry.")
}

session = podmanTest.Podman([]string{"logs", "registry"})
session.WaitWithDefaultTimeout()

session = podmanTest.Podman([]string{"tag", ALPINE, "localhost:5000/alpine:latest"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))

push := podmanTest.Podman([]string{"push", "--creds=podmantest:test", "--format=v2s2", "localhost:5000/alpine:latest"})
push.WaitWithDefaultTimeout()
Expect(push).Should(Exit(0))

session = podmanTest.Podman([]string{"manifest", "add", "--creds=podmantest:test", "foo", "localhost:5000/alpine:latest"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))

push = podmanTest.Podman([]string{"manifest", "push", "--creds=podmantest:test", "foo", "localhost:5000/credstest"})
push.WaitWithDefaultTimeout()
Expect(push).Should(Exit(0))

push = podmanTest.Podman([]string{"manifest", "push", "--creds=podmantest:wrongpasswd", "foo", "localhost:5000/credstest"})
push.WaitWithDefaultTimeout()
Expect(push).To(ExitWithError())
})

It("push --rm", func() {
SkipIfRemote("remote does not support --rm")
session := podmanTest.Podman([]string{"manifest", "create", "foo"})
Expand Down

0 comments on commit f8fa9b5

Please sign in to comment.