in gateway-discovery-ambari/src/main/java/org/apache/knox/gateway/topology/discovery/ambari/RESTInvoker.java [101:177]
JSONObject invoke(String url, String username, String passwordAlias) {
JSONObject result = null;
try {
HttpGet request = new HttpGet(url);
// If no configured username, then use default username alias
String password = null;
if (username == null) {
if (aliasService != null) {
try {
char[] defaultUser = aliasService.getPasswordFromAliasForGateway(DEFAULT_USER_ALIAS);
if (defaultUser != null) {
username = new String(defaultUser);
}
} catch (AliasServiceException e) {
log.aliasServiceUserError(DEFAULT_USER_ALIAS, e.getLocalizedMessage());
}
}
// If username is still null
if (username == null) {
log.aliasServiceUserNotFound();
throw new ConfigurationException("No username is configured for Ambari service discovery.");
}
}
if (aliasService != null) {
// If no password alias is configured, then try the default alias
if (passwordAlias == null) {
passwordAlias = DEFAULT_PWD_ALIAS;
}
try {
char[] pwd = aliasService.getPasswordFromAliasForGateway(passwordAlias);
if (pwd != null) {
password = new String(pwd);
}
} catch (AliasServiceException e) {
log.aliasServicePasswordError(passwordAlias, e.getLocalizedMessage());
}
}
// If the password could not be determined
if (password == null) {
log.aliasServicePasswordNotFound();
throw new ConfigurationException("No password is configured for Ambari service discovery.");
}
// Add an auth header if credentials are available
String encodedCreds = Base64.getEncoder().encodeToString((username + ":" + password).getBytes(StandardCharsets.UTF_8));
request.addHeader(new BasicHeader("Authorization", "Basic " + encodedCreds));
// Ambari CSRF protection
request.addHeader("X-Requested-By", "Knox");
try(CloseableHttpResponse response = httpClient.execute(request)){
if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {
HttpEntity entity = response.getEntity();
if (entity != null) {
result = (JSONObject) JSONValue.parse((EntityUtils.toString(entity)));
log.debugJSON(result.toJSONString());
} else {
log.noJSON(url);
}
} else {
log.unexpectedRestResponseStatusCode(url, response.getStatusLine().getStatusCode());
}
}
} catch (ConnectTimeoutException e) {
log.restInvocationTimedOut(url, e);
} catch (IOException e) {
log.restInvocationError(url, e);
}
return result;
}