Skip to content

Commit a12b466

Browse files
committed
Add class to repository scope
Expose registry error translation for plugin distribution Signed-off-by: Derek McGowan <[email protected]> (github: dmcgowan)
1 parent d1f5e0f commit a12b466

8 files changed

Lines changed: 34 additions & 17 deletions

File tree

distribution/errors.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,25 @@ func shouldV2Fallback(err errcode.Error) bool {
5959
return false
6060
}
6161

62-
func translatePullError(err error, ref reference.Named) error {
62+
// TranslatePullError is used to convert an error from a registry pull
63+
// operation to an error representing the entire pull operation. Any error
64+
// information which is not used by the returned error gets output to
65+
// log at info level.
66+
func TranslatePullError(err error, ref reference.Named) error {
6367
switch v := err.(type) {
6468
case errcode.Errors:
6569
if len(v) != 0 {
6670
for _, extra := range v[1:] {
6771
logrus.Infof("Ignoring extra error returned from registry: %v", extra)
6872
}
69-
return translatePullError(v[0], ref)
73+
return TranslatePullError(v[0], ref)
7074
}
7175
case errcode.Error:
7276
var newErr error
7377
switch v.Code {
7478
case errcode.ErrorCodeDenied:
7579
// ErrorCodeDenied is used when access to the repository was denied
76-
newErr = errors.Errorf("repository %s not found: does not exist or no read access", ref.Name())
80+
newErr = errors.Errorf("repository %s not found: does not exist or no pull access", ref.Name())
7781
case v2.ErrorCodeManifestUnknown:
7882
newErr = errors.Errorf("manifest for %s not found", ref.String())
7983
case v2.ErrorCodeNameUnknown:
@@ -84,7 +88,7 @@ func translatePullError(err error, ref reference.Named) error {
8488
return newErr
8589
}
8690
case xfer.DoNotRetry:
87-
return translatePullError(v.Err, ref)
91+
return TranslatePullError(v.Err, ref)
8892
}
8993

9094
return err

distribution/pull.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func Pull(ctx context.Context, ref reference.Named, imagePullConfig *ImagePullCo
168168
continue
169169
}
170170
logrus.Errorf("Not continuing with pull after error: %v", err)
171-
return translatePullError(err, ref)
171+
return TranslatePullError(err, ref)
172172
}
173173

174174
imagePullConfig.ImageEventLogger(ref.String(), repoInfo.Name(), "pull")
@@ -179,7 +179,7 @@ func Pull(ctx context.Context, ref reference.Named, imagePullConfig *ImagePullCo
179179
lastErr = fmt.Errorf("no endpoints found for %s", ref.String())
180180
}
181181

182-
return translatePullError(lastErr, ref)
182+
return TranslatePullError(lastErr, ref)
183183
}
184184

185185
// writeStatus writes a status message to out. If layersDownloaded is true, the

distribution/registry.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,22 @@ func NewV2Repository(ctx context.Context, repoInfo *registry.RepositoryInfo, end
7070
passThruTokenHandler := &existingTokenHandler{token: authConfig.RegistryToken}
7171
modifiers = append(modifiers, auth.NewAuthorizer(challengeManager, passThruTokenHandler))
7272
} else {
73+
scope := auth.RepositoryScope{
74+
Repository: repoName,
75+
Actions: actions,
76+
}
77+
78+
// Keep image repositories blank for scope compatibility
79+
if repoInfo.Class != "image" {
80+
scope.Class = repoInfo.Class
81+
}
82+
7383
creds := registry.NewStaticCredentialStore(authConfig)
7484
tokenHandlerOptions := auth.TokenHandlerOptions{
7585
Transport: authTransport,
7686
Credentials: creds,
77-
Scopes: []auth.Scope{
78-
auth.RepositoryScope{
79-
Repository: repoName,
80-
Actions: actions,
81-
},
82-
},
83-
ClientID: registry.AuthClientID,
87+
Scopes: []auth.Scope{scope},
88+
ClientID: registry.AuthClientID,
8489
}
8590
tokenHandler := auth.NewTokenHandlerWithOptions(tokenHandlerOptions)
8691
basicHandler := auth.NewBasicHandler(creds)

integration-cli/docker_cli_pull_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (s *DockerHubPullSuite) TestPullNonExistingImage(c *check.C) {
9898
for record := range recordChan {
9999
if len(record.option) == 0 {
100100
c.Assert(record.err, checker.NotNil, check.Commentf("expected non-zero exit status when pulling non-existing image: %s", record.out))
101-
c.Assert(record.out, checker.Contains, fmt.Sprintf("repository %s not found: does not exist or no read access", record.e.repo), check.Commentf("expected image not found error messages"))
101+
c.Assert(record.out, checker.Contains, fmt.Sprintf("repository %s not found: does not exist or no pull access", record.e.repo), check.Commentf("expected image not found error messages"))
102102
} else {
103103
// pull -a on a nonexistent registry should fall back as well
104104
c.Assert(record.err, checker.NotNil, check.Commentf("expected non-zero exit status when pulling non-existing image: %s", record.out))

plugin/distribution/pull.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func Pull(ref reference.Named, rs registry.Service, metaheader http.Header, auth
8585
logrus.Debugf("pull.go: error in ResolveRepository: %v", err)
8686
return nil, err
8787
}
88+
repoInfo.Class = "plugin"
8889

8990
if err := dockerdist.ValidateRepoName(repoInfo.Name()); err != nil {
9091
logrus.Debugf("pull.go: error in ValidateRepoName: %v", err)
@@ -138,9 +139,8 @@ func Pull(ref reference.Named, rs registry.Service, metaheader http.Header, auth
138139
}
139140
manifest, err := msv.Get(context.Background(), "", distribution.WithTag(tag))
140141
if err != nil {
141-
// TODO: change 401 to 404
142142
logrus.Debugf("pull.go: error in msv.Get(): %v", err)
143-
return nil, err
143+
return nil, dockerdist.TranslatePullError(err, repoInfo)
144144
}
145145

146146
_, pl, err := manifest.Payload()

plugin/distribution/push.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func Push(name string, rs registry.Service, metaHeader http.Header, authConfig *
2727
if err != nil {
2828
return "", err
2929
}
30+
repoInfo.Class = "plugin"
3031

3132
if err := dockerdist.ValidateRepoName(repoInfo.Name()); err != nil {
3233
return "", err

registry/config.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,11 @@ func newRepositoryInfo(config *serviceConfig, name reference.Named) (*Repository
280280
return nil, err
281281
}
282282
official := !strings.ContainsRune(name.Name(), '/')
283-
return &RepositoryInfo{name, index, official}, nil
283+
return &RepositoryInfo{
284+
Named: name,
285+
Index: index,
286+
Official: official,
287+
}, nil
284288
}
285289

286290
// ParseRepositoryInfo performs the breakdown of a repository name into a RepositoryInfo, but

registry/types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,7 @@ type RepositoryInfo struct {
6767
// If the registry is official, and the normalized name does not
6868
// contain a '/' (e.g. "foo"), then it is considered an official repo.
6969
Official bool
70+
// Class represents the class of the repository, such as "plugin"
71+
// or "image".
72+
Class string
7073
}

0 commit comments

Comments
 (0)