-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
193 lines (178 loc) · 5.57 KB
/
setup.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
package main
import (
"encoding/json"
"fmt"
"github.com/aws/aws-sdk-go/aws"
awsesh "github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/pkg/errors"
"go_report/auth"
"go_report/gh"
"go_report/store/dynamo"
"log"
"os"
"path/filepath"
"reflect"
"strconv"
"strings"
)
type Config struct {
Port string `json:"port" paramName:"BRS_PORT" paramDefault:"8080"` // Port on which to connect the server
LogFile string `json:"logFile" paramName:"BRS_LOGFILE" paramDefault:"stderr"` // File location for log
TableName string `json:"tableName" paramName:"TABLE_NAME" paramDefault:"BugReports"`
IssueCreationThreshold string `json:"issueCreationThreshold" paramName:"ISSUE_CREATION_THRESHOLD" paramDefault:"x"`
}
//ReadConfigFromFile reads a cfg.json file into a Config struct
func ReadConfigFromFile(fp string) (c Config, err error) {
cfg := Config{}
fd, err := os.Open(fp)
if err != nil {
return cfg, err
}
if err = json.NewDecoder(fd).Decode(&cfg); err != nil {
return cfg, err
}
if err := fd.Close(); err != nil {
return cfg, err
}
return cfg, nil
}
func StartLogger(fp string) (*log.Logger, error) {
var logger *log.Logger
if strings.ToLower(fp) == "stderr" || fp == "2" {
logger = log.New(os.Stderr, "Status: ", log.LstdFlags|log.Lshortfile)
} else if strings.ToLower(fp) == "stdout" || fp == "1" {
logger = log.New(os.Stdout, "Status: ", log.LstdFlags|log.Lshortfile)
} else {
logFile, err := os.OpenFile(fp, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return nil, err
}
logger = log.New(logFile, filepath.Base(fp)+": ", log.LstdFlags|log.Lshortfile)
}
logger.Println("Logger started successfully.")
return logger, nil
}
func startGHService(svc *ssm.SSM) (*gh.Service, error) {
var repo gh.Repo
if err := LoadParams(svc, &repo); err != nil {
return nil, err
}
var mm struct { // middleman between strings & typed gh.Secrets
PrivateKey string `json:"ghPrivateKey" paramName:"GH_APP_KEY,secret"` // pem encoded rsa key
AppID string `json:"ghAppID" paramName:"GH_APP_ID,secret"`
InstallID string `json:"ghInstallID" paramName:"GH_INSTALL_ID,secret"`
//WebhookSecret string `json:"ghWebhookSecret" paramName:"GH_WEBHOOK,secret"` // not needed
ClientID string `json:"ghClientID" paramName:"GH_CLIENT_ID,secret"`
ClientSecret string `json:"ghClientSecret" paramName:"GH_CLIENT_SECRET,secret"`
}
if err := LoadParams(svc, &mm); err != nil {
return nil, err
}
ghshh := gh.Secrets{
PrivateKey: mm.PrivateKey,
ClientID: mm.ClientID,
ClientSecret: mm.ClientSecret,
}
i, err := strconv.Atoi(mm.AppID)
if err != nil {
return nil, err
}
ghshh.AppID = i
i, err = strconv.Atoi(mm.InstallID)
if err != nil {
return nil, err
}
ghshh.InstallID = i
return gh.New(repo, ghshh), nil
}
func startAuthService(svc *ssm.SSM, store *dynamo.Store, ghs *gh.Service, logger *log.Logger) (*auth.Service, error) {
var shh auth.Secrets
if err := LoadParams(svc, &shh); err != nil {
return nil, err
}
return auth.New(store, shh, ghs, logger), nil
}
func DescribeParametersAvailable(svc *ssm.SSM) {
dpo, err := svc.DescribeParameters(&ssm.DescribeParametersInput{MaxResults: aws.Int64(15)})
if err != nil {
panic(err)
}
fmt.Printf("Found Parameters: %+v", dpo.String())
}
func LoadFromParamStore(sesh *awsesh.Session) (cfg Config, auth *auth.Service, ghs *gh.Service, store *dynamo.Store, logger *log.Logger, err error) {
svc := ssm.New(sesh)
//DescribeParametersAvailable(svc)
if err = LoadParams(svc, &cfg); err != nil {
return
}
if logger, err = StartLogger(cfg.LogFile); err != nil {
return
}
store = dynamo.New(sesh, cfg.TableName, logger)
fmt.Printf("%+v %+v", store, cfg)
if ghs, err = startGHService(svc); err != nil {
return
}
if auth, err = startAuthService(svc, store, ghs, logger); err != nil {
return
}
return
}
// structs with paramName & json tags can be autofilled from param store values
// to avoid writing own reflection for setting values, we require a json tag for
// leveraging the stdlib unmarshal.
func LoadParams(svc *ssm.SSM, v interface{}) (err error) {
const tagName = "paramName"
const defaultValueTagName = "paramDefault"
if ok := reflect.ValueOf(v).Kind() == reflect.Ptr; !ok {
return errors.New("LoadParams requires a pointer to a tagged destination structure")
}
// Iterate over all available fields and read the tag value
t, temp := reflect.ValueOf(v).Elem(), map[string]interface{}{}
for i := 0; i < t.NumField(); i++ {
field := t.Type().Field(i)
// get the json and param tags
tag := field.Tag.Get(tagName)
jt := field.Tag.Get("json")
if jt == "" || tag == "" {
continue
}
var pn string
var isSecret bool
if strings.Contains(tag, ",secret") {
pn, isSecret = strings.Split(tag, ",")[0], true
} else {
pn, isSecret = tag, false
}
param, err := svc.GetParameter(&ssm.GetParameterInput{
Name: aws.String(pn),
WithDecryption: aws.Bool(isSecret),
})
// if err, set default value if provided; else fail with error
if err != nil {
dv := field.Tag.Get(defaultValueTagName)
if dv == "" {
fmt.Printf("param: %+v\tsecret: %+v", pn, isSecret)
return err
}
// sets default value of param
param = &ssm.GetParameterOutput{
Parameter: &ssm.Parameter{Value: aws.String(dv)},
}
}
// Now add the value from the param store, to the intermediate map
if strings.Contains(jt, ",") {
jt = strings.Split(jt, ",")[0]
}
temp[jt] = *param.Parameter.Value
}
b, err := json.Marshal(temp)
if err != nil {
return err
}
if err := json.Unmarshal(b, v); err != nil {
return err
}
return nil
}