Skip to content

Commit 637d1b4

Browse files
author
Chris Every
authored
Merge pull request #276 from ovotech/rotate-with-datadog
Post metrics to datadog after rotation
2 parents 12b83a8 + f4d9a11 commit 637d1b4

File tree

2 files changed

+54
-36
lines changed

2 files changed

+54
-36
lines changed

examples/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ If you add a Datadog struct to the config, you can get `cloud-key-rotator` to po
6060
"DatadogAPIKey": "okj23434poz3j4o324p455oz3j4o324",
6161
```
6262

63+
All the fields in the DataDog struct are required.
64+
6365
### Authentication
6466

6567
Regardless of where you run the `cloud-key-rotator` application, you'll need

pkg/rotate/rotatekeys.go

+52-36
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,11 @@ func Rotate(account, provider, project string, c config.Config) (err error) {
104104
}
105105
logger.Infof("Filtered down to %d keys based on current app config", len(providerKeys))
106106
if !c.RotationMode {
107-
postMetric(providerKeys, c.DatadogAPIKey, c.Datadog)
107+
if isDatadogKeySet(c.DatadogAPIKey) {
108+
if metricErr := postMetric(providerKeys, c.DatadogAPIKey, c.Datadog); metricErr != nil {
109+
logger.Infow("Posting metrics errored", metricErr)
110+
}
111+
}
108112
if c.EnableKeyAgeLogging {
109113
obfuscatedKeys := []keys.Key{}
110114
for _, key := range providerKeys {
@@ -135,10 +139,20 @@ func Rotate(account, provider, project string, c config.Config) (err error) {
135139
logger.Infof("Finalised %d keys that are candidates for rotation: %v",
136140
len(rc), rcStrings)
137141

138-
return rotateKeys(rc, c.Credentials)
142+
if err = rotateKeys(rc, c.Credentials); err != nil {
143+
return
144+
}
145+
if isDatadogKeySet(c.DatadogAPIKey) {
146+
// Refresh key ages post rotation
147+
if providerKeys, err = keysOfProviders(account, provider, project, c); err != nil {
148+
return
149+
}
150+
return postMetric(providerKeys, c.DatadogAPIKey, c.Datadog)
151+
}
152+
return
139153
}
140154

141-
//rotatekey creates a new key for the rotation candidate, updates its key locations,
155+
//rotateKey creates a new key for the rotation candidate, updates its key locations,
142156
// and deletes the old key iff the key location update is successful
143157
func rotateKey(rotationCandidate rotationCandidate, creds cred.Credentials) (err error) {
144158
key := rotationCandidate.key
@@ -495,41 +509,43 @@ func validAwsKey(key keys.Key, config config.Config) (valid bool) {
495509
return
496510
}
497511

512+
func isDatadogKeySet(apiKey string) bool {
513+
return len(apiKey) > 0
514+
}
515+
498516
//postMetric posts details of each keys.Key to a metrics api
499517
func postMetric(keys []keys.Key, apiKey string, datadog config.Datadog) (err error) {
500-
if len(apiKey) > 0 {
501-
url := strings.Join([]string{datadogURL, apiKey}, "")
502-
for _, key := range keys {
503-
var jsonString = []byte(
504-
`{ "series" :[{"metric":"` + datadog.MetricName + `",` +
505-
`"points":[[` +
506-
strconv.FormatInt(time.Now().Unix(), 10) +
507-
`, ` + strconv.FormatFloat(key.Age, 'f', 2, 64) +
508-
`]],` +
509-
`"type":"count",` +
510-
`"tags":[` +
511-
`"team:` + datadog.MetricTeam + `",` +
512-
`"project:` + datadog.MetricProject + `",` +
513-
`"environment:` + datadog.MetricEnv + `",` +
514-
`"key:` + key.Name + `",` +
515-
`"provider:` + key.Provider.Provider + `",` +
516-
`"status:` + key.Status + `",` +
517-
`"account:` + key.Account +
518-
`"]}]}`)
519-
var req *http.Request
520-
if req, err = http.NewRequest("POST", url, bytes.NewBuffer(jsonString)); err != nil {
521-
return
522-
}
523-
req.Header.Set("Content-type", "application/json")
524-
client := &http.Client{}
525-
var resp *http.Response
526-
if resp, err = client.Do(req); err != nil {
527-
return
528-
}
529-
defer resp.Body.Close()
530-
if resp.StatusCode != 202 {
531-
err = fmt.Errorf("non-202 status code (%d) returned by Datadog", resp.StatusCode)
532-
}
518+
url := strings.Join([]string{datadogURL, apiKey}, "")
519+
for _, key := range keys {
520+
var jsonString = []byte(
521+
`{ "series" :[{"metric":"` + datadog.MetricName + `",` +
522+
`"points":[[` +
523+
strconv.FormatInt(time.Now().Unix(), 10) +
524+
`, ` + strconv.FormatFloat(key.Age, 'f', 2, 64) +
525+
`]],` +
526+
`"type":"count",` +
527+
`"tags":[` +
528+
`"team:` + datadog.MetricTeam + `",` +
529+
`"project:` + datadog.MetricProject + `",` +
530+
`"environment:` + datadog.MetricEnv + `",` +
531+
`"key:` + key.Name + `",` +
532+
`"provider:` + key.Provider.Provider + `",` +
533+
`"status:` + key.Status + `",` +
534+
`"account:` + key.Account +
535+
`"]}]}`)
536+
var req *http.Request
537+
if req, err = http.NewRequest("POST", url, bytes.NewBuffer(jsonString)); err != nil {
538+
return
539+
}
540+
req.Header.Set("Content-type", "application/json")
541+
client := &http.Client{}
542+
var resp *http.Response
543+
if resp, err = client.Do(req); err != nil {
544+
return
545+
}
546+
defer resp.Body.Close()
547+
if resp.StatusCode != 202 {
548+
err = fmt.Errorf("non-202 status code (%d) returned by Datadog", resp.StatusCode)
533549
}
534550
}
535551
return

0 commit comments

Comments
 (0)