|
| 1 | +// Copyright Jetstack Ltd. See LICENSE for details. |
| 2 | +package audit |
| 3 | + |
| 4 | +import ( |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + |
| 8 | + "k8s.io/apimachinery/pkg/util/sets" |
| 9 | + genericapifilters "k8s.io/apiserver/pkg/endpoints/filters" |
| 10 | + "k8s.io/apiserver/pkg/server" |
| 11 | + genericfilters "k8s.io/apiserver/pkg/server/filters" |
| 12 | + |
| 13 | + "github.com/jetstack/kube-oidc-proxy/cmd/app/options" |
| 14 | +) |
| 15 | + |
| 16 | +type Audit struct { |
| 17 | + opts *options.AuditOptions |
| 18 | + serverConfig *server.CompletedConfig |
| 19 | +} |
| 20 | + |
| 21 | +// New creates a new Audit struct to handle auditing for proxy requests. This |
| 22 | +// is mostly a wrapper for the apiserver auditing handlers to combine them with |
| 23 | +// the proxy. |
| 24 | +func New(opts *options.AuditOptions, externalAddress string, secureServingInfo *server.SecureServingInfo) (*Audit, error) { |
| 25 | + serverConfig := &server.Config{ |
| 26 | + ExternalAddress: externalAddress, |
| 27 | + SecureServing: secureServingInfo, |
| 28 | + |
| 29 | + // Default to treating watch as a long-running operation. |
| 30 | + // Generic API servers have no inherent long-running subresources. |
| 31 | + // This is so watch requests are handled correctly in the audit log. |
| 32 | + LongRunningFunc: genericfilters.BasicLongRunningRequestCheck( |
| 33 | + sets.NewString("watch"), sets.NewString()), |
| 34 | + } |
| 35 | + |
| 36 | + // We do not support dynamic auditing, so leave nil |
| 37 | + if err := opts.ApplyTo(serverConfig, nil, nil, nil, nil); err != nil { |
| 38 | + return nil, err |
| 39 | + } |
| 40 | + |
| 41 | + completed := serverConfig.Complete(nil) |
| 42 | + |
| 43 | + return &Audit{ |
| 44 | + opts: opts, |
| 45 | + serverConfig: &completed, |
| 46 | + }, nil |
| 47 | +} |
| 48 | + |
| 49 | +// Run will run the audit backend if configured. |
| 50 | +func (a *Audit) Run(stopCh <-chan struct{}) error { |
| 51 | + if a.serverConfig.AuditBackend != nil { |
| 52 | + if err := a.serverConfig.AuditBackend.Run(stopCh); err != nil { |
| 53 | + return fmt.Errorf("failed to run the audit backend: %s", err) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return nil |
| 58 | +} |
| 59 | + |
| 60 | +// Shutdown will shutdown the audit backend if configured. |
| 61 | +func (a *Audit) Shutdown() error { |
| 62 | + if a.serverConfig.AuditBackend != nil { |
| 63 | + a.serverConfig.AuditBackend.Shutdown() |
| 64 | + } |
| 65 | + |
| 66 | + return nil |
| 67 | +} |
| 68 | + |
| 69 | +// WithRequest will wrap the given handler to inject the request information |
| 70 | +// into the context which is then used by the wrapped audit handler. |
| 71 | +func (a *Audit) WithRequest(handler http.Handler) http.Handler { |
| 72 | + handler = genericapifilters.WithAudit(handler, a.serverConfig.AuditBackend, a.serverConfig.AuditPolicyChecker, a.serverConfig.LongRunningFunc) |
| 73 | + return genericapifilters.WithRequestInfo(handler, a.serverConfig.RequestInfoResolver) |
| 74 | +} |
| 75 | + |
| 76 | +// WithUnauthorized will wrap the given handler to inject the request |
| 77 | +// information into the context which is then used by the wrapped audit |
| 78 | +// handler. |
| 79 | +func (a *Audit) WithUnauthorized(handler http.Handler) http.Handler { |
| 80 | + handler = genericapifilters.WithFailedAuthenticationAudit(handler, a.serverConfig.AuditBackend, a.serverConfig.AuditPolicyChecker) |
| 81 | + return genericapifilters.WithRequestInfo(handler, a.serverConfig.RequestInfoResolver) |
| 82 | +} |
0 commit comments