-
Notifications
You must be signed in to change notification settings - Fork 651
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rest): introduces a new configuration option,
insecure
which cr…
…eates a https client which is insecure by ignoring host verification and does not validate certificate chains. (#148)
- Loading branch information
Showing
6 changed files
with
148 additions
and
9 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
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
75 changes: 75 additions & 0 deletions
75
echo-rest/src/main/groovy/com/netflix/spinnaker/echo/events/RestClientFactory.groovy
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,75 @@ | ||
/* | ||
* Copyright 2017 Armory, 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.echo.events | ||
|
||
import com.squareup.okhttp.OkHttpClient | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.stereotype.Component | ||
import retrofit.client.Client | ||
import retrofit.client.OkClient | ||
|
||
import javax.net.ssl.HostnameVerifier | ||
import javax.net.ssl.SSLContext | ||
import javax.net.ssl.SSLSocketFactory | ||
import javax.net.ssl.TrustManager | ||
import javax.net.ssl.X509TrustManager | ||
|
||
|
||
interface OkHttpClientFactory { | ||
OkHttpClient getInsecureClient() | ||
} | ||
|
||
@Component | ||
class OkHttpClientFactoryImpl implements OkHttpClientFactory { | ||
|
||
OkHttpClient getInsecureClient() { | ||
// Create a trust manager that does not validate certificate chains | ||
def trustAllCerts = [ | ||
checkClientTrusted: { chain, authType -> }, | ||
checkServerTrusted: { chain, authType -> }, | ||
getAcceptedIssuers: { null } | ||
] | ||
|
||
def nullHostnameVerifier = [ | ||
verify: { hostname, session -> true } | ||
] | ||
|
||
SSLContext sc = SSLContext.getInstance("SSL") | ||
sc.init(null, [trustAllCerts as X509TrustManager] as TrustManager[], null) | ||
|
||
SSLSocketFactory sslSocketFactory = sc.getSocketFactory() | ||
OkHttpClient okHttpClient = new OkHttpClient() | ||
okHttpClient.setSslSocketFactory(sslSocketFactory) | ||
okHttpClient.setHostnameVerifier(nullHostnameVerifier as HostnameVerifier) | ||
return okHttpClient | ||
} | ||
} | ||
|
||
@Component | ||
class RestClientFactory { | ||
|
||
@Autowired | ||
OkHttpClientFactory httpClientFactory | ||
|
||
Client getClient(Boolean insecure) { | ||
if (insecure) { | ||
return new OkClient(httpClientFactory.getInsecureClient()) | ||
} else { | ||
return new OkClient() | ||
} | ||
} | ||
} |
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
24 changes: 24 additions & 0 deletions
24
echo-rest/src/test/groovy/com/netflix/spinnaker/echo/events/OkHttpClientFactorySpec.groovy
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,24 @@ | ||
package com.netflix.spinnaker.echo.events | ||
|
||
import com.squareup.okhttp.OkHttpClient | ||
import spock.lang.Specification | ||
import spock.lang.Subject | ||
|
||
import javax.net.ssl.HostnameVerifier | ||
|
||
class OkHttpClientFactorySpec extends Specification { | ||
|
||
@Subject | ||
OkHttpClientFactory clientFactory = new OkHttpClientFactoryImpl() | ||
OkHttpClient insecureClient | ||
|
||
void 'insecure client does not verify hostname'() { | ||
|
||
when: | ||
insecureClient = clientFactory.getInsecureClient() | ||
|
||
then: | ||
insecureClient.getHostnameVerifier().verify("mockdomain", null) | ||
|
||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
echo-rest/src/test/groovy/com/netflix/spinnaker/echo/events/RestClientFactorySpec.groovy
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,43 @@ | ||
package com.netflix.spinnaker.echo.events | ||
|
||
import com.squareup.okhttp.OkHttpClient | ||
import retrofit.client.Client | ||
import spock.lang.Specification | ||
import spock.lang.Subject | ||
|
||
class RestClientFactorySpec extends Specification{ | ||
|
||
@Subject | ||
RestClientFactory clientFactory = new RestClientFactory() | ||
|
||
Boolean insecure | ||
|
||
OkHttpClientFactory httpClientFactory | ||
|
||
void setup() { | ||
httpClientFactory = Mock(OkHttpClientFactory) | ||
clientFactory.httpClientFactory = httpClientFactory | ||
} | ||
|
||
void 'returns insecure client'() { | ||
given: | ||
insecure = true | ||
|
||
when: | ||
clientFactory.getClient(insecure) | ||
|
||
then: | ||
1 * httpClientFactory.getInsecureClient() >> new OkHttpClient() | ||
} | ||
|
||
void 'returns secure client'() { | ||
given: | ||
insecure = false | ||
|
||
when: | ||
clientFactory.getClient(insecure) | ||
|
||
then: | ||
0 * httpClientFactory.getInsecureClient() >> new OkHttpClient() | ||
} | ||
} |