Skip to content

Commit 859194e

Browse files
committed
chore: extract system+user volume config transformers, test
Move `internal/app/machined/pkg/controllers/block/` `system_volumes.go` + `user_volumes.go` (and extras) to `internal/app/machined/pkg/controllers/block/internal`. Adds plenty of unit tests. Signed-off-by: Laura Brehm <laurabrehm@hey.com>
1 parent 308c6bc commit 859194e

File tree

13 files changed

+2012
-656
lines changed

13 files changed

+2012
-656
lines changed

internal/app/machined/pkg/controllers/block/encryption_meta.go

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
}

internal/app/machined/pkg/controllers/block/encryption_meta_test.go renamed to internal/app/machined/pkg/controllers/block/internal/volumes/encryption_meta_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
// License, v. 2.0. If a copy of the MPL was not distributed with this
33
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
44

5-
package block_test
5+
package volumes_test
66

77
import (
88
"testing"
99

1010
"github.com/stretchr/testify/assert"
1111
"github.com/stretchr/testify/require"
1212

13-
"github.com/siderolabs/talos/internal/app/machined/pkg/controllers/block"
13+
"github.com/siderolabs/talos/internal/app/machined/pkg/controllers/block/internal/volumes"
1414
"github.com/siderolabs/talos/pkg/machinery/config/config"
1515
blockcfg "github.com/siderolabs/talos/pkg/machinery/config/types/block"
1616
"github.com/siderolabs/talos/pkg/machinery/config/types/v1alpha1"
@@ -83,12 +83,12 @@ const (
8383
func TestMarshalEncryptionMeta(t *testing.T) {
8484
t.Parallel()
8585

86-
data, err := block.MarshalEncryptionMeta(legacyEncryptionConfig())
86+
data, err := volumes.MarshalEncryptionMeta(legacyEncryptionConfig())
8787
require.NoError(t, err)
8888

8989
assert.Equal(t, legacyMarshalled, string(data))
9090

91-
data, err = block.MarshalEncryptionMeta(modernEncryptionConfig())
91+
data, err = volumes.MarshalEncryptionMeta(modernEncryptionConfig())
9292
require.NoError(t, err)
9393

9494
assert.Equal(t, modernMarshalled, string(data))
@@ -97,12 +97,12 @@ func TestMarshalEncryptionMeta(t *testing.T) {
9797
func TestUnmarshalEncryptionMeta(t *testing.T) {
9898
t.Parallel()
9999

100-
cfg, err := block.UnmarshalEncryptionMeta([]byte(legacyMarshalled))
100+
cfg, err := volumes.UnmarshalEncryptionMeta([]byte(legacyMarshalled))
101101
require.NoError(t, err)
102102

103103
assertEqualEncryptionConfigs(t, cfg, legacyEncryptionConfig())
104104

105-
cfg, err = block.UnmarshalEncryptionMeta([]byte(modernMarshalled))
105+
cfg, err = volumes.UnmarshalEncryptionMeta([]byte(modernMarshalled))
106106
require.NoError(t, err)
107107

108108
assertEqualEncryptionConfigs(t, cfg, modernEncryptionConfig())

0 commit comments

Comments
 (0)