| 1 |
/* |
| 2 |
Copyright 2017 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package filters |
| 18 |
|
| 19 |
import ( |
| 20 |
"errors" |
| 21 |
"fmt" |
| 22 |
"net/http" |
| 23 |
"strings" |
| 24 |
|
| 25 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 |
utilruntime "k8s.io/apimachinery/pkg/util/runtime" |
| 27 |
auditinternal "k8s.io/apiserver/pkg/apis/audit" |
| 28 |
"k8s.io/apiserver/pkg/audit" |
| 29 |
"k8s.io/apiserver/pkg/endpoints/handlers/responsewriters" |
| 30 |
) |
| 31 |
|
| 32 |
// WithFailedAuthenticationAudit decorates a failed http.Handler used in WithAuthentication handler. |
| 33 |
// It is meant to log only failed authentication requests. |
| 34 |
func WithFailedAuthenticationAudit(failedHandler http.Handler, sink audit.Sink, policy audit.PolicyRuleEvaluator) http.Handler { |
| 35 |
if sink == nil || policy == nil { |
| 36 |
return failedHandler |
| 37 |
} |
| 38 |
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 39 |
ac, err := evaluatePolicyAndCreateAuditEvent(req, policy) |
| 40 |
if err != nil { |
| 41 |
utilruntime.HandleError(fmt.Errorf("failed to create audit event: %v", err)) |
| 42 |
responsewriters.InternalError(w, req, errors.New("failed to create audit event")) |
| 43 |
return |
| 44 |
} |
| 45 |
|
| 46 |
if !ac.Enabled() { |
| 47 |
failedHandler.ServeHTTP(w, req) |
| 48 |
return |
| 49 |
} |
| 50 |
ev := &ac.Event |
| 51 |
|
| 52 |
ev.ResponseStatus = &metav1.Status{} |
| 53 |
ev.ResponseStatus.Message = getAuthMethods(req) |
| 54 |
ev.Stage = auditinternal.StageResponseStarted |
| 55 |
|
| 56 |
rw := decorateResponseWriter(req.Context(), w, ev, sink, ac.RequestAuditConfig.OmitStages) |
| 57 |
failedHandler.ServeHTTP(rw, req) |
| 58 |
}) |
| 59 |
} |
| 60 |
|
| 61 |
func getAuthMethods(req *http.Request) string { |
| 62 |
authMethods := []string{} |
| 63 |
|
| 64 |
if _, _, ok := req.BasicAuth(); ok { |
| 65 |
authMethods = append(authMethods, "basic") |
| 66 |
} |
| 67 |
|
| 68 |
auth := strings.TrimSpace(req.Header.Get("Authorization")) |
| 69 |
parts := strings.Split(auth, " ") |
| 70 |
if len(parts) > 1 && strings.ToLower(parts[0]) == "bearer" { |
| 71 |
authMethods = append(authMethods, "bearer") |
| 72 |
} |
| 73 |
|
| 74 |
token := strings.TrimSpace(req.URL.Query().Get("access_token")) |
| 75 |
if len(token) > 0 { |
| 76 |
authMethods = append(authMethods, "access_token") |
| 77 |
} |
| 78 |
|
| 79 |
if req.TLS != nil && len(req.TLS.PeerCertificates) > 0 { |
| 80 |
authMethods = append(authMethods, "x509") |
| 81 |
} |
| 82 |
|
| 83 |
if len(authMethods) > 0 { |
| 84 |
return fmt.Sprintf("Authentication failed, attempted: %s", strings.Join(authMethods, ", ")) |
| 85 |
} |
| 86 |
return "Authentication failed, no credentials provided" |
| 87 |
} |