| 1 |
/* |
| 2 |
Copyright 2021 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 |
"fmt" |
| 21 |
"net/http" |
| 22 |
"net/http/httptest" |
| 23 |
"testing" |
| 24 |
|
| 25 |
"github.com/google/uuid" |
| 26 |
"k8s.io/apiserver/pkg/audit" |
| 27 |
) |
| 28 |
|
| 29 |
func TestWithAuditID(t *testing.T) { |
| 30 |
largeAuditID := fmt.Sprintf("%s-%s", uuid.New().String(), uuid.New().String()) |
| 31 |
tests := []struct { |
| 32 |
name string |
| 33 |
newAuditIDFunc func() string |
| 34 |
auditIDSpecified string |
| 35 |
auditIDExpected string |
| 36 |
}{ |
| 37 |
{ |
| 38 |
name: "user specifies a value for Audit-ID in the request header", |
| 39 |
auditIDSpecified: "foo-bar-baz", |
| 40 |
auditIDExpected: "foo-bar-baz", |
| 41 |
}, |
| 42 |
{ |
| 43 |
name: "user does not specify a value for Audit-ID in the request header", |
| 44 |
newAuditIDFunc: func() string { |
| 45 |
return "foo-bar-baz" |
| 46 |
}, |
| 47 |
auditIDExpected: "foo-bar-baz", |
| 48 |
}, |
| 49 |
{ |
| 50 |
name: "the value in Audit-ID request header is too large, should not be truncated", |
| 51 |
auditIDSpecified: largeAuditID, |
| 52 |
auditIDExpected: largeAuditID, |
| 53 |
}, |
| 54 |
{ |
| 55 |
name: "the generated Audit-ID is too large, should not be truncated", |
| 56 |
newAuditIDFunc: func() string { |
| 57 |
return largeAuditID |
| 58 |
}, |
| 59 |
auditIDExpected: largeAuditID, |
| 60 |
}, |
| 61 |
} |
| 62 |
|
| 63 |
for _, test := range tests { |
| 64 |
t.Run(test.name, func(t *testing.T) { |
| 65 |
const auditKey = "Audit-ID" |
| 66 |
var ( |
| 67 |
innerHandlerCallCount int |
| 68 |
auditIDGot string |
| 69 |
found bool |
| 70 |
) |
| 71 |
handler := http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) { |
| 72 |
innerHandlerCallCount++ |
| 73 |
|
| 74 |
// does the inner handler see the audit ID? |
| 75 |
v, ok := audit.AuditIDFrom(req.Context()) |
| 76 |
|
| 77 |
found = ok |
| 78 |
auditIDGot = string(v) |
| 79 |
}) |
| 80 |
|
| 81 |
wrapped := WithAuditInit(handler) |
| 82 |
if test.newAuditIDFunc != nil { |
| 83 |
wrapped = withAuditInit(handler, test.newAuditIDFunc) |
| 84 |
} |
| 85 |
|
| 86 |
testRequest, err := http.NewRequest(http.MethodGet, "/api/v1/namespaces", nil) |
| 87 |
if err != nil { |
| 88 |
t.Fatalf("failed to create new http request - %v", err) |
| 89 |
} |
| 90 |
if len(test.auditIDSpecified) > 0 { |
| 91 |
testRequest.Header.Set(auditKey, test.auditIDSpecified) |
| 92 |
} |
| 93 |
|
| 94 |
w := httptest.NewRecorder() |
| 95 |
wrapped.ServeHTTP(w, testRequest) |
| 96 |
|
| 97 |
if innerHandlerCallCount != 1 { |
| 98 |
t.Errorf("WithAuditID: expected the inner handler to be invoked once, but was invoked %d times", innerHandlerCallCount) |
| 99 |
} |
| 100 |
if !found { |
| 101 |
t.Error("WithAuditID: expected request.AuditIDFrom to return true, but got false") |
| 102 |
} |
| 103 |
if test.auditIDExpected != auditIDGot { |
| 104 |
t.Errorf("WithAuditID: expected the request context to have: %q, but got=%q", test.auditIDExpected, auditIDGot) |
| 105 |
} |
| 106 |
|
| 107 |
auditIDEchoed := w.Header().Get(auditKey) |
| 108 |
if test.auditIDExpected != auditIDEchoed { |
| 109 |
t.Errorf("WithAuditID: expected Audit-ID response header: %q, but got: %q", test.auditIDExpected, auditIDEchoed) |
| 110 |
} |
| 111 |
}) |
| 112 |
} |
| 113 |
} |