-
Notifications
You must be signed in to change notification settings - Fork 14
feat: add support for copying certs to dockerd's registry directory #101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.