This repository has been archived by the owner on Apr 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_secret.go
98 lines (92 loc) · 2.55 KB
/
update_secret.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
package forester
import (
"context"
"fmt"
"log"
"net/http"
"time"
)
// UpdateSecret allows users to add or change their stored secrets. Called via GCP Cloud Functions.
func UpdateSecret(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if handleCorsOptions(w, r) {
return
}
log.Print("Starting UpdateSecret")
fb, err := MakeFirebaseManager(&ctx, r)
if err != nil {
writeError(500, "Error", err, w)
return
}
err = r.ParseMultipartForm(102400)
if err != nil {
writeError(500, "Error parsing form values", err, w)
return
}
lotwUser := r.PostFormValue(lotwUsername)
lotwPass := r.PostFormValue(lotwPassword)
qrzUser := r.PostFormValue(qrzUsername)
qrzPass := r.PostFormValue(qrzPassword)
qrzKey := r.PostFormValue(qrzLogbookAPIKey)
if lotwUser == "" && lotwPass == "" && qrzUser == "" && qrzPass == "" && qrzKey == "" {
log.Print("Nothing to do")
w.WriteHeader(204)
return
}
secretStore := NewSecretStore(ctx)
if lotwUser != "" {
log.Printf("Updating %v", lotwUsername)
_, err = checkAndSetSecret(secretStore, fb, lotwUsername, lotwUser)
if err != nil {
writeError(500, "Error storing a secret", err, w)
return
}
}
if lotwPass != "" {
log.Printf("Updating %v", lotwPassword)
_, err = checkAndSetSecret(secretStore, fb, lotwPassword, lotwPass)
if err != nil {
writeError(500, "Error storing a secret", err, w)
return
}
}
if qrzUser != "" {
log.Printf("Updating %v", qrzUsername)
_, err = checkAndSetSecret(secretStore, fb, qrzUsername, qrzUser)
if err != nil {
writeError(500, "Error storing a secret", err, w)
return
}
}
if qrzPass != "" {
log.Printf("Updating %v", qrzPassword)
_, err = checkAndSetSecret(secretStore, fb, qrzPassword, qrzPass)
if err != nil {
writeError(500, "Error storing a secret", err, w)
return
}
}
if qrzKey != "" {
log.Printf("Updating %v", qrzLogbookAPIKey)
_, err = checkAndSetSecret(secretStore, fb, qrzLogbookAPIKey, qrzKey)
if err != nil {
writeError(500, "Error storing a secret", err, w)
return
}
}
w.WriteHeader(204)
}
func checkAndSetSecret(secretStore SecretStore, fb *FirebaseManager, key string, value string) (string, error) {
// Rely on Firestore rules to check that user is an editor on the logbook
err := fb.SetLogbookProperty(
key+"_last_set",
time.Now().UTC().Format(time.RFC3339),
)
if err != nil {
// 403
return "", fmt.Errorf("can't modify logbook, are you an editor? "+
"not saving secret: %v", err)
}
return secretStore.SetSecret(fb.logbookID, key, value)
}