-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathAppAutomateClient.java
More file actions
343 lines (312 loc) · 12.4 KB
/
AppAutomateClient.java
File metadata and controls
343 lines (312 loc) · 12.4 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package com.browserstack.appautomate;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.util.List;
import com.browserstack.automate.Automate.BuildStatus;
import com.browserstack.automate.exception.AppAutomateException;
import com.browserstack.automate.exception.BuildNotFound;
import com.browserstack.automate.exception.InvalidFileExtensionException;
import com.browserstack.automate.exception.SessionNotFound;
import com.browserstack.automate.model.AppUploadResponse;
import com.browserstack.automate.model.Build;
import com.browserstack.automate.model.Session;
import com.browserstack.client.BrowserStackClient;
import com.browserstack.client.BrowserStackRequest;
import com.browserstack.client.exception.BrowserStackException;
import com.browserstack.client.util.Tools;
import com.google.api.client.http.FileContent;
import com.google.api.client.http.HttpHeaders;
import com.google.api.client.http.HttpMediaType;
import com.google.api.client.http.MultipartContent;
import javax.annotation.Nonnull;
public class AppAutomateClient extends BrowserStackClient implements AppAutomate {
private static final String BASE_URL = "https://api-cloud.browserstack.com/app-automate";
/**
*
* @param username Browserstack username
* @param accessKey Browserstack accessKey
*/
public AppAutomateClient(String username, String accessKey) {
super(validateApiBaseUrl(System.getProperty("browserstack.app-automate.api", BASE_URL)),
username, accessKey);
}
/**
* Gets the session associated with the specified identifier.
*
* @param sessionId ID that uniquely identifies a session.
* @return {@link Session} objects containing test session information.
* @throws SessionNotFound session not found error
* @throws AppAutomateException exception for AppAutomate sessions.
*/
@Override
public Session getSession(String sessionId) throws SessionNotFound, AppAutomateException {
try {
return super.getSession(sessionId);
} catch (BrowserStackException e) {
throw new AppAutomateException(e);
}
}
/**
* Gets the filePath of app to be uploaded.
*
* @param filePath absolute path of app to be uploaded.
* @return AppUploadResponse object containing app upload response details.
* @throws AppAutomateException exception for AppAutomate sessions.
* @throws FileNotFoundException file not found at given path
* @throws InvalidFileExtensionException unsupported file extension for app
*/
@Override
public AppUploadResponse uploadApp(String filePath)
throws AppAutomateException, FileNotFoundException, InvalidFileExtensionException {
try {
File file = new File(filePath);
if (!file.exists()) {
throw new FileNotFoundException("File not found at " + filePath);
}
// APS-19024 / INJ-003: canonicalize before validating extension. Without this,
// a symlink such as /tmp/legit.apk -> /etc/passwd would pass a suffix-only
// check on the supplied path and result in an arbitrary local file being
// streamed to the upload endpoint. getCanonicalFile() resolves symlinks
// and ".." segments; we then check the extension on the canonical name.
final File canonical;
try {
canonical = file.getCanonicalFile();
} catch (IOException e) {
throw new AppAutomateException("Unable to canonicalize file path: " + e.getMessage(), 0);
}
final String canonicalName = canonical.getName().toLowerCase();
if (!canonicalName.endsWith(".apk") && !canonicalName.endsWith(".ipa")) {
throw new InvalidFileExtensionException("File extension should be only .apk or .ipa.");
}
file = canonical;
MultipartContent content = new MultipartContent().setMediaType(
new HttpMediaType("multipart/form-data").setParameter("boundary", "__END_OF_PART__"));
FileContent fileContent = new FileContent("multipart/form-data", file);
MultipartContent.Part part = new MultipartContent.Part(fileContent);
part.setHeaders(new HttpHeaders().set("Content-Disposition",
String.format("form-data; name=\"file\"; filename=\"%s\"", file.getName())));
content.addPart(part);
BrowserStackRequest request = newRequest(Method.POST, "/upload");
// Setting read timeout to 0(infinity), as for large files it takes a lot of time.
request.getHttpRequest().setReadTimeout(0);
AppUploadResponse appUploadResponse = request.body(content).asObject(AppUploadResponse.class);
if (appUploadResponse == null || Tools.isStringEmpty(appUploadResponse.getAppUrl())) {
throw new AppAutomateException("App upload failed!", 0);
}
return appUploadResponse;
} catch (BrowserStackException e) {
throw new AppAutomateException(e);
}
}
/**
* Gets the list of builds.
*
* <p>
* A build is an organizational structure for tests.
* </p>
*
* @param status Return only builds that match the specified build status.
* @param limit Limit results to the specified count.
* @return List of {@link Build} objects.
* @throws AppAutomateException exception for AppAutomate sessions.
*/
@Override
public List<Build> getBuilds(final BuildStatus status, final int limit)
throws AppAutomateException {
try {
return super.getBuilds(status, limit);
} catch (BrowserStackException e) {
throw new AppAutomateException(e);
}
}
/**
* Gets the list of builds.
*
* <p>
* A build is an organizational structure for tests.
* </p>
*
* @return List of {@link Build} objects.
* @throws AppAutomateException exception for AppAutomate sessions.
*/
@Override
public List<Build> getBuilds() throws AppAutomateException {
return getBuilds(null, 0);
}
/**
* Gets the list of builds.
*
* <p>
* A build is an organizational structure for tests.
* </p>
*
* @param limit Limit results to the specified count.
* @return List of {@link Build} objects.
* @throws AppAutomateException exception for AppAutomate sessions.
*/
@Override
public List<Build> getBuilds(final int limit) throws AppAutomateException {
return getBuilds(null, limit);
}
/**
* Gets the list of builds.
*
* <p>
* A build is an organizational structure for tests.
* </p>
*
* @param status Include only builds that match the specified build status.
* @return List of {@link Build} objects.
* @throws AppAutomateException exception for AppAutomate sessions.
*/
@Override
public List<Build> getBuilds(final BuildStatus status) throws AppAutomateException {
return getBuilds(status, 0);
}
/**
* Gets the build identified by the build identifier.
*
* @param buildId ID that uniquely identifies a build.
* @return List of {@link Build} objects.
* @throws BuildNotFound could not find build with given id
* @throws AppAutomateException exception for AppAutomate sessions.
*/
@Override
public Build getBuild(final String buildId) throws BuildNotFound, AppAutomateException {
try {
return super.getBuild(buildId);
} catch (BrowserStackException e) {
throw new AppAutomateException(e);
}
}
/**
* Gets the build identified by the build name.
*
* @param buildName - Name of the build to search with
* @return {@link Build} object.
* @throws BuildNotFound could not find build with given id
* @throws AppAutomateException exception for AppAutomate sessions.
*/
@Override
public final Build getBuildByName(@Nonnull final String buildName) throws BuildNotFound, AppAutomateException {
try {
return super.getBuildByName(buildName);
} catch (BrowserStackException e) {
throw new AppAutomateException(e);
}
}
/**
* Delete the build identified by the build identifier.
*
* @param buildId ID that uniquely identifies a build.
* @return true or false based on successful deletion of the build.
* @throws AppAutomateException exception for AppAutomate sessions.
*/
@Override
public boolean deleteBuild(final String buildId) throws AppAutomateException {
try {
return super.deleteBuild(buildId);
} catch (BrowserStackException e) {
throw new AppAutomateException(e);
}
}
/**
* Retrieves the list of sessions existing under a specific build.
*
* @param buildId ID that uniquely identifies a build.
* @param status Include only builds that match the specified build status.
* @param limit Limit results to the specified count.
* @return List of {@link Session} objects containing test session information.
* @throws BuildNotFound could not find build with given id
* @throws AppAutomateException exception for AppAutomate sessions.
*/
@Override
public List<Session> getSessions(final String buildId, final BuildStatus status, final int limit)
throws BuildNotFound, AppAutomateException {
try {
return super.getSessions(buildId, status, limit);
} catch (BrowserStackException e) {
throw new AppAutomateException(e);
}
}
/**
* Retrieves the list of sessions existing under a specific build.
*
* @param buildId ID that uniquely identifies a build.
* @return List of {@link Session} objects containing test session information.
* @throws BuildNotFound could not find build with given id
* @throws AppAutomateException exception for AppAutomate sessions.
*/
@Override
public List<Session> getSessions(final String buildId)
throws BuildNotFound, AppAutomateException {
return getSessions(buildId, null, 0);
}
/**
* Retrieves the list of sessions existing under a specific build.
*
* @param buildId ID that uniquely identifies a build.
* @param limit Limit results to the specified count.
* @return List of {@link Session} objects containing test session information.
* @throws BuildNotFound could not find build with given id
* @throws AppAutomateException exception for AppAutomate sessions.
*/
@Override
public List<Session> getSessions(final String buildId, final int limit)
throws BuildNotFound, AppAutomateException {
return getSessions(buildId, null, limit);
}
/**
* Retrieves the list of sessions existing under a specific build.
*
* @param buildId ID that uniquely identifies a build.
* @param status Include only builds that match the specified build status.
* @return List of {@link Session} objects containing test session information.
* @throws BuildNotFound could not find build with given id
* @throws AppAutomateException exception for AppAutomate sessions.
*/
@Override
public List<Session> getSessions(final String buildId, final BuildStatus status)
throws BuildNotFound, AppAutomateException {
return getSessions(buildId, status, 0);
}
/**
* Validates the App Automate API base URL read from the JVM system property
* {@code browserstack.app-automate.api} (or the built-in default).
* <p>
* Defends against SSRF (APS-19024 / INJ-002): an attacker who can set the
* JVM property could otherwise redirect every signed API request — and the
* Basic Auth credentials those requests carry — to an attacker-controlled
* host. Only {@code https} URLs whose host (parsed via
* {@link java.net.URI#getHost()}) ends in {@code .browserstack.com} are
* accepted.
*
* @param url URL to validate. Must be a syntactically-valid absolute URL.
* @return the same URL, unchanged, when validation passes.
* @throws IllegalArgumentException if the URL is null, malformed, uses a
* non-https scheme, or has a host outside
* {@code *.browserstack.com}.
*/
private static String validateApiBaseUrl(String url) {
if (url == null) {
throw new IllegalArgumentException("App Automate API base URL is null");
}
final URI uri;
try {
uri = URI.create(url);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Malformed App Automate API base URL", e);
}
final String scheme = uri.getScheme();
if (!"https".equalsIgnoreCase(scheme)) {
throw new IllegalArgumentException("Insecure App Automate API base URL scheme: " + scheme);
}
final String host = uri.getHost();
if (host == null || !host.toLowerCase().endsWith(".browserstack.com")) {
throw new IllegalArgumentException("Untrusted App Automate API base URL host: " + host);
}
return url;
}
}