Repository navigation
Expand file tree
/
Copy pathkeyrotation.go
More file actions
82 lines (70 loc) · 1.97 KB
/
Copy pathkeyrotation.go
File metadata and controls
82 lines (70 loc) · 1.97 KB
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
package main
import (
"crypto/rand"
"fmt"
"os"
)
// generateNewKey creates a random 32-character API key using readable characters
func generateNewKey() (string, error) {
const charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
bytes := make([]byte, 32)
_, err := rand.Read(bytes)
if err != nil {
return "", fmt.Errorf("failed to generate random bytes: %v", err)
}
key := make([]byte, 32)
for i := 0; i < 32; i++ {
key[i] = charset[int(bytes[i])%len(charset)]
}
return string(key), nil
}
// rotateKey generates a new API key and writes it to the platform-specific key file
func rotateKey(task Task) (keyRotationResponse, error) {
var keyfilePath string
// Determine key file path based on operating system
keyfilePath, err := GetKeyFilePath()
if err != nil {
return keyRotationResponse{
Success: false,
RotationError: fmt.Sprintf("failed to get key file path: %v", err),
}, nil
}
newKey, err := generateNewKey()
if err != nil {
return keyRotationResponse{
Success: false,
RotationError: fmt.Sprintf("failed to generate new key: %v", err),
}, nil
}
// Write key with restricted permissions (0600 = read/write for owner only)
_, err = os.Stat(keyfilePath)
if err != nil {
return keyRotationResponse{
Success: false,
RotationError: fmt.Sprintf("error checking if key file exists: %v", err),
}, nil
}
if os.IsNotExist(err) {
// Create the file if it does not exist
emptyFile, err := os.Create(keyfilePath)
os.Chmod(keyfilePath, 0600)
if err != nil {
return keyRotationResponse{
Success: false,
RotationError: fmt.Sprintf("failed to create key file: %v", err),
}, nil
}
emptyFile.Close()
}
err = os.WriteFile(keyfilePath, []byte(newKey), 0600)
if err != nil {
return keyRotationResponse{
Success: false,
RotationError: fmt.Sprintf("failed to write key to file: %v", err),
}, nil
}
return keyRotationResponse{
Success: true,
NewKey: newKey,
}, nil
}