Skip to content

Commit d356662

Browse files
authored
Merge pull request #37 from ovotech/add-aws-creds-file
Add functionality for AWS creds to be written to GitHub locations
2 parents 26942e1 + 4859838 commit d356662

File tree

7 files changed

+39
-31
lines changed

7 files changed

+39
-31
lines changed

pkg/crypt/crypt.go

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package crypt
22

33
import (
4-
b64 "encoding/base64"
54
"errors"
65
"os"
76
"strings"
@@ -12,17 +11,11 @@ import (
1211

1312
//EncryptedServiceAccountKey uses github.com/ovotech/mantle to encrypt the
1413
// key string that's passed in
15-
func EncryptedServiceAccountKey(key, kmsKey string) (encKey []byte, err error) {
14+
func EncryptedServiceAccountKey(key, kmsKey string) (encKey []byte) {
1615
const singleLine = false
1716
const disableValidation = true
18-
19-
var decodedKey []byte
20-
if decodedKey, err = b64.StdEncoding.DecodeString(key); err != nil {
21-
return
22-
}
23-
24-
return enc.CipherBytesFromPrimitives([]byte(decodedKey), singleLine,
25-
disableValidation, "", "", "", "", kmsKey), nil
17+
return enc.CipherBytesFromPrimitives([]byte(key), singleLine,
18+
disableValidation, "", "", "", "", kmsKey)
2619
}
2720

2821
//CommitSignKey creates an openPGP Entity based on a user's name, email,

pkg/location/circleci.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var logger = log.StdoutLogger().Sugar()
2121

2222
//updateCircleCI updates the circleCI environment variable by deleting and
2323
//then creating it again with the new key
24-
func (circle CircleCI) Write(serviceAccountName, keyID, key string, creds cred.Credentials) (updated UpdatedLocation, err error) {
24+
func (circle CircleCI) Write(serviceAccountName string, keyWrapper KeyWrapper, creds cred.Credentials) (updated UpdatedLocation, err error) {
2525
logger.Info("Starting CircleCI env var updates")
2626
client := &circleci.Client{Token: creds.CircleCIAPIToken}
2727
keyIDEnvVarName := circle.KeyIDEnvVar
@@ -30,12 +30,12 @@ func (circle CircleCI) Write(serviceAccountName, keyID, key string, creds cred.C
3030
project := splitUsernameProject[1]
3131

3232
if len(keyIDEnvVarName) > 0 {
33-
if err = updateCircleCIEnvVar(username, project, keyIDEnvVarName, keyID, client); err != nil {
33+
if err = updateCircleCIEnvVar(username, project, keyIDEnvVarName, keyWrapper.KeyID, client); err != nil {
3434
return
3535
}
3636
}
3737

38-
if err = updateCircleCIEnvVar(username, project, circle.KeyEnvVar, key, client); err != nil {
38+
if err = updateCircleCIEnvVar(username, project, circle.KeyEnvVar, keyWrapper.Key, client); err != nil {
3939
return
4040
}
4141

pkg/location/github.go

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package location
22

33
import (
4+
b64 "encoding/base64"
45
"errors"
56
"fmt"
67
"io/ioutil"
@@ -25,33 +26,38 @@ type GitHub struct {
2526
CircleCIDeployJobName string
2627
}
2728

28-
func (gitHub GitHub) Write(serviceAccountName, keyID, key string, creds cred.Credentials) (updated UpdatedLocation, err error) {
29+
func (gitHub GitHub) Write(serviceAccountName string, keyWrapper KeyWrapper, creds cred.Credentials) (updated UpdatedLocation, err error) {
2930

3031
if len(creds.KmsKey) == 0 {
3132
err = errors.New("Not updating un-encrypted new key in a Git repository. Use the" +
3233
"'KmsKey' field in config to specify the KMS key to use for encryption")
3334
return
3435
}
35-
36-
// const localDir = "/etc/cloud-key-rotator/cloud-key-rotator-tmp-repo"
36+
var key string
37+
if keyWrapper.KeyProvider == "aws" {
38+
key = fmt.Sprintf("[default]\naws_access_key_id = %s\naws_secret_access_key = %s", keyWrapper.KeyID, keyWrapper.Key)
39+
} else {
40+
var keyBytes []byte
41+
if keyBytes, err = b64.StdEncoding.DecodeString(keyWrapper.Key); err == nil {
42+
key = string(keyBytes)
43+
}
44+
}
3745

3846
const localDir = "/etc/cloud-key-rotator/cloud-key-rotator-tmp-repo"
3947

4048
defer os.RemoveAll(localDir)
4149

42-
// TODO Move me out of git-specific code
43-
var encKey []byte
44-
if encKey, err = crypt.EncryptedServiceAccountKey(key, creds.KmsKey); err != nil {
45-
return
46-
}
47-
4850
var signKey *openpgp.Entity
4951
if signKey, err = crypt.CommitSignKey(creds.GitHubAccount.GitName, creds.GitHubAccount.GitEmail, creds.AkrPass); err != nil {
5052
return
5153
}
5254

5355
var committed *object.Commit
54-
if committed, err = writeKeyToRemoteGitRepo(gitHub, serviceAccountName, encKey, localDir, signKey, creds); err != nil {
56+
const singleLine = false
57+
const disableValidation = true
58+
if committed, err = writeKeyToRemoteGitRepo(gitHub, serviceAccountName,
59+
crypt.EncryptedServiceAccountKey(key, creds.KmsKey),
60+
localDir, signKey, creds); err != nil {
5561
return
5662
}
5763

pkg/location/k8s.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func init() {
4848
}
4949
}
5050

51-
func (k8s K8s) Write(serviceAccountName, keyID, key string, creds cred.Credentials) (updated UpdatedLocation, err error) {
51+
func (k8s K8s) Write(serviceAccountName string, keyWrapper KeyWrapper, creds cred.Credentials) (updated UpdatedLocation, err error) {
5252
var cluster *gkev1.Cluster
5353

5454
if cluster, err = gkeCluster(k8s.Project, k8s.Location, k8s.ClusterName); err != nil {
@@ -60,7 +60,7 @@ func (k8s K8s) Write(serviceAccountName, keyID, key string, creds cred.Credentia
6060
return
6161
}
6262

63-
if _, err = updateK8sSecret(k8s.SecretName, k8s.DataName, k8s.Namespace, key, k8sClient); err != nil {
63+
if _, err = updateK8sSecret(k8s.SecretName, k8s.DataName, k8s.Namespace, keyWrapper.Key, k8sClient); err != nil {
6464
return
6565
}
6666

pkg/location/keywriter.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ import "github.com/ovotech/cloud-key-rotator/pkg/cred"
55

66
//KeyWriter interface
77
type KeyWriter interface {
8-
Write(serviceAccountName, keyID, key string, creds cred.Credentials) (UpdatedLocation, error)
8+
Write(serviceAccountName string, keyWrapper KeyWrapper, creds cred.Credentials) (UpdatedLocation, error)
99
}

pkg/location/locations.go

+7
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,10 @@ type UpdatedLocation struct {
66
LocationURI string
77
LocationIDs []string
88
}
9+
10+
//KeyWrapper type
11+
type KeyWrapper struct {
12+
Key string
13+
KeyID string
14+
KeyProvider string
15+
}

pkg/rotate/rotatekeys.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ func rotateKey(account string, rotationCandidate rotationCandidate, creds cred.C
105105
if newKeyID, newKey, err = createKey(account, key, keyProvider); err != nil {
106106
return
107107
}
108-
if err = updateKeyLocation(account, rotationCandidate.keyLocation, newKeyID, newKey, keyProvider, creds); err != nil {
108+
keyWrapper := location.KeyWrapper{Key: newKey, KeyID: newKeyID, KeyProvider: keyProvider}
109+
if err = updateKeyLocation(account, rotationCandidate.keyLocation, keyWrapper, creds); err != nil {
109110
return
110111
}
111112
return deleteKey(account, key, keyProvider)
@@ -237,7 +238,8 @@ func locationsToUpdate(keyLocation config.KeyLocations) (kws []location.KeyWrite
237238
}
238239

239240
//updateKeyLocation updates locations specified in keyLocations with the new key, e.g. GitHub, CircleCI an K8s
240-
func updateKeyLocation(account string, keyLocations config.KeyLocations, keyID, key, keyProvider string, creds cred.Credentials) (err error) {
241+
func updateKeyLocation(account string, keyLocations config.KeyLocations,
242+
keyWrapper location.KeyWrapper, creds cred.Credentials) (err error) {
241243

242244
// update locations
243245
var updatedLocations []location.UpdatedLocation
@@ -246,7 +248,7 @@ func updateKeyLocation(account string, keyLocations config.KeyLocations, keyID,
246248

247249
var updated location.UpdatedLocation
248250

249-
if updated, err = locationToUpdate.Write(keyLocations.ServiceAccountName, keyID, key, creds); err != nil {
251+
if updated, err = locationToUpdate.Write(keyLocations.ServiceAccountName, keyWrapper, creds); err != nil {
250252
return
251253
}
252254

@@ -255,9 +257,9 @@ func updateKeyLocation(account string, keyLocations config.KeyLocations, keyID,
255257

256258
// all done
257259
logger.Infow("Key locations updated",
258-
"keyProvider", keyProvider,
260+
"keyProvider", keyWrapper.KeyProvider,
259261
"account", account,
260-
"keyID", keyID,
262+
"keyID", keyWrapper.KeyID,
261263
"keyLocationUpdates", updatedLocations)
262264

263265
return

0 commit comments

Comments
 (0)