Skip to content

Commit ec180ec

Browse files
committed
deduplicate type assertion
On-behalf-of: Gerald Morrison (SAP) <gerald.morrison@sap.com> Signed-off-by: Gerald Morrison (SAP) <gerald.morrison@sap.com>
1 parent b9622ff commit ec180ec

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

api/tech/signing/handlers/sigstore/handler.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -320,30 +320,27 @@ func extractECDSAPublicKey(pubKeyBytes []byte) (*ecdsa.PublicKey, error) {
320320
if block == nil {
321321
return nil, fmt.Errorf("no PEM block found in Fulcio public key")
322322
}
323+
var rawPub any
323324
switch block.Type {
324325
case "PUBLIC KEY":
325326
result, err := x509.ParsePKIXPublicKey(block.Bytes)
326327
if err != nil {
327328
return nil, fmt.Errorf("failed to parse public key: %w", err)
328329
}
329-
// cast to ecdsa.PublicKey as we use this in the verification
330-
pub, ok := result.(*ecdsa.PublicKey)
331-
if !ok {
332-
return nil, fmt.Errorf("unexpected public key type: %T", result)
333-
}
334-
return pub, nil
330+
rawPub = result
335331
case "CERTIFICATE":
336332
cert, err := x509.ParseCertificate(block.Bytes)
337333
if err != nil {
338334
return nil, fmt.Errorf("failed to parse Fulcio certificate: %w", err)
339335
}
340-
// cast to ecdsa.PublicKey as we use this in the verification
341-
pub, ok := cert.PublicKey.(*ecdsa.PublicKey)
342-
if !ok {
343-
return nil, fmt.Errorf("unexpected certificate public key type: %T", cert.PublicKey)
344-
}
345-
return pub, nil
336+
rawPub = cert.PublicKey
346337
default:
347338
return nil, fmt.Errorf("unsupported PEM block type: %s", block.Type)
348339
}
340+
// cast to ecdsa.PublicKey as we use this in the verification
341+
pubKey, ok := rawPub.(*ecdsa.PublicKey)
342+
if !ok {
343+
return nil, fmt.Errorf("unexpected public key type: %T", rawPub)
344+
}
345+
return pubKey, nil
349346
}

0 commit comments

Comments
 (0)