forked from opentofu/opentofu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encryption.go
44 lines (37 loc) · 1.36 KB
/
encryption.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
package flow
import (
"github.com/opentofu/opentofu/internal/states/statecrypto/cryptoconfig"
"log"
)
func Encrypt(stateJson []byte, enabledCondition func(config cryptoconfig.Config) bool) ([]byte, error) {
primary, _, err := Configurations()
if err != nil {
log.Printf("[ERROR] failed to encrypt state because configuration was invalid, bailing out")
log.Printf("[TRACE] state encryption configuration error was: %s", err)
return stateJson, err
}
return encryptWithConfig(stateJson, primary, enabledCondition(primary))
}
func encryptWithConfig(stateJson []byte, config cryptoconfig.Config, enabled bool) ([]byte, error) {
if enabled {
candidate, err := attemptEncryption(stateJson, config)
if err != nil {
log.Printf("[TRACE] state encryption error was: %s", err)
log.Printf("[ERROR] failed to encrypt state, bailing out")
return []byte{}, err
}
log.Printf("[TRACE] successfully encrypted state, input %d bytes, output %d bytes", len(stateJson), len(candidate))
return candidate, nil
} else {
log.Printf("[TRACE] no encryption configured, passing through state unchanged")
return stateJson, nil
}
}
func attemptEncryption(stateJson []byte, config cryptoconfig.Config) ([]byte, error) {
stack, err := buildMethodStack(config)
if err != nil {
return stateJson, err
}
stateJson, _, err = stack.Encrypt(stateJson, config)
return stateJson, err
}