createSignatureData creates and signs the image signature with bundle information
(ctx context.Context, imageName string, digestImage name.Digest, signer signature.SignerVerifier)
| 172 | |
| 173 | // createSignatureData creates and signs the image signature with bundle information |
| 174 | func createSignatureData(ctx context.Context, imageName string, digestImage name.Digest, signer signature.SignerVerifier) (*signatureData, error) { |
| 175 | // Create the cosign signature payload and sign it |
| 176 | payload, rawSignature, err := signature.SignImage(signer, digestImage, map[string]interface{}{}) |
| 177 | if err != nil { |
| 178 | return nil, err |
| 179 | } |
| 180 | |
| 181 | signatureBase64 := base64.StdEncoding.EncodeToString(rawSignature) |
| 182 | |
| 183 | // Create the signature structure for the stub rekor entry |
| 184 | signatureStruct := Signature{ |
| 185 | KeyID: "", |
| 186 | Signature: signatureBase64, |
| 187 | } |
| 188 | |
| 189 | signatureJSON, err := json.Marshal(signatureStruct) |
| 190 | if err != nil { |
| 191 | return nil, fmt.Errorf("failed to marshal signature structure: %w", err) |
| 192 | } |
| 193 | |
| 194 | // Get the public key from the signer for hashedrekord validation |
| 195 | publicKey, err := signer.PublicKey() |
| 196 | if err != nil { |
| 197 | return nil, fmt.Errorf("failed to get public key: %w", err) |
| 198 | } |
| 199 | |
| 200 | publicKeyBytes, err := cryptoutils.MarshalPublicKeyToPEM(publicKey) |
| 201 | if err != nil { |
| 202 | return nil, fmt.Errorf("failed to marshal public key: %w", err) |
| 203 | } |
| 204 | |
| 205 | // Create stubs for both Rekor entry signature creation and retrieval endpoints |
| 206 | err = rekor.StubRekorEntryCreationForSignature(ctx, payload, rawSignature, signatureJSON, publicKeyBytes) |
| 207 | if err != nil { |
| 208 | return nil, fmt.Errorf("error stubbing rekor endpoints: %w", err) |
| 209 | } |
| 210 | |
| 211 | // Upload to transparency log to get bundle information like Tekton Chains does |
| 212 | rekorBundle, _, err := uploadToTransparencyLog(ctx, payload, rawSignature, signer) |
| 213 | if err != nil { |
| 214 | return nil, err |
| 215 | } |
| 216 | |
| 217 | // Create the signature layer with bundle information using static.WithBundle |
| 218 | signatureLayer, err := static.NewSignature(payload, signatureBase64, static.WithBundle(rekorBundle)) |
| 219 | if err != nil { |
| 220 | return nil, err |
| 221 | } |
| 222 | |
| 223 | // Extract bundle information from signatureLayer to include in annotations |
| 224 | annotations := map[string]string{ |
| 225 | static.SignatureAnnotationKey: signatureBase64, |
| 226 | } |
| 227 | |
| 228 | // Add bundle annotation if bundle information exists |
| 229 | bundleJSON, err := json.Marshal(rekorBundle) |
| 230 | if err != nil { |
| 231 | return nil, fmt.Errorf("failed to marshal bundle for annotation: %w", err) |
no test coverage detected