Skip to content

Commit dd3352b

Browse files
committed
Add org mapping
Signed-off-by: Tamal Saha <tamal@appscode.com>
1 parent 1c7d5dc commit dd3352b

File tree

4 files changed

+31
-20
lines changed

4 files changed

+31
-20
lines changed

main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func main() {
2727
logger := logging.FromContext(context.Background())
2828

2929
if !opts.EnableSSL {
30-
http.Handle("/", redirect.New())
30+
http.Handle("/", redirect.New(opts))
3131

3232
logger.Infof("Listening on port %d", opts.Port)
3333
logger.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", opts.Port), nil))
@@ -45,7 +45,7 @@ func main() {
4545
}
4646
server := &http.Server{
4747
Addr: ":https",
48-
Handler: redirect.New(),
48+
Handler: redirect.New(opts),
4949
ReadTimeout: 5 * time.Second,
5050
WriteTimeout: 5 * time.Second,
5151
IdleTimeout: 120 * time.Second,

pkg/redirect/options.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import (
55
)
66

77
type Options struct {
8-
CertDir string
9-
CertEmail string
10-
Hosts []string
11-
Port int
12-
EnableSSL bool
8+
CertDir string
9+
CertEmail string
10+
Hosts []string
11+
Port int
12+
EnableSSL bool
13+
OrgMapping map[string]string
1314
}
1415

1516
func NewOptions() *Options {
@@ -18,6 +19,17 @@ func NewOptions() *Options {
1819
CertEmail: "tamal@appscode.com",
1920
Hosts: []string{"r.appscode.com", "r.appscode.ninja", "r.byte.builders"},
2021
Port: 8080,
22+
OrgMapping: map[string]string{
23+
"appscode": "appscode",
24+
"charts": "appscode-charts",
25+
"kubedb": "kubedb",
26+
"kubeform": "kubeform",
27+
"kubestash": "kubestash",
28+
"kubevault": "kubevault",
29+
"library": "appscode-images",
30+
"stash": "stashed",
31+
"voyager": "voyagermesh",
32+
},
2133
}
2234
}
2335

@@ -27,4 +39,5 @@ func (s *Options) AddFlags(fs *pflag.FlagSet) {
2739
fs.StringSliceVar(&s.Hosts, "ssl.hosts", s.Hosts, "Hosts for which certificate will be issued")
2840
fs.IntVar(&s.Port, "port", s.Port, "Port used when SSL is not enabled")
2941
fs.BoolVar(&s.EnableSSL, "ssl", s.EnableSSL, "Set true to enable SSL via Let's Encrypt")
42+
fs.StringToStringVar(&s.OrgMapping, "org-mapping", s.OrgMapping, "Proxy org to GitHub org mappings")
3043
}

pkg/redirect/redirect.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ import (
1616
"knative.dev/pkg/logging"
1717
)
1818

19-
var orgMappings = map[string]string{
20-
"appscode": "appscode-images",
21-
"kubedb": "kubedb-images",
22-
}
19+
var opts *Options
2320

2421
func redact(in http.Header) http.Header {
2522
h := in.Clone()
@@ -29,7 +26,8 @@ func redact(in http.Header) http.Header {
2926
return h
3027
}
3128

32-
func New() http.Handler {
29+
func New(o *Options) http.Handler {
30+
opts = o
3331
router := mux.NewRouter()
3432

3533
router.Use(func(next http.Handler) http.Handler {
@@ -114,7 +112,7 @@ func token(resp http.ResponseWriter, req *http.Request) {
114112

115113
vals := req.URL.Query()
116114
scope := vals.Get("scope")
117-
for orgKey, ghOrg := range orgMappings {
115+
for orgKey, ghOrg := range opts.OrgMapping {
118116
if strings.HasPrefix(scope, "repository:"+orgKey+"/") {
119117
scope = strings.Replace(scope, "repository:"+orgKey+"/", "repository:"+ghOrg+"/", 1)
120118
break
@@ -166,7 +164,7 @@ func proxy(resp http.ResponseWriter, req *http.Request) {
166164
repo := mux.Vars(req)["repo"]
167165
rest := mux.Vars(req)["rest"]
168166

169-
url := fmt.Sprintf("https://ghcr.io/v2/%s/%s/%s", orgMappings[org], repo, rest)
167+
url := fmt.Sprintf("https://ghcr.io/v2/%s/%s/%s", opts.OrgMapping[org], repo, rest)
170168
if query := req.URL.Query().Encode(); query != "" {
171169
url += "?" + query
172170
}
@@ -221,7 +219,7 @@ func proxy(resp http.ResponseWriter, req *http.Request) {
221219
link := back.Header.Get("Link")
222220
if link != "" {
223221
rewrittenLink := link
224-
for orgKey, ghOrg := range orgMappings {
222+
for orgKey, ghOrg := range opts.OrgMapping {
225223
if strings.HasPrefix(link, "/v2/"+ghOrg+"/") {
226224
rewrittenLink = strings.Replace(link, "/v2/"+ghOrg+"/", "/v2/"+orgKey+"/", 1)
227225
break
@@ -240,7 +238,7 @@ func proxy(resp http.ResponseWriter, req *http.Request) {
240238
http.Error(resp, err.Error(), http.StatusInternalServerError)
241239
return
242240
}
243-
for _, ghOrg := range orgMappings {
241+
for _, ghOrg := range opts.OrgMapping {
244242
if strings.HasPrefix(lr.Name, ghOrg+"/") {
245243
lr.Name = strings.TrimPrefix(lr.Name, ghOrg+"/")
246244
break
@@ -279,7 +277,7 @@ func ghpage(resp http.ResponseWriter, req *http.Request) {
279277
logger := logging.FromContext(ctx)
280278

281279
url := req.URL.String()
282-
for orgKey, ghOrg := range orgMappings {
280+
for orgKey, ghOrg := range opts.OrgMapping {
283281
if req.URL.Path == "/"+orgKey || strings.HasPrefix(req.URL.Path, "/"+orgKey+"/") {
284282
url = fmt.Sprintf("https://ghcr.io%s", strings.Replace(req.URL.Path, "/"+orgKey, "/"+ghOrg, 1))
285283
break

pkg/redirect/redirect_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
)
1919

2020
func TestRedirect(t *testing.T) {
21-
s := httptest.NewServer(redirect.New())
21+
s := httptest.NewServer(redirect.New(redirect.NewOptions()))
2222
defer s.Close()
2323

2424
reg := strings.TrimPrefix(s.URL, "http://")
@@ -44,7 +44,7 @@ func TestRedirect(t *testing.T) {
4444
}
4545

4646
func TestGHPageRedirect(t *testing.T) {
47-
s := httptest.NewServer(redirect.New())
47+
s := httptest.NewServer(redirect.New(redirect.NewOptions()))
4848

4949
for _, path := range []string{
5050
"/",
@@ -53,7 +53,7 @@ func TestGHPageRedirect(t *testing.T) {
5353
"/busybox@sha256:abcdef",
5454
} {
5555
t.Run(path, func(t *testing.T) {
56-
req, err := http.NewRequest(http.MethodGet, s.URL+"/appscode"+path, nil)
56+
req, err := http.NewRequest(http.MethodGet, s.URL+"/library"+path, nil)
5757
if err != nil {
5858
t.Fatal(err)
5959
}

0 commit comments

Comments
 (0)