-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
decrypt.go
54 lines (44 loc) · 1.12 KB
/
decrypt.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
package decrypt
import (
"fmt"
"log/slog"
"os"
"github.com/getsops/sops/v3/decrypt"
"sigs.k8s.io/yaml"
)
type sopsFile struct {
Sops map[string]interface{} `yaml:"sops"`
}
// DecryptYamlWithSops reads a `sops` encrypted `yaml` file path
// and decrypt the content using `sops/v3/decrypt.Data`.
// The unencrypted data will be returned bytes.
// Data will be returned as it is if file is not encrypted with
// `sops`. Error will be returned when decryption fails.
func DecryptYamlWithSops(filePath string) ([]byte, error) {
data, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
// return the data immediately if the file is empty
if len(data) == 0 {
return data, nil
}
var m *sopsFile
err = yaml.Unmarshal(data, &m)
if err != nil {
return nil, err
}
if m.isEncrypted() {
slog.Debug(fmt.Sprintf("%s is SOPS encrypted, decrypting", filePath))
decrypted, err := decrypt.Data(data, "yaml")
if err != nil {
return nil, err
}
return decrypted, nil
}
return data, nil
}
// isEncrypted returns true if `sops` key exists.
func (s *sopsFile) isEncrypted() bool {
return len(s.Sops) != 0
}