rio/internal/repo/repo.go

154 lines
3.5 KiB
Go
Raw Normal View History

2024-01-01 13:19:19 +00:00
package repo
2023-12-31 12:26:56 +00:00
import (
"errors"
"strings"
"time"
2024-01-01 13:19:19 +00:00
"git.polynom.me/rio/internal/dns"
"git.polynom.me/rio/internal/pages"
2023-12-31 12:26:56 +00:00
"code.gitea.io/sdk/gitea"
"github.com/patrickmn/go-cache"
log "github.com/sirupsen/logrus"
)
var (
2023-12-31 23:38:39 +00:00
pathCache = cache.New(1*time.Hour, 1*time.Hour)
// Caching the existence of an user
userCache = cache.New(24*time.Hour, 12*time.Hour)
2023-12-31 12:26:56 +00:00
)
type PageCacheEntry struct {
Repository *gitea.Repository
2023-12-31 23:38:39 +00:00
Path string
2023-12-31 12:26:56 +00:00
}
func makePageCacheKey(domain, path string) string {
return domain + "/" + path
}
2023-12-31 23:38:39 +00:00
// / 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.
2023-12-31 12:26:56 +00:00
func lookupRepositoryAndCache(username, reponame, host, domain, path, cname string, giteaClient *gitea.Client) (*gitea.Repository, error) {
log.Debugf("Looking up repository %s/%s", username, reponame)
repo, _, err := giteaClient.GetRepo(username, reponame)
if err != nil {
return nil, err
}
// Check if the CNAME file matches
if cname != "" {
file, _, err := giteaClient.GetFile(
username,
repo.Name,
2024-01-01 13:19:19 +00:00
pages.PagesBranch,
2023-12-31 12:26:56 +00:00
"CNAME",
false,
)
if err != nil {
log.Errorf("Could not verify CNAME of %s/%s: %v\n", username, repo.Name, err)
return nil, err
}
cnameContent := strings.Trim(
string(file[:]),
"\n",
)
if cnameContent != cname {
return nil, errors.New("CNAME mismatch")
}
}
// Cache data
pathCache.Set(
makePageCacheKey(domain, path),
PageCacheEntry{
repo,
path,
},
cache.DefaultExpiration,
)
return repo, nil
}
func RepoFromPath(username, host, cname, path string, giteaClient *gitea.Client) (*gitea.Repository, string, error) {
domain := host
// Guess the repository
key := makePageCacheKey(domain, path)
entry, found := pathCache.Get(key)
if found {
pageEntry := entry.(PageCacheEntry)
return pageEntry.Repository, pageEntry.Path, nil
}
pathParts := strings.Split(path, "/")
if len(pathParts) > 1 {
log.Debugf("Trying repository %s", pathParts[0])
modifiedPath := strings.Join(pathParts[1:], "/")
repo, err := lookupRepositoryAndCache(
username,
pathParts[0],
host,
domain,
modifiedPath,
cname,
giteaClient,
)
if err == nil {
return repo, modifiedPath, nil
}
}
2023-12-31 21:41:51 +00:00
// Allow specifying the repository name in the TXT record
reponame := domain
2023-12-31 21:41:51 +00:00
lookupDomain := domain
if cname != "" {
2023-12-31 21:41:51 +00:00
lookupDomain = cname
}
2024-01-01 13:19:19 +00:00
repoLookup, err := dns.LookupRepoTXT(lookupDomain)
2023-12-31 21:55:37 +00:00
if err != nil && repoLookup != "" {
2023-12-31 21:41:51 +00:00
log.Infof(
"TXT lookup for %s resulted in choosing repository %s",
lookupDomain,
repoLookup,
)
reponame = repoLookup
2023-12-31 23:38:39 +00:00
} else if cname != "" {
2023-12-31 21:55:37 +00:00
// Allow naming the repository "example.org" (But give the TXT record preference)
2023-12-31 23:38:39 +00:00
reponame = cname
}
log.Debugf("Trying repository %s/%s", username, reponame)
2023-12-31 12:26:56 +00:00
repo, err := lookupRepositoryAndCache(
username,
reponame,
2023-12-31 12:26:56 +00:00
host,
domain,
path,
cname,
giteaClient,
)
return repo, path, err
}
// Checks if the username exists as an organisation or an user on the Gitea
// instance, so that an attacker can't just request certificates for random
// usernames.
func CanRequestCertificate(username string, giteaClient *gitea.Client) bool {
if _, found := userCache.Get(username); found {
return true
}
user, _, err := giteaClient.GetUserInfo(username)
if user != nil && err == nil {
userCache.Set(username, true, cache.DefaultExpiration)
return true
}
return false
}