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)
| 40 | // |
| 41 | //nolint:gocyclo |
| 42 | func 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 |
nothing calls this directly
no test coverage detected
searching dependent graphs…