-
Notifications
You must be signed in to change notification settings - Fork 4
/
certauth.go
264 lines (224 loc) · 7.74 KB
/
certauth.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 certauth
import (
"bytes"
"context"
"crypto/x509"
"errors"
"net/http"
"github.com/julienschmidt/httprouter"
)
// These shenanigans are here to ensure we have strings on our context keys, and they are unique to our package
type contextKey string
func (c contextKey) String() string {
return "certauth context " + string(c)
}
const (
//HasAuthorizedOU is used as the request context key, adding info about the authorized OU if authorization succeded
HasAuthorizedOU = contextKey("Has Authorized OU")
//HasAuthorizedCN is used as the request context key, adding info about the authroized CN if authorization succeeded
HasAuthorizedCN = contextKey("Has Authorized CN")
)
// TODO:(jnelson) Maybe a standardValidation method for our stuff? Thu May 14 18:41:41 2015
// Current Auth methods:
// see panthon/auth.py
// - /:endpoint/bindings
// - Standard OU validation (titan)
// - OU:endpoint & CN:matches endpoint parram.
//
// - /bindings/:id
// - Standard OU validation (titan)
// - OU:endpoint & CN:matches endpoint parram. (ygg knows what EP has that ID)
// - OU:site & CN:matches SiteID
// **DEPRECATED** use New with AuthOptions instead
// Options is the configuration for a Auth handler
type Options struct {
// AllowedOUs is an exact string match against the Client Certs OU's
// This gets injected into AuthorizationCheckers using AllowOUsandCNs.
AllowedOUs []string
// AllowedCNs is an exact string match against the Client Certs CN
// This gets injected into AuthorizationCheckers using AllowOUsandCNs.
AllowedCNs []string
// Performs Authorization checks
// Each check validates that the client is authorized to the requested resource.
// See documentation for AuthorizationChecker for details on the checks.
AuthorizationCheckers []AuthorizationChecker
// Populate Headers with auth info
SetReqHeaders bool
// Default handler
AuthErrorHandler http.HandlerFunc
}
// Auth is an instance of the middleware
type Auth struct {
opt Options // **DEPRECATED**
// lists of checkers: auth if any list passes, a list passes if all checkers in the list pass
checkers [][]AuthorizationChecker
setHeaders bool
errorHandler http.Handler
}
// AuthOption is a type of function for configuring an Auth
type AuthOption func(*Auth)
// WithCheckers configures an Auth with the given checkers so that the Auth will pass when all the
// checkers in any WithCheckers AuthOption pass.
// eg: New(WithCheckers(A), WithCheckers(B,C)) will pass on `A || (B && C)`
func WithCheckers(checkers ...AuthorizationChecker) AuthOption {
return func(a *Auth) {
a.checkers = append(a.checkers, checkers)
}
}
func WithHeaders() AuthOption {
return func(a *Auth) {
a.setHeaders = true
}
}
func WithErrorHandler(handler http.Handler) AuthOption {
return func(a *Auth) {
a.errorHandler = handler
}
}
func New(opts ...AuthOption) *Auth {
a := &Auth{
errorHandler: http.HandlerFunc(defaultAuthErrorHandler),
}
for _, opt := range opts {
opt(a)
}
return a
}
// **DEPRECATED** use New instead
// NewAuth returns an auth
func NewAuth(opts ...Options) *Auth {
o := Options{}
if len(opts) != 0 {
o = opts[0]
}
h := defaultAuthErrorHandler
if o.AuthErrorHandler != nil {
h = o.AuthErrorHandler
}
if len(o.AllowedOUs) > 0 || len(o.AllowedCNs) > 0 {
o.AuthorizationCheckers = append(
o.AuthorizationCheckers,
AllowOUsandCNs(o.AllowedOUs, o.AllowedCNs),
)
}
return &Auth{
opt: o,
errorHandler: http.HandlerFunc(h),
checkers: [][]AuthorizationChecker{o.AuthorizationCheckers},
}
}
func defaultAuthErrorHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Authentication Failed", http.StatusForbidden)
}
// Handler implements the http.HandlerFunc for integration with the standard net/http lib.
func (a *Auth) Handler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Let secure process the request. If it returns an error,
// that indicates the request should not continue.
var err error
if r, err = a.Process(w, r); err != nil {
// if process returned an error request should not continue
return
}
h.ServeHTTP(w, r)
})
}
// RouterHandler implements the httprouter.Handle for integration with github.com/julienschmidt/httprouter
func (a *Auth) RouterHandler(h httprouter.Handle) httprouter.Handle {
return httprouter.Handle(func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// Let secure process the request. If it returns an error,
// that indicates the request should not continue.
var err error
if r, err = a.ProcessWithParams(w, r, ps); err != nil {
return
}
h(w, r, ps)
})
}
// Process validates a request and sets context parameters according to the
// configured AuthorizationCheckers.
// Returns an http.Request with additional context values applied, or an error if
// something went wrong.
// In practice, this just calls ProcessWithParams.
func (a *Auth) Process(w http.ResponseWriter, r *http.Request) (*http.Request, error) {
return a.ProcessWithParams(w, r, nil)
}
// ProcessWithParams validates a request and sets context parameters according to the
// configured AuthorizationCheckers.
// Returns an http.Request with additional context values applied, or an error if
// something went wrong.
func (a *Auth) ProcessWithParams(
w http.ResponseWriter, r *http.Request, ps httprouter.Params,
) (*http.Request, error) {
if err := a.ValidateRequest(r); err != nil {
return nil, err
}
ctxParams, err := a.CheckAuthorization(r.TLS.VerifiedChains[0][0], ps)
if err != nil {
a.errorHandler.ServeHTTP(w, r)
return nil, err
}
if len(ctxParams) == 0 {
// No need to update the context; just return the one we already have
return r, nil
}
// Prepare a new context with the additional values
ctx := r.Context()
for k, v := range ctxParams {
ctx = context.WithValue(ctx, k, v)
}
// Replace the context on the request object
return r.WithContext(ctx), nil
}
// ValidateRequest performs verification on the TLS certs and chain
func (a *Auth) ValidateRequest(r *http.Request) error {
// ensure we can process this request
if r.TLS == nil || r.TLS.VerifiedChains == nil {
return errors.New("no cert chain detected")
}
// TODO: Figure out if having multiple validated peer leaf certs is possible. For now, only validate
// one cert, and make sure it matches the first peer certificate
if r.TLS.PeerCertificates != nil {
if !bytes.Equal(r.TLS.PeerCertificates[0].Raw, r.TLS.VerifiedChains[0][0].Raw) {
return errors.New("first peer certificate not first verified chain leaf")
}
}
return nil
}
// CheckAuthorization runs each of the AuthorizationCheckers configured for the server
// and returns an error if any of them return False.
// See the documentation for AuthorizationChecker for more details.
func (a *Auth) CheckAuthorization(
verifiedCert *x509.Certificate, ps httprouter.Params,
) (map[ContextKey]ContextValue, error) {
ou := verifiedCert.Subject.OrganizationalUnit
cn := verifiedCert.Subject.CommonName
ctxParams := make(map[ContextKey]ContextValue)
var (
params map[ContextKey]ContextValue
err error
)
for _, cks := range a.checkers { // trying all the groups of checkers
for _, ck := range cks { // each checker in a group
if ps == nil { // not using httprouter
params, err = ck.CheckAuthorization(ou, cn)
} else { // using httprouter
params, err = ck.CheckAuthorizationWithParams(ou, cn, ps)
}
if err != nil { // stop trying checkers in this group if one fails
break
}
// Collect the context params from each AuthorizationChecker into one map
if params != nil {
for k, v := range params {
ctxParams[k] = v
}
}
}
// non-nil when a group doesn't pass, so nil means a group passed, so we're done
if err == nil {
break
}
}
return ctxParams, err
}