use of com.aws.greengrass.config.Node in project aws-greengrass-nucleus by aws-greengrass.
the class IPCAuthorizationTest method GIVEN_authorizationClient_WHEN_valid_ondemand_lambda_token_provided_THEN_succeeds.
@Test
void GIVEN_authorizationClient_WHEN_valid_ondemand_lambda_token_provided_THEN_succeeds() {
GreengrassService mockService = mock(GreengrassService.class);
when(mockService.getServiceName()).thenReturn("ABCService");
// Pretend to be instance #1 of a lambda
lenient().when(mockService.getName()).thenReturn("ABCService#1");
when(mockService.getServiceConfig()).thenReturn(kernel.findServiceTopic("ServiceName"));
when(mockService.getPrivateConfig()).thenReturn(kernel.findServiceTopic("ServiceName").lookupTopics(PRIVATE_STORE_NAMESPACE_TOPIC));
registerAuthenticationToken(mockService);
Topics authTokensArray = kernel.findServiceTopic(AUTHENTICATION_TOKEN_LOOKUP_KEY);
boolean found = false;
for (Node node : authTokensArray) {
if ("ABCService".equals(Coerce.toString(node))) {
if (found) {
fail("Duplicate entry!");
}
found = true;
}
}
assertTrue(found);
}
use of com.aws.greengrass.config.Node in project aws-greengrass-nucleus by aws-greengrass.
the class DeploymentService method getAllGroupNames.
/**
* Gets the list of all the groups that the thing is a part of. This method is used by log manager.
*
* @return All the group configs.
*/
public Set<String> getAllGroupNames() {
Topics componentsToGroupsTopics = config.lookupTopics(COMPONENTS_TO_GROUPS_TOPICS);
Set<String> allGroupNames = new HashSet<>();
if (componentsToGroupsTopics != null) {
componentsToGroupsTopics.iterator().forEachRemaining(node -> {
Topics groupsTopics = (Topics) node;
groupsTopics.children.values().stream().map(n -> (Topic) n).forEach(topic -> {
String groupName = Coerce.toString(topic);
if (!Utils.isEmpty(groupName)) {
allGroupNames.add(groupName);
}
});
});
}
return allGroupNames;
}
use of com.aws.greengrass.config.Node in project aws-greengrass-nucleus by aws-greengrass.
the class GenericExternalService method addEnv.
protected void addEnv(Exec exec, Topics src) {
if (src == null) {
return;
}
// add parents contributions first
addEnv(exec, src.parent);
Node env = src.getChild(SETENV_CONFIG_NAMESPACE);
if (env instanceof Topics) {
((Topics) env).forEach(n -> {
if (n instanceof Topic) {
exec.setenv(n.getName(), Coerce.toString(((Topic) n).getOnce()));
}
});
}
}
use of com.aws.greengrass.config.Node in project aws-greengrass-nucleus by aws-greengrass.
the class DeploymentServiceTest method GIVEN_deployment_job_WHEN_deployment_process_succeeds_THEN_correctly_map_components_to_groups.
@ParameterizedTest
@EnumSource(Deployment.DeploymentType.class)
void GIVEN_deployment_job_WHEN_deployment_process_succeeds_THEN_correctly_map_components_to_groups(Deployment.DeploymentType type) throws Exception {
String expectedGroupName = EXPECTED_GROUP_NAME;
if (type.equals(Deployment.DeploymentType.LOCAL)) {
expectedGroupName = LOCAL_DEPLOYMENT_GROUP_NAME;
}
String deploymentDocument = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("TestDeploymentDocument.json"), StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n"));
deploymentQueue.offer(new Deployment(deploymentDocument, type, TEST_JOB_ID_1));
Topics allGroupTopics = Topics.of(context, GROUP_TO_ROOT_COMPONENTS_TOPICS, null);
Topics groupMembershipTopics = Topics.of(context, GROUP_MEMBERSHIP_TOPICS, null);
groupMembershipTopics.lookup(expectedGroupName);
Topics deploymentGroupTopics = Topics.of(context, expectedGroupName, allGroupTopics);
Topic pkgTopic1 = Topic.of(context, DeploymentService.GROUP_TO_ROOT_COMPONENTS_VERSION_KEY, "1.0.0");
Topic groupTopic1 = Topic.of(context, DeploymentService.GROUP_TO_ROOT_COMPONENTS_GROUP_CONFIG_ARN, "arn:aws:greengrass:testRegion:12345:configuration:testGroup:12");
Topic groupNameTopic1 = Topic.of(context, DeploymentService.GROUP_TO_ROOT_COMPONENTS_GROUP_NAME, expectedGroupName);
Map<CaseInsensitiveString, Node> pkgDetails = new HashMap<>();
pkgDetails.put(new CaseInsensitiveString(DeploymentService.GROUP_TO_ROOT_COMPONENTS_VERSION_KEY), pkgTopic1);
pkgDetails.put(new CaseInsensitiveString(DeploymentService.GROUP_TO_ROOT_COMPONENTS_GROUP_CONFIG_ARN), groupTopic1);
pkgDetails.put(new CaseInsensitiveString(DeploymentService.GROUP_TO_ROOT_COMPONENTS_GROUP_NAME), groupNameTopic1);
Topics pkgTopics = Topics.of(context, EXPECTED_ROOT_PACKAGE_NAME, deploymentGroupTopics);
pkgTopics.children.putAll(pkgDetails);
deploymentGroupTopics.children.put(new CaseInsensitiveString(EXPECTED_ROOT_PACKAGE_NAME), pkgTopics);
allGroupTopics.children.put(new CaseInsensitiveString(expectedGroupName), deploymentGroupTopics);
when(config.lookupTopics(GROUP_MEMBERSHIP_TOPICS)).thenReturn(groupMembershipTopics);
when(config.lookupTopics(GROUP_TO_ROOT_COMPONENTS_TOPICS)).thenReturn(allGroupTopics);
lenient().when(config.lookupTopics(GROUP_TO_ROOT_COMPONENTS_TOPICS, expectedGroupName)).thenReturn(deploymentGroupTopics);
when(config.lookupTopics(COMPONENTS_TO_GROUPS_TOPICS)).thenReturn(mockComponentsToGroupPackages);
when(mockKernel.locate(any())).thenReturn(mockGreengrassService);
when(mockGreengrassService.getName()).thenReturn(EXPECTED_ROOT_PACKAGE_NAME);
mockFuture.complete(new DeploymentResult(DeploymentStatus.SUCCESSFUL, null));
when(mockExecutorService.submit(any(DefaultDeploymentTask.class))).thenReturn(mockFuture);
doNothing().when(deploymentStatusKeeper).persistAndPublishDeploymentStatus(eq(TEST_JOB_ID_1), eq(type), eq(JobStatus.IN_PROGRESS.toString()), any());
startDeploymentServiceInAnotherThread();
verify(deploymentStatusKeeper, timeout(1000)).persistAndPublishDeploymentStatus(eq(TEST_JOB_ID_1), eq(type), eq(JobStatus.IN_PROGRESS.toString()), any());
verify(deploymentStatusKeeper, timeout(10000)).persistAndPublishDeploymentStatus(eq(TEST_JOB_ID_1), eq(type), eq(JobStatus.SUCCEEDED.toString()), any());
verify(mockExecutorService, timeout(1000)).submit(any(DefaultDeploymentTask.class));
verify(deploymentStatusKeeper, timeout(2000)).persistAndPublishDeploymentStatus(eq(TEST_JOB_ID_1), eq(type), eq(JobStatus.SUCCEEDED.toString()), any());
ArgumentCaptor<Map<String, Object>> mapCaptor = ArgumentCaptor.forClass(Map.class);
verify(mockComponentsToGroupPackages).replaceAndWait(mapCaptor.capture());
Map<String, Object> groupToRootPackages = mapCaptor.getValue();
assertThat(groupToRootPackages, is(IsNull.notNullValue()));
assertThat(groupToRootPackages.entrySet(), IsNot.not(IsEmptyCollection.empty()));
assertThat(groupToRootPackages, hasKey(EXPECTED_ROOT_PACKAGE_NAME));
assertThat((Map<String, Boolean>) groupToRootPackages.get(EXPECTED_ROOT_PACKAGE_NAME), hasKey("arn:aws:greengrass:testRegion:12345:configuration:testGroup:12"));
}
use of com.aws.greengrass.config.Node in project aws-greengrass-nucleus by aws-greengrass.
the class AuthorizationPolicyParser method parseAllAuthorizationPolicies.
/**
* Given a kernel object, construct and return a map of AuthorizationPolicy objects that may exist,
* grouped into lists of the same destination component.
* This is used only upon kernel startup, to initialize all policies.
* Never returns null.
*
* @param kernel Kernel
* @return {@Map} of {@String} keys and {@List} of {@AuthorizationPolicy}'s as values"
*/
public Map<String, List<AuthorizationPolicy>> parseAllAuthorizationPolicies(Kernel kernel) {
Map<String, List<AuthorizationPolicy>> primaryAuthorizationPolicyMap = new HashMap<>();
Topics allServices = kernel.getConfig().findTopics(SERVICES_NAMESPACE_TOPIC);
if (allServices == null) {
logger.atWarn("load-authorization-all-services-component-config-retrieval-error").log("Unable to retrieve services config");
return primaryAuthorizationPolicyMap;
}
// For each component
for (Node service : allServices) {
if (service == null) {
continue;
}
if (!(service instanceof Topics)) {
continue;
}
Topics serviceConfig = (Topics) service;
String componentName = Kernel.findServiceForNode(serviceConfig);
Node accessControlMapTopic = serviceConfig.findNode(CONFIGURATION_CONFIG_KEY, ACCESS_CONTROL_NAMESPACE_TOPIC);
if (accessControlMapTopic == null) {
continue;
}
// Retrieve all policies, mapped to each policy type
Map<String, List<AuthorizationPolicy>> componentAuthorizationPolicyMap = parseAllPoliciesForComponent(accessControlMapTopic, componentName);
// For each policy type (e.g. aws.greengrass.ipc.pubsub)
for (Map.Entry<String, List<AuthorizationPolicy>> policyTypeList : componentAuthorizationPolicyMap.entrySet()) {
String policyType = policyTypeList.getKey();
List<AuthorizationPolicy> policyList = policyTypeList.getValue();
// If multiple components have policies for the same policy type
primaryAuthorizationPolicyMap.computeIfAbsent(policyType, k -> new ArrayList<>()).addAll(policyList);
}
}
return primaryAuthorizationPolicyMap;
}
Aggregations