-
Notifications
You must be signed in to change notification settings - Fork 0
/
vault.go
219 lines (183 loc) · 4.59 KB
/
vault.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// vault.go
package vault
import (
"errors"
"fmt"
"os"
"strings"
"time"
"github.com/a13labs/sectool/internal/crypto"
)
// Vault represents a secure key-value store.
type Vault struct {
filePath string
key []byte
backup bool
}
// NewVault creates a new Vault instance.
func NewVault(filePath string, key []byte) *Vault {
return &Vault{
filePath: filePath,
key: key,
backup: false,
}
}
// readVault reads the vault file contents.
func (v *Vault) readVault() (string, error) {
if !v.vaultFileExists() {
f, err := os.Create(v.filePath)
if err != nil {
return "", errors.New("vault file does not exist")
}
f.Close()
}
decryptedContents, err := crypto.DecryptFromFile(v.filePath, v.key)
if err != nil {
return "", err
}
return decryptedContents, nil
}
// writeVault writes encrypted data to the vault file.
func (v *Vault) writeVault(contents string) error {
if v.backup {
// Create a backup of the existing vault
backupName := v.vaultBackupName()
err := v.backupVault(backupName)
if err != nil {
return err
}
}
// Encrypt the data and write to the vault file
err := crypto.EncryptToFile(contents, v.filePath, v.key, false)
if err != nil {
return err
}
return nil
}
// backupVault creates a backup of the vault file.
func (v *Vault) backupVault(backupName string) error {
contents, err := os.ReadFile(v.filePath)
if err != nil {
return err
}
err = os.WriteFile(backupName, contents, 0600)
if err != nil {
return err
}
return nil
}
// vaultBackupName generates a backup filename with a timestamp.
func (v *Vault) vaultBackupName() string {
timestamp := time.Now().Format("20060102150405")
return fmt.Sprintf("%s_%s", v.filePath, timestamp)
}
// vaultFileExists checks if the vault file exists.
func (v *Vault) vaultFileExists() bool {
_, err := os.Stat(v.filePath)
return !os.IsNotExist(err)
}
// Initialize creates a new vault file if it doesn't exist.
func (v *Vault) Initialize() error {
if v.vaultFileExists() {
return nil
}
// Create an empty vault file
err := os.WriteFile(v.filePath, []byte(""), 0600)
if err != nil {
return err
}
return nil
}
// VaultHasKey checks if the vault contains the specified key.
func (v *Vault) VaultHasKey(key string) bool {
contents, err := v.readVault()
if err != nil {
return false
}
lines := strings.Split(contents, "\n")
for _, line := range lines {
parts := strings.SplitN(line, "=", 2)
if len(parts) == 2 && parts[0] == key {
return true
}
}
return false
}
// VaultGetValue returns the value of a key from the vault.
func (v *Vault) VaultGetValue(key string) (string, error) {
contents, err := v.readVault()
if err != nil {
return "", err
}
lines := strings.Split(contents, "\n")
for _, line := range lines {
parts := strings.SplitN(line, "=", 2)
if len(parts) == 2 && parts[0] == key {
return parts[1], nil
}
}
return "", errors.New("key not found in vault")
}
// VaultListKeys lists all keys in the vault.
func (v *Vault) VaultListKeys() []string {
contents, err := v.readVault()
if err != nil {
return []string{}
}
var keys []string
lines := strings.Split(contents, "\n")
for _, line := range lines {
parts := strings.SplitN(line, "=", 2)
if len(parts) == 2 {
keys = append(keys, parts[0])
}
}
return keys
}
// VaultSetValue sets the value of a key in the vault.
func (v *Vault) VaultSetValue(key, value string) error {
contents, err := v.readVault()
if err != nil {
return err
}
lines := strings.Split(contents, "\n")
for i, line := range lines {
parts := strings.SplitN(line, "=", 2)
if len(parts) == 2 && parts[0] == key {
// Key already exists, update the value
lines[i] = fmt.Sprintf("%s=%s", key, value)
updatedContents := strings.Join(lines, "\n")
return v.writeVault(updatedContents)
}
}
// Key doesn't exist, add a new key-value pair
lines = append(lines, fmt.Sprintf("%s=%s", key, value))
updatedContents := strings.Join(lines, "\n")
return v.writeVault(updatedContents)
}
// VaultDelKey deletes a key from the vault.
func (v *Vault) VaultDelKey(key string) error {
contents, err := v.readVault()
if err != nil {
return err
}
var updatedLines []string
lines := strings.Split(contents, "\n")
keyFound := false
for _, line := range lines {
parts := strings.SplitN(line, "=", 2)
if len(parts) == 2 && parts[0] == key {
keyFound = true
continue
}
updatedLines = append(updatedLines, line)
}
if !keyFound {
return errors.New("key not found in vault")
}
updatedContents := strings.Join(updatedLines, "\n")
return v.writeVault(updatedContents)
}
func (v *Vault) VaultEnableBackup(value bool) {
v.backup = value
}