-
Notifications
You must be signed in to change notification settings - Fork 6
add oci pull to images #175
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
Draft
mac641
wants to merge
23
commits into
master
Choose a base branch
from
oci-puller
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
510fd03
feat(image): add OciPull based on the example from #169
mac641 643508a
refactor: use OciConfig in order to retrieve credentials
mac641 167460c
chore: run go mod tidy
mac641 6971d38
refactor: add logging to image and format install.go
mac641 dbf4f83
fix: fix issue where RegistryURL was mandatory even though
mac641 24abb10
chore: apply latest pixie changes where OciConfig.RegistryURL is opti…
mac641 3573d87
fix(image): remove optional oci:// prefix before parsing image reference
mac641 e330b67
fix(install): add missing check if no OciConfigs have been provided a…
mac641 d5b0104
fix: fix untar destination of oci images and various typos
mac641 526b28e
test: add tests to image's oci-puller
mac641 6518c0b
test(image): implement test for oci image pull from registry with aut…
mac641 f3d6604
refactor(image): remove external dependency from ghcr in tests
mac641 30a8225
fix(image): remove dependency for running tests with superuser permis…
mac641 83c016d
ci: separate oci-pull tests from legacy tests
mac641 9036814
refactor: address changes requested by @majst01
mac641 ce480e9
test(image): address changes requested by @majst01
mac641 42cb2ef
refactor(image): add logging to untar's default case in order to dete…
mac641 3241d4f
test(image): add basic working test for untar
mac641 ac4142c
chore: add latest oci-puller pixie version
mac641 ac5cfff
Merge branch 'master' into oci-puller
mac641 e34f825
chore: add latest oci-puller pixie version
mac641 eaf717d
refactor: log ociConfig for debugging
mac641 c5bf5b8
refactor: address changes requested by @vknabel
mac641 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| //go:build integration | ||
| package image | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/rand" | ||
| "fmt" | ||
| "log/slog" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/foomo/htpasswd" | ||
| "github.com/google/go-containerregistry/pkg/authn" | ||
| "github.com/google/go-containerregistry/pkg/crane" | ||
| "github.com/metal-stack/metal-lib/pkg/pointer" | ||
| "github.com/stretchr/testify/require" | ||
| "github.com/testcontainers/testcontainers-go" | ||
| "github.com/testcontainers/testcontainers-go/wait" | ||
| ) | ||
|
|
||
| func TestOciPull(t *testing.T) { | ||
| var ( | ||
| mountDir = "/tmp/oci-pull-mount-dir" | ||
| extractedBin = "a" | ||
|
|
||
| anonymousUsername = "" | ||
| anonymousPassword = "" | ||
| ) | ||
|
|
||
| t.Run("successful anonymous pull", func(t *testing.T) { | ||
| regIP, regPort, err := startRegistry(nil, nil, nil) | ||
| require.NoError(t, err) | ||
| registry := fmt.Sprintf("%s:%d", regIP, regPort) | ||
|
|
||
| imageRef := fmt.Sprintf("%s/library/image", registry) | ||
| err = createImage(imageRef, "", "") | ||
| require.NoError(t, err) | ||
|
|
||
| err = os.MkdirAll(mountDir, 0777) | ||
| require.NoError(t, err) | ||
| defer os.RemoveAll(mountDir) | ||
|
|
||
| i := NewImage(slog.Default()) | ||
| if err = i.OciPull(t.Context(), imageRef, mountDir, anonymousUsername, anonymousPassword); err != nil { | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| extractedBinFullPath := filepath.Join(mountDir, extractedBin) | ||
| require.FileExists(t, extractedBinFullPath) | ||
| }) | ||
|
|
||
| t.Run("successful authenticated pull", func(t *testing.T) { | ||
| var ( | ||
| username = "test-user" | ||
| password = "test-password" | ||
| ) | ||
|
|
||
| f, err := os.CreateTemp("", "htpasswd") | ||
| require.NoError(t, err) | ||
| defer func() { | ||
| _ = os.Remove(f.Name()) | ||
| }() | ||
|
|
||
| err = htpasswd.SetPassword(f.Name(), username, password, htpasswd.HashBCrypt) | ||
| require.NoError(t, err) | ||
|
|
||
| env := map[string]string{ | ||
| "REGISTRY_AUTH": "htpasswd", | ||
| "REGISTRY_AUTH_HTPASSWD_REALM": "registry-login", | ||
| "REGISTRY_AUTH_HTPASSWD_PATH": "/htpasswd", | ||
| } | ||
| regIP, regPort, err := startRegistry(env, pointer.Pointer(f.Name()), pointer.Pointer("/htpasswd")) | ||
| require.NoError(t, err) | ||
| registry := fmt.Sprintf("%s:%d", regIP, regPort) | ||
|
|
||
| imageRefBehindAuth := fmt.Sprintf("%s/library/image", registry) | ||
| err = createImage(imageRefBehindAuth, username, password) | ||
| require.NoError(t, err) | ||
|
|
||
| err = os.MkdirAll(mountDir, 0777) | ||
| require.NoError(t, err) | ||
| defer os.RemoveAll(mountDir) | ||
|
|
||
| i := NewImage(slog.Default()) | ||
| if err = i.OciPull(t.Context(), imageRefBehindAuth, mountDir, username, password); err != nil { | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| extractedBinFullPath := filepath.Join(mountDir, extractedBin) | ||
| require.FileExists(t, extractedBinFullPath) | ||
| }) | ||
|
|
||
| t.Run("parsing of image refs fails", func(t *testing.T) { | ||
| invalidImageRef := "invalid://" | ||
| i := NewImage(slog.Default()) | ||
| err := i.OciPull(t.Context(), invalidImageRef, mountDir, anonymousUsername, anonymousPassword) | ||
| require.EqualError(t, err, "parsing image reference: could not parse reference: invalid://") | ||
| }) | ||
|
|
||
| t.Run("pulling remote image fails", func(t *testing.T) { | ||
| imageRefDoesNotExist := "oci://does/not/exist:tag" | ||
| i := NewImage(slog.Default()) | ||
| err := i.OciPull(t.Context(), imageRefDoesNotExist, mountDir, anonymousUsername, anonymousPassword) | ||
| require.Error(t, err) | ||
| }) | ||
| } | ||
|
|
||
| // HELPER FUNCTIONS | ||
| func startRegistry(env map[string]string, src, dst *string) (string, int, error) { | ||
| ctx := context.Background() | ||
| var ( | ||
| c testcontainers.Container | ||
| err error | ||
| ) | ||
|
|
||
| req := testcontainers.ContainerRequest{ | ||
| Image: "registry:3", | ||
| ExposedPorts: []string{"5000/tcp"}, | ||
| Env: env, | ||
| WaitingFor: wait.ForAll( | ||
| wait.ForLog("listening on"), | ||
| wait.ForListeningPort("5000/tcp"), | ||
| ), | ||
| } | ||
| c, err = testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ | ||
| ContainerRequest: req, | ||
| Started: true, | ||
| }) | ||
| if err != nil { | ||
| return "", 0, err | ||
| } | ||
| if src != nil && dst != nil { | ||
| err = c.CopyFileToContainer(ctx, *src, *dst, 0o777) | ||
| if err != nil { | ||
| return "", 0, err | ||
| } | ||
| } | ||
|
|
||
| ip, err := c.Host(ctx) | ||
| if err != nil { | ||
| return ip, 0, err | ||
| } | ||
| port, err := c.MappedPort(ctx, "5000") | ||
| if err != nil { | ||
| return ip, port.Int(), err | ||
| } | ||
|
|
||
| return ip, port.Int(), nil | ||
| } | ||
|
|
||
| func createImage(imageName, username, password string, tags ...string) error { | ||
| // ensure every image has distinct content | ||
| buf := make([]byte, 128) | ||
| _, err := rand.Read(buf) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| img, err := crane.Image(map[string][]byte{"a": buf}) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| var auth = authn.Anonymous | ||
| if username != "" || password != "" { | ||
| auth = &authn.Basic{ | ||
| Username: username, | ||
| Password: password, | ||
| } | ||
| } | ||
| err = crane.Push(img, imageName, crane.WithAuth(auth)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| for _, tag := range tags { | ||
| err := crane.Push(img, imageName+":"+tag, crane.WithAuth(auth)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unsure if all cases are covered and or relevant.
Other pitfalls might be:
This should be tested, at least manually, better would be with a automated test ?