-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
148 lines (123 loc) · 2.94 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
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
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
var letterRunes = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
var wg sync.WaitGroup
func RandStringRunes(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}
type GetSecretsResponse struct {
Name string
VersionId string
SecretString string
VersionStages []string
CreatedDate int64
ARN string
}
type GetSecretsErrorResponse struct {
Type string `json:"__type"`
Message string
}
type GetSecretsRequest struct {
SecretId string
}
var dataMap MyMap
type MyMap struct {
m sync.Mutex
data map[string]string
}
func main() {
dataMap = MyMap{data: make(map[string]string)}
baseDir, err := baseDir()
if err != nil {
return
}
createMap(baseDir)
http.HandleFunc("/", postHandler)
http.ListenAndServe(":8080", nil)
}
func postHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
decoder := json.NewDecoder(r.Body)
var r GetSecretsRequest
err := decoder.Decode(&r)
if err != nil {
panic(err)
}
response := GetSecretsResponse{}
value := dataMap.data[r.SecretId]
if value == "" {
json.NewEncoder(w).Encode(GetSecretsErrorResponse{Type: "ResourceNotFoundException", Message: "Secrets Manager can’t find the specified secret."})
return
}
response.SecretString = value
response.ARN = fmt.Sprintf("arn:aws:secretsmanager:us-west-2:1234567789:secret:%s-%s", r.SecretId, RandStringRunes(6))
response.VersionId = RandStringRunes(6)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(response)
} else {
io.WriteString(w, "Method not supported")
}
}
func load(file string, name string) {
defer wg.Done()
content, err := ioutil.ReadFile(file)
if err != nil {
log.Println(err)
}
var key = name[0 : len(name)-len(filepath.Ext(name))]
var finalKey = strings.Replace(key, ".", "/", -1)
dataMap.m.Lock()
dataMap.data[finalKey] = string(content)
dataMap.m.Unlock()
}
func createMap(baseDir string) {
start := time.Now()
// Traverse filepath and update data map
err := filepath.Walk(baseDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Printf("Error %v\n", err)
return err
}
// Max AWS secret size is 4 KB
if !info.IsDir() && info.Size() <= 4096 {
go load(path, info.Name())
wg.Add(1)
}
return nil
})
if err != nil {
panic(err)
}
wg.Wait()
elapsed := time.Since(start)
log.Printf("Added %d secrets in %s", len(dataMap.data), elapsed)
}
func baseDir() (string, error) {
baseDir := os.Getenv("DATA_DIR")
if baseDir == "" {
baseDir = "/data"
}
if _, err := os.Stat(baseDir); os.IsNotExist(err) {
log.Printf("Path \"%s\" does not exist", baseDir)
return "", err
}
return baseDir, nil
}