-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
125 lines (110 loc) · 3.23 KB
/
main.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"fmt"
"net/http"
"github.com/distribution/distribution/v3/registry/auth"
_ "github.com/distribution/distribution/v3/registry/auth/token"
"github.com/google/go-containerregistry/pkg/name"
"github.com/jetstack/jwt-registry-auth/internal/auth/proxy"
"github.com/spf13/cobra"
)
type options struct {
ListenAddress string
Registry registryOptions
Token tokenOptions
}
type registryOptions struct {
Insecure bool
}
type tokenOptions struct {
AutoRedirect bool
Realm string
Service string
Issuer string
RootCertBundle string
}
func main() {
opts := &options{}
cmd := &cobra.Command{
Use: "registry-auth-proxy",
Short: "A reverse proxy for a container registry that offloads authentication to a Token Authentication server.",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("expected exactly one argument")
}
// Create the access controller
accessController, err := auth.GetAccessController("token", map[string]interface{}{
"autoredirect": opts.Token.AutoRedirect,
"realm": opts.Token.Realm,
"service": opts.Token.Service,
"issuer": opts.Token.Issuer,
"rootcertbundle": opts.Token.RootCertBundle,
})
if err != nil {
return fmt.Errorf("creating access controller: %w", err)
}
// Parse the registry host
var nOpts []name.Option
if opts.Registry.Insecure {
nOpts = append(nOpts, name.Insecure)
}
registry, err := name.NewRegistry(args[0], nOpts...)
if err != nil {
return fmt.Errorf("parsing registry host: %w", err)
}
// Create the proxy
pr, err := proxy.NewRegistryProxy(registry, proxy.WithAccessController(accessController))
if err != nil {
return fmt.Errorf("creating registry proxy: %w", err)
}
http.Handle("/", pr)
return http.ListenAndServe(opts.ListenAddress, nil)
},
}
cmd.Flags().StringVar(
&opts.ListenAddress,
"listen-address",
":5000",
"Listen address.",
)
cmd.Flags().BoolVar(
&opts.Registry.Insecure,
"registry-insecure",
false,
"Connect to the remote registry via HTTP.",
)
cmd.Flags().BoolVar(
&opts.Token.AutoRedirect,
"token-auto-redirect",
false,
"When set to true, realm will automatically be set using the Host header of the request as the domain and a path of /auth/token",
)
cmd.Flags().StringVar(
&opts.Token.Realm,
"token-realm",
"",
"The realm in which the registry server authenticates. This is the URL which the client is directed to.",
)
cmd.Flags().StringVar(
&opts.Token.Service,
"token-service",
"",
"The service being authenticated. Typically the hostname of the proxy.",
)
cmd.Flags().StringVar(
&opts.Token.Issuer,
"token-issuer",
"",
"The name of the token issuer. The issuer inserts this into the token so it must match the value configured for the issuer.",
)
cmd.Flags().StringVar(
&opts.Token.RootCertBundle,
"token-root-cert-bundle",
"",
"The absolute path to the root certificate bundle. This bundle contains the public part of the certificates used to sign authentication tokens.",
)
cmd.MarkFlagRequired("token-issuer")
cmd.MarkFlagRequired("token-service")
cmd.MarkFlagRequired("token-root-cert-bundle")
cmd.Execute()
}