/
ArmApi.java
229 lines (199 loc) · 7.67 KB
/
ArmApi.java
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com
* The software in this package is published under the terms of the CPAL v1.0
* license, a copy of which has been included with this distribution in the
* LICENSE.txt file.
*/
package org.mule.tools.maven.plugin.mule.arm;
import org.mule.tools.maven.plugin.mule.AbstractMuleApi;
import org.mule.tools.maven.plugin.mule.TargetType;
import java.io.File;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;
import org.apache.maven.plugin.logging.Log;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.glassfish.jersey.media.multipart.MultiPart;
import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
public class ArmApi extends AbstractMuleApi
{
private static final String APPLICATIONS = "/hybrid/api/v1/applications";
private static final String SERVERS = "/hybrid/api/v1/servers";
private static final String SERVER_GROUPS = "/hybrid/api/v1/serverGroups";
private static final String CLUSTERS = "/hybrid/api/v1/clusters";
private boolean armInsecure;
public ArmApi(Log log, String uri, String username, String password, String environment, String businessGroup, boolean armInsecure)
{
super(uri, log, username, password, environment, businessGroup);
this.armInsecure = armInsecure;
if (armInsecure)
{
log.warn("Using insecure mode for connecting to ARM, please consider configuring your truststore with ARM certificates. This option is insecure and not intended for production use.");
}
}
public Boolean isStarted(int applicationId)
{
Application application = getApplicationStatus(applicationId);
return "STARTED".equals(application.data.lastReportedStatus);
}
public Application getApplicationStatus(int applicationId)
{
return get(uri, APPLICATIONS + "/" + applicationId, Application.class);
}
public String undeployApplication(int applicationId)
{
Response response = delete(uri, APPLICATIONS + "/" + applicationId);
validateStatusSuccess(response);
return response.readEntity(String.class);
}
public String undeployApplication(String appName, TargetType targetType, String target)
{
Integer applicationId = findApplication(appName, targetType, target);
if (applicationId == null)
{
String appNotFoundMessage = "Application %s does not exist on %s %s.";
throw new NotFoundException(String.format(appNotFoundMessage, appName, targetType.toString(), target));
}
return undeployApplication(applicationId);
}
public Application deployApplication(File app, String appName, TargetType targetType, String target)
{
MultiPart body = buildRequestBody(app, appName, targetType, target);
Response response = post(uri, APPLICATIONS, Entity.entity(body, body.getMediaType()));
validateStatusSuccess(response);
return response.readEntity(Application.class);
}
public Application redeployApplication(int applicationId, File app, String appName, TargetType targetType, String target)
{
MultiPart body = buildRequestBody(app, appName, targetType, target);
Response response = patch(uri, APPLICATIONS + "/" + applicationId, Entity.entity(body, body.getMediaType()));
validateStatusSuccess(response);
return response.readEntity(Application.class);
}
private MultiPart buildRequestBody(File app, String appName, TargetType targetType, String target)
{
String id = getId(targetType, target);
FileDataBodyPart applicationPart = new FileDataBodyPart("file", app);
MultiPart body = new FormDataMultiPart()
.field("artifactName", appName)
.field("targetId", id)
.bodyPart(applicationPart);
return body;
}
private String getId(TargetType targetType, String target)
{
String id = null;
switch (targetType)
{
case server:
id = findServerByName(target).id;
break;
case serverGroup:
id = findServerGroupByName(target).id;
break;
case cluster:
id = findClusterByName(target).id;
break;
}
return id;
}
public Applications getApplications()
{
return get(uri, APPLICATIONS, Applications.class);
}
public Target findServerByName(String name)
{
return findTargetByName(name, SERVERS);
}
public Target findServerGroupByName(String name)
{
return findTargetByName(name, SERVER_GROUPS);
}
public Target findClusterByName(String name)
{
return findTargetByName(name, CLUSTERS);
}
private Target findTargetByName(String name, String path)
{
Targets response = get(uri, path, Targets.class);
if (response.data == null) // Workaround because an empty array in the response is mapped as null
{
throw new RuntimeException("Couldn't find target named [" + name + "]");
}
for (int i = 0; i < response.data.length; i++)
{
if (name.equals(response.data[i].name))
{
return response.data[i];
}
}
throw new RuntimeException("Couldn't find target named [" + name + "]");
}
public Integer findApplication(String name, TargetType targetType, String target)
{
Applications apps = getApplications();
Data[] appArray = getApplications().data;
if (appArray == null)
{
return null;
}
String targetId = getId(targetType, target);
for (int i = 0 ; i < appArray.length ; i ++ )
{
if (name.equals(appArray[i].artifact.name) && targetId.equals(appArray[i].target.id))
{
return appArray[i].id;
}
}
return null;
}
protected void configureSecurityContext(ClientBuilder builder)
{
if (armInsecure)
{
try
{
SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(null, new TrustManager[] { new TrustAllManager() }, new java.security.SecureRandom());
builder.hostnameVerifier(new DummyHostnameVerifier()).sslContext(sslcontext);
}
catch (KeyManagementException | NoSuchAlgorithmException e)
{
throw new RuntimeException(e);
}
}
}
private static class DummyHostnameVerifier implements HostnameVerifier
{
public boolean verify(String s, SSLSession sslSession)
{
return true;
}
};
private static class TrustAllManager implements X509TrustManager
{
@Override
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException
{
}
@Override
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException
{
}
@Override
public X509Certificate[] getAcceptedIssuers()
{
return new X509Certificate[0];
}
};
}