Skip to content

Commit

Permalink
Merge pull request containers#411 from umohnani8/certs
Browse files Browse the repository at this point in the history
Add /etc/containers/certs.d as default certs directory
  • Loading branch information
runcom committed Feb 5, 2018
2 parents 2524e50 + bca2678 commit 3ab2e31
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
50 changes: 38 additions & 12 deletions docker/docker_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
Expand All @@ -30,8 +31,6 @@ const (
dockerV1Hostname = "index.docker.io"
dockerRegistry = "registry-1.docker.io"

systemPerHostCertDirPath = "/etc/docker/certs.d"

resolvedPingV2URL = "%s://%s/v2/"
resolvedPingV1URL = "%s://%s/v1/_ping"
tagsPath = "/v2/%s/tags/list"
Expand All @@ -52,6 +51,7 @@ var (
ErrV1NotSupported = errors.New("can't talk to a V1 docker registry")
// ErrUnauthorizedForCredentials is returned when the status code returned is 401
ErrUnauthorizedForCredentials = errors.New("unable to retrieve auth token: invalid username/password")
systemPerHostCertDirPaths = [2]string{"/etc/containers/certs.d", "/etc/docker/certs.d"}
)

// extensionSignature and extensionSignatureList come from github.com/openshift/origin/pkg/dockerregistry/server/signaturedispatcher.go:
Expand Down Expand Up @@ -131,19 +131,42 @@ func serverDefault() *tls.Config {
}

// dockerCertDir returns a path to a directory to be consumed by tlsclientconfig.SetupCertificates() depending on ctx and hostPort.
func dockerCertDir(ctx *types.SystemContext, hostPort string) string {
func dockerCertDir(ctx *types.SystemContext, hostPort string) (string, error) {
if ctx != nil && ctx.DockerCertPath != "" {
return ctx.DockerCertPath
return ctx.DockerCertPath, nil
}
var hostCertDir string
if ctx != nil && ctx.DockerPerHostCertDirPath != "" {
hostCertDir = ctx.DockerPerHostCertDirPath
} else if ctx != nil && ctx.RootForImplicitAbsolutePaths != "" {
hostCertDir = filepath.Join(ctx.RootForImplicitAbsolutePaths, systemPerHostCertDirPath)
} else {
hostCertDir = systemPerHostCertDirPath
return filepath.Join(ctx.DockerPerHostCertDirPath, hostPort), nil
}
return filepath.Join(hostCertDir, hostPort)

var (
hostCertDir string
fullCertDirPath string
)
for _, systemPerHostCertDirPath := range systemPerHostCertDirPaths {
if ctx != nil && ctx.RootForImplicitAbsolutePaths != "" {
hostCertDir = filepath.Join(ctx.RootForImplicitAbsolutePaths, systemPerHostCertDirPath)
} else {
hostCertDir = systemPerHostCertDirPath
}

fullCertDirPath = filepath.Join(hostCertDir, hostPort)
_, err := os.Stat(fullCertDirPath)
if err == nil {
break
}
if os.IsNotExist(err) {
continue
}
if os.IsPermission(err) {
logrus.Debugf("error accessing certs directory due to permissions: %v", err)
continue
}
if err != nil {
return "", err
}
}
return fullCertDirPath, nil
}

// newDockerClientFromRef returns a new dockerClient instance for refHostname (a host a specified in the Docker image reference, not canonicalized to dockerRegistry)
Expand Down Expand Up @@ -177,7 +200,10 @@ func newDockerClientWithDetails(ctx *types.SystemContext, registry, username, pa
// dockerHostname here, because it is more symmetrical to read the configuration in that case as well, and because
// generally the UI hides the existence of the different dockerRegistry. But note that this behavior is
// undocumented and may change if docker/docker changes.
certDir := dockerCertDir(ctx, hostName)
certDir, err := dockerCertDir(ctx, hostName)
if err != nil {
return nil, err
}
if err := tlsclientconfig.SetupCertificates(certDir, tr.TLSClientConfig); err != nil {
return nil, err
}
Expand Down
9 changes: 6 additions & 3 deletions docker/docker_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/containers/image/pkg/docker/config"
"github.com/containers/image/types"
"github.com/containers/storage/pkg/homedir"
Expand All @@ -23,9 +25,9 @@ func TestDockerCertDir(t *testing.T) {
const nondefaultPerHostDir = "/this/is/not/the/default/certs.d"
const variableReference = "$HOME"
const rootPrefix = "/root/prefix"
const registryHostPort = "localhost:5000"
const registryHostPort = "thishostdefinitelydoesnotexist:5000"

systemPerHostResult := filepath.Join(systemPerHostCertDirPath, registryHostPort)
systemPerHostResult := filepath.Join(systemPerHostCertDirPaths[len(systemPerHostCertDirPaths)-1], registryHostPort)
for _, c := range []struct {
ctx *types.SystemContext
expected string
Expand Down Expand Up @@ -85,7 +87,8 @@ func TestDockerCertDir(t *testing.T) {
filepath.Join(variableReference, registryHostPort),
},
} {
path := dockerCertDir(c.ctx, registryHostPort)
path, err := dockerCertDir(c.ctx, registryHostPort)
require.Equal(t, nil, err)
assert.Equal(t, c.expected, path)
}
}
Expand Down

0 comments on commit 3ab2e31

Please sign in to comment.