Compare commits

...

2 Commits

Author SHA1 Message Date
40ce4e81a8 fix: Tests
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2024-01-06 21:30:44 +01:00
e628ec7ecf What is a CNAME? 2024-01-06 21:26:09 +01:00
3 changed files with 17 additions and 16 deletions

View File

@ -26,8 +26,10 @@ import (
func handleSubdomain(domain, cname, path, giteaUrl, defaultCsp string, giteaClient *repo.GiteaClient, w http.ResponseWriter) {
username := ""
if cname != "" {
// If we are accessed via a CNAME, then CNAME contains our <user>.<pages domain> value.
username = strings.Split(cname, ".")[0]
} else {
// If we are directly accessed, then domain contains our <user>.<pages domain> value.
username = strings.Split(domain, ".")[0]
}

View File

@ -42,10 +42,6 @@ func makeCSPCacheKey(username, repositoryName string) string {
return username + ":" + repositoryName
}
// / Try to find the repository with name @reponame of the user @username. If @cname
// / is not "", then it also verifies that the repository contains a "CNAME" with
// / the value of @cname as its content. @host, @domain, and @path are passed for
// / caching on success.
func lookupRepositoryAndCache(username, reponame, branchName, host, domain, path, cname string, giteaClient *GiteaClient) (*Repository, error) {
log.Debugf("CNAME: %s", cname)
log.Debugf("Looking up repository %s/%s", username, reponame)
@ -85,8 +81,8 @@ func lookupRepositoryAndCache(username, reponame, branchName, host, domain, path
)
log.Debugf("CNAME Content: %s", cnameContent)
if cnameContent != cname {
log.Warnf("CNAME mismatch: Repo '%s', CNAME '%s'", cnameContent, cname)
if cnameContent != host {
log.Warnf("CNAME mismatch: Repo '%s', Host '%s'", cnameContent, host)
return nil, errors.New("CNAME mismatch")
}
}
@ -103,6 +99,8 @@ func lookupRepositoryAndCache(username, reponame, branchName, host, domain, path
return &repo, nil
}
// host is the domain name we're accessed from. cname is the domain that host is pointing
// if, if we're accessed via a CNAME. If not, then cname is "".
func RepoFromPath(username, host, cname, path string, giteaClient *GiteaClient) (*Repository, string, error) {
domain := host
@ -117,11 +115,11 @@ func RepoFromPath(username, host, cname, path string, giteaClient *GiteaClient)
// Allow specifying the repository name in the TXT record
reponame := ""
if cname != "" {
repoLookup, err := giteaClient.lookupRepoTXT(cname)
repoLookup, err := giteaClient.lookupRepoTXT(host)
if err == nil && repoLookup != "" {
log.Infof(
"TXT lookup for %s resulted in choosing repository %s",
cname,
host,
repoLookup,
)
reponame = repoLookup
@ -129,6 +127,7 @@ func RepoFromPath(username, host, cname, path string, giteaClient *GiteaClient)
}
pathParts := strings.Split(path, "/")
log.Debugf("reponame='%s' len(pathParts)='%d'", reponame, len(pathParts))
if reponame == "" && len(pathParts) > 1 {
log.Debugf("Trying repository %s", pathParts[0])
modifiedPath := strings.Join(pathParts[1:], "/")

View File

@ -229,9 +229,9 @@ func TestPickingRepositoryValidCNAME(t *testing.T) {
log.SetLevel(log.DebugLevel)
client := GiteaClient{
getRepository: func(username, repositoryName string) (Repository, error) {
if username == "example-user" && repositoryName == "example-user.pages.example.org" {
if username == "example-user" && repositoryName == "example-user.local" {
return Repository{
Name: "example-user.pages.example.org",
Name: "example-user.local",
}, nil
} else {
t.Fatalf("Called with unknown repository %s", repositoryName)
@ -239,14 +239,14 @@ func TestPickingRepositoryValidCNAME(t *testing.T) {
}
},
hasBranch: func(username, repositoryName, branchName string) bool {
if username == "example-user" && repositoryName == "example-user.pages.example.org" && branchName == "pages" {
if username == "example-user" && repositoryName == "example-user.local" && branchName == "pages" {
return true
}
return false
},
GetFile: func(username, repositoryName, branch, path string, since *time.Time) ([]byte, bool, error) {
if username == "example-user" && repositoryName == "example-user.pages.example.org" && branch == "pages" && path == "CNAME" {
if username == "example-user" && repositoryName == "example-user.local" && branch == "pages" && path == "CNAME" {
return []byte("example-user.local"), true, nil
}
@ -261,11 +261,11 @@ func TestPickingRepositoryValidCNAME(t *testing.T) {
},
}
repo, _, err := RepoFromPath("example-user", "example-user.pages.example.org", "example-user.local", "index.html", &client)
repo, _, err := RepoFromPath("example-user", "example-user.local", "example-user.pages.example.org", "index.html", &client)
if err != nil {
t.Fatalf("Error returned: %v", err)
}
if repo.Name != "example-user.pages.example.org" {
if repo.Name != "example-user.local" {
t.Fatalf("Invalid repository name returned: %s", repo.Name)
}
}
@ -313,7 +313,7 @@ func TestPickingRepositoryValidCNAMEWithTXTLookup(t *testing.T) {
},
}
repo, _, err := RepoFromPath("example-user", "example-user.pages.example.org", "example-user.local", "index.html", &client)
repo, _, err := RepoFromPath("example-user", "example-user.local", "example-user.pages.example.org", "index.html", &client)
if err != nil {
t.Fatalf("Error returned: %v", err)
}
@ -364,7 +364,7 @@ func TestPickingRepositoryValidCNAMEWithTXTLookupAndSubdirectory(t *testing.T) {
},
}
repo, _, err := RepoFromPath("example-user", "example-user.pages.example.org", "example-user.local", "blog/index.html", &client)
repo, _, err := RepoFromPath("example-user", "example-user.local", "example-user.pages.example.org", "blog/index.html", &client)
if err != nil {
t.Fatalf("Error returned: %v", err)
}