Skip to content

Commit 69d7310

Browse files
Merge pull request #7 from unee-t/make_sure_we_have_variables_when_we_deploy
FIX - use AWS parameter store when deployed
2 parents f4814bb + 65c1e7d commit 69d7310

File tree

1 file changed

+51
-101
lines changed

1 file changed

+51
-101
lines changed

main.go

Lines changed: 51 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@ import (
1010

1111
"github.com/apex/log"
1212
"github.com/aws/aws-sdk-go-v2/aws"
13-
"github.com/aws/aws-sdk-go-v2/aws/external"
1413
"github.com/aws/aws-sdk-go-v2/service/ssm"
1514
"github.com/aws/aws-sdk-go-v2/service/sts"
1615

17-
"github.com/prometheus/client_golang/prometheus"
18-
1916
"database/sql"
2017

2118
_ "github.com/go-sql-driver/mysql"
@@ -94,22 +91,36 @@ func NewConfig(cfg aws.Config) (e Env, err error) {
9491
log.Infof("NewConfig Log: The AWS Account ID for this environment is: %s", e.AccountID)
9592

9693
// We get the value for the DEFAULT_REGION
97-
defaultRegion, ok := os.LookupEnv("DEFAULT_REGION")
94+
var defaultRegion string
95+
valdefaultRegion, ok := os.LookupEnv("DEFAULT_REGION")
9896
if ok {
99-
log.Infof("NewConfig Log: DEFAULT_REGION was overridden by local env: %s", defaultRegion)
97+
defaultRegion = valdefaultRegion
98+
log.Infof("NewConfig Log: DEFAULT_REGION was overridden by local env: %s", valdefaultRegion)
10099
} else {
101-
log.Fatal("NewConfig fatal: DEFAULT_REGION is unset as an environment variable, this is a fatal problem")
100+
defaultRegion = e.GetSecret("DEFAULT_REGION")
101+
log.Infof("NewConfig Log: We get the DEFAULT_REGION from the AWS parameter store")
102+
}
103+
104+
if defaultRegion == "" {
105+
log.Fatal("NewConfig fatal: DEFAULT_REGION is unset, this is a fatal problem")
102106
}
103107

104108
cfg.Region = defaultRegion
105109
log.Infof("NewConfig Log: The AWS region for this environment has been set to: %s", cfg.Region)
106110

107111
// We get the value for the STAGE
108-
stage, ok := os.LookupEnv("STAGE")
112+
var stage string
113+
valstage, ok := os.LookupEnv("STAGE")
109114
if ok {
110-
log.Infof("NewConfig Log: STAGE was overridden by local env: %s", stage)
115+
stage = valstage
116+
log.Infof("NewConfig Log: STAGE was overridden by local env: %s", valstage)
111117
} else {
112-
log.Fatal("NewConfig fatal: STAGE is unset as an environment variable, this is a fatal problem")
118+
defaultRegion = e.GetSecret("STAGE")
119+
log.Infof("NewConfig Log: We get the STAGE from the AWS parameter store")
120+
}
121+
122+
if stage == "" {
123+
log.Fatal("NewConfig fatal: STAGE is unset, this is a fatal problem")
113124
}
114125

115126
e.Stage = stage
@@ -131,90 +142,6 @@ func NewConfig(cfg aws.Config) (e Env, err error) {
131142
}
132143
}
133144

134-
// NewBzDbConnexion setups the configuration to the Db where we host the BZ installation
135-
// various parameters MUST have been setup in the AWS Parameter store
136-
func NewBzDbConnexion() (h handler, err error) {
137-
138-
// We check if the AWS CLI profile we need has been setup in this environment
139-
awsCliProfile, ok := os.LookupEnv("TRAVIS_AWS_PROFILE")
140-
if ok {
141-
log.Infof("NewBzDbConnexion Log: the AWS CLI profile we use is: %s", awsCliProfile)
142-
} else {
143-
log.Fatal("NewBzDbConnexion Fatal: the AWS CLI profile is unset as an environment variable, this is a fatal problem")
144-
}
145-
146-
cfg, err := external.LoadDefaultAWSConfig(external.WithSharedConfigProfile(awsCliProfile))
147-
if err != nil {
148-
log.WithError(err).Fatal("NewBzDbConnexion Fatal: We do not have the AWS credentials we need")
149-
return
150-
}
151-
152-
// We get the value for the DEFAULT_REGION
153-
defaultRegion, ok := os.LookupEnv("DEFAULT_REGION")
154-
if ok {
155-
log.Infof("NewBzDbConnexion Log: DEFAULT_REGION was overridden by local env: %s", defaultRegion)
156-
} else {
157-
log.Fatal("NewBzDbConnexion Fatal: DEFAULT_REGION is unset as an environment variable, this is a fatal problem")
158-
}
159-
160-
cfg.Region = defaultRegion
161-
log.Infof("NewBzDbConnexion Log: The AWS region for this environment has been set to: %s", cfg.Region)
162-
163-
// We get the value for the API_ACCESS_TOKEN
164-
apiAccessToken, ok := os.LookupEnv("API_ACCESS_TOKEN")
165-
if ok {
166-
log.Infof("NewBzDbConnexion Log: API_ACCESS_TOKEN was overridden by local env: **hidden secret**")
167-
} else {
168-
log.Fatal("NewBzDbConnexion Fatal: API_ACCESS_TOKEN is unset as an environment variable, this is a fatal problem")
169-
}
170-
171-
e, err := NewConfig(cfg)
172-
if err != nil {
173-
log.WithError(err).Warn("NewBzDbConnexion Warning: error getting some of the parameters for that environment")
174-
}
175-
176-
h = handler{
177-
DSN: e.BugzillaDSN(), // `BugzillaDSN` is a function that is defined in the uneet/env/main.go dependency.
178-
APIAccessToken: apiAccessToken,
179-
Code: e.Code,
180-
}
181-
182-
h.db, err = sql.Open("mysql", h.DSN)
183-
if err != nil {
184-
log.WithError(err).Fatal("NewBzDbConnexion fatal: error opening database")
185-
return
186-
}
187-
188-
microservicecheck := prometheus.NewGaugeVec(
189-
prometheus.GaugeOpts{
190-
Name: "microservice",
191-
Help: "Version with DB ping check",
192-
},
193-
[]string{
194-
"commit",
195-
},
196-
)
197-
198-
version := os.Getenv("UP_COMMIT")
199-
200-
go func() {
201-
for {
202-
if h.db.Ping() == nil {
203-
microservicecheck.WithLabelValues(version).Set(1)
204-
} else {
205-
microservicecheck.WithLabelValues(version).Set(0)
206-
}
207-
time.Sleep(pingPollingFreq)
208-
}
209-
}()
210-
211-
err = prometheus.Register(microservicecheck)
212-
if err != nil {
213-
log.Warn("NewBzDbConnexion Warning: prom already registered")
214-
}
215-
return
216-
}
217-
218145
func (e Env) Bucket(svc string) string {
219146

220147
// Most common bucket
@@ -242,8 +169,9 @@ func (e Env) Bucket(svc string) string {
242169
}
243170

244171
func (e Env) SNS(name, region string) string {
172+
// TODO: Check: if service name is empty, should this be a fatal error???
245173
if name == "" {
246-
log.Warn("SNS Wraning: Service string empty")
174+
log.Warn("SNS Warning: Service string empty")
247175
return ""
248176
}
249177
return fmt.Sprintf("arn:aws:sns:%s:%s:%s", region, e.AccountID, name)
@@ -261,7 +189,7 @@ func (e Env) Udomain(service string) string {
261189

262190
// If we have no information on the domain then we stop
263191
if domain == "" {
264-
log.Fatal("Udomain fatal:domain is unset, this is a fatal problem")
192+
log.Fatal("Udomain fatal: DOMAIN is unset, this is a fatal problem")
265193
}
266194

267195
// Based on the Environment we are in we do different things
@@ -287,7 +215,12 @@ func (e Env) BugzillaDSN() string {
287215
bugzillaDbUser = valbugzillaDbUser
288216
log.Infof("BugzillaDSN Log: BUGZILLA_DB_USER was overridden by local env: %s", valbugzillaDbUser)
289217
} else {
290-
log.Fatal("BugzillaDSN Fatal: BUGZILLA_DB_USER is unset as an environment variable, this is a fatal problem")
218+
bugzillaDbUser = e.GetSecret("BUGZILLA_DB_USER")
219+
log.Infof("BugzillaDSN Log: We get the BUGZILLA_DB_USER from the AWS parameter store")
220+
}
221+
222+
if bugzillaDbUser == "" {
223+
log.Fatal("BugzillaDSN Fatal: BUGZILLA_DB_USER is unset, this is a fatal problem")
291224
}
292225

293226
// Get the value of the variable
@@ -297,9 +230,14 @@ func (e Env) BugzillaDSN() string {
297230
bugzillaDbPassword = valbugzillaDbPassword
298231
log.Infof("BugzillaDSN Log: BUGZILLA_DB_PASSWORD was overridden by local env: **hidden_secret**")
299232
} else {
300-
log.Fatal("BugzillaDSN Fatal: BUGZILLA_DB_PASSWORD is unset as an environment variable, this is a fatal problem")
233+
bugzillaDbPassword = e.GetSecret("BUGZILLA_DB_PASSWORD")
234+
log.Infof("BugzillaDSN Log: We get the BUGZILLA_DB_PASSWORD from the AWS parameter store")
301235
}
302-
236+
237+
if bugzillaDbPassword == "" {
238+
log.Fatal("BugzillaDSN Fatal: BUGZILLA_DB_PASSWORD is unset, this is a fatal problem")
239+
}
240+
303241
// Get the value of the variable
304242
var mysqlhost string
305243
valmysqlhost, ok := os.LookupEnv("MYSQL_HOST")
@@ -308,7 +246,11 @@ func (e Env) BugzillaDSN() string {
308246
log.Infof("BugzillaDSN Log: MYSQL_HOST was overridden by local env: %s", valmysqlhost)
309247
} else {
310248
mysqlhost = e.GetSecret("MYSQL_HOST")
311-
log.Fatal("BugzillaDSN Fatal: MYSQL_HOST is unset as an environment variable, this is a fatal problem")
249+
log.Infof("BugzillaDSN Log: We get the MYSQL_HOST from the AWS parameter store")
250+
}
251+
252+
if mysqlhost == "" {
253+
log.Fatal("BugzillaDSN Fatal: MYSQL_HOST is unset, this is a fatal problem")
312254
}
313255

314256
// Get the value of the variable
@@ -319,7 +261,11 @@ func (e Env) BugzillaDSN() string {
319261
log.Infof("BugzillaDSN Log: MYSQL_PORT was overridden by local env: %s", valmysqlport)
320262
} else {
321263
mysqlport = e.GetSecret("MYSQL_PORT")
322-
log.Fatal("BugzillaDSN Fatal: MYSQL_PORT is unset as an environment variable, this is a fatal problem")
264+
log.Infof("BugzillaDSN Log: We get the MYSQL_PORT from the AWS parameter store")
265+
}
266+
267+
if mysqlport == "" {
268+
log.Fatal("BugzillaDSN Fatal: MYSQL_PORT is unset, this is a fatal problem")
323269
}
324270

325271
// Get the value of the variable
@@ -330,7 +276,11 @@ func (e Env) BugzillaDSN() string {
330276
log.Infof("BugzillaDSN Log: BUGZILLA_DB_NAME was overridden by local env: %s", valbugzillaDbName)
331277
} else {
332278
bugzillaDbName = e.GetSecret("BUGZILLA_DB_NAME")
333-
log.Fatal("BugzillaDSN Fatal: BUGZILLA_DB_NAME is unset as an environment variable, this is a fatal problem")
279+
log.Infof("BugzillaDSN Log: We get the BUGZILLA_DB_NAME from the AWS parameter store")
280+
}
281+
282+
if bugzillaDbName == "" {
283+
log.Fatal("BugzillaDSN Fatal: BUGZILLA_DB_NAME is unset, this is a fatal problem")
334284
}
335285

336286
// Build the string that will allow connection to the BZ database

0 commit comments

Comments
 (0)