Skip to content

Commit

Permalink
feat(jenkins): extension point for http request interceptor
Browse files Browse the repository at this point in the history
Allows customization of the Jenkins request interceptor for http calls.
Makes username/password non manditory on jenkins host configuration.

Example use case would be x509 authentication in Jenkins where the identity
is extracted from the client certificate instead of an HTTP request header.
  • Loading branch information
cfieber committed Mar 28, 2018
1 parent 167893d commit 5093a56
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
package com.netflix.spinnaker.igor.config

import com.netflix.spinnaker.igor.IgorConfigurationProperties
import com.netflix.spinnaker.igor.config.auth.AuthRequestInterceptor
import com.netflix.spinnaker.igor.config.client.DefaultJenkinsOkHttpClientProvider
import com.netflix.spinnaker.igor.config.client.DefaultJenkinsRetrofitRequestInterceptorProvider
import com.netflix.spinnaker.igor.config.client.JenkinsOkHttpClientProvider
import com.netflix.spinnaker.igor.config.client.JenkinsRetrofitRequestInterceptorProvider
import com.netflix.spinnaker.igor.jenkins.client.JenkinsClient
import com.netflix.spinnaker.igor.jenkins.service.JenkinsService
import com.netflix.spinnaker.igor.service.BuildMasters
Expand All @@ -32,6 +33,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import retrofit.Endpoints
import retrofit.RequestInterceptor
import retrofit.RestAdapter
import retrofit.client.OkClient
import retrofit.converter.SimpleXMLConverter
Expand All @@ -54,17 +56,24 @@ class JenkinsConfig {
return new DefaultJenkinsOkHttpClientProvider()
}

@Bean
@ConditionalOnMissingBean
JenkinsRetrofitRequestInterceptorProvider jenkinsRetrofitRequestInterceptorProvider() {
return new DefaultJenkinsRetrofitRequestInterceptorProvider()
}

@Bean
Map<String, JenkinsService> jenkinsMasters(BuildMasters buildMasters,
IgorConfigurationProperties igorConfigurationProperties,
@Valid JenkinsProperties jenkinsProperties,
JenkinsOkHttpClientProvider jenkinsOkHttpClientProvider) {
JenkinsOkHttpClientProvider jenkinsOkHttpClientProvider,
JenkinsRetrofitRequestInterceptorProvider jenkinsRetrofitRequestInterceptorProvider) {
log.info "creating jenkinsMasters"
Map<String, JenkinsService> jenkinsMasters = ( jenkinsProperties?.masters?.collectEntries { JenkinsProperties.JenkinsHost host ->
log.info "bootstrapping ${host.address} as ${host.name}"
[(host.name): jenkinsService(
host.name,
jenkinsClient(host, jenkinsOkHttpClientProvider.provide(host), igorConfigurationProperties.client.timeout),
jenkinsClient(host, jenkinsOkHttpClientProvider.provide(host), jenkinsRetrofitRequestInterceptorProvider.provide(host), igorConfigurationProperties.client.timeout),
host.csrf
)]
})
Expand All @@ -77,12 +86,12 @@ class JenkinsConfig {
return new JenkinsService(jenkinsHostId, jenkinsClient, csrf)
}

static JenkinsClient jenkinsClient(JenkinsProperties.JenkinsHost host, OkHttpClient client, int timeout = 30000) {
static JenkinsClient jenkinsClient(JenkinsProperties.JenkinsHost host, OkHttpClient client, RequestInterceptor requestInterceptor, int timeout = 30000) {
client.setReadTimeout(timeout, TimeUnit.MILLISECONDS)

new RestAdapter.Builder()
.setEndpoint(Endpoints.newFixedEndpoint(host.address))
.setRequestInterceptor(new AuthRequestInterceptor(host))
.setRequestInterceptor(requestInterceptor)
.setClient(new OkClient(client))
.setConverter(new SimpleXMLConverter())
.build()
Expand All @@ -91,6 +100,6 @@ class JenkinsConfig {

static JenkinsClient jenkinsClient(JenkinsProperties.JenkinsHost host, int timeout = 30000) {
OkHttpClient client = new OkHttpClient()
jenkinsClient(host, client, timeout)
jenkinsClient(host, client, RequestInterceptor.NONE, timeout)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@ class JenkinsProperties {
@NotEmpty
String address

@NotEmpty
String username

@NotEmpty
String password

Boolean csrf = false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2018 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.igor.config.client;

import com.netflix.spinnaker.igor.config.JenkinsProperties;
import com.netflix.spinnaker.igor.config.auth.AuthRequestInterceptor;
import retrofit.RequestInterceptor;

public class DefaultJenkinsRetrofitRequestInterceptorProvider implements JenkinsRetrofitRequestInterceptorProvider {
@Override
public RequestInterceptor provide(JenkinsProperties.JenkinsHost host) {
return new AuthRequestInterceptor(host);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2018 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.igor.config.client;

import com.netflix.spinnaker.igor.config.JenkinsProperties;
import retrofit.RequestInterceptor;

/**
* Abstracts away the logic for providing a Retrofit RequestInterceptor for Jenkins services.
*/
public interface JenkinsRetrofitRequestInterceptorProvider {

RequestInterceptor provide(JenkinsProperties.JenkinsHost host);
}

0 comments on commit 5093a56

Please sign in to comment.