Skip to content

Commit f5bcf3f

Browse files
author
Chris Every
authored
Merge pull request #159 from ovotech/add-ssm-location
Add SSM location
2 parents 0a464c5 + 7944ffd commit f5bcf3f

File tree

7 files changed

+122
-17
lines changed

7 files changed

+122
-17
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ The tool can update keys held in the following locations:
1212
* GitHub
1313
* GoCd
1414
* K8S (GKE only)
15+
* SSM (AWS Parameter Store)
1516

1617
The tool is packaged as an executable file for native invocation, and as a zip
1718
file for deployment as an AWS Lambda.
@@ -88,10 +89,12 @@ ultimately be updated with the new keys that are generated.
8889
Currently, the following locations are supported:
8990

9091
* EnvVars in CircleCI
92+
* GoCd
9193
* GCS
9294
* Secrets in GKE
9395
* Files (encrypted via [mantle](https://github.com/ovotech/mantle) which
9496
integrates with KMS) in GitHub
97+
* SSM (AWS Parameter Store)
9598

9699
## Rotation Process
97100

pkg/config/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ type KeyLocations struct {
7474
GitHub location.GitHub
7575
Gocd []location.Gocd
7676
K8s []location.K8s
77+
SSM []location.Ssm
7778
}
7879

7980
//ProviderServiceAccounts type

pkg/location/circleci.go

+6-17
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,16 @@ var logger = log.StdoutLogger().Sugar()
3838
func (circle CircleCI) Write(serviceAccountName string, keyWrapper KeyWrapper, creds cred.Credentials) (updated UpdatedLocation, err error) {
3939
logger.Info("Starting CircleCI env var updates")
4040
client := &circleci.Client{Token: creds.CircleCIAPIToken}
41+
provider := keyWrapper.KeyProvider
4142

4243
var keyEnvVar string
43-
if len(circle.KeyEnvVar) > 0 {
44-
keyEnvVar = circle.KeyEnvVar
45-
} else {
46-
var defaultEnvVar envVarDefaults
47-
if defaultEnvVar, err = envVarDefaultsFromProvider(keyWrapper.KeyProvider); err != nil {
48-
return
49-
}
50-
keyEnvVar = defaultEnvVar.keyEnvVar
44+
if keyEnvVar, err = getVarNameFromProvider(provider, circle.KeyEnvVar); err != nil {
45+
return
5146
}
5247

5348
var keyIDEnvVar string
54-
if len(circle.KeyIDEnvVar) > 0 {
55-
keyIDEnvVar = circle.KeyIDEnvVar
56-
} else {
57-
var defaultEnvVar envVarDefaults
58-
if defaultEnvVar, err = envVarDefaultsFromProvider(keyWrapper.KeyProvider); err != nil {
59-
return
60-
}
61-
keyIDEnvVar = defaultEnvVar.keyIDEnvVar
49+
if keyIDEnvVar, err = getVarNameFromProvider(provider, circle.KeyIDEnvVar); err != nil {
50+
return
6251
}
6352

6453
splitUsernameProject := strings.Split(circle.UsernameProject, "/")
@@ -78,7 +67,7 @@ func (circle CircleCI) Write(serviceAccountName string, keyWrapper KeyWrapper, c
7867
updated = UpdatedLocation{
7968
LocationType: "CircleCI",
8069
LocationURI: circle.UsernameProject,
81-
LocationIDs: []string{circle.KeyIDEnvVar, circle.KeyEnvVar}}
70+
LocationIDs: []string{keyIDEnvVar, keyEnvVar}}
8271

8372
return updated, nil
8473
}

pkg/location/gcs.go

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
// Copyright 2019 OVO Technology
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
115
package location
216

317
import (

pkg/location/locations.go

+13
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,16 @@ func envVarDefaultsFromProvider(provider string) (envVarDefaults envVarDefaults,
6767
err = fmt.Errorf("No default env var names available for provider: %s", provider)
6868
return
6969
}
70+
71+
func getVarNameFromProvider(provider, suppliedVarName string) (envName string, err error) {
72+
if len(suppliedVarName) > 0 {
73+
envName = suppliedVarName
74+
} else {
75+
var defaultEnvVar envVarDefaults
76+
if defaultEnvVar, err = envVarDefaultsFromProvider(provider); err != nil {
77+
return
78+
}
79+
envName = defaultEnvVar.keyEnvVar
80+
}
81+
return
82+
}

pkg/location/ssm.go

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2019 OVO Technology
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package location
16+
17+
import (
18+
"github.com/aws/aws-sdk-go/aws"
19+
"github.com/aws/aws-sdk-go/aws/session"
20+
awsSsm "github.com/aws/aws-sdk-go/service/ssm"
21+
"github.com/ovotech/cloud-key-rotator/pkg/cred"
22+
)
23+
24+
// Ssm type
25+
type Ssm struct {
26+
keyParamName string
27+
keyIDParamName string
28+
region string
29+
convertToJSON bool
30+
}
31+
32+
func (ssm Ssm) Write(serviceAccountName string, keyWrapper KeyWrapper, creds cred.Credentials) (updated UpdatedLocation, err error) {
33+
provider := keyWrapper.KeyProvider
34+
var key string
35+
36+
if ssm.convertToJSON {
37+
if key, err = getKeyForFileBasedLocation(keyWrapper); err != nil {
38+
return
39+
}
40+
} else {
41+
key = keyWrapper.Key
42+
}
43+
44+
var keyEnvVar string
45+
if keyEnvVar, err = getVarNameFromProvider(provider, ssm.keyParamName); err != nil {
46+
return
47+
}
48+
49+
var keyIDEnvVar string
50+
if keyIDEnvVar, err = getVarNameFromProvider(provider, ssm.keyIDParamName); err != nil {
51+
return
52+
}
53+
54+
svc := awsSsm.New(session.New())
55+
svc.Config.Region = aws.String(ssm.region)
56+
57+
if len(keyIDEnvVar) > 0 {
58+
if err = updateSSMParameter(keyIDEnvVar, keyWrapper.KeyID, "String", *svc); err != nil {
59+
return
60+
}
61+
}
62+
if err = updateSSMParameter(keyEnvVar, key, "SecureString", *svc); err != nil {
63+
return
64+
}
65+
66+
updated = UpdatedLocation{
67+
LocationType: "SSM",
68+
LocationURI: ssm.region,
69+
LocationIDs: []string{keyIDEnvVar, keyEnvVar}}
70+
return
71+
}
72+
73+
func updateSSMParameter(paramName, paramValue, paramType string, svc awsSsm.SSM) (err error) {
74+
input := &awsSsm.PutParameterInput{
75+
Overwrite: aws.Bool(true),
76+
Name: aws.String(paramName),
77+
Value: aws.String(paramValue),
78+
}
79+
_, err = svc.PutParameter(input)
80+
return
81+
}

pkg/rotate/rotatekeys.go

+4
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,10 @@ func locationsToUpdate(keyLocation config.KeyLocations) (kws []location.KeyWrite
294294
googleAppCredsRequired = true
295295
}
296296

297+
for _, ssm := range keyLocation.SSM {
298+
kws = append(kws, ssm)
299+
}
300+
297301
if googleAppCredsRequired {
298302
ensureGoogleAppCreds()
299303
}

0 commit comments

Comments
 (0)