-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for copying certs to dockerd's registry directory (#…
…101)
- Loading branch information
Showing
10 changed files
with
412 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package dockerutil | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"path/filepath" | ||
|
||
"github.com/spf13/afero" | ||
"golang.org/x/xerrors" | ||
|
||
"github.com/coder/envbox/xunix" | ||
) | ||
|
||
// WriteCertsForRegistry writes the certificates found in the provided directory | ||
// to the correct subdirectory that the Docker daemon uses when pulling images | ||
// from the specified private registry. | ||
func WriteCertsForRegistry(ctx context.Context, registryName, certsDir string) error { | ||
fs := xunix.GetFS(ctx) | ||
|
||
// Docker certs directory. | ||
registryCertsDir := filepath.Join("/etc/docker/certs.d", registryName) | ||
|
||
// If the directory already exists it means someone | ||
// has either wrapped the image or has mounted in certs | ||
// manually. We should assume the user knows what they're | ||
// doing and avoid mucking with their solution. | ||
if _, err := fs.Stat(registryCertsDir); err == nil { | ||
return nil | ||
} | ||
|
||
// Ensure the registry certs directory exists. | ||
err := fs.MkdirAll(registryCertsDir, 0o755) | ||
if err != nil { | ||
return xerrors.Errorf("create registry certs directory: %w", err) | ||
} | ||
|
||
// Check if certsDir is a file. | ||
fileInfo, err := fs.Stat(certsDir) | ||
if err != nil { | ||
return xerrors.Errorf("stat certs directory/file: %w", err) | ||
} | ||
|
||
if !fileInfo.IsDir() { | ||
// If it's a file, copy it directly | ||
err = copyCertFile(fs, certsDir, filepath.Join(registryCertsDir, "ca.crt")) | ||
if err != nil { | ||
return xerrors.Errorf("copy cert file: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
// If it's a directory, copy all cert files in the root of the directory | ||
entries, err := afero.ReadDir(fs, certsDir) | ||
if err != nil { | ||
return xerrors.Errorf("read certs directory: %w", err) | ||
} | ||
|
||
for _, entry := range entries { | ||
if entry.IsDir() { | ||
continue | ||
} | ||
srcPath := filepath.Join(certsDir, entry.Name()) | ||
dstPath := filepath.Join(registryCertsDir, entry.Name()) | ||
err = copyCertFile(fs, srcPath, dstPath) | ||
if err != nil { | ||
return xerrors.Errorf("copy cert file %s: %w", entry.Name(), err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func copyCertFile(fs xunix.FS, src, dst string) error { | ||
srcFile, err := fs.Open(src) | ||
if err != nil { | ||
return xerrors.Errorf("open source file: %w", err) | ||
} | ||
defer srcFile.Close() | ||
|
||
dstFile, err := fs.Create(dst) | ||
if err != nil { | ||
return xerrors.Errorf("create destination file: %w", err) | ||
} | ||
defer dstFile.Close() | ||
|
||
_, err = io.Copy(dstFile, srcFile) | ||
if err != nil { | ||
return xerrors.Errorf("copy file contents: %w", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package dockerutil_test | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/spf13/afero" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/coder/envbox/dockerutil" | ||
"github.com/coder/envbox/xunix" | ||
"github.com/coder/envbox/xunix/xunixfake" | ||
) | ||
|
||
func TestWriteCertsForRegistry(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("SingleCertFile", func(t *testing.T) { | ||
t.Parallel() | ||
// Test setup | ||
fs := xunixfake.NewMemFS() | ||
ctx := xunix.WithFS(context.Background(), fs) | ||
|
||
// Create a test certificate file | ||
certContent := []byte("test certificate content") | ||
err := afero.WriteFile(fs, "/certs/ca.crt", certContent, 0o644) | ||
require.NoError(t, err) | ||
|
||
// Run the function | ||
err = dockerutil.WriteCertsForRegistry(ctx, "test.registry.com", "/certs/ca.crt") | ||
require.NoError(t, err) | ||
|
||
// Check the result | ||
copiedContent, err := afero.ReadFile(fs, "/etc/docker/certs.d/test.registry.com/ca.crt") | ||
require.NoError(t, err) | ||
assert.Equal(t, certContent, copiedContent) | ||
}) | ||
|
||
t.Run("MultipleCertFiles", func(t *testing.T) { | ||
t.Parallel() | ||
// Test setup | ||
fs := xunixfake.NewMemFS() | ||
ctx := xunix.WithFS(context.Background(), fs) | ||
|
||
// Create test certificate files | ||
certFiles := []string{"ca.crt", "client.cert", "client.key"} | ||
for _, file := range certFiles { | ||
err := afero.WriteFile(fs, filepath.Join("/certs", file), []byte("content of "+file), 0o644) | ||
require.NoError(t, err) | ||
} | ||
|
||
// Run the function | ||
err := dockerutil.WriteCertsForRegistry(ctx, "test.registry.com", "/certs") | ||
require.NoError(t, err) | ||
|
||
// Check the results | ||
for _, file := range certFiles { | ||
copiedContent, err := afero.ReadFile(fs, filepath.Join("/etc/docker/certs.d/test.registry.com", file)) | ||
require.NoError(t, err) | ||
assert.Equal(t, []byte("content of "+file), copiedContent) | ||
} | ||
}) | ||
t.Run("ExistingRegistryCertsDir", func(t *testing.T) { | ||
t.Parallel() | ||
// Test setup | ||
fs := xunixfake.NewMemFS() | ||
ctx := xunix.WithFS(context.Background(), fs) | ||
|
||
// Create an existing registry certs directory | ||
registryCertsDir := "/etc/docker/certs.d/test.registry.com" | ||
err := fs.MkdirAll(registryCertsDir, 0o755) | ||
require.NoError(t, err) | ||
|
||
// Create a file in the existing directory | ||
existingContent := []byte("existing certificate content") | ||
err = afero.WriteFile(fs, filepath.Join(registryCertsDir, "existing.crt"), existingContent, 0o644) | ||
require.NoError(t, err) | ||
|
||
// Create a test certificate file in the source directory | ||
certContent := []byte("new certificate content") | ||
err = afero.WriteFile(fs, "/certs/ca.crt", certContent, 0o644) | ||
require.NoError(t, err) | ||
|
||
// Run the function | ||
err = dockerutil.WriteCertsForRegistry(ctx, "test.registry.com", "/certs") | ||
require.NoError(t, err) | ||
|
||
// Check that the existing file was not modified | ||
existingFileContent, err := afero.ReadFile(fs, filepath.Join(registryCertsDir, "existing.crt")) | ||
require.NoError(t, err) | ||
assert.Equal(t, existingContent, existingFileContent) | ||
|
||
// Check that the new file was not copied | ||
_, err = fs.Stat(filepath.Join(registryCertsDir, "ca.crt")) | ||
assert.True(t, os.IsNotExist(err), "New certificate file should not have been copied") | ||
}) | ||
} |
Oops, something went wrong.