@@ -104,7 +104,11 @@ func Rotate(account, provider, project string, c config.Config) (err error) {
104
104
}
105
105
logger .Infof ("Filtered down to %d keys based on current app config" , len (providerKeys ))
106
106
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
+ }
108
112
if c .EnableKeyAgeLogging {
109
113
obfuscatedKeys := []keys.Key {}
110
114
for _ , key := range providerKeys {
@@ -135,10 +139,20 @@ func Rotate(account, provider, project string, c config.Config) (err error) {
135
139
logger .Infof ("Finalised %d keys that are candidates for rotation: %v" ,
136
140
len (rc ), rcStrings )
137
141
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
139
153
}
140
154
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,
142
156
// and deletes the old key iff the key location update is successful
143
157
func rotateKey (rotationCandidate rotationCandidate , creds cred.Credentials ) (err error ) {
144
158
key := rotationCandidate .key
@@ -495,41 +509,43 @@ func validAwsKey(key keys.Key, config config.Config) (valid bool) {
495
509
return
496
510
}
497
511
512
+ func isDatadogKeySet (apiKey string ) bool {
513
+ return len (apiKey ) > 0
514
+ }
515
+
498
516
//postMetric posts details of each keys.Key to a metrics api
499
517
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 )
533
549
}
534
550
}
535
551
return
0 commit comments