@@ -9,25 +9,22 @@ import (
9
9
"os"
10
10
"path/filepath"
11
11
"strings"
12
- "syscall"
13
12
"time"
14
-
15
- "golang.org/x/term"
16
13
)
17
14
18
- //structure of backed up keys
15
+ // Structure of backed up keys (removed Salt field)
19
16
type BackupData struct {
20
17
21
18
Timestamp time.Time `json:"timestamp"`
22
19
23
- SystemInfo SystemInfo `json:"system_info"`
20
+ SystemInfo SystemInfo `json:"system_info"`
24
21
25
- EncryptedKeys map [string ]EncryptedKey `json:"encrypted_keys"`
22
+ EncryptedKeys map [string ]EncryptedKey `json:"encrypted_keys"`
26
23
27
- Salt []byte `json:"salt"`
24
+ EncryptionKey []byte `json:"encryption_key"` // Store the key directly
28
25
}
29
26
30
- // basic system information
27
+ // Basic system information
31
28
type SystemInfo struct {
32
29
33
30
Hostname string `json:"hostname"`
@@ -53,29 +50,23 @@ type EncryptedKey struct {
53
50
type BackupManager struct {
54
51
config * EncryptionConfig
55
52
}
53
+
56
54
func NewBackupManager () * BackupManager {
57
55
return & BackupManager {}
58
56
}
59
57
60
- // create a complete backup of keys
58
+ //create a complete backup of keys (no password required)
61
59
func (bm * BackupManager ) CreateBackup (customPaths []string ) error {
62
60
fmt .Println ("Starting key backup process..." )
63
61
64
- //password for encryption
65
- password , err := bm .getPassword ()
66
- if err != nil {
67
- return fmt .Errorf ("failed to get password: %w" , err )
68
- }
69
-
70
- // generate salt
71
- salt , err := GenerateSalt ()
62
+ //generate random encryption key (no password needed)
63
+ key , err := GenerateKey ()
72
64
if err != nil {
73
- return fmt .Errorf ("failed to generate salt : %w" , err )
65
+ return fmt .Errorf ("failed to generate encryption key : %w" , err )
74
66
}
75
67
76
68
bm .config = & EncryptionConfig {
77
- Password : password ,
78
- Salt : salt ,
69
+ Key : key ,
79
70
}
80
71
81
72
// search standard locations
@@ -90,7 +81,6 @@ func (bm *BackupManager) CreateBackup(customPaths []string) error {
90
81
91
82
//combine all locations
92
83
allLocations := append (standardLocations , customLocations ... )
93
-
94
84
if len (allLocations ) == 0 {
95
85
fmt .Println ("No key locations found to backup." )
96
86
return nil
@@ -101,7 +91,7 @@ func (bm *BackupManager) CreateBackup(customPaths []string) error {
101
91
Timestamp : time .Now (),
102
92
SystemInfo : bm .getSystemInfo (),
103
93
EncryptedKeys : make (map [string ]EncryptedKey ),
104
- Salt : salt ,
94
+ EncryptionKey : key , // Store the key in backup data
105
95
}
106
96
107
97
//encrypt and store keys
@@ -116,17 +106,15 @@ func (bm *BackupManager) CreateBackup(customPaths []string) error {
116
106
117
107
//creating tarball for the backup storing
118
108
fmt .Println ("Creating backup tarball..." )
119
- tarballPath := fmt .Sprintf ("dist/key-backup-%s.tar.gz" ,
109
+ tarballPath := fmt .Sprintf ("dist/key-backup-%s.tar.gz" ,
120
110
time .Now ().Format ("2006-01-02-15-04-05" ))
121
-
122
111
err = bm .createTarball (backupData , tarballPath )
123
112
if err != nil {
124
113
return fmt .Errorf ("failed to create tarball: %w" , err )
125
114
}
126
115
127
116
fmt .Printf ("Backup completed successfully: %s\n " , tarballPath )
128
117
fmt .Printf ("Backed up %d key files\n " , len (backupData .EncryptedKeys ))
129
-
130
118
return nil
131
119
}
132
120
@@ -136,18 +124,14 @@ func (bm *BackupManager) processLocation(location KeyLocation, backupData *Backu
136
124
//get file info for permissions
137
125
fileInfo , err := os .Stat (filePath )
138
126
if err != nil {
139
-
140
- continue
141
-
142
- }
127
+ continue
128
+ }
143
129
144
130
// call encryption of the file
145
131
encryptedData , err := EncryptFile (filePath , bm .config )
146
132
if err != nil {
147
-
148
- return fmt .Errorf ("failed to encrypt %s: %w" , filePath , err )
149
-
150
- }
133
+ return fmt .Errorf ("failed to encrypt %s: %w" , filePath , err )
134
+ }
151
135
152
136
// store encrypted key
153
137
keyID := filepath .Base (filePath ) + "_" + strings .ReplaceAll (filePath , "/" , "_" )
@@ -164,13 +148,10 @@ func (bm *BackupManager) processLocation(location KeyLocation, backupData *Backu
164
148
// processCustomPaths converts custom paths to KeyLocation objects
165
149
func (bm * BackupManager ) processCustomPaths (customPaths []string ) []KeyLocation {
166
150
var locations []KeyLocation
167
-
168
151
for _ , path := range customPaths {
169
152
if path == "" {
170
-
171
- continue
172
-
173
- }
153
+ continue
154
+ }
174
155
175
156
// Expand home directory
176
157
if strings .HasPrefix (path , "~/" ) {
@@ -211,7 +192,6 @@ func (bm *BackupManager) processCustomPaths(customPaths []string) []KeyLocation
211
192
})
212
193
}
213
194
}
214
-
215
195
return locations
216
196
}
217
197
@@ -222,37 +202,15 @@ func (bm *BackupManager) getSystemInfo() SystemInfo {
222
202
if username == "" {
223
203
username = os .Getenv ("USERNAME" )
224
204
}
225
-
226
205
return SystemInfo {
227
206
Hostname : hostname ,
228
207
Username : username ,
229
208
OS : "linux" ,
230
209
}
231
210
}
232
211
233
- // prompt users for encryption password
234
- func (bm * BackupManager ) getPassword () (string , error ) {
235
- fmt .Print ("Enter password for key encryption: " )
236
- bytePassword , err := term .ReadPassword (int (syscall .Stdin ))
237
- if err != nil {
238
- return "" , err
239
- }
240
- fmt .Println ()
241
-
242
- password := string (bytePassword )
243
- if len (password ) < 8 {
244
- return "" , fmt .Errorf ("password must be at least 8 characters long" ) ////just for better recurity - can add more such conditions
245
- }
246
-
247
- return password , nil
248
- }
249
-
250
- //compressed tarball with the backup data
212
+ //create compressed tarball with the backup data
251
213
func (bm * BackupManager ) createTarball (backupData * BackupData , tarballPath string ) error {
252
- // if err := os.MkdirAll(filepath.Dir(tarballPath), 0755); err != nil {
253
- // return err
254
- // }
255
-
256
214
// Create tarball file
257
215
file , err := os .Create (tarballPath )
258
216
if err != nil {
@@ -296,7 +254,6 @@ func (bm *BackupManager) createTarball(backupData *BackupData, tarballPath strin
296
254
func GetCustomPaths () []string {
297
255
var paths []string
298
256
scanner := bufio .NewScanner (os .Stdin )
299
-
300
257
fmt .Println ("\n Enter additional key locations (one per line, empty line to finish):" )
301
258
fmt .Println ("Examples: ~/mykeys/, /opt/certificates/, ~/.config/app/keys" )
302
259
@@ -310,9 +267,7 @@ func GetCustomPaths() []string {
310
267
if path == "" {
311
268
break
312
269
}
313
-
314
270
paths = append (paths , path )
315
271
}
316
-
317
272
return paths
318
273
}
0 commit comments