Skip to content

Commit f5ecdac

Browse files
author
Chris Every
authored
Merge pull request #163 from ovotech/genericise-git
Genericise Git
2 parents d4b8145 + 3aa2c3f commit f5ecdac

File tree

6 files changed

+43
-43
lines changed

6 files changed

+43
-43
lines changed

README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ The tool can update keys held in the following locations:
99

1010
* CircleCI
1111
* GCS
12-
* GitHub
12+
* Git
1313
* GoCd
1414
* K8S (GKE only)
1515
* SSM (AWS Parameter Store)
@@ -93,7 +93,7 @@ Currently, the following locations are supported:
9393
* GCS
9494
* Secrets in GKE
9595
* Files (encrypted via [mantle](https://github.com/ovotech/mantle) which
96-
integrates with KMS) in GitHub
96+
integrates with KMS) in Git
9797
* SSM (AWS Parameter Store)
9898

9999
## Rotation Process
@@ -121,7 +121,7 @@ are stored, e.g.:
121121
"AccountKeyLocations": [{
122122
"ServiceAccountName": "cloud-key-client-test",
123123
"RotationAgeThresholdMins": 60,
124-
"GitHub": {
124+
"Git": {
125125
"FilePath": "service-account.txt",
126126
"OrgRepo": "ovotech/cloud-key-rotator",
127127
"VerifyCircleCISuccess": true,
@@ -148,17 +148,17 @@ deployment has been successful after committing to a GitHub repository. If that
148148
verification isn't required, you can disable it using the `VerifyCircleCISuccess`
149149
boolean.
150150

151-
For any GitHub key location, the whole process will be aborted
151+
For any Git key location, the whole process will be aborted
152152
if there is no `KmsKey` value set. Unencrypted keys should **never** be committed
153153
to a Git repository.
154154

155155
## GPG Commit Signing
156156

157-
Commits to GitHub repositories are required to be GPG signed. In order to
157+
Commits to Git repositories are required to be GPG signed. In order to
158158
achieve this, you need to provide 4 things:
159159

160-
* `Username` of the GitHub user commits will be made on behalf of, set in config
161-
* `Email` address of GitHub user, set in config
160+
* `Username` of the Git user commits will be made on behalf of, set in config
161+
* `Email` address of Git user, set in config
162162
* `ArmouredKeyRing`, aka GPG private key, stored in `/etc/cloud-key-rotator/akr.asc`
163163
* `Passphrase` to the ArmouredKeyRing
164164

pkg/config/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ type KeyLocations struct {
7171
ServiceAccountName string
7272
CircleCI []location.CircleCI
7373
GCS []location.Gcs
74-
GitHub location.GitHub
74+
Git location.Git
7575
Gocd []location.Gocd
7676
K8s []location.K8s
7777
SSM []location.Ssm

pkg/cred/creds.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ package cred
1717
// Credentials type
1818
type Credentials struct {
1919
CircleCIAPIToken string
20-
GitHubAccount GitHubAccount
20+
GitAccount GitAccount
2121
AkrPass string
2222
KmsKey string
2323
GocdServer GocdServer
2424
}
2525

26-
// GitHubAccount type
27-
type GitHubAccount struct {
28-
GitHubAccessToken string
29-
GitName string
30-
GitEmail string
26+
// GitAccount type
27+
type GitAccount struct {
28+
GitAccessToken string
29+
GitName string
30+
GitEmail string
3131
}
3232

3333
// GocdServer type

pkg/location/github.go pkg/location/git.go

+25-25
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ import (
3131
gitHttp "gopkg.in/src-d/go-git.v4/plumbing/transport/http"
3232
)
3333

34-
//GitHub type
35-
type GitHub struct {
34+
//Git type
35+
type Git struct {
3636
Filepath string
3737
OrgRepo string
3838
VerifyCircleCISuccess bool
3939
CircleCIDeployJobName string
4040
}
4141

42-
func (gitHub GitHub) Write(serviceAccountName string, keyWrapper KeyWrapper, creds cred.Credentials) (updated UpdatedLocation, err error) {
42+
func (git Git) Write(serviceAccountName string, keyWrapper KeyWrapper, creds cred.Credentials) (updated UpdatedLocation, err error) {
4343

4444
if len(creds.KmsKey) == 0 {
4545
err = errors.New("Not updating un-encrypted new key in a Git repository. Use the" +
@@ -56,81 +56,81 @@ func (gitHub GitHub) Write(serviceAccountName string, keyWrapper KeyWrapper, cre
5656
defer os.RemoveAll(localDir)
5757

5858
var signKey *openpgp.Entity
59-
if signKey, err = crypt.CommitSignKey(creds.GitHubAccount.GitName, creds.GitHubAccount.GitEmail, creds.AkrPass); err != nil {
59+
if signKey, err = crypt.CommitSignKey(creds.GitAccount.GitName, creds.GitAccount.GitEmail, creds.AkrPass); err != nil {
6060
return
6161
}
6262

6363
var committed *object.Commit
6464
const singleLine = false
6565
const disableValidation = true
66-
if committed, err = writeKeyToRemoteGitRepo(gitHub, serviceAccountName,
66+
if committed, err = writeKeyToRemoteGitRepo(git, serviceAccountName,
6767
crypt.EncryptedServiceAccountKey(key, creds.KmsKey),
6868
localDir, signKey, creds); err != nil {
6969
return
7070
}
7171

72-
if gitHub.VerifyCircleCISuccess {
73-
err = verifyCircleCIJobSuccess(gitHub.OrgRepo,
72+
if git.VerifyCircleCISuccess {
73+
err = verifyCircleCIJobSuccess(git.OrgRepo,
7474
fmt.Sprintf("%s", committed.ID()),
75-
gitHub.CircleCIDeployJobName, creds.CircleCIAPIToken)
75+
git.CircleCIDeployJobName, creds.CircleCIAPIToken)
7676
}
7777

7878
updated = UpdatedLocation{
79-
LocationType: "GitHub",
80-
LocationURI: gitHub.OrgRepo,
81-
LocationIDs: []string{gitHub.Filepath}}
79+
LocationType: "Git",
80+
LocationURI: git.OrgRepo,
81+
LocationIDs: []string{git.Filepath}}
8282

8383
return
8484
}
8585

8686
//writeKeyToRemoteGitRepo handles the writing of the supplied key to the *remote*
87-
// Git repo defined in the GitHub struct
88-
func writeKeyToRemoteGitRepo(gitHub GitHub, serviceAccountName string,
87+
// Git repo defined in the Git struct
88+
func writeKeyToRemoteGitRepo(gitt Git, serviceAccountName string,
8989
key []byte, localDir string, signKey *openpgp.Entity, creds cred.Credentials) (committed *object.Commit, err error) {
9090
var repo *git.Repository
91-
if repo, err = cloneGitRepo(localDir, gitHub.OrgRepo,
92-
creds.GitHubAccount.GitHubAccessToken); err != nil {
91+
if repo, err = cloneGitRepo(localDir, gitt.OrgRepo,
92+
creds.GitAccount.GitAccessToken); err != nil {
9393
return
9494
}
95-
logger.Infof("Cloned git repo: %s", gitHub.OrgRepo)
95+
logger.Infof("Cloned git repo: %s", gitt.OrgRepo)
9696
var commit plumbing.Hash
97-
if commit, err = writeKeyToLocalGitRepo(gitHub, repo, key, serviceAccountName,
97+
if commit, err = writeKeyToLocalGitRepo(gitt, repo, key, serviceAccountName,
9898
localDir, signKey, creds); err != nil {
9999
return
100100
}
101101
if committed, err = repo.CommitObject(commit); err != nil {
102102
return
103103
}
104-
logger.Infof("Committed to local git repo: %s", gitHub.OrgRepo)
104+
logger.Infof("Committed to local git repo: %s", gitt.OrgRepo)
105105
if err = repo.Push(&git.PushOptions{Auth: &gitHttp.BasicAuth{
106106
Username: "abc123", // yes, this can be anything except an empty string
107-
Password: creds.GitHubAccount.GitHubAccessToken,
107+
Password: creds.GitAccount.GitAccessToken,
108108
},
109109
Progress: os.Stdout}); err != nil {
110110
return
111111
}
112-
logger.Infof("Pushed to remote git repo: %s", gitHub.OrgRepo)
112+
logger.Infof("Pushed to remote git repo: %s", gitt.OrgRepo)
113113
return
114114
}
115115

116116
//writeKeyToLocalGitRepo handles the writing of the supplied key to the *local*
117-
// Git repo defined in the GitHub struct
118-
func writeKeyToLocalGitRepo(gitHub GitHub, repo *git.Repository, key []byte,
117+
// Git repo defined in the Git struct
118+
func writeKeyToLocalGitRepo(gitt Git, repo *git.Repository, key []byte,
119119
serviceAccountName, localDir string, signKey *openpgp.Entity, creds cred.Credentials) (commmit plumbing.Hash, err error) {
120120
var w *git.Worktree
121121
if w, err = repo.Worktree(); err != nil {
122122
return
123123
}
124-
fullFilePath := localDir + "/" + gitHub.Filepath
124+
fullFilePath := localDir + "/" + gitt.Filepath
125125
if err = ioutil.WriteFile(fullFilePath, key, 0644); err != nil {
126126
return
127127
}
128128
w.Add(fullFilePath)
129129
autoStage := true
130130
return w.Commit(fmt.Sprintf("CKR updating %s", serviceAccountName), &git.CommitOptions{
131131
Author: &object.Signature{
132-
Name: creds.GitHubAccount.GitName,
133-
Email: creds.GitHubAccount.GitEmail,
132+
Name: creds.GitAccount.GitName,
133+
Email: creds.GitAccount.GitEmail,
134134
When: time.Now(),
135135
},
136136
All: autoStage,

pkg/location/keywriter.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
package location
1616

17-
//keyWriter defines the function signature for writing key to a location, e.g. CircleCI, K8S cluster or GitHub.
17+
//keyWriter defines the function signature for writing key to a location, e.g. CircleCI, K8S cluster or Git.
1818
import "github.com/ovotech/cloud-key-rotator/pkg/cred"
1919

2020
//KeyWriter interface

pkg/rotate/rotatekeys.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@ func locationsToUpdate(keyLocation config.KeyLocations) (kws []location.KeyWrite
281281
googleAppCredsRequired = true
282282
}
283283

284-
if len(keyLocation.GitHub.OrgRepo) > 0 {
285-
kws = append(kws, keyLocation.GitHub)
284+
if len(keyLocation.Git.OrgRepo) > 0 {
285+
kws = append(kws, keyLocation.Git)
286286
}
287287

288288
for _, gocd := range keyLocation.Gocd {
@@ -305,7 +305,7 @@ func locationsToUpdate(keyLocation config.KeyLocations) (kws []location.KeyWrite
305305
return
306306
}
307307

308-
//updateKeyLocation updates locations specified in keyLocations with the new key, e.g. GitHub, CircleCI and K8s
308+
//updateKeyLocation updates locations specified in keyLocations with the new key, e.g. Git, CircleCI and K8s
309309
func updateKeyLocation(account string, keyLocations config.KeyLocations,
310310
keyWrapper location.KeyWrapper, creds cred.Credentials) (err error) {
311311

0 commit comments

Comments
 (0)