Skip to content

Commit ac2d5cd

Browse files
committed
Adding option for ignoring certificate validation
1 parent 6d82106 commit ac2d5cd

File tree

9 files changed

+208
-16
lines changed

9 files changed

+208
-16
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ deploy:
77
provider: releases
88
api_key:
99
secure: HaLgQjTqegx0wexEexbBKB+D1DGwbiW7zQgLMpbA/d1PUQRLHMIZLNE8u1V5/4/qLSRqmp/VdLHwvSWbcc7dPrzNN09rwEb7HPLORkCvSHq6/pOgnJ1xIWU4CMjNewjxYS94AnQN+0+PFdFw7mqTuf6cz/IVCZxypCEzckziI220DLYBXBS9vNSko55979yhQv7U31vd/CJ63cp21qvemAcFncQr98FKq6JsbpMtOOjAqjvN/VPpnphhoV7DxbiKEMZ9ZePClg69HpZ+Q/o2qUm/BKd32ZmpZosTE27Gaj81TVnLeLfveeBRt0jTK4gisS5++t6AfK01DHGexT4hVgG34jkjQQX76imCMa+sd61Ixs2P4rVRl9zqJKFBlxoeOkdXhenLqDZL0c/jKYXXtIln6f24rnQ26t1L7zbdI2I0G+gS0aZfXAMSXoFioZCplydRkuuHrr/HOVmlBk7LF36cvXSbv2UVwATrNjOZs0Sd0fBxrOtnQ7l9g3vSufV59kHE/ic80Lrbn8Pphh3gRDsOrVGF/vHvz5Aho0kCxLwqoz6hajQ0kIVvfIz3kC2xITeFWYwYgUUupLUopj2GiIpOdcPhZJ/9Lr0+zr3cxW/l7orWwmuVT/Eo9JdyhX+GaYvRvNkU9x1A1VFF/R3AOc2eSDi+zmCASbqdA9vE/Zs=
10-
file: build/libs/xlr-ucd-plugin-1.1.0.jar
10+
file: build/libs/xlr-ucd-plugin-1.2.0.jar
1111
skip_cleanup: true
1212
on:
1313
tags: true

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ plugins {
33
id "com.xebialabs.xlr.docker" version "1.2.1"
44
}
55

6-
version='1.1.0'
6+
version='1.2.0'
77

88
apply plugin: 'java'
99
apply plugin: 'idea'

src/main/resources/synthetic.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<synthetic xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.xebialabs.com/deployit/synthetic" xsi:schemaLocation="http://www.xebialabs.com/deployit/synthetic synthetic.xsd">
1111

1212
<type type="ucd.Server" extends="configuration.HttpConnection">
13+
<property name="disableSslVerification" required="true" kind="boolean" default="false" description="Disable SSL verification" />
1314
</type>
1415

1516
<type type="ucd.Task" extends="xlrelease.PythonScript" virtual="true">
@@ -46,4 +47,4 @@
4647
<property name="requestStatus" category="output" label="Request Status" description="The request status" kind="string" />
4748
<property name="requestResult" category="output" label="Request Result" description="The request result" kind="string" />
4849
</type>
49-
</synthetic>
50+
</synthetic>

src/main/resources/ucd/ApplicationProcessRequest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
from ucd.UCDClientUtil import UCD_Client_Util
88

99

10-
ucd_client = UCD_Client_Util.create_ucd_client(server, username, password)
10+
verifySsl = not server['disableSslVerification']
11+
ucd_client = UCD_Client_Util.create_ucd_client(server, username, password, verifySsl)
1112

1213
requestId = ucd_client.application_process_request(application, applicationProcess, environment, versions)
1314

src/main/resources/ucd/ApplicationProcessRequestStatus.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import time, sys
77
from ucd.UCDClientUtil import UCD_Client_Util
88

9-
10-
ucd_client = UCD_Client_Util.create_ucd_client(server, username, password)
9+
verifySsl = not server['disableSslVerification']
10+
ucd_client = UCD_Client_Util.create_ucd_client(server, username, password, verifySsl)
1111
trial = 0
1212
request_status = None
1313
request_response = None
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
import urllib
2+
3+
from xlrelease import HttpResponse
4+
5+
from java.lang import String
6+
7+
from org.apache.commons.codec.binary import Base64
8+
from org.apache.http import HttpHost
9+
from org.apache.http.client.config import RequestConfig
10+
from org.apache.http.util import EntityUtils
11+
from org.apache.http.client.methods import HttpGet, HttpPost, HttpPut, HttpDelete
12+
from org.apache.http.entity import StringEntity
13+
from org.apache.http.impl.client import HttpClients
14+
from org.apache.http.conn.ssl import SSLContextBuilder, SSLConnectionSocketFactory, TrustStrategy
15+
16+
class TrustAllStrategy(TrustStrategy):
17+
def isTrusted(self, chain, authType):
18+
return True
19+
20+
class HttpRequest:
21+
def __init__(self, username = None, password = None, verify = True):
22+
"""
23+
Builds an HttpRequest
24+
25+
:param username: the username for basic authentication
26+
(optional, no authentication will be used if empty)
27+
:param password: an password
28+
(optional)
29+
"""
30+
self.username = username
31+
self.password = password
32+
self.verify = verify
33+
34+
def doRequest(self, **options):
35+
"""
36+
Performs an HTTP Request
37+
38+
:param options: A keyword arguments object with the following properties :
39+
method: the HTTP method : 'GET', 'PUT', 'POST', 'DELETE'
40+
(optional: GET will be used if empty)
41+
context: the context url
42+
(optional: the url on HttpConnection will be used if empty)
43+
body: the body of the HTTP request for PUT & POST calls
44+
(optional: an empty body will be used if empty)
45+
contentType: the content type to use
46+
(optional, no content type will be used if empty)
47+
headers: a dictionary of headers key/values
48+
(optional, no headers will be used if empty)
49+
:return: an HttpResponse instance
50+
"""
51+
request = self.buildRequest(
52+
options.get('method', 'GET'),
53+
options.get('context', ''),
54+
options.get('body', ''),
55+
options.get('contentType', None),
56+
options.get('headers', None))
57+
58+
return self.executeRequest(request)
59+
60+
61+
def get(self, context, **options):
62+
"""
63+
Performs an Http GET Request
64+
65+
:param context: the context url
66+
:param options: the options keyword argument described in doRequest()
67+
:return: an HttpResponse instance
68+
"""
69+
options['method'] = 'GET'
70+
options['context'] = context
71+
return self.doRequest(**options)
72+
73+
74+
def put(self, context, body, **options):
75+
"""
76+
Performs an Http PUT Request
77+
78+
:param context: the context url
79+
:param body: the body of the HTTP request
80+
:param options: the options keyword argument described in doRequest()
81+
:return: an HttpResponse instance
82+
"""
83+
options['method'] = 'PUT'
84+
options['context'] = context
85+
options['body'] = body
86+
return self.doRequest(**options)
87+
88+
89+
def post(self, context, body, **options):
90+
"""
91+
Performs an Http POST Request
92+
93+
:param context: the context url
94+
:param body: the body of the HTTP request
95+
:param options: the options keyword argument described in doRequest()
96+
:return: an HttpResponse instance
97+
"""
98+
options['method'] = 'POST'
99+
options['context'] = context
100+
options['body'] = body
101+
return self.doRequest(**options)
102+
103+
104+
def delete(self, context, **options):
105+
"""
106+
Performs an Http DELETE Request
107+
108+
:param context: the context url
109+
:param options: the options keyword argument described in doRequest()
110+
:return: an HttpResponse instance
111+
"""
112+
options['method'] = 'DELETE'
113+
options['context'] = context
114+
return self.doRequest(**options)
115+
116+
117+
def buildRequest(self, method, context, body, contentType, headers):
118+
119+
method = method.upper()
120+
121+
if method == 'GET':
122+
request = HttpGet(context)
123+
elif method == 'POST':
124+
request = HttpPost(context)
125+
request.setEntity(StringEntity(body))
126+
elif method == 'PUT':
127+
request = HttpPut(context)
128+
request.setEntity(StringEntity(body))
129+
elif method == 'DELETE':
130+
request = HttpDelete(context)
131+
else:
132+
raise Exception('Unsupported method: ' + method)
133+
134+
request.addHeader('Content-Type', contentType)
135+
request.addHeader('Accept', contentType)
136+
self.setCredentials(request)
137+
self.setHeaders(request, headers)
138+
139+
return request
140+
141+
142+
def setCredentials(self, request):
143+
if self.username:
144+
username = self.username
145+
password = self.password
146+
else:
147+
return
148+
149+
encoding = Base64.encodeBase64String(String(username + ':' + password).getBytes())
150+
request.addHeader('Authorization', 'Basic ' + encoding)
151+
152+
153+
def setHeaders(self, request, headers):
154+
if headers:
155+
for key in headers:
156+
request.setHeader(key, headers[key])
157+
158+
159+
def executeRequest(self, request):
160+
client = None
161+
response = None
162+
try:
163+
if (self.verify):
164+
client = HttpClients.createDefault()
165+
else:
166+
client = self.createHttpClient()
167+
168+
response = client.execute(request)
169+
status = response.getStatusLine().getStatusCode()
170+
entity = response.getEntity()
171+
result = EntityUtils.toString(entity, "UTF-8") if entity else None
172+
headers = response.getAllHeaders()
173+
EntityUtils.consume(entity)
174+
175+
return HttpResponse.HttpResponse(status, result, headers)
176+
finally:
177+
if response:
178+
response.close()
179+
if client:
180+
client.close()
181+
182+
183+
def createHttpClient(self):
184+
builder = SSLContextBuilder()
185+
builder.loadTrustMaterial(None, TrustAllStrategy())
186+
187+
tlsVersions = ["TLSv1", "TLSv1.1", "TLSv1.2"]
188+
socketfactory = SSLConnectionSocketFactory(builder.build(), tlsVersions, None, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)
189+
# print 'DEBUG: Created custom HttpClient to trust all certs\n'
190+
return HttpClients.custom().setSSLSocketFactory(socketfactory).build()

src/main/resources/ucd/ListSystemConfiguration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
from ucd.UCDClientUtil import UCD_Client_Util
88

9-
10-
ucd_client = UCD_Client_Util.create_ucd_client(server, username, password)
9+
verifySsl = not server['disableSslVerification']
10+
ucd_client = UCD_Client_Util.create_ucd_client(server, username, password, verifySsl)
1111

1212
systemConfiguration = ucd_client.list_system_configuration()
1313

src/main/resources/ucd/UCDClient.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
#
66

77
import json
8-
from xlrelease.HttpRequest import HttpRequest
8+
from ucd.HttpRequest import HttpRequest
99

1010
class UCD_Client(object):
11-
def __init__(self, http_connection, username=None, password=None):
12-
self.http_request = HttpRequest(http_connection, username, password)
11+
def __init__(self, http_connection, username=None, password=None, verify = True):
12+
self.http_request = HttpRequest(http_connection, username, password, verify)
1313

1414
@staticmethod
15-
def create_client(http_connection, username=None, password=None):
16-
return UCD_Client(http_connection, username, password)
15+
def create_client(http_connection, username=None, password=None, verify = True):
16+
return UCD_Client(http_connection, username, password, verify)
1717

1818
def list_system_configuration(self):
1919
system_configuration_endpoint = "/cli/systemConfiguration"

src/main/resources/ucd/UCDClientUtil.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
class UCD_Client_Util(object):
1111

1212
@staticmethod
13-
def create_ucd_client(container, username, password):
14-
client = UCD_Client.create_client(container, username, password)
15-
return client
13+
def create_ucd_client(container, username, password, verifySsl):
14+
client = UCD_Client.create_client(container, username, password, verifySsl)
15+
return client

0 commit comments

Comments
 (0)