-
Notifications
You must be signed in to change notification settings - Fork 0
/
sops.go
90 lines (70 loc) · 1.79 KB
/
sops.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
Copyright © 2024 Patrick Hermann [email protected]
*/
package cli
import (
"fmt"
"filippo.io/age"
"github.com/getsops/sops/v3"
"github.com/getsops/sops/v3/aes"
keysource "github.com/getsops/sops/v3/age"
"github.com/getsops/sops/v3/cmd/sops/common"
"github.com/getsops/sops/v3/decrypt"
"github.com/getsops/sops/v3/keys"
"github.com/getsops/sops/v3/keyservice"
"github.com/getsops/sops/v3/stores/yaml"
)
var sopsVersion = "3.8.1"
var unencryptedSuffix = "_unencrypted"
func EncryptStore(ageKey, rawData string) (encryptedData string) {
store := yaml.Store{}
branches, err := store.LoadPlainFile([]byte(rawData))
if err != nil {
panic(err)
}
masterKey, err := keysource.MasterKeyFromRecipient(ageKey)
if err != nil {
panic(err)
}
tree := sops.Tree{
Branches: branches,
Metadata: sops.Metadata{
KeyGroups: []sops.KeyGroup{
[]keys.MasterKey{masterKey},
},
UnencryptedSuffix: unencryptedSuffix,
Version: sopsVersion,
},
}
dataKey, errs := tree.GenerateDataKeyWithKeyServices(
[]keyservice.KeyServiceClient{keyservice.NewLocalClient()},
)
if errs != nil {
panic(errs)
}
common.EncryptTree(common.EncryptTreeOpts{
DataKey: dataKey,
Tree: &tree,
Cipher: aes.NewCipher(),
})
result, err := store.EmitEncryptedFile(tree)
if err != nil {
panic(err)
}
encryptedData = string(result)
return encryptedData
}
func GenerateAgeIdentitdy() (identity *age.X25519Identity) {
identity, err := age.GenerateX25519Identity()
if err != nil {
panic(err)
}
return
}
func DecryptSopsFile(encryptedSopsFilePath, fileExtension string) (err error, decryptedSopsFile string) {
plain, err := decrypt.File(encryptedSopsFilePath, fileExtension)
if err != nil {
fmt.Println(fmt.Errorf("FAILED TO DECRYPT: %w", err))
}
return err, string(plain)
}