forked from kubernetes/kubernetes
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UPSTREAM: 99237: Use the audit ID of a request for better correlation
apiserver: manage audit ID associated with a request Manage the audit ID early in the request handling logic so that it can be used by different layers to improve correlation. - If the caller does not specify a value for Audit-ID in the request header, we generate a new audit ID - If a user specified Audit-ID is too large, we truncate it - We echo the Audit-ID value to the caller via the response Header 'Audit-ID' apiserver: improve correlation by using the audit ID - when we forward the request to the aggregated server, set the audit ID in the new request header. This allows audit logs from aggregated apiservers to be correlated with the kube-apiserver. - use the audit ID in the current tracer - use the audit ID in httplog - when a request panics, log an error with the audit ID.
- Loading branch information
Showing
16 changed files
with
442 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
staging/src/k8s.io/apiserver/pkg/endpoints/filters/with_auditid.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
Copyright 2021 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package filters | ||
|
||
import ( | ||
"net/http" | ||
|
||
"k8s.io/apimachinery/pkg/types" | ||
auditinternal "k8s.io/apiserver/pkg/apis/audit" | ||
"k8s.io/apiserver/pkg/endpoints/request" | ||
|
||
"github.com/google/uuid" | ||
) | ||
|
||
// WithAuditID attaches the Audit-ID associated with a request to the context. | ||
// | ||
// a. If the caller does not specify a value for Audit-ID in the request header, we generate a new audit ID | ||
// b. We echo the Audit-ID value to the caller via the response Header 'Audit-ID'. | ||
func WithAuditID(handler http.Handler) http.Handler { | ||
return withAuditID(handler, func() string { | ||
return uuid.New().String() | ||
}) | ||
} | ||
|
||
func withAuditID(handler http.Handler, newAuditIDFunc func() string) http.Handler { | ||
if newAuditIDFunc == nil { | ||
return handler | ||
} | ||
|
||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
|
||
auditID := r.Header.Get(auditinternal.HeaderAuditID) | ||
if len(auditID) == 0 { | ||
auditID = newAuditIDFunc() | ||
} | ||
|
||
// Note: we save the user specified value of the Audit-ID header as is, no truncation is performed. | ||
r = r.WithContext(request.WithAuditID(ctx, types.UID(auditID))) | ||
|
||
// We echo the Audit-ID in to the response header. | ||
// It's not guaranteed Audit-ID http header is sent for all requests. | ||
// For example, when user run "kubectl exec", apiserver uses a proxy handler | ||
// to deal with the request, users can only get http headers returned by kubelet node. | ||
// | ||
// This filter will also be used by other aggregated api server(s). For an aggregated API | ||
// we don't want to see the same audit ID appearing more than once. | ||
if value := w.Header().Get(auditinternal.HeaderAuditID); len(value) == 0 { | ||
w.Header().Set(auditinternal.HeaderAuditID, auditID) | ||
} | ||
|
||
handler.ServeHTTP(w, r) | ||
}) | ||
} |
113 changes: 113 additions & 0 deletions
113
staging/src/k8s.io/apiserver/pkg/endpoints/filters/with_auditid_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
Copyright 2021 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package filters | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/google/uuid" | ||
"k8s.io/apiserver/pkg/endpoints/request" | ||
) | ||
|
||
func TestWithAuditID(t *testing.T) { | ||
largeAuditID := fmt.Sprintf("%s-%s", uuid.New().String(), uuid.New().String()) | ||
tests := []struct { | ||
name string | ||
newAuditIDFunc func() string | ||
auditIDSpecified string | ||
auditIDExpected string | ||
}{ | ||
{ | ||
name: "user specifies a value for Audit-ID in the request header", | ||
auditIDSpecified: "foo-bar-baz", | ||
auditIDExpected: "foo-bar-baz", | ||
}, | ||
{ | ||
name: "user does not specify a value for Audit-ID in the request header", | ||
newAuditIDFunc: func() string { | ||
return "foo-bar-baz" | ||
}, | ||
auditIDExpected: "foo-bar-baz", | ||
}, | ||
{ | ||
name: "the value in Audit-ID request header is too large, should not be truncated", | ||
auditIDSpecified: largeAuditID, | ||
auditIDExpected: largeAuditID, | ||
}, | ||
{ | ||
name: "the generated Audit-ID is too large, should not be truncated", | ||
newAuditIDFunc: func() string { | ||
return largeAuditID | ||
}, | ||
auditIDExpected: largeAuditID, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
const auditKey = "Audit-ID" | ||
var ( | ||
innerHandlerCallCount int | ||
auditIDGot string | ||
found bool | ||
) | ||
handler := http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) { | ||
innerHandlerCallCount++ | ||
|
||
// does the inner handler see the audit ID? | ||
v, ok := request.AuditIDFrom(req.Context()) | ||
|
||
found = ok | ||
auditIDGot = string(v) | ||
}) | ||
|
||
wrapped := WithAuditID(handler) | ||
if test.newAuditIDFunc != nil { | ||
wrapped = withAuditID(handler, test.newAuditIDFunc) | ||
} | ||
|
||
testRequest, err := http.NewRequest(http.MethodGet, "/api/v1/namespaces", nil) | ||
if err != nil { | ||
t.Fatalf("failed to create new http request - %v", err) | ||
} | ||
if len(test.auditIDSpecified) > 0 { | ||
testRequest.Header.Set(auditKey, test.auditIDSpecified) | ||
} | ||
|
||
w := httptest.NewRecorder() | ||
wrapped.ServeHTTP(w, testRequest) | ||
|
||
if innerHandlerCallCount != 1 { | ||
t.Errorf("WithAuditID: expected the inner handler to be invoked once, but was invoked %d times", innerHandlerCallCount) | ||
} | ||
if !found { | ||
t.Error("WithAuditID: expected request.AuditIDFrom to return true, but got false") | ||
} | ||
if test.auditIDExpected != auditIDGot { | ||
t.Errorf("WithAuditID: expected the request context to have: %q, but got=%q", test.auditIDExpected, auditIDGot) | ||
} | ||
|
||
auditIDEchoed := w.Header().Get(auditKey) | ||
if test.auditIDExpected != auditIDEchoed { | ||
t.Errorf("WithAuditID: expected Audit-ID response header: %q, but got: %q", test.auditIDExpected, auditIDEchoed) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.