forked from getsops/sotp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
118 lines (103 loc) · 3.02 KB
/
main.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
package main
import (
"fmt"
"log"
"os"
"regexp"
"time"
"github.com/xlzd/gotp"
"go.mozilla.org/sops/v3/aes"
"go.mozilla.org/sops/v3/cmd/sops/common"
"go.mozilla.org/sops/v3/cmd/sops/formats"
"go.mozilla.org/sops/v3/keyservice"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"gopkg.in/yaml.v2"
)
// Config is the structure of the yaml configuration file
type Config struct {
Accounts []Account
}
// Account is an item in the yaml configuration file
type Account struct {
Name string
TOTPSecret string
}
var accountNameRe = `^[a-zA-Z0-9-_\.]{5,64}`
func main() {
if len(os.Args) != 2 {
fmt.Println("usage: sotp <account_name>")
os.Exit(1)
}
accountName := os.Args[1]
if !regexp.MustCompile(accountNameRe).MatchString(accountName) {
log.Fatalf("account name %q does not comply to regular expression %q", accountName, accountNameRe)
}
cfg, err := decryptConfig("config.yaml")
if err != nil {
log.Fatal("failed to access configuration at 'config.yaml'", err)
}
var totpSecret string
for _, account := range cfg.Accounts {
if account.Name == accountName {
totpSecret = account.TOTPSecret
break
}
}
if totpSecret == "" {
log.Fatalf("no totp information found for account %q", accountName)
}
otp := gotp.NewDefaultTOTP(totpSecret)
fmt.Println("current one-time password is:", otp.Now())
}
func decryptConfig(path string) (cfg Config, err error) {
// Read the file into an []byte
encryptedData, err := os.ReadFile(path)
if err != nil {
return cfg, fmt.Errorf("failed to read file %q: %w", path, err)
}
var svcs []keyservice.KeyServiceClient
svcs = append(svcs, keyservice.NewLocalClient())
// try connecting to unix:///tmp/sops.sock
conn, err := grpc.Dial("unix:///tmp/sops.sock", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err == nil {
// ignore errors but only add the keyservice if the dial call succeded
svcs = append(svcs, keyservice.NewKeyServiceClient(conn))
}
store := common.StoreForFormat(formats.Yaml)
// Load SOPS file and access the data key
tree, err := store.LoadEncryptedFile(encryptedData)
if err != nil {
return cfg, err
}
key, err := tree.Metadata.GetDataKeyWithKeyServices(svcs)
if err != nil {
return cfg, err
}
// Decrypt the tree
cipher := aes.NewCipher()
mac, err := tree.Decrypt(key, cipher)
if err != nil {
return cfg, err
}
// Compute the hash of the cleartext tree and compare it with
// the one that was stored in the document. If they match,
// integrity was preserved
originalMac, err := cipher.Decrypt(
tree.Metadata.MessageAuthenticationCode,
key,
tree.Metadata.LastModified.Format(time.RFC3339),
)
if originalMac != mac {
return cfg, fmt.Errorf("Failed to verify data integrity. expected mac %q, got %q", originalMac, mac)
}
cleartext, err := store.EmitPlainFile(tree.Branches)
if err != nil {
return cfg, fmt.Errorf("failed to decrypt file: %w", err)
}
err = yaml.Unmarshal(cleartext, &cfg)
if err != nil {
return cfg, fmt.Errorf("failed to unmarshal cleartext into yaml: %w", err)
}
return
}