Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(web): Add X-SPINNAKER-* optional data to HTTP response header (#…
…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
1 parent
abf0fcf
commit dfb77de
Showing
6 changed files
with
411 additions
and
41 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
38 changes: 0 additions & 38 deletions
38
gate-web/src/main/groovy/com/netflix/spinnaker/gate/interceptors/RequestIdInterceptor.java
This file was deleted.
Oops, something went wrong.
60 changes: 60 additions & 0 deletions
60
...eb/src/main/groovy/com/netflix/spinnaker/gate/interceptors/ResponseHeaderInterceptor.java
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,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; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...netflix/spinnaker/gate/interceptors/ResponseHeaderInterceptorConfigurationProperties.java
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,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()); | ||
} |
89 changes: 89 additions & 0 deletions
89
...lix/spinnaker/gate/interceptors/ResponseHeaderInterceptorConfigurationPropertiesTest.java
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,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 {} | ||
} |
Oops, something went wrong.