-
Notifications
You must be signed in to change notification settings - Fork 62
/
provider.go
305 lines (253 loc) · 13.4 KB
/
provider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
Copyright The Ratify Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package azurekeyvault
// This class is based on implementation from azure secret store csi provider
// Source: https://github.com/Azure/secrets-store-csi-driver-provider-azure/tree/release-1.4/pkg/provider
import (
"context"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"fmt"
"reflect"
"strings"
"time"
re "github.com/deislabs/ratify/errors"
"github.com/deislabs/ratify/internal/logger"
"github.com/deislabs/ratify/pkg/certificateprovider"
"github.com/deislabs/ratify/pkg/certificateprovider/azurekeyvault/types"
"github.com/deislabs/ratify/pkg/metrics"
"golang.org/x/crypto/pkcs12"
kv "github.com/Azure/azure-sdk-for-go/services/keyvault/v7.1/keyvault"
"github.com/Azure/go-autorest/autorest/azure"
"gopkg.in/yaml.v2"
)
const (
providerName string = "azurekeyvault"
PKCS12ContentType string = "application/x-pkcs12"
PEMContentType string = "application/x-pem-file"
)
var logOpt = logger.Option{
ComponentType: logger.CertProvider,
}
type akvCertProvider struct{}
// init calls to register the provider
func init() {
certificateprovider.Register(providerName, Create())
}
func Create() certificateprovider.CertificateProvider {
// returning a simple provider for now, overtime we will add metrics and other related properties
return &akvCertProvider{}
}
// returns an array of certificates based on certificate properties defined in attrib map
// get certificate retrieve the entire cert chain using getSecret API call
func (s *akvCertProvider) GetCertificates(ctx context.Context, attrib map[string]string) ([]*x509.Certificate, certificateprovider.CertificatesStatus, error) {
keyvaultURI := types.GetKeyVaultURI(attrib)
cloudName := types.GetCloudName(attrib)
tenantID := types.GetTenantID(attrib)
workloadIdentityClientID := types.GetClientID(attrib)
if keyvaultURI == "" {
return nil, nil, re.ErrorCodeConfigInvalid.NewError(re.CertProvider, providerName, re.AKVLink, nil, "keyvaultUri is not set", re.HideStackTrace)
}
if tenantID == "" {
return nil, nil, re.ErrorCodeConfigInvalid.NewError(re.CertProvider, providerName, re.AKVLink, nil, "tenantID is not set", re.HideStackTrace)
}
if workloadIdentityClientID == "" {
return nil, nil, re.ErrorCodeConfigInvalid.NewError(re.CertProvider, providerName, re.AKVLink, nil, "clientID is not set", re.HideStackTrace)
}
azureCloudEnv, err := parseAzureEnvironment(cloudName)
if err != nil {
return nil, nil, re.ErrorCodeConfigInvalid.NewError(re.CertProvider, providerName, re.EmptyLink, nil, fmt.Sprintf("cloudName %s is not valid", cloudName), re.HideStackTrace)
}
keyVaultCerts, err := getKeyvaultRequestObj(ctx, attrib)
if err != nil {
return nil, nil, re.ErrorCodeConfigInvalid.NewError(re.CertProvider, providerName, re.AKVLink, err, "failed to get keyvault request object from provider attributes", re.HideStackTrace)
}
if len(keyVaultCerts) == 0 {
return nil, nil, re.ErrorCodeConfigInvalid.NewError(re.CertProvider, providerName, re.EmptyLink, nil, "no keyvault certificate configured", re.PrintStackTrace)
}
logger.GetLogger(ctx, logOpt).Debugf("vaultURI %s", keyvaultURI)
kvClient, err := initializeKvClient(ctx, azureCloudEnv.KeyVaultEndpoint, tenantID, workloadIdentityClientID)
if err != nil {
return nil, nil, re.ErrorCodePluginInitFailure.NewError(re.CertProvider, providerName, re.AKVLink, err, "failed to get keyvault client", re.HideStackTrace)
}
certs := []*x509.Certificate{}
certsStatus := []map[string]string{}
for _, keyVaultCert := range keyVaultCerts {
logger.GetLogger(ctx, logOpt).Debugf("fetching secret from key vault, certName %v, keyvault %v", keyVaultCert.CertificateName, keyvaultURI)
// fetch the object from Key Vault
// GetSecret is required so we can fetch the entire cert chain. See issue https://github.com/deislabs/ratify/issues/695 for details
startTime := time.Now()
secretBundle, err := kvClient.GetSecret(ctx, keyvaultURI, keyVaultCert.CertificateName, keyVaultCert.CertificateVersion)
if err != nil {
return nil, nil, fmt.Errorf("failed to get secret objectName:%s, objectVersion:%s, error: %w", keyVaultCert.CertificateName, keyVaultCert.CertificateVersion, err)
}
certResult, certProperty, err := getCertsFromSecretBundle(ctx, secretBundle, keyVaultCert.CertificateName)
if err != nil {
return nil, nil, fmt.Errorf("failed to get certificates from secret bundle:%w", err)
}
metrics.ReportAKVCertificateDuration(ctx, time.Since(startTime).Milliseconds(), keyVaultCert.CertificateName)
certs = append(certs, certResult...)
certsStatus = append(certsStatus, certProperty...)
}
return certs, getCertStatusMap(certsStatus), nil
}
// azure keyvault provider certificate status is a map from "certificates" key to an array of of certificate status
func getCertStatusMap(certsStatus []map[string]string) certificateprovider.CertificatesStatus {
status := certificateprovider.CertificatesStatus{}
status[types.CertificatesStatus] = certsStatus
return status
}
// parse the requested keyvault cert object from the input attributes
func getKeyvaultRequestObj(ctx context.Context, attrib map[string]string) ([]types.KeyVaultCertificate, error) {
keyVaultCerts := []types.KeyVaultCertificate{}
certificatesStrings := types.GetCertificates(attrib)
if certificatesStrings == "" {
return nil, re.ErrorCodeConfigInvalid.NewError(re.CertProvider, providerName, re.EmptyLink, nil, "certificates is not set", re.HideStackTrace)
}
logger.GetLogger(ctx, logOpt).Debugf("certificates string defined in ratify certStore class, certificates %v", certificatesStrings)
objects, err := types.GetCertificatesArray(certificatesStrings)
if err != nil {
return nil, re.ErrorCodeDataDecodingFailure.NewError(re.CertProvider, providerName, re.EmptyLink, err, "failed to yaml unmarshal objects", re.HideStackTrace)
}
logger.GetLogger(ctx, logOpt).Debugf("unmarshaled objects yaml, objectsArray %v", objects.Array)
for i, object := range objects.Array {
var keyVaultCert types.KeyVaultCertificate
if err = yaml.Unmarshal([]byte(object), &keyVaultCert); err != nil {
return nil, re.ErrorCodeDataDecodingFailure.NewError(re.CertProvider, providerName, re.EmptyLink, err, fmt.Sprintf("unmarshal failed for keyVaultCerts at index: %d", i), re.PrintStackTrace)
}
// remove whitespace from all fields in keyVaultCert
formatKeyVaultCertificate(&keyVaultCert)
keyVaultCerts = append(keyVaultCerts, keyVaultCert)
}
logger.GetLogger(ctx, logOpt).Debugf("unmarshaled %v key vault objects, keyVaultObjects: %v", len(keyVaultCerts), keyVaultCerts)
return keyVaultCerts, nil
}
// return a certificate status object that consist of the cert name, version and last refreshed time
func getCertStatusProperty(certificateName, version, lastRefreshed string) map[string]string {
certProperty := map[string]string{}
certProperty[types.CertificateName] = certificateName
certProperty[types.CertificateVersion] = version
certProperty[types.CertificateLastRefreshed] = lastRefreshed
return certProperty
}
// formatKeyVaultCertificate formats the fields in KeyVaultCertificate
func formatKeyVaultCertificate(object *types.KeyVaultCertificate) {
if object == nil {
return
}
objectPtr := reflect.ValueOf(object)
objectValue := objectPtr.Elem()
for i := 0; i < objectValue.NumField(); i++ {
field := objectValue.Field(i)
if field.Type() != reflect.TypeOf("") {
continue
}
str := field.Interface().(string)
str = strings.TrimSpace(str)
field.SetString(str)
}
}
// parseAzureEnvironment returns azure environment by name
func parseAzureEnvironment(cloudName string) (*azure.Environment, error) {
var env azure.Environment
var err error
if cloudName == "" {
env = azure.PublicCloud
} else {
env, err = azure.EnvironmentFromName(cloudName)
}
return &env, err
}
func initializeKvClient(ctx context.Context, keyVaultEndpoint, tenantID, clientID string) (*kv.BaseClient, error) {
kvClient := kv.New()
kvEndpoint := strings.TrimSuffix(keyVaultEndpoint, "/")
err := kvClient.AddToUserAgent("ratify")
if err != nil {
return nil, re.ErrorCodeConfigInvalid.NewError(re.CertProvider, providerName, re.AKVLink, err, "failed to add user agent to keyvault client", re.PrintStackTrace)
}
kvClient.Authorizer, err = getAuthorizerForWorkloadIdentity(ctx, tenantID, clientID, kvEndpoint)
if err != nil {
return nil, re.ErrorCodeAuthDenied.NewError(re.CertProvider, providerName, re.AKVLink, err, "failed to get authorizer for keyvault client", re.PrintStackTrace)
}
return &kvClient, nil
}
// Parse the secret bundle and return an array of certificates
// In a certificate chain scenario, all certificates from root to leaf will be returned
func getCertsFromSecretBundle(ctx context.Context, secretBundle kv.SecretBundle, certName string) ([]*x509.Certificate, []map[string]string, error) {
if secretBundle.ContentType == nil || secretBundle.Value == nil || secretBundle.ID == nil {
return nil, nil, re.ErrorCodeCertInvalid.NewError(re.CertProvider, providerName, re.EmptyLink, nil, "found invalid secret bundle for certificate %s, contentType, value, and id must not be nil", re.HideStackTrace)
}
version := getObjectVersion(*secretBundle.ID)
// This aligns with notation akv implementation
// akv plugin supports both PKCS12 and PEM. https://github.com/Azure/notation-azure-kv/blob/558e7345ef8318783530de6a7a0a8420b9214ba8/Notation.Plugin.AzureKeyVault/KeyVault/KeyVaultClient.cs#L192
if *secretBundle.ContentType != PKCS12ContentType &&
*secretBundle.ContentType != PEMContentType {
return nil, nil, re.ErrorCodeCertInvalid.NewError(re.CertProvider, providerName, re.EmptyLink, nil, fmt.Sprintf("certificate %s version %s, unsupported secret content type %s, supported type are %s and %s", certName, version, *secretBundle.ContentType, PKCS12ContentType, PEMContentType), re.HideStackTrace)
}
results := []*x509.Certificate{}
certsStatus := []map[string]string{}
lastRefreshed := time.Now().Format(time.RFC3339)
data := []byte(*secretBundle.Value)
if *secretBundle.ContentType == PKCS12ContentType {
p12, err := base64.StdEncoding.DecodeString(*secretBundle.Value)
if err != nil {
return nil, nil, re.ErrorCodeCertInvalid.NewError(re.CertProvider, providerName, re.EmptyLink, err, fmt.Sprintf("azure keyvault certificate provider: failed to decode PKCS12 Value. Certificate %s, version %s", certName, version), re.HideStackTrace)
}
blocks, err := pkcs12.ToPEM(p12, "")
if err != nil {
return nil, nil, re.ErrorCodeCertInvalid.NewError(re.CertProvider, providerName, re.EmptyLink, err, fmt.Sprintf("azure keyvault certificate provider: failed to convert PKCS12 Value to PEM. Certificate %s, version %s", certName, version), re.HideStackTrace)
}
var pemData []byte
for _, b := range blocks {
pemData = append(pemData, pem.EncodeToMemory(b)...)
}
data = pemData
}
block, rest := pem.Decode(data)
for block != nil {
switch block.Type {
case "PRIVATE KEY":
logger.GetLogger(ctx, logOpt).Warnf("azure keyvault certificate provider: certificate %s, version %s private key skipped. Please see doc to learn how to create a new certificate in keyvault with non exportable keys. https://learn.microsoft.com/en-us/azure/key-vault/certificates/how-to-export-certificate?tabs=azure-cli#exportable-and-non-exportable-keys", certName, version)
case "CERTIFICATE":
var pemData []byte
pemData = append(pemData, pem.EncodeToMemory(block)...)
decodedCerts, err := certificateprovider.DecodeCertificates(pemData)
if err != nil {
return nil, nil, re.ErrorCodeCertInvalid.NewError(re.CertProvider, providerName, re.EmptyLink, err, fmt.Sprintf("azure keyvault certificate provider: failed to decode Certificate %s, version %s", certName, version), re.HideStackTrace)
}
for _, cert := range decodedCerts {
results = append(results, cert)
certProperty := getCertStatusProperty(certName, version, lastRefreshed)
certsStatus = append(certsStatus, certProperty)
}
default:
logger.GetLogger(ctx, logOpt).Warnf("certificate '%s', version '%s': azure keyvault certificate provider detected unknown block type %s", certName, version, block.Type)
}
block, rest = pem.Decode(rest)
if block == nil && len(rest) > 0 {
return nil, nil, re.ErrorCodeCertInvalid.NewError(re.CertProvider, providerName, re.EmptyLink, nil, fmt.Sprintf("certificate '%s', version '%s': azure keyvault certificate provider error, block is nil and remaining block to parse > 0", certName, version), re.HideStackTrace)
}
}
logger.GetLogger(ctx, logOpt).Debugf("azurekeyvault certprovider getCertsFromSecretBundle: %v certificates parsed, Certificate '%s', version '%s'", len(results), certName, version)
return results, certsStatus, nil
}
// getObjectVersion parses the id to retrieve the version
// of object fetched
// example id format - https://kindkv.vault.azure.net/secrets/actual/1f304204f3624873aab40231241243eb
// TODO (aramase) follow up on https://github.com/Azure/azure-rest-api-specs/issues/10825 to provide
// a native way to obtain the version
func getObjectVersion(id string) string {
splitID := strings.Split(id, "/")
return splitID[len(splitID)-1]
}