-
Notifications
You must be signed in to change notification settings - Fork 0
/
kms.go
145 lines (139 loc) · 3.93 KB
/
kms.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package service
import (
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/adrianosela/padl/api/auth"
"github.com/adrianosela/padl/api/payloads"
"github.com/adrianosela/padl/lib/keys"
"github.com/gorilla/mux"
)
func (s *Service) addKeyEndpoints() {
s.Router.Methods(http.MethodGet).Path("/key/{kid}").HandlerFunc(s.getPubKeyHandler) // note no auth
s.Router.Methods(http.MethodPost).Path("/key/{kid}/decrypt").Handler(
s.Auth(s.decryptSecretHandler, []string{auth.ServiceAccountAudience, auth.PadlAPIAudience}...))
}
func (s *Service) getPubKeyHandler(w http.ResponseWriter, r *http.Request) {
// get key id from request URL
var id string
if id = mux.Vars(r)["kid"]; id == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("no key id in request URL"))
return
}
// get key from store, no need to check privs, pub keys are public
pub, err := s.keystore.GetPubKey(id)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("could not get key: %s", err)))
return
}
if pub == nil {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("key not found"))
return
}
// return success
pubByt, err := json.Marshal(&pub)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("could marshal response: %s", err)))
return
}
w.WriteHeader(http.StatusOK)
w.Write(pubByt)
return
}
func (s *Service) decryptSecretHandler(w http.ResponseWriter, r *http.Request) {
claims := GetClaims(r)
// get key id from request URL
var id string
if id = mux.Vars(r)["kid"]; id == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("no key id in request URL"))
return
}
// get payload
var decryptPl *payloads.DecryptSecretRequest
if err := unmarshalRequestBody(r, &decryptPl); err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("could not unmarshall request body"))
return
}
// validate payload
if err := decryptPl.Validate(); err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("could not validate decrypt secret request: %s", err)))
return
}
// get key from store
key, err := s.keystore.GetPrivKey(id)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("error attempting to get key: %s", err)))
return
}
// get owning project for the key
p, err := s.database.GetProject(key.Project)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("could get project: %s", err)))
return
}
ok := p.HasUser(claims.Subject)
if !ok {
svcAcctParts := strings.Split(claims.Subject, "@")
if len(svcAcctParts) < 2 {
ok = false
} else {
svcAcctDetails := strings.Split(svcAcctParts[0], ".")
if (len(svcAcctDetails) < 2) {
ok = false
} else {
ok = p.HasServiceAccount(svcAcctDetails[0])
if p.Name != svcAcctDetails[1] {
ok = false
}
}
}
}
// treat not having visibility of a key the same as the key not existing
if key == nil || !ok {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("key not found"))
return
}
// decode pem
pkey, err := keys.DecodePrivKeyPEM([]byte(key.PEM))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("could not decode pem"))
return
}
// decode secret
raw, err := base64.StdEncoding.DecodeString(decryptPl.Secret)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("secret is not base64 encoded"))
return
}
// decrypt secret
message, err := keys.DecryptMessage(raw, pkey)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("could not decrypt secret: %s", err)))
return
}
// send success
mbyt, err := json.Marshal(&payloads.DecryptSecretResponse{Message: base64.StdEncoding.EncodeToString(message)})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("could marshal response: %s", err)))
return
}
w.WriteHeader(http.StatusOK)
w.Write(mbyt)
return
}