/
gui_redirect.go
105 lines (91 loc) · 2.88 KB
/
gui_redirect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/******************************************************************************
*
* Copyright 2020 SAP SE
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
package apicmd
import (
"net/http"
"slices"
"strings"
"github.com/gorilla/mux"
"github.com/sapcc/go-bits/httpext"
"github.com/sapcc/keppel/internal/auth"
"github.com/sapcc/keppel/internal/keppel"
)
// guiRedirecter is an api.API that implements the GUI redirect.
type guiRedirecter struct {
db *keppel.DB
urlStr string
}
// AddTo implements the api.API interface.
func (g *guiRedirecter) AddTo(r *mux.Router) {
// check if this feature is enabled
if g.urlStr == "" {
return
}
r.Methods("GET").Path("/{account:[a-z0-9-]{1,48}}/{repository:.+}").HandlerFunc(g.tryRedirectToGUI)
}
func (g *guiRedirecter) tryRedirectToGUI(w http.ResponseWriter, r *http.Request) {
// only attempt to redirect if it's a web browser doing the request
if !strings.Contains(r.Header.Get("Accept"), "text/html") {
respondNotFound(w, r)
return
}
vars := mux.Vars(r)
// do we have this account/repo?
account, err := keppel.FindAccount(g.db, vars["account"])
if err != nil || account == nil {
respondNotFound(w, r)
return
}
repoName := stripTagAndDigest(vars["repository"])
repo, err := keppel.FindRepository(g.db, repoName, *account)
if err != nil || repo == nil {
respondNotFound(w, r)
return
}
// is it publicly readable?
policies, err := account.ParseRBACPolicies()
if err != nil {
respondNotFound(w, r)
return
}
for _, policy := range policies {
if !slices.Contains(policy.Permissions, keppel.GrantsAnonymousPull) {
continue
}
ip := httpext.GetRequesterIPFor(r)
if policy.Matches(ip, repo.Name, auth.AnonymousUserIdentity.UserName()) {
// do the redirect
s := g.urlStr
s = strings.ReplaceAll(s, "%AUTH_TENANT_ID%", account.AuthTenantID)
s = strings.ReplaceAll(s, "%ACCOUNT_NAME%", account.Name)
s = strings.ReplaceAll(s, "%REPO_NAME%", repo.Name)
w.Header().Set("Location", s)
w.WriteHeader(http.StatusFound)
return
}
}
respondNotFound(w, r)
}
func stripTagAndDigest(repoRef string) string {
repoRef, _, _ = strings.Cut(repoRef, ":")
repoRef, _, _ = strings.Cut(repoRef, "@")
return repoRef
}
func respondNotFound(w http.ResponseWriter, _ *http.Request) {
http.Error(w, "404 page not found", http.StatusNotFound)
}