-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- rename gh-app-key to gh-app-key-file for clarity - change git credentials writer to append a line if there is an existing .git-credentials file and in the case of the github app to replace the old github app line - removed automatically setting --write-git-creds to true when using a github app and instead requiring this is set specifically
- Loading branch information
Showing
15 changed files
with
255 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,35 +29,59 @@ func TestWriteGitCreds_WriteFile(t *testing.T) { | |
Equals(t, expContents, string(actContents)) | ||
} | ||
|
||
// Test that if the file already exists and its contents will be modified if | ||
// we write our config that we error out | ||
func TestWriteGitCreds_WillNotOverwrite(t *testing.T) { | ||
// Test that if the file already exists and it doesn't have the line we would | ||
// have written, we write it. | ||
func TestWriteGitCreds_Appends(t *testing.T) { | ||
tmp, cleanup := TempDir(t) | ||
defer cleanup() | ||
|
||
credsFile := filepath.Join(tmp, ".git-credentials") | ||
err := ioutil.WriteFile(credsFile, []byte("contents"), 0600) | ||
Ok(t, err) | ||
|
||
actErr := events.WriteGitCreds("user", "token", "hostname", tmp, logger, false) | ||
expErr := fmt.Sprintf("can't write git-credentials to %s because that file has contents that would be overwritten", tmp+"/.git-credentials") | ||
ErrEquals(t, expErr, actErr) | ||
err = events.WriteGitCreds("user", "token", "hostname", tmp, logger, false) | ||
Ok(t, err) | ||
|
||
expContents := "contents\nhttps://user:token@hostname" | ||
actContents, err := ioutil.ReadFile(filepath.Join(tmp, ".git-credentials")) | ||
Ok(t, err) | ||
Equals(t, expContents, string(actContents)) | ||
} | ||
|
||
// Test that if the file already exists and its contents will NOT be modified if | ||
// we write our config that we don't error. | ||
func TestWriteGitCreds_NoErrIfContentsSame(t *testing.T) { | ||
// Test that if the file already exists and it already has the line expected | ||
// we do nothing. | ||
func TestWriteGitCreds_NoModification(t *testing.T) { | ||
tmp, cleanup := TempDir(t) | ||
defer cleanup() | ||
|
||
credsFile := filepath.Join(tmp, ".git-credentials") | ||
contents := `https://user:token@hostname` | ||
|
||
contents := "line1\nhttps://user:token@hostname\nline2" | ||
err := ioutil.WriteFile(credsFile, []byte(contents), 0600) | ||
Ok(t, err) | ||
|
||
err = events.WriteGitCreds("user", "token", "hostname", tmp, logger, false) | ||
Ok(t, err) | ||
actContents, err := ioutil.ReadFile(filepath.Join(tmp, ".git-credentials")) | ||
Ok(t, err) | ||
Equals(t, contents, string(actContents)) | ||
} | ||
|
||
// Test that the github app credentials get replaced. | ||
func TestWriteGitCreds_ReplaceApp(t *testing.T) { | ||
tmp, cleanup := TempDir(t) | ||
defer cleanup() | ||
|
||
credsFile := filepath.Join(tmp, ".git-credentials") | ||
contents := "line1\nhttps://x-access-token:[email protected]\nline2" | ||
err := ioutil.WriteFile(credsFile, []byte(contents), 0600) | ||
Ok(t, err) | ||
|
||
err = events.WriteGitCreds("x-access-token", "token", "github.com", tmp, logger, true) | ||
Ok(t, err) | ||
expContets := "line1\nhttps://x-access-token:[email protected]\nline2" | ||
actContents, err := ioutil.ReadFile(filepath.Join(tmp, ".git-credentials")) | ||
Ok(t, err) | ||
Equals(t, expContets, string(actContents)) | ||
} | ||
|
||
// Test that if we can't read the existing file to see if the contents will be | ||
|
@@ -70,9 +94,9 @@ func TestWriteGitCreds_ErrIfCannotRead(t *testing.T) { | |
err := ioutil.WriteFile(credsFile, []byte("can't see me!"), 0000) | ||
Ok(t, err) | ||
|
||
expErr := fmt.Sprintf("trying to read %s to ensure we're not overwriting it: open %s: permission denied", credsFile, credsFile) | ||
expErr := fmt.Sprintf("open %s: permission denied", credsFile) | ||
actErr := events.WriteGitCreds("user", "token", "hostname", tmp, logger, false) | ||
ErrEquals(t, expErr, actErr) | ||
ErrContains(t, expErr, actErr) | ||
} | ||
|
||
// Test that if we can't write, we error out. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package events | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/mitchellh/go-homedir" | ||
"github.com/pkg/errors" | ||
"github.com/runatlantis/atlantis/server/events/models" | ||
"github.com/runatlantis/atlantis/server/events/vcs" | ||
"github.com/runatlantis/atlantis/server/logging" | ||
) | ||
|
||
// GithubAppWorkingDir implements WorkingDir. | ||
// It acts as a proxy to an instance of WorkingDir that refreshes the app's token | ||
// before every clone, given Github App tokens expire quickly | ||
type GithubAppWorkingDir struct { | ||
WorkingDir | ||
Credentials vcs.GithubCredentials | ||
GithubHostname string | ||
} | ||
|
||
// Clone writes a fresh token for Github App authentication | ||
func (g *GithubAppWorkingDir) Clone(log *logging.SimpleLogger, baseRepo models.Repo, headRepo models.Repo, p models.PullRequest, workspace string) (string, bool, error) { | ||
|
||
log.Info("Refreshing git tokens for Github App") | ||
|
||
token, err := g.Credentials.GetToken() | ||
if err != nil { | ||
return "", false, errors.Wrap(err, "getting github token") | ||
} | ||
|
||
home, err := homedir.Dir() | ||
if err != nil { | ||
return "", false, errors.Wrap(err, "getting home dir to write ~/.git-credentials file") | ||
} | ||
|
||
// https://developer.github.com/apps/building-github-apps/authenticating-with-github-apps/#http-based-git-access-by-an-installation | ||
if err := WriteGitCreds("x-access-token", token, g.GithubHostname, home, log, true); err != nil { | ||
return "", false, err | ||
} | ||
|
||
authURL := fmt.Sprintf("://x-access-token:%s", token) | ||
baseRepo.CloneURL = strings.Replace(baseRepo.CloneURL, "://:", authURL, 1) | ||
headRepo.CloneURL = strings.Replace(headRepo.CloneURL, "://:", authURL, 1) | ||
return g.WorkingDir.Clone(log, baseRepo, headRepo, p, workspace) | ||
} |
Oops, something went wrong.