Skip to content

Commit

Permalink
feat(web): Add X-SPINNAKER-* optional data to HTTP response header (#…
Browse files Browse the repository at this point in the history
…1610)

* test(web): Add unit test for RequestIdInterceptor

* feat(web): Refactor RequestIdInterceptor to be able to set additional X-SPINNAKER header info besides request ID to response header, and renaming it to ResponseHeaderInterceptor to better reflect its functionality. The motivation for this enhancement is to allow better correlation of API interaction with users making these requests.

A new configuration property "interceptors.responseHeader.fields" is added for configuring the X-SPINNAKER-* header fields to be added to the response header by the interceptor. This property takes a comma delimited string or yaml list of the header fields, for example to add X-SPINNAKER-REQUEST-ID and X-SPINNAKER-USER to the response header:

interceptors.responseHeader.fields=X-SPINNAKER-REQUEST-ID,X-SPINNAKER-USER

or in yaml form:

interceptors:
  responseHeader:
    fields:
      - X-SPINNAKER-REQUEST-ID
      - X-SPINNAKER-USER

When this configuration property is not defined, the interceptor defaults to adding X-SPINNAKER-REQUEST-ID field to the response, which is the current behavior.
  • Loading branch information
kchoy-sfdc committed Feb 3, 2023
1 parent abf0fcf commit dfb77de
Show file tree
Hide file tree
Showing 6 changed files with 411 additions and 41 deletions.
Expand Up @@ -19,13 +19,14 @@ package com.netflix.spinnaker.gate.config
import com.netflix.spectator.api.Registry
import com.netflix.spinnaker.gate.filters.ContentCachingFilter
import com.netflix.spinnaker.gate.interceptors.RequestContextInterceptor
import com.netflix.spinnaker.gate.interceptors.RequestIdInterceptor

import com.netflix.spinnaker.gate.interceptors.ResponseHeaderInterceptor
import com.netflix.spinnaker.gate.interceptors.ResponseHeaderInterceptorConfigurationProperties
import com.netflix.spinnaker.gate.retrofit.UpstreamBadRequest
import com.netflix.spinnaker.kork.dynamicconfig.DynamicConfigService
import com.netflix.spinnaker.kork.web.interceptors.MetricsInterceptor
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.ApplicationContext
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.ComponentScan
Expand All @@ -45,6 +46,7 @@ import javax.servlet.http.HttpServletResponse

@Configuration
@ComponentScan
@EnableConfigurationProperties(ResponseHeaderInterceptorConfigurationProperties.class)
public class GateWebConfig implements WebMvcConfigurer {
@Autowired
Registry registry
Expand All @@ -58,6 +60,9 @@ public class GateWebConfig implements WebMvcConfigurer {
@Value('${rate-limit.learning:true}')
Boolean rateLimitLearningMode

@Autowired
ResponseHeaderInterceptorConfigurationProperties responseHeaderInterceptorConfigurationProperties

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(
Expand All @@ -66,7 +71,7 @@ public class GateWebConfig implements WebMvcConfigurer {
)
)

registry.addInterceptor(new RequestIdInterceptor())
registry.addInterceptor(new ResponseHeaderInterceptor(responseHeaderInterceptorConfigurationProperties))
registry.addInterceptor(new RequestContextInterceptor())
}

Expand Down

This file was deleted.

@@ -0,0 +1,60 @@
/*
* Copyright 2017 Netflix, Inc.
*
* 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 com.netflix.spinnaker.gate.interceptors;

import static com.netflix.spinnaker.kork.common.Header.REQUEST_ID;

import com.netflix.spinnaker.security.AuthenticatedRequest;
import java.util.Optional;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
* Return values (e.g. X-SPINNAKER-*) stored in the AuthenticatedRequest (backed by MDC and set via
* com.netflix.spinnaker.filters.AuthenticatedRequestFilter) to gate callers as a response header.
* For X-SPINNAKER-REQUEST-ID, if its value is absent from AuthenticatedRequest, the value of
* X-SPINNAKER-EXECUTION-ID is returned as the request ID, or a UUID is generated and returned if
* X-SPINNAKER-EXECUTION-ID is also absent. For other fields, no values are returned if they are
* absent from AuthenticatedRequest.
*/
public class ResponseHeaderInterceptor extends HandlerInterceptorAdapter {

private final ResponseHeaderInterceptorConfigurationProperties properties;

public ResponseHeaderInterceptor(ResponseHeaderInterceptorConfigurationProperties properties) {
this.properties = properties;
}

@Override
public boolean preHandle(
HttpServletRequest request, HttpServletResponse response, Object handler) {
for (String field : this.properties.getFields()) {
// getSpinnakerRequestId() contains logic to either return the spinnaker
// execution id or generate a new one if no current value exists in MDC,
// the generic get() does not contain such logic
// check whether we are processing the request id to make sure we call
// the right method to retain the above logic
Optional<String> value =
field.equals(REQUEST_ID.getHeader())
? AuthenticatedRequest.getSpinnakerRequestId()
: AuthenticatedRequest.get(field);
value.ifPresent(v -> response.setHeader(field, v));
}
return true;
}
}
@@ -0,0 +1,30 @@
/*
* Copyright 2022 Salesforce, Inc.
*
* 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 com.netflix.spinnaker.gate.interceptors;

import com.netflix.spinnaker.kork.common.Header;
import java.util.List;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

@Data
@ConfigurationProperties(prefix = "interceptors.response-header")
public class ResponseHeaderInterceptorConfigurationProperties {
// default to having request id in response header, the original behavior
// before other fields are added
private List<String> fields = List.of(Header.REQUEST_ID.getHeader());
}
@@ -0,0 +1,89 @@
/*
* Copyright 2022 Salesforce, Inc.
*
* 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 com.netflix.spinnaker.gate.interceptors;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

import com.netflix.spinnaker.kork.common.Header;
import org.junit.jupiter.api.Test;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;

public class ResponseHeaderInterceptorConfigurationPropertiesTest {

@Test
public void testResponseHeaderInterceptorSettingsDefault() {
ApplicationContextRunner runner =
new ApplicationContextRunner()
.withUserConfiguration(ResponseHeaderInterceptorTestConfiguration.class);

runner.run(
ctx -> {
ResponseHeaderInterceptorConfigurationProperties properties =
ctx.getBean(ResponseHeaderInterceptorConfigurationProperties.class);

assertThat(properties.getFields().size(), equalTo(1));
assertThat(properties.getFields(), contains(Header.REQUEST_ID.getHeader()));
});
}

@Test
public void testResponseHeaderInterceptorSettingsAllFields() {
String[] values = {
"X-SPINNAKER-REQUEST-ID",
"X-SPINNAKER-USER",
"X-SPINNAKER-EXECUTION-ID",
"X-SPINNAKER-EXECUTION-TYPE",
"X-SPINNAKER-APPLICATION"
};
String value = String.join(",", values);

ApplicationContextRunner runner =
new ApplicationContextRunner()
.withPropertyValues("interceptors.responseHeader.fields=" + value)
.withUserConfiguration(ResponseHeaderInterceptorTestConfiguration.class);

runner.run(
ctx -> {
ResponseHeaderInterceptorConfigurationProperties properties =
ctx.getBean(ResponseHeaderInterceptorConfigurationProperties.class);

assertThat(properties.getFields().size(), equalTo(values.length));
assertThat(properties.getFields(), containsInAnyOrder(values));
});
}

@Test
public void testResponseHeaderInterceptorSettingsNoFields() {
ApplicationContextRunner runner =
new ApplicationContextRunner()
.withPropertyValues("interceptors.responseHeader.fields=")
.withUserConfiguration(ResponseHeaderInterceptorTestConfiguration.class);

runner.run(
ctx -> {
ResponseHeaderInterceptorConfigurationProperties properties =
ctx.getBean(ResponseHeaderInterceptorConfigurationProperties.class);

assertThat(properties.getFields(), empty());
});
}

@EnableConfigurationProperties(ResponseHeaderInterceptorConfigurationProperties.class)
static class ResponseHeaderInterceptorTestConfiguration {}
}

0 comments on commit dfb77de

Please sign in to comment.