-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
264 lines (227 loc) · 6.11 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package main
import (
"context"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"os/signal"
"strconv"
"syscall"
"github.com/caido/grafana-auth-proxy/pkg/extraction"
"github.com/caido/grafana-auth-proxy/pkg/identity"
"github.com/caido/grafana-auth-proxy/pkg/validation"
"github.com/joho/godotenv"
"github.com/lestrrat-go/jwx/jwk"
"github.com/urfave/cli/v2"
)
func loadJwk(c *cli.Context) (*jwk.Set, error) {
// Get JWK fetch URL
jwkUrl := c.String("jwk")
if jwkUrl == "" {
return nil, errors.New("a JWK URL is required")
}
// Fetch JWK
response, err := http.Get(jwkUrl)
if err != nil {
errorMessage := fmt.Sprintf("The HTTP request failed with error %s\n", err)
return nil, errors.New(errorMessage)
}
// Decode JWK
data, _ := ioutil.ReadAll(response.Body)
set, err := jwk.ParseString(string(data))
if err != nil {
log.Printf("Failed to parse jwk token")
return nil, err
}
return set, nil
}
func createRequestsHandler(c *cli.Context) (*RequestsHandler, error) {
// Prepare token extractor
extractors := make([]extraction.Extractor, 0, 2)
cookieName := c.String("cookie")
if c.Bool("cookieAuth") && cookieName != "" {
extractors = append(extractors, extraction.NewCookieExtractor(cookieName))
log.Printf("JWT Cookie name : %s", cookieName)
}
headerName := c.String("header")
headerPrefix := c.String("prefix")
if c.Bool("headerAuth") && headerName != "" && headerPrefix != "" {
extractors = append(extractors, extraction.NewHeaderExtractor(headerName, headerPrefix))
log.Printf("JWT Header name : %s", headerName)
log.Printf("JWT Header prefix : %s", headerPrefix)
}
if len(extractors) == 0 {
return nil, errors.New("must specify either cookie or header authentication")
}
tokenExtractor := extraction.NewTokenExtractor(extractors...)
// Prepare token validator
keys, err := loadJwk(c)
if err != nil {
return nil, err
}
algorithms := c.StringSlice("algorithms")
if len(algorithms) == 0 {
return nil, errors.New("a least one JWT algorithm is required")
}
audience := c.String("audience")
if audience == "" {
return nil, errors.New("a JWT audience is required")
}
issuer := c.String("issuer")
if issuer == "" {
return nil, errors.New("a JWT issuer is required")
}
log.Printf("JWT accepted algorithms : %v", algorithms)
log.Printf("JWT accepted audience : %s", audience)
log.Printf("JWT accepted issuer : %s", issuer)
tokenValidator := validation.NewTokenValidator(keys, algorithms, audience, issuer)
// Prepare identity provider
claimName := c.String("claim")
if claimName == "" {
return nil, errors.New("a JWT Grafana claim is required")
}
log.Printf("JWT Grafana authentication claim : %s", claimName)
identityProvider := identity.NewTokenProvider(claimName)
// Prepare requests handler
rawURL := c.String("url")
if rawURL == "" {
return nil, errors.New("an URL is required")
}
servedUrl, err := url.Parse(rawURL)
if err != nil {
return nil, err
}
log.Printf("Proxy serving : %s", servedUrl)
requestsHandler := &RequestsHandler{servedUrl, tokenExtractor, tokenValidator, identityProvider}
return requestsHandler, nil
}
func launchProxy(c *cli.Context) error {
// Build requests handler
requestsHandler, err := createRequestsHandler(c)
if err != nil {
return err
}
// Find port
port := c.Int("port")
if port == 0 {
return errors.New("a port is required")
}
log.Printf("Proxy running on port : %d", port)
// Start server
server := http.Server{Addr: ":" + strconv.Itoa(port), Handler: requestsHandler}
go func() {
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal(err)
}
}()
// Handle shutdown
select {
case <-c.Context.Done():
return server.Shutdown(c.Context)
}
}
func main() {
var err error
// Load .env file
err = godotenv.Load(".env")
if err != nil {
log.Printf("Unable to load a .env file")
}
// Build the app
app := &cli.App{
Action: launchProxy,
Flags: []cli.Flag{
&cli.IntFlag{
Name: "port",
Required: true,
Usage: "port used by the proxy",
EnvVars: []string{"PROXY_PORT"},
},
&cli.StringFlag{
Name: "url",
Required: true,
Usage: "URL served by the proxy",
EnvVars: []string{"PROXY_SERVED_URL"},
},
&cli.StringFlag{
Name: "jwk",
Required: true,
Usage: "URL to fetch the JWK from",
EnvVars: []string{"PROXY_JWK_FETCH_URL"},
},
&cli.BoolFlag{
Name: "cookieAuth",
Value: false,
Usage: "enable cookie authentication",
EnvVars: []string{"PROXY_COOKIE_AUTH"},
},
&cli.StringFlag{
Name: "cookie",
Usage: "cookie to extract token from",
EnvVars: []string{"PROXY_COOKIE"},
},
&cli.BoolFlag{
Name: "headerAuth",
Value: false,
Usage: "enable header authentication",
EnvVars: []string{"PROXY_HEADER_AUTH"},
},
&cli.StringFlag{
Name: "header",
Value: "Bearer",
Usage: "header to extract token from",
EnvVars: []string{"PROXY_HEADER"},
},
&cli.StringFlag{
Name: "prefix",
Value: "Bearer",
Usage: "header prefix to expect",
EnvVars: []string{"PROXY_HEADER_PREFIX"},
},
&cli.StringSliceFlag{
Name: "algorithms",
Usage: "JWT algorithms to accept",
Value: cli.NewStringSlice("RS256"),
EnvVars: []string{"PROXY_JWT_ALGORITHMS"},
},
&cli.StringFlag{
Name: "audience",
Required: true,
Usage: "JWT audience to accept",
EnvVars: []string{"PROXY_JWT_AUDIENCE"},
},
&cli.StringFlag{
Name: "issuer",
Required: true,
Usage: "JWT issuer to accept",
EnvVars: []string{"PROXY_JWT_ISSUER"},
},
&cli.StringFlag{
Name: "claim",
Required: true,
Usage: "JWT claim to use for Grafana authentication",
EnvVars: []string{"PROXY_JWT_GRAFANA_CLAIM"},
},
},
}
// Handle signals
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
os.Exit(1)
}()
// Run the app
ctx, cancel := context.WithCancel(context.Background())
err = app.RunContext(ctx, os.Args)
if err != nil {
log.Fatal(err)
}
// Handle graceful shutdown
<-sigs
cancel()
}