|
| 1 | +package image |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "io/ioutil" |
| 7 | + "os" |
| 8 | + |
| 9 | + "github.com/openshift/origin/pkg/client" |
| 10 | + "github.com/openshift/origin/pkg/cmd/templates" |
| 11 | + "github.com/openshift/origin/pkg/cmd/util/clientcmd" |
| 12 | + imageapi "github.com/openshift/origin/pkg/image/api" |
| 13 | + "github.com/spf13/cobra" |
| 14 | + kapi "k8s.io/kubernetes/pkg/api" |
| 15 | + "k8s.io/kubernetes/pkg/api/unversioned" |
| 16 | + kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" |
| 17 | +) |
| 18 | + |
| 19 | +var ( |
| 20 | + verifyImageSignatureLongDesc = templates.LongDesc(` |
| 21 | + Verifies the imported image signature using the local public key. |
| 22 | +
|
| 23 | + This command verifies if the signature attached to an image is trusted by |
| 24 | + using the provided public GPG key. |
| 25 | + By default this command will not record the signature condition back to the Image object but only |
| 26 | + print the verification status to the console. |
| 27 | +
|
| 28 | + To record a new condition, you have to pass the "--confirm" flag. |
| 29 | + `) |
| 30 | + |
| 31 | + verifyImageSignatureExample = templates.Examples(` |
| 32 | + # Verify the image signature using the public key and record the status as a condition to image |
| 33 | + %[1]s sha256:c841e9b64e4579bd56c794bdd7c36e1c257110fd2404bebbb8b613e4935228c4 --public-key=production.gpg --confirm |
| 34 | + `) |
| 35 | +) |
| 36 | + |
| 37 | +type VerifyImageSignatureOptions struct { |
| 38 | + InputImage string |
| 39 | + PublicKeyFilename string |
| 40 | + PublicKey []byte |
| 41 | + Confirm bool |
| 42 | + Remove bool |
| 43 | + CurrentUser string |
| 44 | + |
| 45 | + Client client.Interface |
| 46 | + Out io.Writer |
| 47 | + ErrOut io.Writer |
| 48 | +} |
| 49 | + |
| 50 | +func NewCmdVerifyImageSignature(name, fullName string, f *clientcmd.Factory, out, errOut io.Writer) *cobra.Command { |
| 51 | + opts := &VerifyImageSignatureOptions{ErrOut: errOut, Out: out} |
| 52 | + cmd := &cobra.Command{ |
| 53 | + Use: fmt.Sprintf("%s IMAGE [--confirm]", name), |
| 54 | + Short: "Verifies the given IMAGE signature with local public key", |
| 55 | + Long: verifyImageSignatureLongDesc, |
| 56 | + Example: fmt.Sprintf(verifyImageSignatureExample, name), |
| 57 | + Run: func(cmd *cobra.Command, args []string) { |
| 58 | + kcmdutil.CheckErr(opts.Complete(f, cmd, args, out)) |
| 59 | + if opts.Remove { |
| 60 | + kcmdutil.CheckErr(opts.removeImageSignature()) |
| 61 | + } else { |
| 62 | + kcmdutil.CheckErr(opts.Run()) |
| 63 | + } |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + cmd.Flags().BoolVar(&opts.Confirm, "confirm", opts.Confirm, "If true, the result of the verification will be recorded to an image object.") |
| 68 | + cmd.Flags().BoolVar(&opts.Remove, "remove", opts.Remove, "If set, the current signature verification will be removed from the image.") |
| 69 | + cmd.Flags().StringVar(&opts.PublicKeyFilename, "public-key", opts.PublicKeyFilename, "A path to a public GPG key to be used for verification.") |
| 70 | + return cmd |
| 71 | +} |
| 72 | + |
| 73 | +func (o *VerifyImageSignatureOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command, args []string, out io.Writer) error { |
| 74 | + if len(args) != 1 { |
| 75 | + return kcmdutil.UsageError(cmd, "exactly one image must be specified") |
| 76 | + } |
| 77 | + o.InputImage = args[0] |
| 78 | + var err error |
| 79 | + |
| 80 | + // If --public-key is provided only this key will be used for verification and the |
| 81 | + // .gnupg/pubring.gpg will be ignored. |
| 82 | + if len(o.PublicKeyFilename) > 0 { |
| 83 | + o.PublicKey, err = ioutil.ReadFile(o.PublicKeyFilename) |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + } |
| 88 | + if o.Client, _, err = f.Clients(); err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + if o.Confirm && !o.Remove { |
| 92 | + if me, err := o.Client.Users().Get("~"); err != nil { |
| 93 | + return err |
| 94 | + } else { |
| 95 | + o.CurrentUser = me.Name |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return nil |
| 100 | +} |
| 101 | + |
| 102 | +// verifySignature verifies the image signature and return the identity when the signature |
| 103 | +// is valid. |
| 104 | +// TODO: This should be calling the 'containers/image' library in future. |
| 105 | +func (o *VerifyImageSignatureOptions) verifySignature(signature []byte) (string, error) { |
| 106 | + var ( |
| 107 | + mechanism SigningMechanism |
| 108 | + err error |
| 109 | + ) |
| 110 | + // If public key is specified, use JUST that key for verification. Otherwise use all |
| 111 | + // keys in local GPG public keyring. |
| 112 | + if len(o.PublicKeyFilename) == 0 { |
| 113 | + mechanism, err = newGPGSigningMechanismInDirectory("") |
| 114 | + } else { |
| 115 | + mechanism, _, err = newEphemeralGPGSigningMechanism(o.PublicKey) |
| 116 | + } |
| 117 | + if err != nil { |
| 118 | + return "", err |
| 119 | + } |
| 120 | + defer mechanism.Close() |
| 121 | + _, identity, err := mechanism.Verify(signature) |
| 122 | + if err != nil { |
| 123 | + return "", err |
| 124 | + } |
| 125 | + return string(identity), nil |
| 126 | +} |
| 127 | + |
| 128 | +// removeImageSignature removes the current image signature from the Image object by |
| 129 | +// erasing all signature fields that were previously set (when image signature was |
| 130 | +// previously verified). |
| 131 | +func (o *VerifyImageSignatureOptions) removeImageSignature() error { |
| 132 | + img, err := o.Client.Images().Get(o.InputImage) |
| 133 | + if err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + if len(img.Signatures) == 0 { |
| 137 | + return fmt.Errorf("%s does not have signature", img.Name) |
| 138 | + } |
| 139 | + if !o.Confirm { |
| 140 | + fmt.Fprintf(o.Out, "(add --confirm to record signature verification status to server\n") |
| 141 | + return nil |
| 142 | + } |
| 143 | + for i := range img.Signatures { |
| 144 | + newConditions := []imageapi.SignatureCondition{} |
| 145 | + img.Signatures[i].Conditions = newConditions |
| 146 | + img.Signatures[i].IssuedBy = nil |
| 147 | + } |
| 148 | + _, err = o.Client.Images().Update(img) |
| 149 | + return err |
| 150 | +} |
| 151 | + |
| 152 | +func (o *VerifyImageSignatureOptions) Run() error { |
| 153 | + img, err := o.Client.Images().Get(o.InputImage) |
| 154 | + if err != nil { |
| 155 | + return err |
| 156 | + } |
| 157 | + if len(img.Signatures) == 0 { |
| 158 | + return fmt.Errorf("%s does not have signature", img.Name) |
| 159 | + } |
| 160 | + |
| 161 | + for i, s := range img.Signatures { |
| 162 | + // Do the actual verification of the image signature |
| 163 | + signedBy, signatureErr := o.verifySignature(s.Content) |
| 164 | + |
| 165 | + if signatureErr != nil { |
| 166 | + if o.Confirm { |
| 167 | + fmt.Fprintf(o.ErrOut, "%s: %v\n", o.InputImage, signatureErr) |
| 168 | + } else { |
| 169 | + return fmt.Errorf("%s: %v", o.InputImage, signatureErr) |
| 170 | + } |
| 171 | + } else { |
| 172 | + fmt.Fprintf(o.Out, "%s signature is verified (signed by key: %q)\n", o.InputImage, signedBy) |
| 173 | + } |
| 174 | + |
| 175 | + if signatureErr != nil { |
| 176 | + // If an error occured during signature verification, remove the verified fields |
| 177 | + // (if present). |
| 178 | + fmt.Fprintf(o.ErrOut, "%s signature cannot be verified: %v\n", o.InputImage, signatureErr) |
| 179 | + if err := o.removeImageSignature(); err == nil { |
| 180 | + os.Exit(1) |
| 181 | + } else { |
| 182 | + return err |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + if o.Confirm { |
| 187 | + now := unversioned.Now() |
| 188 | + newConditions := []imageapi.SignatureCondition{ |
| 189 | + { |
| 190 | + Type: imageapi.SignatureTrusted, |
| 191 | + Status: kapi.ConditionTrue, |
| 192 | + LastProbeTime: now, |
| 193 | + LastTransitionTime: now, |
| 194 | + Reason: "verified manually", |
| 195 | + Message: fmt.Sprintf("verified by user %s", o.CurrentUser), |
| 196 | + }, |
| 197 | + // FIXME: This condition is required to be set for validation. |
| 198 | + { |
| 199 | + Type: imageapi.SignatureForImage, |
| 200 | + Status: kapi.ConditionTrue, |
| 201 | + LastProbeTime: now, |
| 202 | + LastTransitionTime: now, |
| 203 | + }, |
| 204 | + } |
| 205 | + img.Signatures[i].Conditions = newConditions |
| 206 | + img.Signatures[i].IssuedBy = &imageapi.SignatureIssuer{} |
| 207 | + // TODO: This should not be just a key id but a human-readable identity. |
| 208 | + img.Signatures[i].IssuedBy.CommonName = signedBy |
| 209 | + // Record updated information back to the server |
| 210 | + if _, err := o.Client.Images().Update(img); err != nil { |
| 211 | + return err |
| 212 | + } |
| 213 | + } else { |
| 214 | + fmt.Fprintf(o.Out, "(add --confirm to record signature verification status to server\n") |
| 215 | + return nil |
| 216 | + } |
| 217 | + |
| 218 | + } |
| 219 | + return nil |
| 220 | +} |
0 commit comments