MCPcopy Create free account
hub / github.com/docker/cli / PushTrustedReference

Function PushTrustedReference

cmd/docker-trust/internal/trust/trust_push.go:42–150  ·  view source on GitHub ↗

PushTrustedReference pushes a canonical reference to the trust server. nolint:gocyclo

(ctx context.Context, ioStreams Streams, repoInfo *RepositoryInfo, ref reference.Named, authConfig registrytypes.AuthConfig, in io.Reader, userAgent string)

Source from the content-addressed store, hash-verified

40//
41//nolint:gocyclo
42func PushTrustedReference(ctx context.Context, ioStreams Streams, repoInfo *RepositoryInfo, ref reference.Named, authConfig registrytypes.AuthConfig, in io.Reader, userAgent string) error {
43 // If it is a trusted push we would like to find the target entry which match the
44 // tag provided in the function and then do an AddTarget later.
45 notaryTarget := &client.Target{}
46 // Count the times of calling for handleTarget,
47 // if it is called more that once, that should be considered an error in a trusted push.
48 cnt := 0
49 handleTarget := func(msg jsonstream.JSONMessage) {
50 cnt++
51 if cnt > 1 {
52 // handleTarget should only be called once. This will be treated as an error.
53 return
54 }
55
56 var pushResult PushResult
57 err := json.Unmarshal(*msg.Aux, &pushResult)
58 if err == nil && pushResult.Tag != "" {
59 if dgst, err := digest.Parse(pushResult.Digest); err == nil {
60 h, err := hex.DecodeString(dgst.Hex())
61 if err != nil {
62 notaryTarget = nil
63 return
64 }
65 notaryTarget.Name = pushResult.Tag
66 notaryTarget.Hashes = data.Hashes{string(dgst.Algorithm()): h}
67 notaryTarget.Length = int64(pushResult.Size)
68 }
69 }
70 }
71
72 var tag string
73 switch x := ref.(type) {
74 case reference.Digested:
75 return errors.New("cannot push a digest reference")
76 case reference.Tagged:
77 tag = x.Tag()
78 default:
79 // We want trust signatures to always take an explicit tag,
80 // otherwise it will act as an untrusted push.
81 if err := jsonstream.Display(ctx, in, ioStreams.Out()); err != nil {
82 return err
83 }
84 _, _ = fmt.Fprintln(ioStreams.Err(), "No tag specified, skipping trust metadata push")
85 return nil
86 }
87
88 if err := jsonstream.Display(ctx, in, ioStreams.Out(), jsonstream.WithAuxCallback(handleTarget)); err != nil {
89 return err
90 }
91
92 if cnt > 1 {
93 return errors.New("internal error: only one call to handleTarget expected")
94 }
95
96 if notaryTarget == nil {
97 return errors.New("no targets found, provide a specific tag in order to sign it")
98 }
99

Callers

nothing calls this directly

Calls 14

GetNotaryRepositoryFunction · 0.85
NotaryErrorFunction · 0.85
AddToAllSignableRolesFunction · 0.85
AddTargetMethod · 0.80
OutMethod · 0.65
ErrMethod · 0.65
InMethod · 0.65
TagMethod · 0.45
ListTargetsMethod · 0.45
GetCryptoServiceMethod · 0.45
IDMethod · 0.45
InitializeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…