-
Notifications
You must be signed in to change notification settings - Fork 2
/
decrypt.go
134 lines (114 loc) · 3.05 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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"io/fs"
"os"
"os/exec"
"path/filepath"
"strings"
"filippo.io/age"
"filippo.io/age/armor"
)
const (
strongboxIdentityFilename = ".strongbox_identity"
strongboxKeyringFilename = ".strongbox_keyring"
)
var (
encryptedFilePrefix = []byte("# STRONGBOX ENCRYPTED RESOURCE")
errEncryptedFileFound = errors.New("encrypted file found")
)
func ensureDecryption(ctx context.Context, cwd string, app applicationInfo) error {
keyringData, identityData, err := secretData(ctx, app.destinationNamespace, app.keyringSecret)
if err != nil {
if errors.Is(err, errNotFound) {
return nil
}
return err
}
if keyringData == nil && identityData == nil {
return nil
}
// create Strongbox keyRing file
if keyringData != nil {
keyRingPath := filepath.Join(cwd, strongboxKeyringFilename)
if err := os.WriteFile(keyRingPath, keyringData, 0644); err != nil {
return err
}
if err := runStrongboxDecryption(ctx, cwd, keyRingPath); err != nil {
return fmt.Errorf("unable to decrypt err:%s", err)
}
}
if identityData != nil {
identityPath := filepath.Join(cwd, strongboxIdentityFilename)
if err := os.WriteFile(identityPath, identityData, 0644); err != nil {
return err
}
if err := strongboxAgeRecursiveDecrypt(ctx, cwd, identityData); err != nil {
return fmt.Errorf("unable to decrypt err:%s", err)
}
}
return nil
}
func secretData(ctx context.Context, destinationNamespace string, si secretInfo) ([]byte, []byte, error) {
secret, err := secret(ctx, destinationNamespace, si)
if err != nil {
return nil, nil, err
}
return secret.Data[strongboxKeyringFilename], secret.Data[strongboxIdentityFilename], nil
}
// runStrongboxDecryption will try to decrypt files in cwd using given keyRing file
func runStrongboxDecryption(ctx context.Context, cwd, keyringPath string) error {
s := exec.CommandContext(ctx, "strongbox", "-keyring", keyringPath, "-decrypt", "-recursive", cwd)
stderr, err := s.CombinedOutput()
if err != nil {
return fmt.Errorf("error running strongbox err:%s ", stderr)
}
return nil
}
func strongboxAgeRecursiveDecrypt(ctx context.Context, cwd string, identityData []byte) error {
identities, err := age.ParseIdentities(bytes.NewBuffer(identityData))
if err != nil {
return err
}
return filepath.Walk(cwd, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
// skip .git directory
if info.Name() == ".git" {
return fs.SkipDir
}
return nil
}
file, err := os.OpenFile(path, os.O_RDWR, 0644)
if err != nil {
return err
}
defer file.Close()
in, err := io.ReadAll(file)
if err != nil {
return err
}
if !strings.HasPrefix(string(in), armor.Header) {
return nil
}
armorReader := armor.NewReader(bytes.NewReader(in))
ar, err := age.Decrypt(armorReader, identities...)
if err != nil {
return err
}
if _, err := file.Seek(0, io.SeekStart); err != nil {
return err
}
n, err := io.Copy(file, ar)
if err != nil {
return err
}
return file.Truncate(n)
})
}