Skip to content

Commit

Permalink
Refactor testhelper registry to work for Linux/Windows
Browse files Browse the repository at this point in the history
Replace dependency on `htpasswd` binary, instead generate file in Golang

Signed-off-by: Micah Young <ymicah@vmware.com>
  • Loading branch information
Micah Young authored and Javier Romero and Andrew Meyer committed May 5, 2020
1 parent 70d22be commit 2f9f125
Show file tree
Hide file tree
Showing 3 changed files with 336 additions and 14 deletions.
20 changes: 6 additions & 14 deletions testhelpers/registry.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package testhelpers

import (
"bytes"
"context"
"encoding/base64"
"fmt"
Expand All @@ -17,6 +16,7 @@ import (
dockertypes "github.com/docker/docker/api/types"
dockercontainer "github.com/docker/docker/api/types/container"
"github.com/docker/go-connections/nat"
"golang.org/x/crypto/bcrypt"
)

var registryContainerName = "registry:2"
Expand Down Expand Up @@ -89,7 +89,7 @@ func startRegistry(t *testing.T, runRegistryName, username, password string) str
AssertNil(t, PullImageWithAuth(dockerCli(t), registryContainerName, ""))
ctx := context.Background()

htpasswdTar := generateHtpasswd(ctx, t, username, password)
htpasswdTar := generateHtpasswd(t, username, password)

ctr, err := dockerCli(t).ContainerCreate(ctx, &dockercontainer.Config{
Image: registryContainerName,
Expand Down Expand Up @@ -123,20 +123,12 @@ func startRegistry(t *testing.T, runRegistryName, username, password string) str
return runRegistryPort
}

func generateHtpasswd(ctx context.Context, t *testing.T, username string, password string) io.Reader {
func generateHtpasswd(t *testing.T, username string, password string) io.Reader {
// https://docs.docker.com/registry/deploying/#restricting-access
htpasswdCtr, err := dockerCli(t).ContainerCreate(ctx, &dockercontainer.Config{
Image: registryContainerName,
Entrypoint: []string{"htpasswd", "-Bbn", username, password},
}, &dockercontainer.HostConfig{
AutoRemove: true,
}, nil, "")
AssertNil(t, err)
// HTPASSWD format: https://github.com/foomo/htpasswd/blob/e3a90e78da9cff06a83a78861847aa9092cbebdd/hashing.go#L23
passwordBytes, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)

var b bytes.Buffer
err = RunContainer(ctx, dockerCli(t), htpasswdCtr.ID, &b, &b)
AssertNil(t, err)
reader, err := archive.CreateSingleFileTarReader("/registry_test_htpasswd", b.String())
reader, err := archive.CreateSingleFileTarReader("/registry_test_htpasswd", username+":"+string(passwordBytes))
AssertNil(t, err)

return reader
Expand Down
35 changes: 35 additions & 0 deletions vendor/golang.org/x/crypto/bcrypt/base64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

295 changes: 295 additions & 0 deletions vendor/golang.org/x/crypto/bcrypt/bcrypt.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2f9f125

Please sign in to comment.