Skip to content

Commit

Permalink
Merge pull request #46 from XenitAB/case-insensitive-take-2
Browse files Browse the repository at this point in the history
Path matching is now case-insensitive
  • Loading branch information
bittrance authored Mar 8, 2022
2 parents e3fdce6 + d21235c commit e502d8a
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ repository which is does not have access to.
The proxy reads its configuration from a JSON file. It contains a list of repositories that can be accessed through the proxy and the Kubernetes namespaces which should receive a Secret.

When using Azure DevOps a [PAT](https://docs.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=preview-page) has to be
configured for Git Auth Proxy to append to authorized requests.
configured for Git Auth Proxy to append to authorized requests. Note that organization and repository names are matched case-insensitive.

```json
{
Expand Down
2 changes: 1 addition & 1 deletion e2e/e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ PID=$!
sleep 2
TOKEN=$(kubectl -n tenant-1 get secret org-proj-repo --template={{.data.token}} | base64 -d -w 0)

STATUS=$(curl -s -o /dev/null -w "%{http_code}" -u username:$TOKEN http://localhost:8080/org/proj/_apis/git/repositories/repo)
STATUS=$(curl -s -o /dev/null -w "%{http_code}" -u username:$TOKEN http://localhost:8080/Org/proj/_apis/git/repositories/repo)
if [ $STATUS != "200" ]; then
exit 1
fi
Expand Down
6 changes: 3 additions & 3 deletions pkg/auth/azure_devops.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ func newAzureDevops(pat string) *azureDevops {
}

func (a *azureDevops) getPathRegex(organization, project, repository string) ([]*regexp.Regexp, error) {
baseApi, err := regexp.Compile(fmt.Sprintf(`/%s/_apis\b`, organization))
baseApi, err := regexp.Compile(fmt.Sprintf(`(?i)/%s/_apis\b`, organization))
if err != nil {
return nil, fmt.Errorf("invalid base api regex: %w", err)
}
git, err := regexp.Compile(fmt.Sprintf(`/%s/%s/_git/%s(/.*)?\b`, organization, project, repository))
git, err := regexp.Compile(fmt.Sprintf(`(?i)/%s/%s/_git/%s(/.*)?\b`, organization, project, repository))
if err != nil {
return nil, err
}
api, err := regexp.Compile(fmt.Sprintf(`/%s/%s/_apis/git/repositories/%s(/.*)?\b`, organization, project, repository))
api, err := regexp.Compile(fmt.Sprintf(`(?i)/%s/%s/_apis/git/repositories/%s(/.*)?\b`, organization, project, repository))
if err != nil {
return nil, err
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/auth/azure_devops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ func TestAzureDevOpsPermitted(t *testing.T) {
require.NoError(t, err, "token should be permitted")
}

func TestAzureDevOpsPermittedCaseInsensitive(t *testing.T) {
authz := getAzureDevOpsAuthorizer()
endpoint, err := authz.GetEndpointById("foo-org-proj-repo")
require.NoError(t, err)
path := "/Org/proJ/_git/repo"
err = authz.IsPermitted(path, endpoint.Token)
require.NoError(t, err, "token should be permitted")
}

func TestAzureDevOpsPermittedExtraPath(t *testing.T) {
authz := getAzureDevOpsAuthorizer()
endpoint, err := authz.GetEndpointById("foo-org-proj-repo")
Expand Down
4 changes: 2 additions & 2 deletions pkg/auth/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ func newGithub(appID, installationID int64, privateKey []byte) (*github, error)
}

func (g *github) getPathRegex(organization, project, repository string) ([]*regexp.Regexp, error) {
git, err := regexp.Compile(fmt.Sprintf(`/%s/%s(/.*)?\b`, organization, repository))
git, err := regexp.Compile(fmt.Sprintf(`(?i)/%s/%s(/.*)?\b`, organization, repository))
if err != nil {
return nil, err
}
api, err := regexp.Compile(fmt.Sprintf(`/api/v3/(.*)/%s/%s/(/.*)?\b`, organization, repository))
api, err := regexp.Compile(fmt.Sprintf(`(?i)/api/v3/(.*)/%s/%s/(/.*)?\b`, organization, repository))
if err != nil {
return nil, err
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/auth/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ func TestGitHubAuthorization(t *testing.T) {
path: "/org/repo",
allow: true,
},
{
name: "allow repo",
path: "/Org/repO",
allow: true,
},
{
name: "allow api",
path: "/api/v3/org/repo",
Expand Down

0 comments on commit e502d8a

Please sign in to comment.