| 1 |
package simplesigning |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"strings" |
| 8 |
|
| 9 |
"go.podman.io/image/v5/docker/reference" |
| 10 |
internalSig "go.podman.io/image/v5/internal/signature" |
| 11 |
internalSigner "go.podman.io/image/v5/internal/signer" |
| 12 |
"go.podman.io/image/v5/signature" |
| 13 |
"go.podman.io/image/v5/signature/signer" |
| 14 |
) |
| 15 |
|
| 16 |
// simpleSigner is a signer.SignerImplementation implementation for simple signing signatures. |
| 17 |
type simpleSigner struct { |
| 18 |
mech signature.SigningMechanism |
| 19 |
keyFingerprint string |
| 20 |
passphrase string // "" if not provided. |
| 21 |
} |
| 22 |
|
| 23 |
type Option func(*simpleSigner) error |
| 24 |
|
| 25 |
// WithKeyFingerprint returns an Option for NewSigner, specifying a key to sign with, using the provided GPG key fingerprint. |
| 26 |
func WithKeyFingerprint(keyFingerprint string) Option { |
| 27 |
return func(s *simpleSigner) error { |
| 28 |
s.keyFingerprint = keyFingerprint |
| 29 |
return nil |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
// WithPassphrase returns an Option for NewSigner, specifying a passphrase for the private key. |
| 34 |
// If this is not specified, the system may interactively prompt using a gpg-agent / pinentry. |
| 35 |
func WithPassphrase(passphrase string) Option { |
| 36 |
return func(s *simpleSigner) error { |
| 37 |
// The gpgme implementation can’t use passphrase with \n; reject it here for consistent behavior. |
| 38 |
if strings.Contains(passphrase, "\n") { |
| 39 |
return errors.New("invalid passphrase: must not contain a line break") |
| 40 |
} |
| 41 |
s.passphrase = passphrase |
| 42 |
return nil |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
// NewSigner returns a signature.Signer which creates “simple signing” signatures using the user’s default |
| 47 |
// GPG configuration ($GNUPGHOME / ~/.gnupg). |
| 48 |
// |
| 49 |
// The set of options must identify a key to sign with, probably using a WithKeyFingerprint. |
| 50 |
// |
| 51 |
// The caller must call Close() on the returned Signer. |
| 52 |
func NewSigner(opts ...Option) (*signer.Signer, error) { |
| 53 |
mech, err := signature.NewGPGSigningMechanism() |
| 54 |
if err != nil { |
| 55 |
return nil, fmt.Errorf("initializing GPG: %w", err) |
| 56 |
} |
| 57 |
succeeded := false |
| 58 |
defer func() { |
| 59 |
if !succeeded { |
| 60 |
mech.Close() |
| 61 |
} |
| 62 |
}() |
| 63 |
if err := mech.SupportsSigning(); err != nil { |
| 64 |
return nil, fmt.Errorf("Signing not supported: %w", err) |
| 65 |
} |
| 66 |
|
| 67 |
s := simpleSigner{ |
| 68 |
mech: mech, |
| 69 |
} |
| 70 |
for _, o := range opts { |
| 71 |
if err := o(&s); err != nil { |
| 72 |
return nil, err |
| 73 |
} |
| 74 |
} |
| 75 |
if s.keyFingerprint == "" { |
| 76 |
return nil, errors.New("no key identity provided for simple signing") |
| 77 |
} |
| 78 |
// Ideally, we should look up (and unlock?) the key at this point already, but our current SigningMechanism API does not allow that. |
| 79 |
|
| 80 |
succeeded = true |
| 81 |
return internalSigner.NewSigner(&s), nil |
| 82 |
} |
| 83 |
|
| 84 |
// ProgressMessage returns a human-readable sentence that makes sense to write before starting to create a single signature. |
| 85 |
func (s *simpleSigner) ProgressMessage() string { |
| 86 |
return "Signing image using simple signing" |
| 87 |
} |
| 88 |
|
| 89 |
// SignImageManifest creates a new signature for manifest m as dockerReference. |
| 90 |
func (s *simpleSigner) SignImageManifest(ctx context.Context, m []byte, dockerReference reference.Named) (internalSig.Signature, error) { |
| 91 |
if reference.IsNameOnly(dockerReference) { |
| 92 |
return nil, fmt.Errorf("reference %s can’t be signed, it has neither a tag nor a digest", dockerReference.String()) |
| 93 |
} |
| 94 |
simpleSig, err := signature.SignDockerManifestWithOptions(m, dockerReference.String(), s.mech, s.keyFingerprint, &signature.SignOptions{ |
| 95 |
Passphrase: s.passphrase, |
| 96 |
}) |
| 97 |
if err != nil { |
| 98 |
return nil, err |
| 99 |
} |
| 100 |
return internalSig.SimpleSigningFromBlob(simpleSig), nil |
| 101 |
} |
| 102 |
|
| 103 |
func (s *simpleSigner) Close() error { |
| 104 |
return s.mech.Close() |
| 105 |
} |