Skip to content

Commit 17c553b

Browse files
refactor: improve docker cred retrieval (#156)
## Description Simplifies docker cred retrieval logic and removes docker/cli as a dependency --------- Signed-off-by: Austin Abro <AustinAbro321@gmail.com>
1 parent eb2c7a8 commit 17c553b

File tree

3 files changed

+11
-59
lines changed

3 files changed

+11
-59
lines changed

oci/common.go

+11-47
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import (
1111
"strings"
1212

1313
"github.com/defenseunicorns/pkg/helpers/v2"
14-
"github.com/docker/cli/cli/config"
15-
"github.com/docker/cli/cli/config/configfile"
1614
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
1715
"oras.land/oras-go/v2/registry"
1816
"oras.land/oras-go/v2/registry/remote"
1917
"oras.land/oras-go/v2/registry/remote/auth"
18+
"oras.land/oras-go/v2/registry/remote/credentials"
19+
"oras.land/oras-go/v2/registry/remote/retry"
2020
)
2121

2222
const (
@@ -136,56 +136,20 @@ func (o *OrasRemote) setRepository(ref registry.Reference) error {
136136
ref.Registry = "ghcr.io"
137137
ref.Repository = "defenseunicorns/packages/" + ref.Repository
138138
}
139-
client, err := o.createAuthClient(ref)
139+
storeOpts := credentials.StoreOptions{}
140+
credStore, err := credentials.NewStoreFromDocker(storeOpts)
140141
if err != nil {
141-
return err
142+
return fmt.Errorf("failed to get credentials: %w", err)
142143
}
144+
client := &auth.Client{
145+
Client: retry.DefaultClient,
146+
Cache: auth.NewCache(),
147+
Credential: credentials.Credential(credStore),
148+
}
149+
o.log.Debug("gathering credentials from default Docker config file", "credentials_configured", credStore.IsAuthConfigured())
143150

144151
o.repo.Reference = ref
145152
o.repo.Client = client
146153

147154
return nil
148155
}
149-
150-
// createAuthClient returns an auth client for the given reference.
151-
//
152-
// The credentials are pulled using Docker's default credential store.
153-
//
154-
// TODO: instead of using Docker's cred store, should use the new one from ORAS to remove that dep
155-
func (o *OrasRemote) createAuthClient(ref registry.Reference) (*auth.Client, error) {
156-
157-
client := o.repo.Client.(*auth.Client)
158-
o.log.Debug(fmt.Sprintf("Loading docker config file from default config location: %s for %s", config.Dir(), ref))
159-
cfg, err := config.Load(config.Dir())
160-
if err != nil {
161-
return nil, err
162-
}
163-
if !cfg.ContainsAuth() {
164-
o.log.Debug("no docker config file found")
165-
return client, nil
166-
}
167-
168-
configs := []*configfile.ConfigFile{cfg}
169-
170-
var key = ref.Registry
171-
if key == "registry-1.docker.io" {
172-
// Docker stores its credentials under the following key, otherwise credentials use the registry URL
173-
key = "https://index.docker.io/v1/"
174-
}
175-
176-
authConf, err := configs[0].GetCredentialsStore(key).Get(key)
177-
if err != nil {
178-
return nil, fmt.Errorf("unable to get credentials for %s: %w", key, err)
179-
}
180-
181-
cred := auth.Credential{
182-
Username: authConf.Username,
183-
Password: authConf.Password,
184-
AccessToken: authConf.RegistryToken,
185-
RefreshToken: authConf.IdentityToken,
186-
}
187-
188-
client.Credential = auth.StaticCredential(ref.Registry, cred)
189-
190-
return client, nil
191-
}

oci/go.mod

-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ replace github.com/defenseunicorns/pkg/helpers/v2 => ../helpers
77
require (
88
github.com/defenseunicorns/pkg/helpers/v2 v2.0.1
99
github.com/distribution/distribution/v3 v3.0.0-alpha.1
10-
github.com/docker/cli v27.2.1+incompatible
1110
github.com/goccy/go-yaml v1.11.3
1211
github.com/opencontainers/image-spec v1.1.0
1312
github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5
@@ -25,7 +24,6 @@ require (
2524
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
2625
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
2726
github.com/distribution/reference v0.5.0 // indirect
28-
github.com/docker/docker-credential-helpers v0.8.0 // indirect
2927
github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c // indirect
3028
github.com/docker/go-metrics v0.0.1 // indirect
3129
github.com/fatih/color v1.16.0 // indirect
@@ -46,7 +44,6 @@ require (
4644
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
4745
github.com/opencontainers/go-digest v1.0.0 // indirect
4846
github.com/otiai10/copy v1.14.0 // indirect
49-
github.com/pkg/errors v0.9.1 // indirect
5047
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
5148
github.com/prometheus/client_golang v1.18.0 // indirect
5249
github.com/prometheus/client_model v0.5.0 // indirect
@@ -85,5 +82,4 @@ require (
8582
google.golang.org/protobuf v1.33.0 // indirect
8683
gopkg.in/yaml.v2 v2.4.0 // indirect
8784
gopkg.in/yaml.v3 v3.0.1 // indirect
88-
gotest.tools/v3 v3.5.1 // indirect
8985
)

oci/go.sum

-8
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ github.com/distribution/distribution/v3 v3.0.0-alpha.1 h1:jn7I1gvjOvmLztH1+1cLiU
2828
github.com/distribution/distribution/v3 v3.0.0-alpha.1/go.mod h1:LCp4JZp1ZalYg0W/TN05jarCQu+h4w7xc7ZfQF4Y/cY=
2929
github.com/distribution/reference v0.5.0 h1:/FUIFXtfc/x2gpa5/VGfiGLuOIdYa1t65IKK2OFGvA0=
3030
github.com/distribution/reference v0.5.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
31-
github.com/docker/cli v27.2.1+incompatible h1:U5BPtiD0viUzjGAjV1p0MGB8eVA3L3cbIrnyWmSJI70=
32-
github.com/docker/cli v27.2.1+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8=
33-
github.com/docker/docker-credential-helpers v0.8.0 h1:YQFtbBQb4VrpoPxhFuzEBPQ9E16qz5SpHLS+uswaCp8=
34-
github.com/docker/docker-credential-helpers v0.8.0/go.mod h1:UGFXcuoQ5TxPiB54nHOZ32AWRqQdECoh/Mg0AlEYb40=
3531
github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c h1:+pKlWGMw7gf6bQ+oDZB4KHQFypsfjYlq/C4rfL7D3g8=
3632
github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c/go.mod h1:Uw6UezgYA44ePAFQYUehOuCzmy5zmg/+nl2ZfMWGkpA=
3733
github.com/docker/go-metrics v0.0.1 h1:AgB/0SvBxihN0X8OR4SjsblXkbMvalQ8cjmtKQ2rQV8=
@@ -124,8 +120,6 @@ github.com/otiai10/mint v1.5.1/go.mod h1:MJm72SBthJjz8qhefc4z1PYEieWmy8Bku7CjcAq
124120
github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5 h1:Ii+DKncOVM8Cu1Hc+ETb5K+23HdAMvESYE3ZJ5b5cMI=
125121
github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5/go.mod h1:iIss55rKnNBTvrwdmkUpLnDpZoAHvWaiq5+iMmen4AE=
126122
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
127-
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
128-
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
129123
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
130124
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
131125
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
@@ -254,7 +248,5 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
254248
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
255249
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
256250
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
257-
gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU=
258-
gotest.tools/v3 v3.5.1/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU=
259251
oras.land/oras-go/v2 v2.5.0 h1:o8Me9kLY74Vp5uw07QXPiitjsw7qNXi8Twd+19Zf02c=
260252
oras.land/oras-go/v2 v2.5.0/go.mod h1:z4eisnLP530vwIOUOJeBIj0aGI0L1C3d53atvCBqZHg=

0 commit comments

Comments
 (0)