-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Update with newer version of ORAS
- Loading branch information
Showing
5 changed files
with
234 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package getter | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
var matchRegistries = []*regexp.Regexp{ | ||
regexp.MustCompile("azurecr.io"), | ||
regexp.MustCompile("gcr.io"), | ||
regexp.MustCompile("registry.gitlab.com"), | ||
regexp.MustCompile("[0-9]{12}.dkr.ecr.[a-z0-9-]*.amazonaws.com"), | ||
} | ||
|
||
// OCIDetector implements Detector to detect OCI registry URLs and turn | ||
// them into URLs that the OCI getter can understand. | ||
type OCIDetector struct{} | ||
|
||
// Detect will detect if the source is an OCI registry | ||
func (d *OCIDetector) Detect(src, _ string) (string, bool, error) { | ||
if len(src) == 0 { | ||
return "", false, nil | ||
} | ||
|
||
if containsOCIRegistry(src) || containsLocalRegistry(src) { | ||
url, err := d.detectHTTP(src) | ||
if err != nil { | ||
return "", false, fmt.Errorf("detect http: %w", err) | ||
} | ||
|
||
return url, true, nil | ||
} | ||
|
||
return "", false, nil | ||
} | ||
|
||
func containsOCIRegistry(src string) bool { | ||
for _, matchRegistry := range matchRegistries { | ||
if matchRegistry.MatchString(src) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
func containsLocalRegistry(src string) bool { | ||
return strings.Contains(src, "127.0.0.1:5000") || strings.Contains(src, "localhost:5000") | ||
} | ||
|
||
func (d *OCIDetector) detectHTTP(src string) (string, error) { | ||
parts := strings.Split(src, "/") | ||
if len(parts) < 2 { | ||
return "", fmt.Errorf( | ||
"URL is not a valid Azure registry URL") | ||
} | ||
|
||
return "oci://" + getRepositoryFromURL(src), nil | ||
} | ||
|
||
func getRepositoryFromURL(url string) string { | ||
if repositoryContainsTag(url) { | ||
return url | ||
} | ||
|
||
return url + ":latest" | ||
} | ||
|
||
func repositoryContainsTag(repository string) bool { | ||
path := strings.Split(repository, "/") | ||
return pathContainsTag(path[len(path)-1]) | ||
} | ||
|
||
func pathContainsTag(path string) bool { | ||
return strings.Contains(path, ":") | ||
} |
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,68 @@ | ||
package getter | ||
|
||
import "testing" | ||
|
||
func TestOCIDetector_Detect(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
expected string | ||
}{ | ||
{ | ||
"should detect azurecr", | ||
"user.azurecr.io/policies:tag", | ||
"oci://user.azurecr.io/policies:tag", | ||
}, | ||
{ | ||
"should detect gcr", | ||
"gcr.io/conftest/policies:tag", | ||
"oci://gcr.io/conftest/policies:tag", | ||
}, | ||
{ | ||
"should detect ecr", | ||
"123456789012.dkr.ecr.us-east-1.amazonaws.com/conftest/policies:tag", | ||
"oci://123456789012.dkr.ecr.us-east-1.amazonaws.com/conftest/policies:tag", | ||
}, | ||
{ | ||
"should detect gitlab", | ||
"registry.gitlab.com/conftest/policies:tag", | ||
"oci://registry.gitlab.com/conftest/policies:tag", | ||
}, | ||
{ | ||
"should add latest tag", | ||
"user.azurecr.io/policies", | ||
"oci://user.azurecr.io/policies:latest", | ||
}, | ||
{ | ||
"should detect 127.0.0.1:5000 as most likely being an OCI registry", | ||
"127.0.0.1:5000/policies:tag", | ||
"oci://127.0.0.1:5000/policies:tag", | ||
}, | ||
{ | ||
"should detect 127.0.0.1:5000 as most likely being an OCI registry and tag it properly if no tag is supplied", | ||
"127.0.0.1:5000/policies", | ||
"oci://127.0.0.1:5000/policies:latest", | ||
}, | ||
{ | ||
"should detect localhost:5000 as most likely being an OCI registry and tag it properly if no tag is supplied", | ||
"localhost:5000/policies", | ||
"oci://localhost:5000/policies:latest", | ||
}, | ||
} | ||
pwd := "/pwd" | ||
d := &OCIDetector{} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
out, ok, err := d.Detect(tt.input, pwd) | ||
if err != nil { | ||
t.Fatalf("OCIDetector.Detect() error = %v", err) | ||
} | ||
if !ok { | ||
t.Fatal("OCIDetector.Detect() not ok, should have detected") | ||
} | ||
if out != tt.expected { | ||
t.Errorf("OCIDetector.Detect() output = %v, want %v", out, tt.expected) | ||
} | ||
}) | ||
} | ||
} |
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,74 @@ | ||
package getter | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"os" | ||
"path" | ||
|
||
"oras.land/oras-go/v2" | ||
"oras.land/oras-go/v2/content/file" | ||
"oras.land/oras-go/v2/registry/remote" | ||
) | ||
|
||
// OCIGetter is responsible for handling OCI repositories | ||
type OCIGetter struct { | ||
getter | ||
} | ||
|
||
// ClientMode returns the client mode directory | ||
func (g *OCIGetter) ClientMode(u *url.URL) (ClientMode, error) { | ||
return ClientModeDir, nil | ||
} | ||
|
||
// Get gets the repository as the specified url | ||
func (g *OCIGetter) Get(path string, u *url.URL) error { | ||
ctx := g.Context() | ||
|
||
src, err := g.getRepository(u) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
reference := src.Reference.Reference | ||
|
||
if reference == "" { | ||
reference = "latest" | ||
} | ||
|
||
err = os.MkdirAll(path, os.ModePerm) | ||
if err != nil { | ||
return fmt.Errorf("make directory for OCI storage: %w", err) | ||
} | ||
|
||
dst, err := file.New(path) | ||
if err != nil { | ||
return fmt.Errorf("cannot create file destination OCIGetter: %w", err) | ||
} | ||
defer dst.Close() | ||
|
||
_, err = oras.Copy(ctx, src, reference, dst, reference, oras.DefaultCopyOptions) | ||
if err != nil { | ||
return fmt.Errorf("unable to copy OCI artifact: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (g *OCIGetter) getRepository(u *url.URL) (*remote.Repository, error) { | ||
repository, err := remote.NewRepository(getReferenceFromURL(u)) | ||
if err != nil { | ||
return nil, fmt.Errorf("invalid OCI URL: %w", err) | ||
} | ||
|
||
return repository, nil | ||
} | ||
|
||
func getReferenceFromURL(u *url.URL) (string) { | ||
return path.Join(u.Host, u.Path) | ||
} | ||
|
||
// GetFile is currently a NOOP | ||
func (g *OCIGetter) GetFile(dst string, u *url.URL) error { | ||
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
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