-
Notifications
You must be signed in to change notification settings - Fork 7
/
registration.gradle
206 lines (205 loc) · 10.1 KB
/
registration.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import groovy.json.JsonSlurper
import groovy.json.JsonOutput
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509ExtendedTrustManager;
import java.security.cert.X509Certificate;
class TrustAllManager extends X509ExtendedTrustManager {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) { }
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) { }
public void checkClientTrusted (X509Certificate [] chain, String authType, Socket socket) {
}
public void checkServerTrusted (X509Certificate [] chain, String authType, Socket socket) {
}
public void checkClientTrusted (X509Certificate [] chain, String authType, SSLEngine engine) {
}
public void checkServerTrusted (X509Certificate [] chain, String authType, SSLEngine engine) {
}
}
subprojects {
afterEvaluate {
def isMyMicroServiceApp = project.hasProperty('isServer') && project['isServer'] == true
if (isMyMicroServiceApp) {
def skipCertValidation = {
if (project.hasProperty('ignoreCerts') && (ignoreCerts == 'true')){
logger.warn "Disabling certificate validation!!!"
TrustAllManager[] trustAllCerts = new TrustAllManager[1]
trustAllCerts[0] = new TrustAllManager()
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}
}
def reinitSSL = {
SSLContext sc = SSLContext.getInstance("TLS");
//passing nulls here makes it grab the defaults again
sc.init(null, null, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}
def registrationProperties = new Properties()
def assertPropertyDefined = { propName, conf ->
if(!conf.containsKey(propName)){
throw new GradleException("Missing required property: "+propName)
}
}
tasks.register("assembleRegistrationConfig")//Stub task to be configured by the customer
tasks.register("resolveProperties") {
dependsOn assembleRegistrationConfig
onlyIf {
!project.hasProperty('isTemplate')
}
doLast{
if (project.hasProperty('env')){
file("${buildDir}/resources/dist/payload/envs/${env}.properties").withInputStream { registrationProperties.load(it) }
} else {
ant.input(message: "Provide path of registration properties file:", addproperty: 'envInteractive')
file(ant.envInteractive).withInputStream { registrationProperties.load(it) }
}
}
}
tasks.register("getBearerToken") {
dependsOn resolveProperties
onlyIf {
!project.hasProperty('isTemplate')
}
doLast{
try{
skipCertValidation()
if(registrationProperties.containsKey("authToken")){
logger.info "Token provided, skipping authentication."
} else {
assertPropertyDefined('auth_uri', registrationProperties)
def authUri = registrationProperties.getProperty("auth_uri")
def req = new URL(authUri).openConnection()
req.setRequestMethod("POST")
req.setRequestProperty("User-Agent","ConnectivitySDK")
req.setRequestProperty("Accept", "application/json")
req.setDoOutput(true)
req.setUseCaches(false);
if(authUri.contains("/icp4d-api/v1/authorize")){
//That's a CPD endpoint
assertPropertyDefined('username', registrationProperties)
assertPropertyDefined('password', registrationProperties)
def username = registrationProperties.getProperty("username")
def password = registrationProperties.getProperty("password")
req.setRequestProperty("Content-Type", "application/json")
def body = new JsonSlurper().parseText('{ "username":"","password":""}')
body.username = username
body.password = password
def byteBody = JsonOutput.toJson(body).getBytes("UTF-8")
req.setRequestProperty("Content-Length", Integer.toString(byteBody.length));
req.getOutputStream().write(byteBody)
}else{
//Assume cloud
assertPropertyDefined('apikey', registrationProperties)
def apikey = registrationProperties.getProperty("apikey")
req.setRequestProperty("Content-Type", "application/x-www-form-urlencoded")
def urlParams = ("grant_type=urn:ibm:params:oauth:grant-type:apikey&apikey="+apikey).getBytes("UTF-8")
req.setRequestProperty("Content-Length", Integer.toString(urlParams.length));
req.getOutputStream().write(urlParams)
}
logger.debug "Status code: ${req.getResponseCode()}" // HTTP request done on first read
if(100 <= req.getResponseCode() && req.getResponseCode() <= 399) {
def resp = new JsonSlurper().parseText(req.getInputStream().getText())
logger.debug "Response: ${resp}"
if(resp.access_token != null){
registrationProperties.setProperty("authToken", resp.access_token)
} else {
//CPD returns bearer token under token rather than access_token
registrationProperties.setProperty("authToken", resp.token)
}
}else {
throw new GradleException(new JsonSlurper().parseText(req.getErrorStream().getText()).toString())
}
}
} finally {
reinitSSL()
}
}
}
tasks.register("register") {
dependsOn getBearerToken
group 'Registration'
onlyIf {
!project.hasProperty('isTemplate')
}
doLast {
try {
skipCertValidation()
assertPropertyDefined("flight_uri", registrationProperties)
assertPropertyDefined("datasource_types_uri", registrationProperties)
def reqUrl = new URL(registrationProperties.getProperty("datasource_types_uri"))
def req = reqUrl.openConnection()
req.setRequestMethod("POST")
req.setRequestProperty("User-Agent","ConnectivitySDK")
req.setRequestProperty("Authorization", "Bearer "+ registrationProperties.getProperty("authToken"))
req.setRequestProperty("Accept-Language", "en")
req.setRequestProperty("Accept-Encoding", "gzip, deflate, br")
req.setRequestProperty("Accept", "application/json")
req.setRequestProperty("Content-Type", "application/json")
def body = new JsonSlurper().parseText('{ "origin_country":"us", "flight_info": { "flight_uri": "", "ssl_certificate": "","ssl_certificate_validation": false } }')
body.flight_info.flight_uri = registrationProperties.getProperty("flight_uri")
if(registrationProperties.containsKey("ssl_certificate")){
body.flight_info.ssl_certificate = file(registrationProperties.getProperty("ssl_certificate")).text
}
if(registrationProperties.containsKey("ssl_certificate_validation")){
body.flight_info.ssl_certificate_validation = registrationProperties.getProperty("ssl_certificate_validation")
}
req.setDoOutput(true)
req.setUseCaches(false);
def byteBody = JsonOutput.toJson(body).getBytes("UTF-8")
req.setRequestProperty("Content-Length", Integer.toString(byteBody.length));
println "Executing HTTP request: POST ${reqUrl}"
req.getOutputStream().write(byteBody)
logger.debug "Status code: ${req.getResponseCode()}" // HTTP request done on first read
if(100 <= req.getResponseCode() && req.getResponseCode() <= 399) {
logger.debug "Response: "+ new JsonSlurper().parseText(req.getInputStream().getText().toString())
}else {
throw new GradleException(new JsonSlurper().parseText(req.getErrorStream().getText()).toString())
}
} finally {
reinitSSL()
}
}
}
tasks.register("unregister") {
dependsOn getBearerToken
group 'Registration'
doLast {
try{
skipCertValidation()
def datasourceType = ""
if (project.hasProperty('datasourceType')){
datasourceType = project.getProperty('datasourceType')
} else {
ant.input(message: "Provide id of datasource type you want to remove:", addproperty: 'datasourceTypeInteractive')
datasourceType = ant.datasourceTypeInteractive
}
assertPropertyDefined("datasource_types_uri", registrationProperties)
def req = new URL(registrationProperties.getProperty("datasource_types_uri")+"/"+datasourceType).openConnection()
req.setRequestMethod("DELETE")
req.setRequestProperty("User-Agent","ConnectivitySDK")
req.setRequestProperty("Authorization", "Bearer "+ registrationProperties.getProperty("authToken"))
req.setRequestProperty("Accept-Language", "en")
req.setRequestProperty("Accept-Encoding", "gzip, deflate, br")
req.setUseCaches(false);
logger.debug "Status code: ${req.getResponseCode()}" // HTTP request done on first read
if(req.getResponseCode() == 204) {
logger.debug "Asset "+datasourceType+" was deleted."
}else {
throw new GradleException(new JsonSlurper().parseText(req.getErrorStream().getText()).toString())
}
} finally {
reinitSSL()
}
}
}
}
}
}