Skip to content

Commit 3b0fe01

Browse files
committed
Add support for disk encryption key in GCPMachine
Add support for the disk encryption key for the boot disk and and additional disks.
1 parent 9788374 commit 3b0fe01

File tree

8 files changed

+741
-5
lines changed

8 files changed

+741
-5
lines changed

api/v1beta1/gcpmachine_types.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ type AttachedDiskSpec struct {
5454
// Defaults to 30GB. For "local-ssd" size is always 375GB.
5555
// +optional
5656
Size *int64 `json:"size,omitempty"`
57+
// EncryptionKey defines the KMS key to be used to encrypt the disk.
58+
// +optional
59+
EncryptionKey *CustomerEncryptionKey `json:"encryptionKey,omitempty"`
5760
}
5861

5962
// IPForwarding represents the IP forwarding configuration for the GCP machine.
@@ -146,6 +149,65 @@ const (
146149
HostMaintenancePolicyTerminate HostMaintenancePolicy = "Terminate"
147150
)
148151

152+
// KeyType is a type for disk encryption.
153+
type KeyType string
154+
155+
const (
156+
// CustomerManagedKey (CMEK) references an encryption key stored in Google Cloud KMS.
157+
CustomerManagedKey KeyType = "Managed"
158+
// CustomerSuppliedKey (CSEK) specifies an encryption key to use.
159+
CustomerSuppliedKey KeyType = "Supplied"
160+
)
161+
162+
// ManagedKey is a reference to a key managed by the Cloud Key Management Service.
163+
type ManagedKey struct {
164+
// KMSKeyName is the name of the encryption key that is stored in Google Cloud KMS. For example:
165+
// "kmsKeyName": "projects/kms_project_id/locations/region/keyRings/key_region/cryptoKeys/key
166+
// +kubebuilder:validation:Required
167+
KMSKeyName string `json:"kmsKeyName,omitempty"`
168+
}
169+
170+
// SuppliedKey contains a key for disk encryption.
171+
// +kubebuilder:validation:MinProperties=1
172+
type SuppliedKey struct {
173+
// RawKey specifies a 256-bit customer-supplied encryption key, encoded in RFC 4648
174+
// base64 to either encrypt or decrypt this resource. You can provide either the rawKey or the rsaEncryptedKey.
175+
// For example: "rawKey": "SGVsbG8gZnJvbSBHb29nbGUgQ2xvdWQgUGxhdGZvcm0="
176+
// +optional
177+
RawKey []byte `json:"rawKey,omitempty"`
178+
// RSAEncryptedKey specifies an RFC 4648 base64 encoded, RSA-wrapped 2048-bit customer-supplied encryption
179+
// key to either encrypt or decrypt this resource. You can provide either the rawKey or the
180+
// rsaEncryptedKey.
181+
// For example: "rsaEncryptedKey": "ieCx/NcW06PcT7Ep1X6LUTc/hLvUDYyzSZPPVCVPTVEohpeHASqC8uw5TzyO9U+Fka9JFHi
182+
// z0mBibXUInrC/jEk014kCK/NPjYgEMOyssZ4ZINPKxlUh2zn1bV+MCaTICrdmuSBTWlUUiFoDi
183+
// D6PYznLwh8ZNdaheCeZ8ewEXgFQ8V+sDroLaN3Xs3MDTXQEMMoNUXMCZEIpg9Vtp9x2oe=="
184+
// The key must meet the following requirements before you can provide it to Compute Engine:
185+
// 1. The key is wrapped using a RSA public key certificate provided by Google.
186+
// 2. After being wrapped, the key must be encoded in RFC 4648 base64 encoding.
187+
// Gets the RSA public key certificate provided by Google at: https://cloud-certs.storage.googleapis.com/google-cloud-csek-ingress.pem
188+
// +optional
189+
RSAEncryptedKey []byte `json:"rsaEncryptedKey,omitempty"`
190+
}
191+
192+
// CustomerEncryptionKey supports both Customer-Managed or Customer-Supplied encryption keys .
193+
type CustomerEncryptionKey struct {
194+
// KeyType is the type of encryption key. Must be either Managed, aka Customer-Managed Encryption Key (CMEK) or
195+
// Supplied, aka Customer-Supplied EncryptionKey (CSEK).
196+
// +kubebuilder:validation:Enum=Managed;Supplied
197+
KeyType KeyType `json:"keyType"`
198+
// KMSKeyServiceAccount is the service account being used for the encryption request for the given KMS key.
199+
// If absent, the Compute Engine default service account is used. For example:
200+
// "kmsKeyServiceAccount": "name@project_id.iam.gserviceaccount.com/
201+
// +optional
202+
KMSKeyServiceAccount *string `json:"kmsKeyServiceAccount,omitempty"`
203+
// ManagedKey references keys managed by the Cloud Key Management Service. This should be set when KeyType is Managed.
204+
// +optional
205+
ManagedKey *ManagedKey `json:"managedKey,omitempty"`
206+
// SuppliedKey provides the key used to create or manage a disk. This should be set when KeyType is Managed.
207+
// +optional
208+
SuppliedKey *SuppliedKey `json:"suppliedKey,omitempty"`
209+
}
210+
149211
// GCPMachineSpec defines the desired state of GCPMachine.
150212
type GCPMachineSpec struct {
151213
// InstanceType is the type of instance to create. Example: n1.standard-2
@@ -252,6 +314,10 @@ type GCPMachineSpec struct {
252314
// +kubebuilder:validation:Enum=Enabled;Disabled
253315
// +optional
254316
ConfidentialCompute *ConfidentialComputePolicy `json:"confidentialCompute,omitempty"`
317+
318+
// RootDiskEncryptionKey defines the KMS key to be used to encrypt the root disk.
319+
// +optional
320+
RootDiskEncryptionKey *CustomerEncryptionKey `json:"rootDiskEncryptionKey,omitempty"`
255321
}
256322

257323
// MetadataItem defines a single piece of metadata associated with an instance.

api/v1beta1/gcpmachine_webhook.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ var _ webhook.Validator = &GCPMachine{}
5050
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
5151
func (m *GCPMachine) ValidateCreate() (admission.Warnings, error) {
5252
clusterlog.Info("validate create", "name", m.Name)
53-
return nil, validateConfidentialCompute(m.Spec)
53+
54+
if err := validateConfidentialCompute(m.Spec); err != nil {
55+
return nil, err
56+
}
57+
return nil, validateCustomerEncryptionKey(m.Spec)
5458
}
5559

5660
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
@@ -117,3 +121,36 @@ func validateConfidentialCompute(spec GCPMachineSpec) error {
117121
}
118122
return nil
119123
}
124+
125+
func checkKeyType(key *CustomerEncryptionKey) error {
126+
switch key.KeyType {
127+
case CustomerManagedKey:
128+
if key.ManagedKey == nil || key.SuppliedKey != nil {
129+
return fmt.Errorf("CustomerEncryptionKey KeyType of Managed requires only ManagedKey to be set")
130+
}
131+
case CustomerSuppliedKey:
132+
if key.SuppliedKey == nil || key.ManagedKey != nil {
133+
return fmt.Errorf("CustomerEncryptionKey KeyType of Supplied requires only SuppliedKey to be set")
134+
}
135+
default:
136+
return fmt.Errorf("invalid value for CustomerEncryptionKey KeyType %s", key.KeyType)
137+
}
138+
return nil
139+
}
140+
141+
func validateCustomerEncryptionKey(spec GCPMachineSpec) error {
142+
if spec.RootDiskEncryptionKey != nil {
143+
if err := checkKeyType(spec.RootDiskEncryptionKey); err != nil {
144+
return err
145+
}
146+
}
147+
148+
for _, disk := range spec.AdditionalDisks {
149+
if disk.EncryptionKey != nil {
150+
if err := checkKeyType(disk.EncryptionKey); err != nil {
151+
return err
152+
}
153+
}
154+
}
155+
return nil
156+
}

api/v1beta1/gcpmachine_webhook_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,43 @@ func TestGCPMachine_ValidateCreate(t *testing.T) {
8585
},
8686
wantErr: true,
8787
},
88+
{
89+
name: "GCPMachine with RootDiskEncryptionKey KeyType Managed and Managed field not set",
90+
GCPMachine: &GCPMachine{
91+
Spec: GCPMachineSpec{
92+
RootDiskEncryptionKey: &CustomerEncryptionKey{
93+
KeyType: CustomerManagedKey,
94+
},
95+
},
96+
},
97+
wantErr: true,
98+
},
99+
{
100+
name: "GCPMachine with RootDiskEncryptionKey KeyType Supplied and Supplied field not set",
101+
GCPMachine: &GCPMachine{
102+
Spec: GCPMachineSpec{
103+
RootDiskEncryptionKey: &CustomerEncryptionKey{
104+
KeyType: CustomerSuppliedKey,
105+
},
106+
},
107+
},
108+
wantErr: true,
109+
},
110+
{
111+
name: "GCPMachine with AdditionalDisk Encryption KeyType Managed and Managed field not set",
112+
GCPMachine: &GCPMachine{
113+
Spec: GCPMachineSpec{
114+
AdditionalDisks: []AttachedDiskSpec{
115+
{
116+
EncryptionKey: &CustomerEncryptionKey{
117+
KeyType: CustomerManagedKey,
118+
},
119+
},
120+
},
121+
},
122+
},
123+
wantErr: true,
124+
},
88125
}
89126
for _, test := range tests {
90127
test := test

api/v1beta1/zz_generated.deepcopy.go

Lines changed: 80 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cloud/scope/machine.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ func (m *MachineScope) InstanceImageSpec() *compute.AttachedDisk {
235235
diskType = *t
236236
}
237237

238-
return &compute.AttachedDisk{
238+
disk := &compute.AttachedDisk{
239239
AutoDelete: true,
240240
Boot: true,
241241
InitializeParams: &compute.AttachedDiskInitializeParams{
@@ -245,6 +245,27 @@ func (m *MachineScope) InstanceImageSpec() *compute.AttachedDisk {
245245
SourceImage: sourceImage,
246246
},
247247
}
248+
249+
if m.GCPMachine.Spec.RootDiskEncryptionKey != nil {
250+
if m.GCPMachine.Spec.RootDiskEncryptionKey.KeyType == infrav1.CustomerManagedKey && m.GCPMachine.Spec.RootDiskEncryptionKey.ManagedKey != nil {
251+
disk.DiskEncryptionKey = &compute.CustomerEncryptionKey{
252+
KmsKeyName: m.GCPMachine.Spec.RootDiskEncryptionKey.ManagedKey.KMSKeyName,
253+
}
254+
if m.GCPMachine.Spec.RootDiskEncryptionKey.KMSKeyServiceAccount != nil {
255+
disk.DiskEncryptionKey.KmsKeyServiceAccount = *m.GCPMachine.Spec.RootDiskEncryptionKey.KMSKeyServiceAccount
256+
}
257+
} else if m.GCPMachine.Spec.RootDiskEncryptionKey.KeyType == infrav1.CustomerSuppliedKey && m.GCPMachine.Spec.RootDiskEncryptionKey.SuppliedKey != nil {
258+
disk.DiskEncryptionKey = &compute.CustomerEncryptionKey{
259+
RawKey: string(m.GCPMachine.Spec.RootDiskEncryptionKey.SuppliedKey.RawKey),
260+
RsaEncryptedKey: string(m.GCPMachine.Spec.RootDiskEncryptionKey.SuppliedKey.RSAEncryptedKey),
261+
}
262+
if m.GCPMachine.Spec.RootDiskEncryptionKey.KMSKeyServiceAccount != nil {
263+
disk.DiskEncryptionKey.KmsKeyServiceAccount = *m.GCPMachine.Spec.RootDiskEncryptionKey.KMSKeyServiceAccount
264+
}
265+
}
266+
}
267+
268+
return disk
248269
}
249270

250271
// InstanceAdditionalDiskSpec returns compute instance additional attched-disk spec.
@@ -269,6 +290,25 @@ func (m *MachineScope) InstanceAdditionalDiskSpec() []*compute.AttachedDisk {
269290
// https://cloud.google.com/compute/docs/disks/local-ssd#choose_an_interface
270291
additionalDisk.Interface = "NVME"
271292
}
293+
if disk.EncryptionKey != nil {
294+
if m.GCPMachine.Spec.RootDiskEncryptionKey.KeyType == infrav1.CustomerManagedKey && m.GCPMachine.Spec.RootDiskEncryptionKey.ManagedKey != nil {
295+
additionalDisk.DiskEncryptionKey = &compute.CustomerEncryptionKey{
296+
KmsKeyName: m.GCPMachine.Spec.RootDiskEncryptionKey.ManagedKey.KMSKeyName,
297+
}
298+
if m.GCPMachine.Spec.RootDiskEncryptionKey.KMSKeyServiceAccount != nil {
299+
additionalDisk.DiskEncryptionKey.KmsKeyServiceAccount = *m.GCPMachine.Spec.RootDiskEncryptionKey.KMSKeyServiceAccount
300+
}
301+
} else if m.GCPMachine.Spec.RootDiskEncryptionKey.KeyType == infrav1.CustomerSuppliedKey && m.GCPMachine.Spec.RootDiskEncryptionKey.SuppliedKey != nil {
302+
additionalDisk.DiskEncryptionKey = &compute.CustomerEncryptionKey{
303+
RawKey: string(m.GCPMachine.Spec.RootDiskEncryptionKey.SuppliedKey.RawKey),
304+
RsaEncryptedKey: string(m.GCPMachine.Spec.RootDiskEncryptionKey.SuppliedKey.RSAEncryptedKey),
305+
}
306+
if m.GCPMachine.Spec.RootDiskEncryptionKey.KMSKeyServiceAccount != nil {
307+
additionalDisk.DiskEncryptionKey.KmsKeyServiceAccount = *m.GCPMachine.Spec.RootDiskEncryptionKey.KMSKeyServiceAccount
308+
}
309+
}
310+
}
311+
272312
additionalDisks = append(additionalDisks, additionalDisk)
273313
}
274314

0 commit comments

Comments
 (0)