Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add integration test for apiserver hsts #100285

Merged
merged 1 commit into from
Aug 5, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 48 additions & 0 deletions test/integration/apiserver/apiserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,54 @@ func TestCacheControl(t *testing.T) {
}
}

// Tests that the apiserver returns HSTS headers as expected.
func TestHSTS(t *testing.T) {
controlPlaneConfig := framework.NewIntegrationTestControlPlaneConfigWithOptions(&framework.ControlPlaneConfigOptions{})
controlPlaneConfig.GenericConfig.OpenAPIConfig = framework.DefaultOpenAPIConfig()
controlPlaneConfig.GenericConfig.HSTSDirectives = []string{"max-age=31536000", "includeSubDomains"}
instanceConfig, _, closeFn := framework.RunAnAPIServer(controlPlaneConfig)
defer closeFn()

rt, err := restclient.TransportFor(instanceConfig.GenericAPIServer.LoopbackClientConfig)
if err != nil {
t.Fatal(err)
}

paths := []string{
// untyped
"/",
// health
"/healthz",
// openapi
"/openapi/v2",
// discovery
"/api",
"/api/v1",
"/apis",
"/apis/apps",
"/apis/apps/v1",
// apis
"/api/v1/namespaces",
"/apis/apps/v1/deployments",
}
for _, path := range paths {
t.Run(path, func(t *testing.T) {
req, err := http.NewRequest("GET", instanceConfig.GenericAPIServer.LoopbackClientConfig.Host+path, nil)
if err != nil {
t.Fatal(err)
}
resp, err := rt.RoundTrip(req)
if err != nil {
t.Fatal(err)
}
cc := resp.Header.Get("Strict-Transport-Security")
if !strings.Contains(cc, "max-age=31536000; includeSubDomains") {
t.Errorf("expected max-age=31536000; includeSubDomains, got %q", cc)
}
})
}
}

// Tests that the apiserver returns 202 status code as expected.
func Test202StatusCode(t *testing.T) {
s, clientSet, closeFn := setup(t)
Expand Down