Skip to content

Commit 1f3ea07

Browse files
authored
repo allow list (#1636)
* repo allow list for gitlab
1 parent de53657 commit 1f3ea07

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

backend/utils/allowlist.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package utils
2+
3+
import (
4+
"github.com/samber/lo"
5+
"log"
6+
"net/url"
7+
"os"
8+
"strings"
9+
)
10+
11+
func contains(slice []string, item string) bool {
12+
for _, s := range slice {
13+
if s == item {
14+
return true
15+
}
16+
}
17+
return false
18+
}
19+
20+
func ExtractCleanRepoName(gitlabURL string) (string, error) {
21+
// Parse the URL
22+
parsedURL, err := url.Parse(gitlabURL)
23+
if err != nil {
24+
return "", err
25+
}
26+
27+
// The repository name is typically the last part of the path
28+
// We use path.Base to handle cases where there might be a trailing slash
29+
repoName := parsedURL.Hostname() + parsedURL.Path
30+
31+
// If the URL ends with .git, remove it
32+
repoName = strings.TrimSuffix(repoName, ".git")
33+
34+
return repoName, nil
35+
}
36+
37+
func IsInRepoAllowList(repoUrl string) bool {
38+
allowList := os.Getenv("DIGGER_REPO_ALLOW_LIST")
39+
allowedReposUrls := strings.Split(allowList, ",")
40+
// gitlab.com/diggerhq/test
41+
// https://gitlab.com/diggerhq/test
42+
43+
repoName, err := ExtractCleanRepoName(repoUrl)
44+
if err != nil {
45+
log.Printf("warning could not parse url: %v", repoUrl)
46+
}
47+
48+
exists := lo.Contains(allowedReposUrls, repoName)
49+
50+
return exists
51+
52+
}

backend/utils/allowlist_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package utils
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"os"
6+
"testing"
7+
)
8+
9+
func TestExtractRepoName(t *testing.T) {
10+
url := "http://gitlab.com/mike/dispora.git"
11+
repoName, _ := ExtractCleanRepoName(url)
12+
assert.Equal(t, "gitlab.com/mike/dispora", repoName)
13+
14+
url = "http://git.mydomain.com/mike/dispora.git"
15+
repoName, _ = ExtractCleanRepoName(url)
16+
assert.Equal(t, "git.mydomain.com/mike/dispora", repoName)
17+
}
18+
19+
func TestRepoAllowList(t *testing.T) {
20+
os.Setenv("DIGGER_REPO_ALLOW_LIST", "gitlab.com/diggerdev/digger-demo,gitlab.com/diggerdev/alsoallowed")
21+
url := "http://gitlab.com/mike/dispora.git"
22+
allowed := IsInRepoAllowList(url)
23+
assert.False(t, allowed)
24+
25+
url = "http://gitlab.com/diggerdev/digger-demo2.git"
26+
allowed = IsInRepoAllowList(url)
27+
assert.False(t, allowed)
28+
29+
url = "http://gitlab.com/diggerdev/digger-demo.git"
30+
allowed = IsInRepoAllowList(url)
31+
assert.True(t, allowed)
32+
33+
}

ee/backend/controllers/gitlab.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ func (d DiggerEEController) GitlabWebHookHandler(c *gin.Context) {
6767

6868
log.Printf("gitlab event type: %v\n", reflect.TypeOf(event))
6969

70+
repoUrl := GetGitlabRepoUrl(event)
71+
if !utils.IsInRepoAllowList(repoUrl) {
72+
log.Printf("repo: '%v' is not in allow list, ignoring ...", repoUrl)
73+
return
74+
}
75+
7076
switch event := event.(type) {
7177
case *gitlab.MergeCommentEvent:
7278
log.Printf("IssueCommentEvent, action: %v \n", event.ObjectAttributes.Description)
@@ -100,6 +106,21 @@ func (d DiggerEEController) GitlabWebHookHandler(c *gin.Context) {
100106
c.JSON(200, "ok")
101107
}
102108

109+
func GetGitlabRepoUrl(event interface{}) string {
110+
var repoUrl = ""
111+
switch event := event.(type) {
112+
case *gitlab.MergeCommentEvent:
113+
repoUrl = event.Project.GitHTTPURL
114+
case *gitlab.MergeEvent:
115+
repoUrl = event.Project.GitHTTPURL
116+
case *gitlab.PushEvent:
117+
repoUrl = event.Project.GitHTTPURL
118+
default:
119+
log.Printf("Unhandled event, event type %v", reflect.TypeOf(event))
120+
}
121+
return repoUrl
122+
}
123+
103124
func handlePullRequestEvent(gitlabProvider utils.GitlabProvider, payload *gitlab.MergeEvent, ciBackendProvider ci_backends.CiBackendProvider, organisationId uint) error {
104125
projectId := payload.Project.ID
105126
repoFullName := payload.Project.PathWithNamespace

0 commit comments

Comments
 (0)