-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #195 from fluxcd/rejected-branch-push
Fail push if a ref update is rejected
- Loading branch information
Showing
5 changed files
with
102 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,25 @@ | ||
package controllers | ||
|
||
import ( | ||
"context" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
"github.com/go-git/go-billy/v5/memfs" | ||
"github.com/go-git/go-git/v5" | ||
gogit "github.com/go-git/go-git/v5" | ||
"github.com/go-git/go-git/v5/plumbing" | ||
"github.com/go-git/go-git/v5/plumbing/object" | ||
"github.com/go-git/go-git/v5/storage/memory" | ||
"github.com/go-logr/logr" | ||
|
||
"github.com/fluxcd/pkg/gittestserver" | ||
"github.com/fluxcd/source-controller/pkg/git" | ||
) | ||
|
||
func populateRepoFromFixture(repo *git.Repository, fixture string) error { | ||
func populateRepoFromFixture(repo *gogit.Repository, fixture string) error { | ||
working, err := repo.Worktree() | ||
if err != nil { | ||
return err | ||
|
@@ -59,7 +64,7 @@ func populateRepoFromFixture(repo *git.Repository, fixture string) error { | |
return err | ||
} | ||
|
||
if _, err = working.Commit("Initial revision from "+fixture, &git.CommitOptions{ | ||
if _, err = working.Commit("Initial revision from "+fixture, &gogit.CommitOptions{ | ||
Author: &object.Signature{ | ||
Name: "Testbot", | ||
Email: "[email protected]", | ||
|
@@ -73,7 +78,7 @@ func populateRepoFromFixture(repo *git.Repository, fixture string) error { | |
} | ||
|
||
func TestRepoForFixture(t *testing.T) { | ||
repo, err := git.Init(memory.NewStorage(), memfs.New()) | ||
repo, err := gogit.Init(memory.NewStorage(), memfs.New()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
@@ -92,7 +97,7 @@ func TestIgnoreBrokenSymlink(t *testing.T) { | |
} | ||
defer os.RemoveAll(tmp) | ||
|
||
repo, err := git.PlainInit(tmp, false) | ||
repo, err := gogit.PlainInit(tmp, false) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
@@ -106,3 +111,72 @@ func TestIgnoreBrokenSymlink(t *testing.T) { | |
t.Fatalf("expected no changes but got: %v", err) | ||
} | ||
} | ||
|
||
// this is a hook script that will reject a ref update for a branch | ||
// that's not `main` | ||
const rejectBranch = ` | ||
if [ "$1" != "refs/heads/main" ]; then | ||
echo "*** Rejecting push to non-main branch $1" >&2 | ||
exit 1 | ||
fi | ||
` | ||
|
||
func TestPushRejected(t *testing.T) { | ||
// Check that pushing to a repository which rejects a ref update | ||
// results in an error. Why would a repo reject an update? If yu | ||
// use e.g., branch protection in GitHub, this is what happens -- | ||
// see | ||
// https://github.com/fluxcd/image-automation-controller/issues/194. | ||
|
||
branch := "push-branch" | ||
|
||
gitServer, err := gittestserver.NewTempGitServer() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
gitServer.AutoCreate() | ||
gitServer.InstallUpdateHook(rejectBranch) | ||
|
||
if err = gitServer.StartHTTP(); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// this is currently defined in update_test.go, but handy right here .. | ||
if err = initGitRepo(gitServer, "testdata/appconfig", "main", "/appconfig.git"); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
tmp, err := ioutil.TempDir("", "gotest-imageauto-git") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
repoURL := gitServer.HTTPAddress() + "/appconfig.git" | ||
repo, err := gogit.PlainClone(tmp, false, &gogit.CloneOptions{ | ||
URL: repoURL, | ||
ReferenceName: plumbing.NewBranchReferenceName("main"), | ||
}) | ||
|
||
// This is here to guard against push in general being broken | ||
err = push(context.TODO(), tmp, "main", repoAccess{ | ||
url: repoURL, | ||
auth: &git.Auth{}, | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// This is not under test, but needed for the next bit | ||
if err = switchBranch(repo, branch); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// This is supposed to fail, because the hook rejects the branch | ||
// pushed to. | ||
err = push(context.TODO(), tmp, branch, repoAccess{ | ||
url: repoURL, | ||
auth: &git.Auth{}, | ||
}) | ||
if err == nil { | ||
t.Error("push to a forbidden branch is expected to fail, but succeeded") | ||
} | ||
} |
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