|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +package volumes |
| 6 | + |
| 7 | +import ( |
| 8 | + "encoding/json" |
| 9 | + "fmt" |
| 10 | + |
| 11 | + "github.com/siderolabs/talos/pkg/machinery/config/config" |
| 12 | + blocktype "github.com/siderolabs/talos/pkg/machinery/config/types/block" |
| 13 | + "github.com/siderolabs/talos/pkg/machinery/config/types/v1alpha1" |
| 14 | + "github.com/siderolabs/talos/pkg/machinery/resources/block" |
| 15 | +) |
| 16 | + |
| 17 | +// MarshalEncryptionMeta is a function to persist encryption config to the META value. |
| 18 | +func MarshalEncryptionMeta(cfg config.EncryptionConfig) ([]byte, error) { |
| 19 | + return json.Marshal(cfg) |
| 20 | +} |
| 21 | + |
| 22 | +// UnmarshalEncryptionMeta is a function to load encryption config from the META value. |
| 23 | +func UnmarshalEncryptionMeta(data []byte) (config.EncryptionConfig, error) { |
| 24 | + var encryptionFromMeta blocktype.EncryptionSpec |
| 25 | + |
| 26 | + if err := json.Unmarshal(data, &encryptionFromMeta); err != nil { |
| 27 | + var legacyEncryption v1alpha1.EncryptionConfig |
| 28 | + |
| 29 | + if legacyErr := json.Unmarshal(data, &legacyEncryption); legacyErr != nil { |
| 30 | + return nil, fmt.Errorf("error unmarshalling state encryption meta key: %w", err) |
| 31 | + } |
| 32 | + |
| 33 | + return &legacyEncryption, nil |
| 34 | + } |
| 35 | + |
| 36 | + return &encryptionFromMeta, nil |
| 37 | +} |
| 38 | + |
| 39 | +// ConvertEncryptionConfiguration converts a `config.EncryptionConfig` into a |
| 40 | +// `block.EncryptionSpec`, and writes it into `out`. |
| 41 | +func ConvertEncryptionConfiguration(in config.EncryptionConfig, out *block.VolumeConfigSpec) error { |
| 42 | + if in == nil { |
| 43 | + out.Encryption = block.EncryptionSpec{} |
| 44 | + |
| 45 | + return nil |
| 46 | + } |
| 47 | + |
| 48 | + out.Encryption.Provider = in.Provider() |
| 49 | + out.Encryption.Cipher = in.Cipher() |
| 50 | + out.Encryption.KeySize = in.KeySize() |
| 51 | + out.Encryption.BlockSize = in.BlockSize() |
| 52 | + out.Encryption.PerfOptions = in.Options() |
| 53 | + |
| 54 | + out.Encryption.Keys = make([]block.EncryptionKey, len(in.Keys())) |
| 55 | + |
| 56 | + for i, key := range in.Keys() { |
| 57 | + out.Encryption.Keys[i].Slot = key.Slot() |
| 58 | + out.Encryption.Keys[i].LockToSTATE = key.LockToSTATE() |
| 59 | + |
| 60 | + switch { |
| 61 | + case key.Static() != nil: |
| 62 | + out.Encryption.Keys[i].Type = block.EncryptionKeyStatic |
| 63 | + out.Encryption.Keys[i].StaticPassphrase = key.Static().Key() |
| 64 | + case key.NodeID() != nil: |
| 65 | + out.Encryption.Keys[i].Type = block.EncryptionKeyNodeID |
| 66 | + case key.KMS() != nil: |
| 67 | + out.Encryption.Keys[i].Type = block.EncryptionKeyKMS |
| 68 | + out.Encryption.Keys[i].KMSEndpoint = key.KMS().Endpoint() |
| 69 | + case key.TPM() != nil: |
| 70 | + out.Encryption.Keys[i].Type = block.EncryptionKeyTPM |
| 71 | + out.Encryption.Keys[i].TPMCheckSecurebootStatusOnEnroll = key.TPM().CheckSecurebootOnEnroll() |
| 72 | + out.Encryption.Keys[i].TPMPCRs = key.TPM().PCRs() |
| 73 | + out.Encryption.Keys[i].TPMPubKeyPCRs = key.TPM().PubKeyPCRs() |
| 74 | + default: |
| 75 | + return fmt.Errorf("unsupported encryption key type: slot %d", key.Slot()) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return nil |
| 80 | +} |
0 commit comments