Source:
private AuthConfig getAuthConfigFromEC2InstanceRole() throws IOException { log.debug("No user and password set for ECR, checking EC2 instance role"); try (CloseableHttpClient client = HttpClients.custom().useSystemProperties().build()) { RequestConfig conf = RequestConfig.custom().setConnectionRequestTimeout(1000) .setConnectTimeout(1000) .setSocketTimeout(1000).build(); HttpGet request = new HttpGet("<LINK_0>"); request.setConfig(conf); String instanceRole; try (CloseableHttpResponse response = client.execute(request)) { if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { log.debug("No instance role found, return code was %d", response.getStatusLine().getStatusCode()); return null; } try (InputStream is = response.getEntity().getContent()) { instanceRole = IOUtils.toString(is, StandardCharsets.UTF_8); } } log.debug("Found instance role %s, getting temporary security credentials", instanceRole); request = new HttpGet( "<LINK_0>/" + instanceRole); request.setConfig(conf); try (CloseableHttpResponse response = client.execute(request)) { if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { log.debug("No security credential found, return code was %d", response.getStatusLine().getStatusCode()); return null; } try (Reader r = new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8)) { JsonObject securityCredentials = new Gson().fromJson(r, JsonObject.class); String user = securityCredentials.getAsJsonPrimitive("AccessKeyId").getAsString(); String password = securityCredentials.getAsJsonPrimitive("SecretAccessKey").getAsString(); String token = securityCredentials.getAsJsonPrimitive("Token").getAsString(); log.debug("Received temporary access key %s...", user.substring(0, 8)); return new AuthConfig(user, password, "none", token); } } } }

Target:
private AuthConfig getAuthConfigFromEC2InstanceRole() throws IOException { log.debug("No user and password set for ECR, checking EC2 instance role"); try (CloseableHttpClient client = HttpClients.custom().useSystemProperties().build()) { RequestConfig conf = RequestConfig.custom().setConnectionRequestTimeout(1000).setConnectTimeout(1000) .setSocketTimeout(1000).build(); HttpGet request = new HttpGet("<LINK_0>"); request.setConfig(conf); String instanceRole; try (CloseableHttpResponse response = client.execute(request)) { if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { log.debug("No instance role found, return code was %d", response.getStatusLine().getStatusCode()); return null; } try (InputStream is = response.getEntity().getContent()) { instanceRole = IOUtils.toString(is, StandardCharsets.UTF_8); } } log.debug("Found instance role %s, getting temporary security credentials", instanceRole); request = new HttpGet("<LINK_0>/" + UrlEscapers.urlPathSegmentEscaper().escape(instanceRole)); request.setConfig(conf); try (CloseableHttpResponse response = client.execute(request)) { if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { log.debug("No security credential found, return code was %d", response.getStatusLine().getStatusCode()); return null; } try (Reader r = new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8)) { JsonObject securityCredentials = new Gson().fromJson(r, JsonObject.class); String user = securityCredentials.getAsJsonPrimitive("AccessKeyId").getAsString(); String password = securityCredentials.getAsJsonPrimitive("SecretAccessKey").getAsString(); String token = securityCredentials.getAsJsonPrimitive("Token").getAsString(); log.debug("Received temporary access key %s...", user.substring(0, 8)); return new AuthConfig(user, password, "none", token); } } } }