Browse code

registry: Remove reference.go

This removes some very old vestigial code that really should have been
removed during the content addressability transition. It implements
something called "reference" but it behaves differently from the actual
reference package. This was only used by client-side content trust code,
and is relatively easy to extricate.

Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>

Aaron Lehmann authored on 2016/12/07 04:27:27
Showing 3 changed files
... ...
@@ -55,16 +55,6 @@ func runPull(dockerCli *command.DockerCli, opts pullOptions) error {
55 55
 		fmt.Fprintf(dockerCli.Out(), "Using default tag: %s\n", reference.DefaultTag)
56 56
 	}
57 57
 
58
-	var tag string
59
-	switch x := distributionRef.(type) {
60
-	case reference.Canonical:
61
-		tag = x.Digest().String()
62
-	case reference.NamedTagged:
63
-		tag = x.Tag()
64
-	}
65
-
66
-	registryRef := registry.ParseReference(tag)
67
-
68 58
 	// Resolve the Repository name from fqn to RepositoryInfo
69 59
 	repoInfo, err := registry.ParseRepositoryInfo(distributionRef)
70 60
 	if err != nil {
... ...
@@ -76,9 +66,10 @@ func runPull(dockerCli *command.DockerCli, opts pullOptions) error {
76 76
 	authConfig := command.ResolveAuthConfig(ctx, dockerCli, repoInfo.Index)
77 77
 	requestPrivilege := command.RegistryAuthenticationPrivilegedFunc(dockerCli, repoInfo.Index, "pull")
78 78
 
79
-	if command.IsTrusted() && !registryRef.HasDigest() {
80
-		// Check if tag is digest
81
-		err = trustedPull(ctx, dockerCli, repoInfo, registryRef, authConfig, requestPrivilege)
79
+	// Check if reference has a digest
80
+	_, isCanonical := distributionRef.(reference.Canonical)
81
+	if command.IsTrusted() && !isCanonical {
82
+		err = trustedPull(ctx, dockerCli, repoInfo, distributionRef, authConfig, requestPrivilege)
82 83
 	} else {
83 84
 		err = imagePullPrivileged(ctx, dockerCli, authConfig, distributionRef.String(), requestPrivilege, opts.all)
84 85
 	}
... ...
@@ -46,9 +46,9 @@ var (
46 46
 )
47 47
 
48 48
 type target struct {
49
-	reference registry.Reference
50
-	digest    digest.Digest
51
-	size      int64
49
+	name   string
50
+	digest digest.Digest
51
+	size   int64
52 52
 }
53 53
 
54 54
 // trustedPush handles content trust pushing of an image
... ...
@@ -81,7 +81,7 @@ func trustedPush(ctx context.Context, cli *command.DockerCli, repoInfo *registry
81 81
 				target = nil
82 82
 				return
83 83
 			}
84
-			target.Name = registry.ParseReference(pushResult.Tag).String()
84
+			target.Name = pushResult.Tag
85 85
 			target.Hashes = data.Hashes{string(pushResult.Digest.Algorithm()): h}
86 86
 			target.Length = int64(pushResult.Size)
87 87
 		}
... ...
@@ -93,11 +93,9 @@ func trustedPush(ctx context.Context, cli *command.DockerCli, repoInfo *registry
93 93
 		return errors.New("cannot push a digest reference")
94 94
 	case reference.NamedTagged:
95 95
 		tag = x.Tag()
96
-	}
97
-
98
-	// We want trust signatures to always take an explicit tag,
99
-	// otherwise it will act as an untrusted push.
100
-	if tag == "" {
96
+	default:
97
+		// We want trust signatures to always take an explicit tag,
98
+		// otherwise it will act as an untrusted push.
101 99
 		if err = jsonmessage.DisplayJSONMessagesToStream(responseBody, cli.Out(), nil); err != nil {
102 100
 			return err
103 101
 		}
... ...
@@ -234,7 +232,7 @@ func imagePushPrivileged(ctx context.Context, cli *command.DockerCli, authConfig
234 234
 }
235 235
 
236 236
 // trustedPull handles content trust pulling of an image
237
-func trustedPull(ctx context.Context, cli *command.DockerCli, repoInfo *registry.RepositoryInfo, ref registry.Reference, authConfig types.AuthConfig, requestPrivilege types.RequestPrivilegeFunc) error {
237
+func trustedPull(ctx context.Context, cli *command.DockerCli, repoInfo *registry.RepositoryInfo, ref reference.Named, authConfig types.AuthConfig, requestPrivilege types.RequestPrivilegeFunc) error {
238 238
 	var refs []target
239 239
 
240 240
 	notaryRepo, err := GetNotaryRepository(cli, repoInfo, authConfig, "pull")
... ...
@@ -243,7 +241,7 @@ func trustedPull(ctx context.Context, cli *command.DockerCli, repoInfo *registry
243 243
 		return err
244 244
 	}
245 245
 
246
-	if ref.String() == "" {
246
+	if tagged, isTagged := ref.(reference.NamedTagged); !isTagged {
247 247
 		// List all targets
248 248
 		targets, err := notaryRepo.ListTargets(releasesRole, data.CanonicalTargetsRole)
249 249
 		if err != nil {
... ...
@@ -266,14 +264,14 @@ func trustedPull(ctx context.Context, cli *command.DockerCli, repoInfo *registry
266 266
 			return notaryError(repoInfo.FullName(), fmt.Errorf("No trusted tags for %s", repoInfo.FullName()))
267 267
 		}
268 268
 	} else {
269
-		t, err := notaryRepo.GetTargetByName(ref.String(), releasesRole, data.CanonicalTargetsRole)
269
+		t, err := notaryRepo.GetTargetByName(tagged.Tag(), releasesRole, data.CanonicalTargetsRole)
270 270
 		if err != nil {
271 271
 			return notaryError(repoInfo.FullName(), err)
272 272
 		}
273 273
 		// Only get the tag if it's in the top level targets role or the releases delegation role
274 274
 		// ignore it if it's in any other delegation roles
275 275
 		if t.Role != releasesRole && t.Role != data.CanonicalTargetsRole {
276
-			return notaryError(repoInfo.FullName(), fmt.Errorf("No trust data for %s", ref.String()))
276
+			return notaryError(repoInfo.FullName(), fmt.Errorf("No trust data for %s", tagged.Tag()))
277 277
 		}
278 278
 
279 279
 		logrus.Debugf("retrieving target for %s role\n", t.Role)
... ...
@@ -286,7 +284,7 @@ func trustedPull(ctx context.Context, cli *command.DockerCli, repoInfo *registry
286 286
 	}
287 287
 
288 288
 	for i, r := range refs {
289
-		displayTag := r.reference.String()
289
+		displayTag := r.name
290 290
 		if displayTag != "" {
291 291
 			displayTag = ":" + displayTag
292 292
 		}
... ...
@@ -300,19 +298,16 @@ func trustedPull(ctx context.Context, cli *command.DockerCli, repoInfo *registry
300 300
 			return err
301 301
 		}
302 302
 
303
-		// If reference is not trusted, tag by trusted reference
304
-		if !r.reference.HasDigest() {
305
-			tagged, err := reference.WithTag(repoInfo, r.reference.String())
306
-			if err != nil {
307
-				return err
308
-			}
309
-			trustedRef, err := reference.WithDigest(reference.TrimNamed(repoInfo), r.digest)
310
-			if err != nil {
311
-				return err
312
-			}
313
-			if err := TagTrusted(ctx, cli, trustedRef, tagged); err != nil {
314
-				return err
315
-			}
303
+		tagged, err := reference.WithTag(repoInfo, r.name)
304
+		if err != nil {
305
+			return err
306
+		}
307
+		trustedRef, err := reference.WithDigest(reference.TrimNamed(repoInfo), r.digest)
308
+		if err != nil {
309
+			return err
310
+		}
311
+		if err := TagTrusted(ctx, cli, trustedRef, tagged); err != nil {
312
+			return err
316 313
 		}
317 314
 	}
318 315
 	return nil
... ...
@@ -533,9 +528,9 @@ func convertTarget(t client.Target) (target, error) {
533 533
 		return target{}, errors.New("no valid hash, expecting sha256")
534 534
 	}
535 535
 	return target{
536
-		reference: registry.ParseReference(t.Name),
537
-		digest:    digest.NewDigestFromHex("sha256", hex.EncodeToString(h)),
538
-		size:      t.Length,
536
+		name:   t.Name,
537
+		digest: digest.NewDigestFromHex("sha256", hex.EncodeToString(h)),
538
+		size:   t.Length,
539 539
 	}, nil
540 540
 }
541 541
 
542 542
deleted file mode 100644
... ...
@@ -1,68 +0,0 @@
1
-package registry
2
-
3
-import (
4
-	"strings"
5
-
6
-	"github.com/docker/distribution/digest"
7
-)
8
-
9
-// Reference represents a tag or digest within a repository
10
-type Reference interface {
11
-	// HasDigest returns whether the reference has a verifiable
12
-	// content addressable reference which may be considered secure.
13
-	HasDigest() bool
14
-
15
-	// ImageName returns an image name for the given repository
16
-	ImageName(string) string
17
-
18
-	// Returns a string representation of the reference
19
-	String() string
20
-}
21
-
22
-type tagReference struct {
23
-	tag string
24
-}
25
-
26
-func (tr tagReference) HasDigest() bool {
27
-	return false
28
-}
29
-
30
-func (tr tagReference) ImageName(repo string) string {
31
-	return repo + ":" + tr.tag
32
-}
33
-
34
-func (tr tagReference) String() string {
35
-	return tr.tag
36
-}
37
-
38
-type digestReference struct {
39
-	digest digest.Digest
40
-}
41
-
42
-func (dr digestReference) HasDigest() bool {
43
-	return true
44
-}
45
-
46
-func (dr digestReference) ImageName(repo string) string {
47
-	return repo + "@" + dr.String()
48
-}
49
-
50
-func (dr digestReference) String() string {
51
-	return dr.digest.String()
52
-}
53
-
54
-// ParseReference parses a reference into either a digest or tag reference
55
-func ParseReference(ref string) Reference {
56
-	if strings.Contains(ref, ":") {
57
-		dgst, err := digest.ParseDigest(ref)
58
-		if err == nil {
59
-			return digestReference{digest: dgst}
60
-		}
61
-	}
62
-	return tagReference{tag: ref}
63
-}
64
-
65
-// DigestReference creates a digest reference using a digest
66
-func DigestReference(dgst digest.Digest) Reference {
67
-	return digestReference{digest: dgst}
68
-}