-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathssm.go
310 lines (248 loc) · 7.98 KB
/
ssm.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package main
import (
"context"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"time"
"database/sql"
"github.com/go-kit/kit/endpoint"
httptransport "github.com/go-kit/kit/transport/http"
_ "github.com/lib/pq"
)
// "github.com/lib/pq"
// StringService provides operations on strings.
type StringService interface {
Wrap(string, *rsa.PrivateKey) (string, error)
Unwrap(string, *rsa.PrivateKey) (string, error)
Healthcheck() (string, string)
}
// stringService is a concrete implementation of StringService
type stringService struct{}
func (stringService) Wrap(s string, key *rsa.PrivateKey) (string, error) {
log.Println("Received a wrap request.")
if s == "" {
return "", ErrEmpty
}
secretMessage := []byte(s)
label := []byte("ssm_wrap")
rng := rand.Reader
ciphertext, err := rsa.EncryptOAEP(sha256.New(), rng, &key.PublicKey, secretMessage, label)
if err != nil {
log.Printf("Error from encryption: %s\n", err)
return "", err
}
encoded := base64.StdEncoding.EncodeToString(ciphertext)
return encoded, nil
}
func (stringService) Unwrap(s string, key *rsa.PrivateKey) (string, error) {
log.Println("Received an unwrap request.")
ciphertext, err := base64.StdEncoding.DecodeString(s)
if err != nil {
log.Printf("Error from decoding: %s\n", err)
return "", err
}
label := []byte("ssm_wrap")
rng := rand.Reader
plaintext, err := rsa.DecryptOAEP(sha256.New(), rng, key, ciphertext, label)
if err != nil {
log.Printf("Error from decryption: %s\n", err)
return "", err
}
return string(plaintext), nil
}
func (stringService) Healthcheck() (string, string) {
log.Println("Received a healthcheck request.")
return os.Getenv("SSM_VERSION"), os.Getenv("SSM_LOCATION")
}
// ErrEmpty is returned when an input string is empty.
var ErrEmpty = errors.New("empty string")
// For each method, we define request and response structs
type wrapRequest struct {
Key string `json:"key"`
}
type wrapResponse struct {
Cipher string `json:"cipher"`
Err string `json:"err,omitempty"` // errors don't define JSON marshaling
}
type unwrapRequest struct {
Cipher string `json:"cipher"`
}
type unwrapResponse struct {
Key string `json:"key"`
Err string `json:"err,omitempty"` // errors don't define JSON marshaling
}
type healthcheckResponse struct {
Version string `json:"version"`
Location string `json:"location"`
}
// Endpoints are a primary abstraction in go-kit. An endpoint represents a single RPC (method in our service interface)
func makeWrapEndpoint(svc StringService, key *rsa.PrivateKey) endpoint.Endpoint {
return func(_ context.Context, request interface{}) (interface{}, error) {
req := request.(wrapRequest)
v, err := svc.Wrap(req.Key, key)
if err != nil {
return wrapResponse{v, err.Error()}, nil
}
return wrapResponse{v, ""}, nil
}
}
func makeUnwrapEndpoint(svc StringService, key *rsa.PrivateKey) endpoint.Endpoint {
return func(_ context.Context, request interface{}) (interface{}, error) {
req := request.(unwrapRequest)
v, err := svc.Unwrap(req.Cipher, key)
if err != nil {
return unwrapResponse{v, err.Error()}, nil
}
return unwrapResponse{v, ""}, nil
}
}
func makeHealthcheckEndpoint(svc StringService) endpoint.Endpoint {
return func(_ context.Context, request interface{}) (interface{}, error) {
v, l := svc.Healthcheck()
return healthcheckResponse{v, l}, nil
}
}
func getPSQLParams(baseDir string) map[string]string {
var paramMap = make(map[string]string)
dirs, err := os.ReadDir(baseDir)
log.Println(baseDir)
if err != nil {
log.Fatal(err)
}
for _, dir := range dirs {
dirPath := filepath.Join(baseDir, dir.Name())
log.Println(dirPath)
typePath := filepath.Join(dirPath, "type")
dat_type, _ := os.ReadFile(typePath)
btype := string(dat_type)
providerPath := filepath.Join(dirPath, "provider")
dat_provider, _ := os.ReadFile(providerPath)
provider := string(dat_provider)
if btype == "postgresql" && provider == "redhat" {
log.Println("Postgresql binding data found")
hostPath := filepath.Join(dirPath, "host")
dat_host, _ := os.ReadFile(hostPath)
paramMap["host"] = string(dat_host)
userPath := filepath.Join(dirPath, "username")
dat_user, _ := os.ReadFile(userPath)
paramMap["username"] = string(dat_user)
passPath := filepath.Join(dirPath, "password")
dat_pass, _ := os.ReadFile(passPath)
paramMap["password"] = string(dat_pass)
portPath := filepath.Join(dirPath, "port")
dat_port, _ := os.ReadFile(portPath)
paramMap["port"] = string(dat_port)
databasePath := filepath.Join(dirPath, "database")
dat_database, _ := os.ReadFile(databasePath)
paramMap["database"] = string(dat_database)
return paramMap
}
}
return paramMap
}
func getMasterKey(baseKey string) string {
time.Sleep(5 * time.Second)
connParams := getPSQLParams("/bindings")
connStr := fmt.Sprintf("host=%s user=%s password=%s dbname=%s sslmode=disable port=%s", connParams["host"],
connParams["username"], connParams["password"], connParams["database"], connParams["port"])
//log.Println(connStr)
db, err := sql.Open("postgres", connStr)
if err != nil {
log.Println("Error connecting to database")
log.Fatal(err)
}
log.Println("Sucessful connection to database!")
_, create_err := db.Query("CREATE TABLE IF NOT EXISTS keys (name VARCHAR(10), value TEXT)")
if create_err != nil {
log.Println("Error creating keys table")
log.Fatal(create_err)
}
rows := db.QueryRow("SELECT value FROM keys WHERE name = 'MASTERKEY'")
var value string
scan_err := rows.Scan(&value)
if scan_err == nil {
return value
} else {
log.Println("Master Key not found, creating")
sqlStatement := `
INSERT INTO keys (name, value)
VALUES ($1, $2)`
_, insert_err := db.Exec(sqlStatement, "MASTERKEY", baseKey)
if insert_err != nil {
log.Println("Error inserting master key")
log.Fatal(insert_err.Error())
}
}
return baseKey
}
func getPrivateKey() *rsa.PrivateKey {
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
log.Fatalf("Error generating encryption key: %s\n", err)
}
keyBytes := x509.MarshalPKCS1PrivateKey(key)
keyString := base64.StdEncoding.EncodeToString(keyBytes)
keyString = getMasterKey(keyString)
keyBytes, _ = base64.StdEncoding.DecodeString(keyString)
key, err = x509.ParsePKCS1PrivateKey(keyBytes)
if err != nil {
log.Fatalf("Error Parsing encryption key: %s\n", err)
}
return key
}
// Transports expose the service to the network. In this first example we utilize JSON over HTTP.
func main() {
svc := stringService{}
key := getPrivateKey()
wrapHandler := httptransport.NewServer(
makeWrapEndpoint(svc, key),
decodeWrapRequest,
encodeResponse,
)
unwrapHandler := httptransport.NewServer(
makeUnwrapEndpoint(svc, key),
decodeUnwrapRequest,
encodeResponse,
)
healthcheckHandler := httptransport.NewServer(
makeHealthcheckEndpoint(svc),
decodeHealthcheckRequest,
encodeResponse,
)
log.Println("Setting the endpoint handlers.")
http.Handle("/wrap", wrapHandler)
http.Handle("/unwrap", unwrapHandler)
http.Handle("/healthcheck", healthcheckHandler)
log.Println("Starting Listener on port 8080.")
log.Fatal(http.ListenAndServe(":8080", nil))
}
func decodeWrapRequest(_ context.Context, r *http.Request) (interface{}, error) {
var request wrapRequest
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
return nil, err
}
return request, nil
}
func decodeUnwrapRequest(_ context.Context, r *http.Request) (interface{}, error) {
var request unwrapRequest
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
return nil, err
}
return request, nil
}
func decodeHealthcheckRequest(_ context.Context, r *http.Request) (interface{}, error) {
return nil, nil
}
func encodeResponse(_ context.Context, w http.ResponseWriter, response interface{}) error {
return json.NewEncoder(w).Encode(response)
}