InstanceMetadataServiceResourceFetcher extends EC2ResourceFetcher { @Override public String readResource(URI endpoint, CredentialsEndpointRetryPolicy retryPolicy, Map<String, String> headers) { if (SDKGlobalConfiguration.isEc2MetadataDisabled()) { throw new AmazonClientException("EC2 Instance Metadata Service is disabled"); } Map<String, String> newHeaders = addDefaultHeaders(headers); String token = getToken(); if (token != null) { newHeaders.put(TOKEN_HEADER, token); } return doReadResource(endpoint, retryPolicy, newHeaders); } private  InstanceMetadataServiceResourceFetcher(); @SdkTestInternalApi  InstanceMetadataServiceResourceFetcher(ConnectionUtils connectionUtils); static InstanceMetadataServiceResourceFetcher getInstance(); @Override String readResource(URI endpoint, CredentialsEndpointRetryPolicy retryPolicy, Map<String, String> headers);  }
@Test public void metadataDisabled_shouldThrowException() { System.setProperty(SDKGlobalConfiguration.AWS_EC2_METADATA_DISABLED_SYSTEM_PROPERTY, "true"); try { resourceFetcher.readResource(endpoint); fail("no exception"); } catch (AmazonClientException exception) { assertTrue(exception.getMessage().contains("EC2 Instance Metadata Service is disabled")); } System.clearProperty(SDKGlobalConfiguration.AWS_EC2_METADATA_DISABLED_SYSTEM_PROPERTY); }
@Test public void token200_shouldSendToken() { generateSuccessfulTokenStub(); generateStub(200, SUCCESS_BODY); assertEquals(SUCCESS_BODY, resourceFetcher.readResource(endpoint)); verifySecureWorkflowHeaders(); }
@Test public void token404_shouldFallbackInsecureWorkflow() { generateErrorTokenStub(404); generateStub(200, SUCCESS_BODY); assertEquals(SUCCESS_BODY, resourceFetcher.readResource(endpoint)); verifyInsecureWorkflowHeaders(); }
@Test public void tokenNonRetryableError_shouldFallbackInsecureWorkflow() { generateErrorTokenStub(405); generateStub(200, SUCCESS_BODY); assertEquals(SUCCESS_BODY, resourceFetcher.readResource(endpoint)); verifyInsecureWorkflowHeaders(); }
@Test public void token400_shouldThrowException() { generateErrorTokenStub(400); try { resourceFetcher.readResource(endpoint); fail("no exception"); } catch (AmazonServiceException exception) { assertEquals(exception.getStatusCode(), 400); } verify(putRequestedFor(urlPathEqualTo(TOKEN_PATH)).withHeader("x-aws-ec2-metadata-token-ttl-seconds", equalTo("21600"))); }
@Test public void token503_shouldThrowException() { generateErrorTokenStub(503); try { resourceFetcher.readResource(endpoint); fail("no exception"); } catch (AmazonServiceException exception) { assertEquals(exception.getStatusCode(), 503); } verify(putRequestedFor(urlPathEqualTo(TOKEN_PATH)).withHeader("x-aws-ec2-metadata-token-ttl-seconds", equalTo("21600"))); }
@Test public void token403_shouldFallbackInsecureWorkflow() { generateErrorTokenStub(403); generateStub(200, SUCCESS_BODY); assertEquals(SUCCESS_BODY, resourceFetcher.readResource(endpoint)); verifyInsecureWorkflowHeaders(); }
@Test public void tokenTimeoutException_shouldFallbackInsecureWorkflow() throws IOException { generateStub(200, SUCCESS_BODY); Mockito.when(mockConnection.connectToEndpoint(eq(tokenEndpoint), any(Map.class), eq("PUT"))) .thenThrow(new SocketTimeoutException()); Mockito.when(mockConnection.connectToEndpoint(eq(endpoint), any(Map.class), eq("GET"))) .thenCallRealMethod(); assertEquals(SUCCESS_BODY, new InstanceMetadataServiceResourceFetcher(mockConnection).readResource(endpoint)); verify(getRequestedFor(urlPathEqualTo(CREDENTIALS_PATH)).withoutHeader("x-aws-ec2-metadata-token")); }
@Test public void tokenOtherIOException_shouldThrowException() throws IOException { IOException ioException = new IOException("foobar"); Mockito.when(mockConnection.connectToEndpoint(eq(tokenEndpoint), any(Map.class), eq("PUT"))).thenThrow(ioException); try { new InstanceMetadataServiceResourceFetcher(mockConnection).readResource(endpoint); fail("no exception"); } catch (SdkClientException exception) { assertEquals(exception.getCause(), ioException); } }
@Test public void metadataNonRetryableError_shouldThrowException() { generateSuccessfulTokenStub(); generateStub(404, ""); try { resourceFetcher.readResource(endpoint); fail("no exception"); } catch (SdkClientException exception) { assertTrue(exception.getMessage().contains("The requested metadata is not found at")); } verifySecureWorkflowHeaders(); }
@Test public void metadata500_shouldThrowException() { generateSuccessfulTokenStub(); generateStub(500, "{\"code\":\"500 Internal Server Error\",\"message\":\"ERROR_MESSAGE\"}"); try { resourceFetcher.readResource(endpoint); fail("Expected AmazonServiceException"); } catch (AmazonServiceException ase) { assertEquals(500, ase.getStatusCode()); assertEquals("500 Internal Server Error", ase.getErrorCode()); assertEquals("ERROR_MESSAGE", ase.getErrorMessage()); } }
@Test public void metadata401_shouldThrowException() throws IOException { generateSuccessfulTokenStub(); generateStub(401, "{\"code\":\"401 Unauthorized\",\"message\":\"ERROR_MESSAGE\"}"); try { resourceFetcher.readResource(endpoint); fail("Expected AmazonServiceException"); } catch (AmazonServiceException ase) { assertEquals(401, ase.getStatusCode()); assertEquals("401 Unauthorized", ase.getErrorCode()); assertEquals("ERROR_MESSAGE", ase.getErrorMessage()); } }