Skip to content

Commit

Permalink
feat(core): Allow configurable headers for rest endpoints (#143)
Browse files Browse the repository at this point in the history
* Allow configuration of arbitrary headers to rest endpoints
* Allow customizable headers from a file
* Allow customizable headers in the echo configuration yml
  • Loading branch information
andrewbackes authored and Matt Duftler committed May 8, 2017
1 parent 03d9a6f commit cae72ff
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@

package com.netflix.spinnaker.echo.config

import static retrofit.Endpoints.newFixedEndpoint

import org.apache.commons.codec.binary.Base64
import com.netflix.spinnaker.echo.rest.RestService
import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import org.apache.commons.codec.binary.Base64
import org.springframework.beans.factory.annotation.Value
import org.springframework.beans.factory.config.ConfigurableBeanFactory
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
Expand All @@ -34,9 +33,12 @@ import retrofit.client.Client
import retrofit.client.OkClient
import retrofit.converter.JacksonConverter

import static retrofit.Endpoints.newFixedEndpoint

/**
* Rest endpoint configuration
*/
@Slf4j
@Configuration
@ConditionalOnProperty('rest.enabled')
@CompileStatic
Expand All @@ -54,8 +56,44 @@ class RestConfig {
return LogLevel.valueOf(retrofitLogLevel)
}

interface RequestInterceptorAttacher {
void attach(RestAdapter.Builder builder, RequestInterceptor interceptor)
}

@Bean
RestUrls restServices(RestProperties restProperties, Client retrofitClient, LogLevel retrofitLogLevel) {
RequestInterceptorAttacher requestInterceptorAttacher() {
new RequestInterceptorAttacher() {
@Override
public void attach(RestAdapter.Builder builder, RequestInterceptor interceptor) {
builder.setRequestInterceptor(interceptor)
}
}
}

interface HeadersFromFile {
Map<String, String> headers(String path)
}

@Bean
HeadersFromFile headersFromFile() {
new HeadersFromFile() {
Map<String, String> headers(String path) {
Map<String, String> headers = new HashMap<>()
new File(path).eachLine { line ->
def pair = line.split(":")
if (pair.length == 2) {
headers[pair[0]] = pair[1].trim()
} else {
log.warn("Could not parse header '$line' in '$path'")
}
}
return headers
}
}
}

@Bean
RestUrls restServices(RestProperties restProperties, Client retrofitClient, LogLevel retrofitLogLevel, RequestInterceptorAttacher requestInterceptorAttacher, HeadersFromFile headersFromFile) {

RestUrls restUrls = new RestUrls()

Expand All @@ -68,16 +106,31 @@ class RestConfig {
.setLogLevel(retrofitLogLevel)
.setConverter(new JacksonConverter())

Map<String, String> headers = new HashMap<>()

if (endpoint.username && endpoint.password) {
RequestInterceptor authInterceptor = new RequestInterceptor() {
String auth = "Basic " + Base64.encodeBase64String("${endpoint.username}:${endpoint.password}".getBytes())
headers["Authorization"] = auth
}

if (endpoint.headers) {
headers += endpoint.headers
}

if (endpoint.headersFile) {
headers += headersFromFile.headers(endpoint.headersFile)
}

if (headers) {
RequestInterceptor headerInterceptor = new RequestInterceptor() {
@Override
public void intercept(RequestInterceptor.RequestFacade request) {
String auth = "Basic " + Base64.encodeBase64String("${endpoint.username}:${endpoint.password}".getBytes())
request.addHeader("Authorization", auth)
headers.each { k, v ->
request.addHeader(k, v)
}
}
}

restAdapterBuilder.setRequestInterceptor(authInterceptor)
requestInterceptorAttacher.attach(restAdapterBuilder, headerInterceptor)
}

restUrls.services.add(
Expand All @@ -90,5 +143,4 @@ class RestConfig {

restUrls
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class RestProperties {
String url
String username
String password
Map<String, String> headers
String headersFile
Boolean flatten = false

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.netflix.spinnaker.echo.config

import retrofit.RequestInterceptor
import retrofit.RestAdapter
import spock.lang.Specification
import spock.lang.Subject

class RestConfigSpec extends Specification {

@Subject
config = new RestConfig()

def request = Mock(RequestInterceptor.RequestFacade)
def EmptyHeadersFile = Mock(RestConfig.HeadersFromFile)
def attacher = new RestConfig.RequestInterceptorAttacher() {
RequestInterceptor interceptor
@Override
public void attach(RestAdapter.Builder builder, RequestInterceptor interceptor) {
this.interceptor = interceptor
}
}

void configureRestServices(RestProperties.RestEndpointConfiguration endpoint, RestConfig.HeadersFromFile headersFromFile) {
RestProperties restProperties = new RestProperties(endpoints: [endpoint])
config.restServices(restProperties, config.retrofitClient(), config.retrofitLogLevel("BASIC"), attacher, headersFromFile)
}

void "Generate basic auth header"() {
given:
RestProperties.RestEndpointConfiguration endpoint = new RestProperties.RestEndpointConfiguration(
url: "http://localhost:9090",
username: "testuser",
password: "testpassword")
configureRestServices(endpoint, EmptyHeadersFile)

when:
attacher.interceptor.intercept(request)

then:
1 * request.addHeader("Authorization", "Basic dGVzdHVzZXI6dGVzdHBhc3N3b3Jk")
0 * request.addHeader(_, _)
}

void "'Authorization' header over generated basic auth header"() {
given:
RestProperties.RestEndpointConfiguration endpoint = new RestProperties.RestEndpointConfiguration(
url: "http://localhost:9090",
username: "testuser",
password: "testpassword",
headers: ["Authorization": "FromConfig"])
configureRestServices(endpoint, EmptyHeadersFile)

when:
attacher.interceptor.intercept(request)

then:
1 * request.addHeader("Authorization", "FromConfig")
0 * request.addHeader(_, _)
}

void "'Authorization' headerFile over all others"() {
given:
RestProperties.RestEndpointConfiguration endpoint = new RestProperties.RestEndpointConfiguration(
url: "http://localhost:9090",
username: "testuser",
password: "testpassword",
headers: ["Authorization": "FromConfig"],
headersFile: "/testfile")
RestConfig.HeadersFromFile headersFromFile = new RestConfig.HeadersFromFile() {
@Override
Map<String, String> headers(String path) {
return [
"Authorization": "FromFile"
]
}
}
configureRestServices(endpoint, headersFromFile)

when:
attacher.interceptor.intercept(request)

then:
1 * request.addHeader("Authorization", "FromFile")
0 * request.addHeader(_, _)
}
}

0 comments on commit cae72ff

Please sign in to comment.