/
gcpkms.go
1165 lines (1007 loc) · 37.8 KB
/
gcpkms.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package gcpkms
import (
"context"
"crypto/sha1" //nolint: gosec // We use sha1 to hash trust domain names in 128 bytes to avoid label value restrictions
"crypto/sha256"
"encoding/hex"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"hash/crc32"
"os"
"strings"
"sync"
"time"
"unicode"
"cloud.google.com/go/iam"
"cloud.google.com/go/iam/apiv1/iampb"
"cloud.google.com/go/kms/apiv1/kmspb"
"github.com/andres-erbsen/clock"
"github.com/gofrs/uuid/v5"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/hcl"
keymanagerv1 "github.com/spiffe/spire-plugin-sdk/proto/spire/plugin/server/keymanager/v1"
configv1 "github.com/spiffe/spire-plugin-sdk/proto/spire/service/common/config/v1"
"github.com/spiffe/spire/pkg/common/catalog"
"github.com/spiffe/spire/pkg/common/diskutil"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/fieldmaskpb"
)
const (
pluginName = "gcp_kms"
algorithmTag = "algorithm"
cryptoKeyNameTag = "crypto_key_name"
cryptoKeyVersionNameTag = "crypto_key_version_name"
cryptoKeyVersionStateTag = "crypto_key_version_state"
scheduledDestroyTimeTag = "scheduled_destroy_time"
reasonTag = "reason"
disposeCryptoKeysFrequency = time.Hour * 48
keepActiveCryptoKeysFrequency = time.Hour * 6
maxStaleDuration = time.Hour * 24 * 14 // Two weeks.
cryptoKeyNamePrefix = "spire-key"
labelNameServerID = "spire-server-id"
labelNameLastUpdate = "spire-last-update"
labelNameServerTD = "spire-server-td"
labelNameActive = "spire-active"
getPublicKeyMaxAttempts = 10
)
func BuiltIn() catalog.BuiltIn {
return builtin(New())
}
func builtin(p *Plugin) catalog.BuiltIn {
return catalog.MakeBuiltIn(pluginName,
keymanagerv1.KeyManagerPluginServer(p),
configv1.ConfigServiceServer(p),
)
}
type keyEntry struct {
cryptoKey *kmspb.CryptoKey
cryptoKeyVersionName string
publicKey *keymanagerv1.PublicKey
}
type pluginHooks struct {
newKMSClient func(context.Context, ...option.ClientOption) (cloudKeyManagementService, error)
clk clock.Clock
// Used for testing only.
disposeCryptoKeysSignal chan error
enqueueDestructionSignal chan error
keepActiveCryptoKeysSignal chan error
scheduleDestroySignal chan error
setInactiveSignal chan error
}
type pluginData struct {
customPolicy *iam.Policy3
serverID string
tdHash string
}
// Plugin is the main representation of this keymanager plugin.
type Plugin struct {
keymanagerv1.UnsafeKeyManagerServer
configv1.UnsafeConfigServer
cancelTasks context.CancelFunc
config *Config
configMtx sync.RWMutex
entries map[string]keyEntry
entriesMtx sync.RWMutex
pd *pluginData
pdMtx sync.RWMutex
hooks pluginHooks
kmsClient cloudKeyManagementService
log hclog.Logger
scheduleDestroy chan string
}
// Config provides configuration context for the plugin.
type Config struct {
// File path location where key metadata used by the plugin is persisted.
KeyMetadataFile string `hcl:"key_metadata_file" json:"key_metadata_file"`
KeyIdentifierFile string `hcl:"key_identifier_file" json:"key_identifier_file"`
// Key metadata used by the plugin.
KeyIdentifierValue string `hcl:"key_identifier_value" json:"key_identifier_value"`
// File path location to a custom IAM Policy (v3) that will be set to
// created CryptoKeys.
KeyPolicyFile string `hcl:"key_policy_file" json:"key_policy_file"`
// KeyRing is the resource ID of the key ring where the keys managed by this
// plugin reside, in the format projects/*/locations/*/keyRings/*.
KeyRing string `hcl:"key_ring" json:"key_ring"`
// Path to the service account file used to authenticate with the Cloud KMS
// API. If not specified, the value of the GOOGLE_APPLICATION_CREDENTIALS
// environment variable is used.
ServiceAccountFile string `hcl:"service_account_file" json:"service_account_file"`
}
// New returns an instantiated plugin.
func New() *Plugin {
return newPlugin(newKMSClient)
}
// newPlugin returns a new plugin instance.
func newPlugin(
newKMSClient func(context.Context, ...option.ClientOption) (cloudKeyManagementService, error),
) *Plugin {
return &Plugin{
entries: make(map[string]keyEntry),
hooks: pluginHooks{
newKMSClient: newKMSClient,
clk: clock.New(),
},
scheduleDestroy: make(chan string, 120),
}
}
func (p *Plugin) Close() error {
if p.kmsClient == nil {
return nil
}
p.log.Debug("Closing the connection to the Cloud KMS API service")
return p.kmsClient.Close()
}
// Configure sets up the plugin.
func (p *Plugin) Configure(ctx context.Context, req *configv1.ConfigureRequest) (*configv1.ConfigureResponse, error) {
config, err := parseAndValidateConfig(req.HclConfiguration)
if err != nil {
return nil, err
}
var serverID = config.KeyIdentifierValue
if serverID == "" && config.KeyMetadataFile != "" {
p.log.Warn("'key_metadata_file' is deprecated in favor of 'key_identifier_file' and will be removed in a future version")
serverID, err = getOrCreateServerID(config.KeyMetadataFile)
if err != nil {
return nil, err
}
}
if serverID == "" && config.KeyIdentifierFile != "" {
serverID, err = getOrCreateServerID(config.KeyIdentifierFile)
if err != nil {
return nil, err
}
}
p.log.Debug("Loaded server id", "server_id", serverID)
var customPolicy *iam.Policy3
if config.KeyPolicyFile != "" {
if customPolicy, err = parsePolicyFile(config.KeyPolicyFile); err != nil {
return nil, status.Errorf(codes.Internal, "could not parse policy file: %v", err)
}
}
// Label values do not allow "." and have a maximum length of 63 characters.
// https://cloud.google.com/kms/docs/creating-managing-labels#requirements
// Hash the trust domain name to avoid restrictions.
tdHashBytes := sha1.Sum([]byte(req.CoreConfiguration.TrustDomain)) //nolint: gosec // We use sha1 to hash trust domain names in 128 bytes to avoid label restrictions
tdHashString := hex.EncodeToString(tdHashBytes[:])
p.setPluginData(&pluginData{
customPolicy: customPolicy,
serverID: serverID,
tdHash: tdHashString,
})
var opts []option.ClientOption
if config.ServiceAccountFile != "" {
opts = append(opts, option.WithCredentialsFile(config.ServiceAccountFile))
}
kc, err := p.hooks.newKMSClient(ctx, opts...)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to create Google Cloud KMS client: %v", err)
}
fetcher := &keyFetcher{
keyRing: config.KeyRing,
kmsClient: kc,
log: p.log,
serverID: serverID,
tdHash: tdHashString,
}
p.log.Debug("Fetching keys from Cloud KMS", "key_ring", config.KeyRing)
keyEntries, err := fetcher.fetchKeyEntries(ctx)
if err != nil {
return nil, err
}
p.setCache(keyEntries)
p.kmsClient = kc
// Cancel previous tasks in case of re configure.
if p.cancelTasks != nil {
p.cancelTasks()
}
p.configMtx.Lock()
defer p.configMtx.Unlock()
p.config = config
// Start long-running tasks.
ctx, p.cancelTasks = context.WithCancel(context.Background())
go p.scheduleDestroyTask(ctx)
go p.keepActiveCryptoKeysTask(ctx)
go p.disposeCryptoKeysTask(ctx)
return &configv1.ConfigureResponse{}, nil
}
// GenerateKey creates a key in KMS. If a key already exists in the local storage,
// it is updated.
func (p *Plugin) GenerateKey(ctx context.Context, req *keymanagerv1.GenerateKeyRequest) (*keymanagerv1.GenerateKeyResponse, error) {
if req.KeyId == "" {
return nil, status.Error(codes.InvalidArgument, "key id is required")
}
if req.KeyType == keymanagerv1.KeyType_UNSPECIFIED_KEY_TYPE {
return nil, status.Error(codes.InvalidArgument, "key type is required")
}
pubKey, err := p.createKey(ctx, req.KeyId, req.KeyType)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to generate key: %v", err)
}
return &keymanagerv1.GenerateKeyResponse{
PublicKey: pubKey,
}, nil
}
// GetPublicKey returns the public key for a given key
func (p *Plugin) GetPublicKey(_ context.Context, req *keymanagerv1.GetPublicKeyRequest) (*keymanagerv1.GetPublicKeyResponse, error) {
if req.KeyId == "" {
return nil, status.Error(codes.InvalidArgument, "key id is required")
}
entry, ok := p.getKeyEntry(req.KeyId)
if !ok {
return nil, status.Errorf(codes.NotFound, "key %q not found", req.KeyId)
}
return &keymanagerv1.GetPublicKeyResponse{
PublicKey: entry.publicKey,
}, nil
}
// GetPublicKeys returns the publicKey for all the keys.
func (p *Plugin) GetPublicKeys(context.Context, *keymanagerv1.GetPublicKeysRequest) (*keymanagerv1.GetPublicKeysResponse, error) {
var keys []*keymanagerv1.PublicKey
p.entriesMtx.RLock()
defer p.entriesMtx.RUnlock()
for _, key := range p.entries {
keys = append(keys, key.publicKey)
}
return &keymanagerv1.GetPublicKeysResponse{PublicKeys: keys}, nil
}
// SetLogger sets a logger.
func (p *Plugin) SetLogger(log hclog.Logger) {
p.log = log
}
// SignData creates a digital signature for the data to be signed.
func (p *Plugin) SignData(ctx context.Context, req *keymanagerv1.SignDataRequest) (*keymanagerv1.SignDataResponse, error) {
if req.KeyId == "" {
return nil, status.Error(codes.InvalidArgument, "key id is required")
}
if req.SignerOpts == nil {
return nil, status.Error(codes.InvalidArgument, "signer opts is required")
}
keyEntry, hasKey := p.getKeyEntry(req.KeyId)
if !hasKey {
return nil, status.Errorf(codes.NotFound, "key %q not found", req.KeyId)
}
var (
hashAlgo keymanagerv1.HashAlgorithm
digest *kmspb.Digest
)
switch opts := req.SignerOpts.(type) {
case *keymanagerv1.SignDataRequest_HashAlgorithm:
hashAlgo = opts.HashAlgorithm
case *keymanagerv1.SignDataRequest_PssOptions:
// RSASSA-PSS is not supported by this plugin.
// See the comment in cryptoKeyVersionAlgorithmFromKeyType function for
// more details.
return nil, status.Error(codes.InvalidArgument, "the only RSA signature scheme supported is RSASSA-PKCS1-v1_5")
default:
return nil, status.Errorf(codes.InvalidArgument, "unsupported signer opts type %T", opts)
}
switch {
case hashAlgo == keymanagerv1.HashAlgorithm_UNSPECIFIED_HASH_ALGORITHM:
return nil, status.Error(codes.InvalidArgument, "hash algorithm is required")
case hashAlgo == keymanagerv1.HashAlgorithm_SHA256:
digest = &kmspb.Digest{
Digest: &kmspb.Digest_Sha256{Sha256: req.Data},
}
case hashAlgo == keymanagerv1.HashAlgorithm_SHA384:
digest = &kmspb.Digest{
Digest: &kmspb.Digest_Sha384{Sha384: req.Data},
}
default:
return nil, status.Error(codes.InvalidArgument, "hash algorithm not supported")
}
signResp, err := p.kmsClient.AsymmetricSign(ctx, &kmspb.AsymmetricSignRequest{
Name: keyEntry.cryptoKeyVersionName,
Digest: digest,
})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to sign: %v", err)
}
// Perform integrity verification.
if int64(crc32Checksum(signResp.Signature)) != signResp.SignatureCrc32C.Value {
return nil, status.Error(codes.Internal, "error signing: response corrupted in-transit")
}
return &keymanagerv1.SignDataResponse{
Signature: signResp.Signature,
KeyFingerprint: keyEntry.publicKey.Fingerprint,
}, nil
}
// createKey creates a new CryptoKey with a new CryptoKeyVersion in Cloud KMS
// if there is not already a cached entry with the specified SPIRE Key ID.
// If the cache already has an entry with this SPIRE Key ID, a new
// CryptoKeyVersion is added to the corresponding CryptoKey in Cloud KMS and the
// old CryptoKeyVersion is enqueued for destruction.
// If there is a specified IAM policy through the KeyPolicyFile configuration,
// that policy is set to the created CryptoKey. If there is no IAM policy specified,
// a default policy is constructed and attached. This function requests Cloud KMS
// to get the public key of the created CryptoKeyVersion. A keyEntry is returned
// with the CryptoKey, CryptoKeyVersion and public key.
func (p *Plugin) createKey(ctx context.Context, spireKeyID string, keyType keymanagerv1.KeyType) (*keymanagerv1.PublicKey, error) {
// If we already have this SPIRE Key ID cached, a new CryptoKeyVersion is
// added to the existing CryptoKey and the cache is updated. The old
// CryptoKeyVersion is enqueued for destruction.
if entry, ok := p.getKeyEntry(spireKeyID); ok {
return p.addCryptoKeyVersionToCachedEntry(ctx, entry, spireKeyID, keyType)
}
algorithm, err := cryptoKeyVersionAlgorithmFromKeyType(keyType)
if err != nil {
return nil, err
}
cryptoKeyID, err := p.generateCryptoKeyID(spireKeyID)
if err != nil {
return nil, fmt.Errorf("could not generate CryptoKeyID: %w", err)
}
cryptoKeyLabels, err := p.getCryptoKeyLabels()
if err != nil {
return nil, status.Errorf(codes.Internal, "could not get CryptoKey labels: %v", err)
}
config, err := p.getConfig()
if err != nil {
return nil, err
}
cryptoKey, err := p.kmsClient.CreateCryptoKey(ctx, &kmspb.CreateCryptoKeyRequest{
CryptoKey: &kmspb.CryptoKey{
Labels: cryptoKeyLabels,
Purpose: kmspb.CryptoKey_ASYMMETRIC_SIGN,
VersionTemplate: &kmspb.CryptoKeyVersionTemplate{
Algorithm: algorithm,
},
},
CryptoKeyId: cryptoKeyID,
Parent: config.KeyRing,
})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to create CryptoKey: %v", err)
}
log := p.log.With(cryptoKeyNameTag, cryptoKey.Name)
log.Debug("CryptoKey created", algorithmTag, algorithm)
if err := p.setIamPolicy(ctx, cryptoKey.Name); err != nil {
log.Debug("Failed to set IAM policy")
return nil, status.Errorf(codes.Internal, "failed to set IAM policy: %v", err)
}
cryptoKeyVersionName := cryptoKey.Name + "/cryptoKeyVersions/1"
log.Debug("CryptoKeyVersion version added", cryptoKeyVersionNameTag, cryptoKeyVersionName)
pubKey, err := getPublicKeyFromCryptoKeyVersion(ctx, p.log, p.kmsClient, cryptoKeyVersionName)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get public key: %v", err)
}
newKeyEntry := keyEntry{
cryptoKey: cryptoKey,
cryptoKeyVersionName: cryptoKeyVersionName,
publicKey: &keymanagerv1.PublicKey{
Id: spireKeyID,
Type: keyType,
PkixData: pubKey,
Fingerprint: makeFingerprint(pubKey),
},
}
p.setKeyEntry(spireKeyID, newKeyEntry)
return newKeyEntry.publicKey, nil
}
// addCryptoKeyVersionToCachedEntry adds a new CryptoKeyVersion to an existing
// CryptoKey, updating the cached entries.
func (p *Plugin) addCryptoKeyVersionToCachedEntry(ctx context.Context, entry keyEntry, spireKeyID string, keyType keymanagerv1.KeyType) (*keymanagerv1.PublicKey, error) {
algorithm, err := cryptoKeyVersionAlgorithmFromKeyType(keyType)
if err != nil {
return nil, err
}
log := p.log.With(cryptoKeyNameTag, entry.cryptoKey.Name)
// Check if the algorithm has changed and update if needed.
if entry.cryptoKey.VersionTemplate.Algorithm != algorithm {
entry.cryptoKey.VersionTemplate.Algorithm = algorithm
_, err := p.kmsClient.UpdateCryptoKey(ctx, &kmspb.UpdateCryptoKeyRequest{
CryptoKey: entry.cryptoKey,
UpdateMask: &fieldmaskpb.FieldMask{
Paths: []string{"version_template.algorithm"},
},
})
if err != nil {
return nil, fmt.Errorf("failed to update CryptoKey with updated algorithm: %w", err)
}
log.Debug("CryptoKey updated", algorithmTag, algorithm)
}
cryptoKeyVersion, err := p.kmsClient.CreateCryptoKeyVersion(ctx, &kmspb.CreateCryptoKeyVersionRequest{
Parent: entry.cryptoKey.Name,
CryptoKeyVersion: &kmspb.CryptoKeyVersion{
State: kmspb.CryptoKeyVersion_ENABLED,
},
})
if err != nil {
return nil, fmt.Errorf("failed to create CryptoKeyVersion: %w", err)
}
log.Debug("CryptoKeyVersion added", cryptoKeyVersionNameTag, cryptoKeyVersion.Name)
pubKey, err := getPublicKeyFromCryptoKeyVersion(ctx, p.log, p.kmsClient, cryptoKeyVersion.Name)
if err != nil {
return nil, fmt.Errorf("failed to get public key: %w", err)
}
newKeyEntry := keyEntry{
cryptoKey: entry.cryptoKey,
cryptoKeyVersionName: cryptoKeyVersion.Name,
publicKey: &keymanagerv1.PublicKey{
Id: spireKeyID,
Type: keyType,
PkixData: pubKey,
Fingerprint: makeFingerprint(pubKey),
},
}
p.setKeyEntry(spireKeyID, newKeyEntry)
if err := p.enqueueDestruction(entry.cryptoKeyVersionName); err != nil {
log.Error("Failed to enqueue CryptoKeyVersion for destruction", reasonTag, err)
}
return newKeyEntry.publicKey, nil
}
// disposeCryptoKeys looks for active CryptoKeys that haven't been updated
// during the maxStaleDuration time window. Those keys are then enqueued for
// destruction.
func (p *Plugin) disposeCryptoKeys(ctx context.Context) error {
p.log.Debug("Looking for CryptoKeys to dispose")
config, err := p.getConfig()
if err != nil {
return err
}
disposeCryptoKeysFilter, err := p.getDisposeCryptoKeysFilter()
if err != nil {
return err
}
itCryptoKeys := p.kmsClient.ListCryptoKeys(ctx, &kmspb.ListCryptoKeysRequest{
Parent: config.KeyRing,
Filter: disposeCryptoKeysFilter,
})
for {
cryptoKey, err := itCryptoKeys.Next()
if errors.Is(err, iterator.Done) {
break
}
if err != nil {
p.log.Error("Failure listing CryptoKeys to dispose", reasonTag, err)
return err
}
itCryptoKeyVersions := p.kmsClient.ListCryptoKeyVersions(ctx, &kmspb.ListCryptoKeyVersionsRequest{
Parent: cryptoKey.Name,
Filter: "state = " + kmspb.CryptoKeyVersion_ENABLED.String(),
})
// If the CryptoKey doesn't have any enabled CryptoKeyVersion, mark it
// as inactive so it's not returned future calls.
cryptoKeyVersion, err := itCryptoKeyVersions.Next()
if errors.Is(err, iterator.Done) {
p.setInactive(ctx, cryptoKey)
continue
}
for {
if err != nil {
p.log.Error("Failure listing CryptoKeyVersios", reasonTag, err)
return err
}
if err := p.enqueueDestruction(cryptoKeyVersion.Name); err != nil {
p.log.With(cryptoKeyNameTag, cryptoKey.Name).Error("Failed to enqueue CryptoKeyVersion for destruction", reasonTag, err)
}
cryptoKeyVersion, err = itCryptoKeyVersions.Next()
if errors.Is(err, iterator.Done) {
// No more enabled CryptoKeyVersions in this CryptoKey.
break
}
}
}
return nil
}
// disposeCryptoKeysTask will be run every 24hs.
// It will schedule the destruction of CryptoKeyVersions that have a
// spire-last-update label value older than two weeks.
// It will only schedule the destruction of CryptoKeyVersions belonging to the
// current trust domain but not the current server. The spire-server-td and
// spire-server-id labels are used to identify the trust domain and server.
func (p *Plugin) disposeCryptoKeysTask(ctx context.Context) {
ticker := p.hooks.clk.Ticker(disposeCryptoKeysFrequency)
defer ticker.Stop()
p.notifyDisposeCryptoKeys(nil)
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
err := p.disposeCryptoKeys(ctx)
p.notifyDisposeCryptoKeys(err)
}
}
}
// enqueueDestruction enqueues the specified CryptoKeyVersion for destruction.
func (p *Plugin) enqueueDestruction(cryptoKeyVersionName string) (err error) {
select {
case p.scheduleDestroy <- cryptoKeyVersionName:
p.log.Debug("CryptoKeyVersion enqueued for destruction", cryptoKeyVersionNameTag, cryptoKeyVersionName)
default:
err = fmt.Errorf("could not enqueue CryptoKeyVersion %q for destruction", cryptoKeyVersionName)
}
p.notifyEnqueueDestruction(err)
return err
}
// getAuthenticatedServiceAccount gets the email of the authenticated service
// account that is interacting with the Cloud KMS Service.
func (p *Plugin) getAuthenticatedServiceAccount() (email string, err error) {
tokenInfo, err := p.kmsClient.GetTokeninfo()
if err != nil {
return "", fmt.Errorf("could not get token information: %w", err)
}
if tokenInfo.Email == "" {
return "", errors.New("could not get email of authenticated service account; email is empty")
}
return tokenInfo.Email, nil
}
// getConfig gets the configuration of the plugin.
func (p *Plugin) getConfig() (*Config, error) {
p.configMtx.RLock()
defer p.configMtx.RUnlock()
if p.config == nil {
return nil, status.Error(codes.FailedPrecondition, "not configured")
}
return p.config, nil
}
// getCryptoKeyLabels gets the labels that must be set to a new CryptoKey
// that is being created.
func (p *Plugin) getCryptoKeyLabels() (map[string]string, error) {
pd, err := p.getPluginData()
if err != nil {
return nil, err
}
return map[string]string{
labelNameServerTD: pd.tdHash,
labelNameServerID: pd.serverID,
labelNameActive: "true",
}, nil
}
// getDisposeCryptoKeysFilter gets the filter to be used to get the list of
// CryptoKeys that are stale but are still marked as active.
func (p *Plugin) getDisposeCryptoKeysFilter() (string, error) {
now := p.hooks.clk.Now()
pd, err := p.getPluginData()
if err != nil {
return "", err
}
return fmt.Sprintf("labels.%s = %s AND labels.%s != %s AND labels.%s = true AND labels.%s < %d",
labelNameServerTD, pd.tdHash, labelNameServerID, pd.serverID, labelNameActive, labelNameLastUpdate, now.Add(-maxStaleDuration).Unix()), nil
}
// getKeyEntry gets the entry from the cache that matches the provided
// SPIRE Key ID
func (p *Plugin) getKeyEntry(keyID string) (ke keyEntry, ok bool) {
p.entriesMtx.RLock()
defer p.entriesMtx.RUnlock()
ke, ok = p.entries[keyID]
return ke, ok
}
// getPluginData gets the pluginData structure maintained by the plugin.
func (p *Plugin) getPluginData() (*pluginData, error) {
p.pdMtx.RLock()
defer p.pdMtx.RUnlock()
if p.pd == nil {
return nil, status.Error(codes.FailedPrecondition, "plugin data not yet initialized")
}
return p.pd, nil
}
// setIamPolicy sets the IAM policy specified in the KeyPolicyFile to the given
// resource. If there is no KeyPolicyFile specified, a default policy is constructed
// and set to the resource.
func (p *Plugin) setIamPolicy(ctx context.Context, cryptoKeyName string) (err error) {
log := p.log.With(cryptoKeyNameTag, cryptoKeyName)
// Get the handle to be able to inspect and change the policy of the
// CryptoKey.
h := p.kmsClient.ResourceIAM(cryptoKeyName)
if h == nil {
return errors.New("could not get Cloud KMS Handle")
}
// We use V3 for policies.
h3 := h.V3()
if h3 == nil {
return errors.New("could not get Cloud KMS Handle3")
}
// Get the policy.
policy, err := h3.Policy(ctx)
if err != nil {
return fmt.Errorf("failed to retrieve IAM policy: %w", err)
}
// We expect the policy to be empty.
if len(policy.Bindings) > 0 {
// The policy is not empty, log the situation and do not replace it.
log.Warn("The CryptoKey already has a policy. No policy will be set.")
return nil
}
pd, err := p.getPluginData()
if err != nil {
return err
}
if pd.customPolicy != nil {
// There is a custom policy defined.
if err := h3.SetPolicy(ctx, pd.customPolicy); err != nil {
return fmt.Errorf("failed to set custom IAM policy: %w", err)
}
log.Debug("IAM policy updated to use custom policy")
return nil
}
// No custom policy defined. Build the default policy.
serviceAccount, err := p.getAuthenticatedServiceAccount()
if err != nil {
return status.Errorf(codes.Internal, "failed to get current identity: %v", err)
}
policy.Bindings = []*iampb.Binding{
{
Role: "roles/cloudkms.signerVerifier",
Members: []string{fmt.Sprintf("serviceAccount:%s", serviceAccount)},
},
}
if err := h3.SetPolicy(ctx, policy); err != nil {
return fmt.Errorf("failed to set default IAM policy: %w", err)
}
log.Debug("IAM policy updated to use default policy")
return nil
}
// setKeyEntry gets the entry from the cache that matches the provided
// SPIRE Key ID
func (p *Plugin) setKeyEntry(keyID string, ke keyEntry) {
p.entriesMtx.Lock()
defer p.entriesMtx.Unlock()
p.entries[keyID] = ke
}
// setPluginData sets the pluginData structure maintained by the plugin.
func (p *Plugin) setPluginData(pd *pluginData) {
p.pdMtx.Lock()
defer p.pdMtx.Unlock()
p.pd = pd
}
// keepActiveCryptoKeys keeps CryptoKeys managed by this plugin active updating
// the spire-last-update label with the current Unix time.
func (p *Plugin) keepActiveCryptoKeys(ctx context.Context) error {
p.log.Debug("Keeping CryptoKeys managed by this server active")
p.entriesMtx.Lock()
defer p.entriesMtx.Unlock()
var errs []string
for _, entry := range p.entries {
entry.cryptoKey.Labels[labelNameLastUpdate] = fmt.Sprint(p.hooks.clk.Now().Unix())
_, err := p.kmsClient.UpdateCryptoKey(ctx, &kmspb.UpdateCryptoKeyRequest{
UpdateMask: &fieldmaskpb.FieldMask{
Paths: []string{"labels"},
},
CryptoKey: entry.cryptoKey,
})
if err != nil {
p.log.Error("Failed to update CryptoKey", cryptoKeyNameTag, entry.cryptoKey.Name, reasonTag, err)
errs = append(errs, err.Error())
}
}
if errs != nil {
return fmt.Errorf(strings.Join(errs, "; "))
}
return nil
}
// keepActiveCryptoKeysTask updates the CryptoKeys in the cache every 6 hours,
// setting the spire-last-update label to the current (Unix) time.
// This is done to be able to detect CryptoKeys that are inactive (not in use
// by any server).
func (p *Plugin) keepActiveCryptoKeysTask(ctx context.Context) {
ticker := p.hooks.clk.Ticker(keepActiveCryptoKeysFrequency)
defer ticker.Stop()
p.notifyKeepActiveCryptoKeys(nil)
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
err := p.keepActiveCryptoKeys(ctx)
p.notifyKeepActiveCryptoKeys(err)
}
}
}
func (p *Plugin) notifyDestroy(err error) {
if p.hooks.scheduleDestroySignal != nil {
p.hooks.scheduleDestroySignal <- err
}
}
func (p *Plugin) notifyDisposeCryptoKeys(err error) {
if p.hooks.disposeCryptoKeysSignal != nil {
p.hooks.disposeCryptoKeysSignal <- err
}
}
func (p *Plugin) notifyEnqueueDestruction(err error) {
if p.hooks.enqueueDestructionSignal != nil {
p.hooks.enqueueDestructionSignal <- err
}
}
func (p *Plugin) notifySetInactive(err error) {
if p.hooks.setInactiveSignal != nil {
p.hooks.setInactiveSignal <- err
}
}
func (p *Plugin) notifyKeepActiveCryptoKeys(err error) {
if p.hooks.keepActiveCryptoKeysSignal != nil {
p.hooks.keepActiveCryptoKeysSignal <- err
}
}
// scheduleDestroyTask is a long running task that schedules the destruction
// of inactive CryptoKeyVersions and sets the corresponding CryptoKey as inactive.
func (p *Plugin) scheduleDestroyTask(ctx context.Context) {
backoffMin := 1 * time.Second
backoffMax := 60 * time.Second
backoff := backoffMin
for {
select {
case <-ctx.Done():
return
case cryptoKeyVersionName := <-p.scheduleDestroy:
log := p.log.With(cryptoKeyVersionNameTag, cryptoKeyVersionName)
destroyedCryptoKeyVersion, err := p.kmsClient.DestroyCryptoKeyVersion(ctx, &kmspb.DestroyCryptoKeyVersionRequest{
Name: cryptoKeyVersionName,
})
switch status.Code(err) {
case codes.NotFound:
// CryptoKeyVersion is not found, no CryptoKeyVersion to destroy
log.Warn("CryptoKeyVersion not found")
backoff = backoffMin
p.notifyDestroy(err)
continue
case codes.OK:
log.Debug("CryptoKeyVersion scheduled for destruction", scheduledDestroyTimeTag, destroyedCryptoKeyVersion.DestroyTime.AsTime())
backoff = backoffMin
p.notifyDestroy(nil)
continue
default:
log.Error("It was not possible to schedule CryptoKeyVersion for destruction", reasonTag, err)
// There was an error in the DestroyCryptoKeyVersion call.
// Try to get the CryptoKeyVersion to know the state of the
// CryptoKeyVersion and if we need to re-enqueue.
cryptoKeyVersion, err := p.kmsClient.GetCryptoKeyVersion(ctx, &kmspb.GetCryptoKeyVersionRequest{
Name: cryptoKeyVersionName,
})
switch status.Code(err) {
case codes.NotFound:
// Purely defensive. We don't really expect this situation,
// because this should have been captured during the
// DestroyCryptoKeyVersion call that was just performed.
log.Warn("CryptoKeyVersion not found")
backoff = backoffMin
p.notifyDestroy(err)
continue
case codes.OK:
if cryptoKeyVersion.State != kmspb.CryptoKeyVersion_ENABLED {
// Something external to the plugin modified the state
// of the CryptoKeyVersion. Do not try to schedule it for
// destruction.
log.Warn("CryptoKeyVersion is not enabled, will not be scheduled for destruction", cryptoKeyVersionStateTag, cryptoKeyVersion.State.String())
backoff = backoffMin
p.notifyDestroy(err)
continue
}
default:
// The GetCryptoKeyVersion call failed. Log this and re-enqueue
// the CryptoKey for destruction. Hopefully, this is a
// recoverable error.
log.Error("Could not get the CryptoKeyVersion while trying to schedule it for destruction", reasonTag, err)
}
select {
case p.scheduleDestroy <- cryptoKeyVersionName:
log.Debug("CryptoKeyVersion re-enqueued for destruction")
default:
log.Error("Failed to re-enqueue CryptoKeyVersion for destruction")
}
}
p.notifyDestroy(err)
backoff = min(backoff*2, backoffMax)
p.hooks.clk.Sleep(backoff)
}
}
}
// setInactive updates the spire-active label in the specified CryptoKey to
// indicate that is inactive.
func (p *Plugin) setInactive(ctx context.Context, cryptoKey *kmspb.CryptoKey) {
log := p.log.With(cryptoKeyNameTag, cryptoKey.Name)
cryptoKey.Labels[labelNameActive] = "false"
_, err := p.kmsClient.UpdateCryptoKey(ctx, &kmspb.UpdateCryptoKeyRequest{
UpdateMask: &fieldmaskpb.FieldMask{
Paths: []string{"labels"},
},
CryptoKey: cryptoKey,
})
if err != nil {
log.Error("Could not update CryptoKey as incactive", reasonTag, err)
}
log.Debug("CryptoKey updated as inactive", cryptoKeyNameTag, cryptoKey.Name)
p.notifySetInactive(err)
}
// setCache sets the cached entries with the provided entries.
func (p *Plugin) setCache(keyEntries []*keyEntry) {
p.entriesMtx.Lock()
defer p.entriesMtx.Unlock()
p.entries = make(map[string]keyEntry)
for _, e := range keyEntries {
p.entries[e.publicKey.Id] = *e
p.log.Debug("Cloud KMS key loaded", cryptoKeyVersionNameTag, e.cryptoKeyVersionName, algorithmTag, e.cryptoKey.VersionTemplate.Algorithm)
}
}
// createServerID creates a randomly generated UUID to be used as a server ID
// and stores it in the specified idPath.
func createServerID(idPath string) (string, error) {
id, err := generateUniqueID()
if err != nil {
return "", status.Errorf(codes.Internal, "failed to generate ID for server: %v", err)
}
err = diskutil.WritePrivateFile(idPath, []byte(id))
if err != nil {
return "", status.Errorf(codes.Internal, "failed to persist server ID on path: %v", err)
}
return id, nil
}
// cryptoKeyVersionAlgorithmFromKeyType gets the corresponding algorithm of the
// CryptoKeyVersion from the provided key type.
// The returned CryptoKeyVersion_CryptoKeyVersionAlgorithm indicates the
// parameters that must be used for signing.
func cryptoKeyVersionAlgorithmFromKeyType(keyType keymanagerv1.KeyType) (kmspb.CryptoKeyVersion_CryptoKeyVersionAlgorithm, error) {
// CryptoKeyVersion_CryptoKeyVersionAlgorithm specifies the padding algorithm
// and the digest algorithm for RSA signatures. The key type in the Key
// Manager interface does not contain the information about these parameters
// for signing. Currently, there is no way in SPIRE to specify custom
// parameters when signing through the ca.ServerCA interface and
// x509.CreateCertificate defaults to RSASSA-PKCS-v1_5 as the padding
// algorithm and a SHA256 digest. Therefore, for RSA signing keys we
// choose the corresponding CryptoKeyVersion_CryptoKeyVersionAlgorithm using
// RSASSA-PKCS-v1_5 for padding and a SHA256 digest.
switch {
case keyType == keymanagerv1.KeyType_EC_P256:
return kmspb.CryptoKeyVersion_EC_SIGN_P256_SHA256, nil
case keyType == keymanagerv1.KeyType_EC_P384:
return kmspb.CryptoKeyVersion_EC_SIGN_P384_SHA384, nil
case keyType == keymanagerv1.KeyType_RSA_2048:
return kmspb.CryptoKeyVersion_RSA_SIGN_PKCS1_2048_SHA256, nil
case keyType == keymanagerv1.KeyType_RSA_4096:
return kmspb.CryptoKeyVersion_RSA_SIGN_PKCS1_4096_SHA256, nil
default:
return kmspb.CryptoKeyVersion_CRYPTO_KEY_VERSION_ALGORITHM_UNSPECIFIED, fmt.Errorf("unsupported key type %q", keyType)
}
}
// generateCryptoKeyID returns a new identifier to be used as a CryptoKeyID.
// The returned identifier has the form: spire-key-<UUID>-<SPIRE-KEY-ID>,
// where UUID is a new randomly generated UUID and SPIRE-KEY-ID is provided
// through the spireKeyID parameter.