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

feat(web): add ability to skip X-SPINNAKER-ACCOUNTS in SpinnakerRequestInterceptor and SpinnakerRequestHeaderInterceptor #1148

Merged
merged 12 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,3 @@ interface Retrofit1Service {
fun getSomething(@Path("user") user: String?, callback: Callback<List<*>?>?)

}


Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private open class TestConfiguration {

@Bean
open fun spinnakerRequestInterceptor(): SpinnakerRequestInterceptor {
return SpinnakerRequestInterceptor(OkHttpClientConfigurationProperties())
return SpinnakerRequestInterceptor(true)
}

@Bean
Expand Down
3 changes: 3 additions & 0 deletions kork-web/kork-web.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ dependencies {
runtimeOnly "org.hibernate.validator:hibernate-validator"

testImplementation project(":kork-test")
testImplementation "com.github.tomakehurst:wiremock-jre8-standalone"
testImplementation "ch.qos.logback:logback-classic"
testImplementation "ch.qos.logback:logback-core"
testImplementation "com.squareup.retrofit2:retrofit"
testImplementation "org.junit.jupiter:junit-jupiter-params"
testImplementation "org.spockframework:spock-core"
testImplementation "org.spockframework:spock-spring"
testImplementation "org.springframework.boot:spring-boot-starter-test"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,37 @@

package com.netflix.spinnaker.okhttp

import com.netflix.spinnaker.kork.common.Header
import com.netflix.spinnaker.security.AuthenticatedRequest
import retrofit.RequestInterceptor

class SpinnakerRequestInterceptor implements RequestInterceptor {
private final OkHttpClientConfigurationProperties okHttpClientConfigurationProperties
private final boolean propagateSpinnakerHeaders;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one thing I really dislike about the HTTP spec is that they don't define a limit for header size, so servers just arbitrarily choose one. One thing we can do is also only send the header if the size is manageable (whatever that may be). So rather than needing to configure this, it can just send this header when the header values is less than 8kb or something which should work on most servers.


SpinnakerRequestInterceptor(OkHttpClientConfigurationProperties okHttpClientConfigurationProperties) {
this.okHttpClientConfigurationProperties = okHttpClientConfigurationProperties
/**
* Don't propagate X-SPINNAKER-ACCOUNTS. Only relevant when propagateSpinnakerHeaders is true.
*/
private final boolean skipAccountsHeader;

SpinnakerRequestInterceptor(boolean propagateSpinnakerHeaders) {
this.propagateSpinnakerHeaders = propagateSpinnakerHeaders
this.skipAccountsHeader = false
}

SpinnakerRequestInterceptor(boolean propagateSpinnakerHeaders,
boolean skipAccountsHeader) {
this.propagateSpinnakerHeaders = propagateSpinnakerHeaders
this.skipAccountsHeader = skipAccountsHeader
}

void intercept(RequestInterceptor.RequestFacade request) {
if (!okHttpClientConfigurationProperties.propagateSpinnakerHeaders) {
if (!propagateSpinnakerHeaders) {
// noop
return
}

AuthenticatedRequest.authenticationHeaders.each { String key, Optional<String> value ->
if (value.present) {
if (value.present && (!skipAccountsHeader || !Header.ACCOUNTS.getHeader().equals(key))) {
request.addHeader(key, value.get())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ public class OkHttpClientComponents {

@Bean
public SpinnakerRequestInterceptor spinnakerRequestInterceptor() {
return new SpinnakerRequestInterceptor(clientProperties);
return new SpinnakerRequestInterceptor(clientProperties.getPropagateSpinnakerHeaders());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seems to be no way to configure sending the account headers?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right....sort of. The SpinnakerRequestInterceptor bean keeps the same behavior -- to send the account headers. But now it's possible to construct another SpinnakerRequestInterceptor bean that doesn't.

}

@Bean
public SpinnakerRequestHeaderInterceptor spinnakerRequestHeaderInterceptor() {
return new SpinnakerRequestHeaderInterceptor(clientProperties);
return new SpinnakerRequestHeaderInterceptor(clientProperties.getPropagateSpinnakerHeaders());
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.netflix.spinnaker.okhttp;

import com.netflix.spinnaker.kork.common.Header;
import com.netflix.spinnaker.security.AuthenticatedRequest;
import java.io.IOException;
import okhttp3.Interceptor;
Expand All @@ -28,24 +29,34 @@
*/
public class SpinnakerRequestHeaderInterceptor implements Interceptor {

private final OkHttpClientConfigurationProperties okHttpClientConfigurationProperties;
private final boolean propagateSpinnakerHeaders;

/** Don't propagate X-SPINNAKER-ACCOUNTS. Only relevant when propagateSpinnakerHeaders is true. */
private final boolean skipAccountsHeader;

public SpinnakerRequestHeaderInterceptor(boolean propagateSpinnakerHeaders) {
this.propagateSpinnakerHeaders = propagateSpinnakerHeaders;
this.skipAccountsHeader = false;
}

public SpinnakerRequestHeaderInterceptor(
OkHttpClientConfigurationProperties okHttpClientConfigurationProperties) {
this.okHttpClientConfigurationProperties = okHttpClientConfigurationProperties;
boolean propagateSpinnakerHeaders, boolean skipAccountsHeader) {
this.propagateSpinnakerHeaders = propagateSpinnakerHeaders;
this.skipAccountsHeader = skipAccountsHeader;
}

@Override
public Response intercept(Chain chain) throws IOException {
Request.Builder builder = chain.request().newBuilder();
if (!okHttpClientConfigurationProperties.getPropagateSpinnakerHeaders()) {
if (!propagateSpinnakerHeaders) {
return chain.proceed(builder.build());
}

AuthenticatedRequest.getAuthenticationHeaders()
.forEach(
(key, value) -> {
if (value.isPresent()) {
if (value.isPresent()
&& (!skipAccountsHeader || !Header.ACCOUNTS.getHeader().equals(key))) {
builder.addHeader(key, value.get());
}
});
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2024 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.config;

import static org.assertj.core.api.Assertions.assertThat;

import com.netflix.spinnaker.okhttp.OkHttp3MetricsInterceptor;
import com.netflix.spinnaker.okhttp.OkHttpMetricsInterceptor;
import com.netflix.spinnaker.okhttp.SpinnakerRequestHeaderInterceptor;
import com.netflix.spinnaker.okhttp.SpinnakerRequestInterceptor;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.springframework.boot.context.annotation.UserConfigurations;
import org.springframework.boot.task.TaskExecutorBuilder;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;

class OkHttpClientComponentsTest {

private final ApplicationContextRunner runner =
new ApplicationContextRunner()
.withBean(TaskExecutorBuilder.class)
.withConfiguration(UserConfigurations.of(OkHttpClientComponents.class));

@BeforeEach
void init(TestInfo testInfo) {
System.out.println("--------------- Test " + testInfo.getDisplayName());
}

@Test
void verifyValidConfiguration() {
runner.run(
ctx -> {
assertThat(ctx).hasSingleBean(SpinnakerRequestInterceptor.class);
assertThat(ctx).hasSingleBean(SpinnakerRequestHeaderInterceptor.class);
assertThat(ctx).hasSingleBean(OkHttpMetricsInterceptor.class);
assertThat(ctx).hasSingleBean(OkHttp3MetricsInterceptor.class);
});
}
}
Loading