Skip to content

Commit

Permalink
Merge pull request #36 from dprince/crane_insecure_option
Browse files Browse the repository at this point in the history
Add insecure option for Crane imageresolver
  • Loading branch information
zach-source committed Aug 8, 2023
2 parents a2c0ff5 + 4e89427 commit dbe96d8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
19 changes: 15 additions & 4 deletions pkg/imageresolver/crane.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var _ ImageResolver = CraneResolver{}
type CraneResolver struct {
authenticator authn.Authenticator
useDefault bool
insecure bool
}

// CraneOption is a function that configures the `CraneResolver`
Expand All @@ -30,6 +31,13 @@ func WithDefaultKeychain() CraneOption {
}
}

// Insecure returns a CraneOption that sets the auth to insecure
func Insecure() CraneOption {
return func(res *CraneResolver) {
res.insecure = true
}
}

// NewCraneResolver returns a CraneResolver with the applied options.
func NewCraneResolver(opts ...CraneOption) CraneResolver {
res := CraneResolver{authenticator: authn.Anonymous}
Expand All @@ -43,11 +51,14 @@ func NewCraneResolver(opts ...CraneOption) CraneResolver {
func (res CraneResolver) ResolveImageReference(imageReference string) (string, error) {
var digest string
var err error
if res.useDefault {
digest, err = crane.Digest(imageReference)
} else {
digest, err = crane.Digest(imageReference, crane.WithAuth(res.authenticator))
craneOpts := []crane.Option{}
if !res.useDefault {
craneOpts = append(craneOpts, crane.WithAuth(res.authenticator))
}
if res.insecure {
craneOpts = append(craneOpts, crane.Insecure)
}
digest, err = crane.Digest(imageReference, craneOpts...)
if err != nil {
return "", err
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/imageresolver/imageresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ func GetResolver(resolver ResolverOption, args map[string]string) (ImageResolver
opts = append(opts, WithDefaultKeychain())
}
}
insecure := args["insecure"]
if insecure == "true" {
opts = append(opts, Insecure())
}

return NewCraneResolver(opts...), nil
default:
Expand Down
17 changes: 17 additions & 0 deletions pkg/imageresolver/imageresolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ var _ = Describe("GetResolver", func() {
Expect(err).To(BeNil())
Expect(resolver).NotTo(BeNil())
Expect(resolver.(CraneResolver).useDefault).To(BeTrue())
Expect(resolver.(CraneResolver).insecure).To(BeFalse())
})
})
})

var _ = Describe("GetResolver", func() {
Describe("CraneAuth", func() {
It("returns a Crane resolver with the insecure option", func() {
args := make(map[string]string)
args["usedefault"] = "true"
args["insecure"] = "true"

resolver, err := GetResolver(ResolverCrane, args)
Expect(err).To(BeNil())
Expect(resolver).NotTo(BeNil())
Expect(resolver.(CraneResolver).useDefault).To(BeTrue())
Expect(resolver.(CraneResolver).insecure).To(BeTrue())
})
})
})

0 comments on commit dbe96d8

Please sign in to comment.