1
0
Fork 0
mirror of https://github.com/external-secrets/external-secrets.git synced 2024-12-14 11:57:59 +00:00

fix: support parsing PEM from multiple blocks (#2110)

Before this PR it was required that the first PEM block contains the
certificate.
This PR parses all PEM blocks and returns the first certificate found.

Signed-off-by: Moritz Johner <beller.moritz@googlemail.com>
This commit is contained in:
Moritz Johner 2023-03-11 13:08:31 +01:00 committed by GitHub
parent da9689ccfd
commit c20fc5b8b3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 8 deletions

View file

@ -309,15 +309,31 @@ func (a *Azure) DeleteSecret(ctx context.Context, remoteRef esv1beta1.PushRemote
}
func getCertificateFromValue(value []byte) (*x509.Certificate, error) {
// 1st: try decode pkcs12
_, localCert, err := pkcs12.Decode(value, "")
if err != nil {
pemBlock, _ := pem.Decode(value)
if pemBlock == nil {
return x509.ParseCertificate(value)
}
return x509.ParseCertificate(pemBlock.Bytes)
if err == nil {
return localCert, nil
}
return localCert, err
// 2nd: try DER
localCert, err = x509.ParseCertificate(value)
if err == nil {
return localCert, nil
}
// 3nd: parse PEM blocks
for {
block, rest := pem.Decode(value)
value = rest
if block == nil {
break
}
cert, err := x509.ParseCertificate(block.Bytes)
if err == nil {
return cert, nil
}
}
return nil, fmt.Errorf("could not parse certificate value as PKCS#12, DER or PEM")
}
func getKeyFromValue(value []byte) (interface{}, error) {

View file

@ -606,6 +606,20 @@ func TestAzureKeyVaultPushSecret(t *testing.T) {
}
}
certPEMWithGarbageSuccess := func(smtc *secretManagerTestCase) {
pemCert, _ := base64.StdEncoding.DecodeString("LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLQpNSUlFb3dJQkFBS0NBUUVBNWNXTTdWYnd2dGtxWXVLd1lrVUhlSXRMQnd2eUd2ekhzODQwbDZpT00rZXZneEVXCmcydFAvb3NYeG1Rb1ZOdDJLUXRScEwweTh4K3JhWHhRRm5VTlJEc3pHSEkyWTdmWnVVRWd0M2FQQVhmUUlMNm0KazdJ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VHhXYUs5dXFPSXBhcklzCktZd3NwK2ZMZVdMYlE2a0dmbW5PNWlEUmQrb09ock5Zb29UWlZLOWZRUjVyRDJlNEIrZWEwcnpRRlhBQVVqSjUKT1hhYnhiRHJRK09NZWp3YzRIcXFzeHp0SmdEMmFQMmVLMi9MNVBXUHVnMEUrMWc4QUJaZlZib2gvTTNNSGdiegpQZ2FUcWd1ekdGZGtHM0VYdStPaEdiVTAvKzc3Vk1uWmkySVFabi9hd0dVYTdYK01QMEJEdmpWWTVrVnBNWjFoCmMyQ2xEamd4TnNMR3Rpa080Y1diNXNRUlIyR1lNM2RNazUwVlFjdEo1UnFzUnM2cE9DWERYRTNSZVZRajRoTkIKZVd2YURXUTJLbXlPYWk1NWRiRHJsSitudTgzT21DcDU5UnpQNWs1OFphRFhubEMzOFV3VHQwcTFENyt6RjB0cwoxRzkzMnVFQkhXWUh1T0E9Ci0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K")
smtc.setValue = pemCert
smtc.pushRef = fakeRef{
key: certName,
}
smtc.certOutput = keyvault.CertificateBundle{
X509Thumbprint: pointer.String("123"),
Tags: map[string]*string{
"managed-by": pointer.String("external-secrets"),
},
}
}
certDERSuccess := func(smtc *secretManagerTestCase) {
derCert, _ := base64.StdEncoding.DecodeString("MIIFpzCCA4+gAwIBAgIUMHaT6mdo/we+n+t4PvGBYiGCIq4wDQYJKoZIhvcNAQELBQAwYzELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDEcMBoGA1UEAwwTYW5vdGhlci1mb28tYmFyLmNvbTAeFw0yMjA2MDkxNzQ1MzZaFw0yMzA2MDkxNzQ1MzZaMGMxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQxHDAaBgNVBAMME2Fub3RoZXItZm9vLWJhci5jb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4gSKRi3fePLiWpt9XfXoGUCEj7q5phblLPgdKUCrhGP1n2jYmXscWLMykAna20chlrqVYnCa8JUp4LftFDp89YToXOFHnFmnNCvcIrDfFyDfhl4UMFpK0OUKqTTxWaK9uqOIparIsKYwsp+fLeWLbQ6kGfmnO5iDRd+oOhrNYooTZVK9fQR5rD2e4B+ea0rzQFXAAUjJ5OXabxbDrQ+OMejwc4HqqsxztJgD2aP2eK2/L5PWPug0E+1g8ABZfVboh/M3MHgbzPgaTqguzGFdkG3EXu+OhGbU0/+77VMnZi2IQZn/awGUa7X+MP0BDvjVY5kVpMZ1hc2ClDjgxNsLGtikO4cWb5sQRR2GYM3dMk50VQctJ5RqsRs6pOCXDXE3ReVQj4hNBeWvaDWQ2KmyOai55dbDrlJ+nu83OmCp59RzP5k58ZaDXnlC38UwTt0q1D7+zF0ts1G932uEBHWYHuOA=")
smtc.setValue = derCert
@ -683,7 +697,7 @@ func TestAzureKeyVaultPushSecret(t *testing.T) {
smtc.certOutput = keyvault.CertificateBundle{
X509Thumbprint: pointer.String("123"),
}
smtc.expectError = "value from secret is not a valid certificate: x509: malformed certificate"
smtc.expectError = "value from secret is not a valid certificate: could not parse certificate value as PKCS#12, DER or PEM"
}
certNoPermissions := func(smtc *secretManagerTestCase) {
@ -705,6 +719,7 @@ func TestAzureKeyVaultPushSecret(t *testing.T) {
successCases := []*secretManagerTestCase{
makeValidSecretManagerTestCaseCustom(certP12Success),
makeValidSecretManagerTestCaseCustom(certPEMSuccess),
makeValidSecretManagerTestCaseCustom(certPEMWithGarbageSuccess),
makeValidSecretManagerTestCaseCustom(certDERSuccess),
makeValidSecretManagerTestCaseCustom(certImportCertificateError),
makeValidSecretManagerTestCaseCustom(certFingerprintMatches),