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
|
package secrets
import (
"fmt"
"io"
"os"
"path"
"strings"
"filippo.io/age"
)
func DecryptSecret(secret string) (string, error) {
homepath, err := os.UserHomeDir()
if err != nil {
fmt.Println("could not get home dir")
os.Exit(1)
}
sshFolder := path.Join(homepath, ".ssh")
entries, err := os.ReadDir(sshFolder)
var identities []*age.X25519Identity
for _, file := range entries {
if !strings.HasSuffix(".pub", file.Name()) {
keybytes, err := os.ReadFile(path.Join(sshFolder, file.Name()))
identity, err := age.ParseX25519Identity(strings.TrimSpace(string(keybytes)))
if err != nil {
continue
}
identities = append(identities, identity)
}
}
if len(identities) == 0 {
return "", fmt.Errorf("could not parse any identities")
}
for _, id := range identities {
result, err := age.Decrypt(strings.NewReader(secret), id)
if err != nil {
continue
}
b, _ := io.ReadAll(result)
return string(b), nil
}
return "", fmt.Errorf("could not find identity")
}
|