use of com.aws.greengrass.config.Subscriber in project aws-greengrass-nucleus by aws-greengrass.
the class CredentialRequestHandlerTest method GIVEN_credential_handler_WHEN_called_handle_THEN_returns_creds.
@Test
@SuppressWarnings("PMD.CloseResource")
void GIVEN_credential_handler_WHEN_called_handle_THEN_returns_creds() throws Exception {
ArgumentCaptor<String> pathCaptor = ArgumentCaptor.forClass(String.class);
when(mockCloudHelper.sendHttpRequest(any(), any(), pathCaptor.capture(), any(), any())).thenReturn(CLOUD_RESPONSE);
when(mockAuthNHandler.doAuthentication(anyString())).thenReturn("ServiceA");
when(mockAuthZHandler.isAuthorized(any(), any())).thenReturn(true);
ArgumentCaptor<Subscriber> subscriberArgumentCaptor = ArgumentCaptor.forClass(Subscriber.class);
when(mockDeviceConfig.getIotRoleAlias().subscribe(subscriberArgumentCaptor.capture())).thenReturn(null);
CredentialRequestHandler handler = new CredentialRequestHandler(mockCloudHelper, mockConnectionManager, mockAuthNHandler, mockAuthZHandler, mockDeviceConfig);
handler.setIotCredentialsPath(ROLE_ALIAS);
when(mockAuthNHandler.doAuthentication(anyString())).thenReturn("ServiceA");
Headers mockHeaders = mock(Headers.class);
when(mockHeaders.getFirst(any())).thenReturn(AUTHN_TOKEN);
when(mockExchange.getResponseBody()).thenReturn(mockStream);
when(mockExchange.getRequestHeaders()).thenReturn(mockHeaders);
when(mockExchange.getRequestURI()).thenReturn(TES_URI);
when(mockExchange.getRequestMethod()).thenReturn(REQUEST_METHOD);
handler.handle(mockExchange);
int expectedStatus = 200;
byte[] serializedResponse = getExpectedResponse();
int expectedResponseLength = serializedResponse.length;
verify(mockExchange, times(1)).sendResponseHeaders(expectedStatus, expectedResponseLength);
verify(mockStream, times(1)).write(serializedResponse);
mockStream.close();
subscriberArgumentCaptor.getValue().published(WhatHappened.childChanged, Topic.of(mock(Context.class), "role", "role"));
handler.getAwsCredentialsBypassCache();
assertThat(pathCaptor.getValue(), containsString("role"));
}
use of com.aws.greengrass.config.Subscriber in project aws-greengrass-nucleus by aws-greengrass.
the class TokenExchangeServiceTest method GIVEN_token_exchange_service_WHEN_started_THEN_correct_env_set.
@ParameterizedTest
@ValueSource(ints = { 0, 3000 })
void GIVEN_token_exchange_service_WHEN_started_THEN_correct_env_set(int port) throws Exception {
Topic portTopic = mock(Topic.class);
when(portTopic.dflt(anyInt())).thenReturn(portTopic);
when(portTopic.subscribe(any())).thenAnswer((a) -> {
((Subscriber) a.getArgument(0)).published(WhatHappened.initialized, portTopic);
return null;
});
when(portTopic.getOnce()).thenReturn(port);
Topic roleTopic = mock(Topic.class);
when(roleTopic.subscribe(any())).thenAnswer((a) -> {
((Subscriber) a.getArgument(0)).published(WhatHappened.initialized, roleTopic);
return null;
});
when(roleTopic.getOnce()).thenReturn(MOCK_ROLE_ALIAS);
when(roleTopic.withValue(anyString())).thenReturn(roleTopic);
when(roleTopic.dflt(anyString())).thenReturn(roleTopic);
Topic mockUriTopic = mock(Topic.class);
Topics mockConfig = mock(Topics.class);
when(config.getRoot()).thenReturn(mockConfig);
when(config.lookup(CONFIGURATION_CONFIG_KEY, PORT_TOPIC)).thenReturn(portTopic);
when(config.lookup(CONFIGURATION_CONFIG_KEY, ACTIVE_PORT_TOPIC)).thenReturn(portTopic);
when(mockConfig.lookup(SETENV_CONFIG_NAMESPACE, TES_URI_ENV_VARIABLE_NAME)).thenReturn(mockUriTopic);
when(configuration.lookup(SERVICES_NAMESPACE_TOPIC, DEFAULT_NUCLEUS_COMPONENT_NAME, CONFIGURATION_CONFIG_KEY, IOT_ROLE_ALIAS_TOPIC)).thenReturn(roleTopic);
TokenExchangeService tes = new TokenExchangeService(config, mockCredentialHandler, mockAuthZHandler, executorService, deviceConfigurationWithRoleAlias(MOCK_ROLE_ALIAS));
tes.postInject();
tes.startup();
Thread.sleep(5000L);
tes.shutdown();
verify(mockUriTopic).overrideValue(stringArgumentCaptor.capture());
String tesUrl = stringArgumentCaptor.getValue();
URI uri = new URI(tesUrl);
assertEquals("localhost", uri.getHost());
assertEquals("/2016-11-01/credentialprovider/", uri.getPath());
if (port == 0) {
// If port is 0, then service should url should be set to random port
assertTrue(uri.getPort() > 0);
} else {
assertEquals(port, uri.getPort());
}
verify(mockAuthZHandler).registerComponent(stringArgumentCaptor.capture(), operationsCaptor.capture());
assertEquals(TOKEN_EXCHANGE_SERVICE_TOPICS, stringArgumentCaptor.getValue());
assertTrue(operationsCaptor.getValue().contains(TokenExchangeService.AUTHZ_TES_OPERATION));
}
Aggregations