Search in sources :

Example 1 with Node

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);
}
Also used : Topics(com.aws.greengrass.config.Topics) GreengrassService(com.aws.greengrass.lifecyclemanager.GreengrassService) Node(com.aws.greengrass.config.Node) Test(org.junit.jupiter.api.Test)

Example 2 with Node

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;
}
Also used : PackageLoadingException(com.aws.greengrass.componentmanager.exceptions.PackageLoadingException) SerializerFactory.getRecipeSerializer(com.amazon.aws.iot.greengrass.component.common.SerializerFactory.getRecipeSerializer) SerializerFactory.getRecipeSerializerJson(com.amazon.aws.iot.greengrass.component.common.SerializerFactory.getRecipeSerializerJson) DeploymentDocumentConverter(com.aws.greengrass.deployment.converter.DeploymentDocumentConverter) InvalidRequestException(com.aws.greengrass.deployment.exceptions.InvalidRequestException) THING_GROUP_RESOURCE_NAME_PREFIX(com.aws.greengrass.deployment.converter.DeploymentDocumentConverter.THING_GROUP_RESOURCE_NAME_PREFIX) Deployment(com.aws.greengrass.deployment.model.Deployment) DeploymentTask(com.aws.greengrass.deployment.model.DeploymentTask) Future(java.util.concurrent.Future) DEVICE_DEPLOYMENT_GROUP_NAME_PREFIX(com.aws.greengrass.deployment.DefaultDeploymentTask.DEVICE_DEPLOYMENT_GROUP_NAME_PREFIX) State(com.aws.greengrass.dependency.State) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DEFAULT(com.aws.greengrass.deployment.model.Deployment.DeploymentStage.DEFAULT) Duration(java.time.Duration) Map(java.util.Map) DeploymentDocument(com.aws.greengrass.deployment.model.DeploymentDocument) Path(java.nio.file.Path) DependencyResolver(com.aws.greengrass.componentmanager.DependencyResolver) DeploymentResult(com.aws.greengrass.deployment.model.DeploymentResult) Node(com.aws.greengrass.config.Node) CancellationException(java.util.concurrent.CancellationException) LocalOverrideRequest(com.aws.greengrass.deployment.model.LocalOverrideRequest) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LOCAL_DEPLOYMENT_GROUP_NAME(com.aws.greengrass.deployment.converter.DeploymentDocumentConverter.LOCAL_DEPLOYMENT_GROUP_NAME) Set(java.util.Set) Collectors(java.util.stream.Collectors) Topics(com.aws.greengrass.config.Topics) Kernel(com.aws.greengrass.lifecyclemanager.Kernel) List(java.util.List) Stream(java.util.stream.Stream) DeploymentTaskFailureException(com.aws.greengrass.deployment.exceptions.DeploymentTaskFailureException) DEPLOYMENT_ID_LOG_KEY(com.aws.greengrass.deployment.DeploymentConfigMerger.DEPLOYMENT_ID_LOG_KEY) ImplementsService(com.aws.greengrass.dependency.ImplementsService) ComponentStore(com.aws.greengrass.componentmanager.ComponentStore) ComponentIdentifier(com.aws.greengrass.componentmanager.models.ComponentIdentifier) SerializerFactory(com.aws.greengrass.util.SerializerFactory) Setter(lombok.Setter) VERSION_CONFIG_KEY(com.aws.greengrass.componentmanager.KernelConfigResolver.VERSION_CONFIG_KEY) Getter(lombok.Getter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Configuration(com.amazon.aws.iot.greengrass.configuration.common.Configuration) UpdateSystemPolicyService(com.aws.greengrass.lifecyclemanager.UpdateSystemPolicyService) Coerce(com.aws.greengrass.util.Coerce) StandardCopyOption(java.nio.file.StandardCopyOption) HashSet(java.util.HashSet) Inject(javax.inject.Inject) ComponentManager(com.aws.greengrass.componentmanager.ComponentManager) GreengrassService(com.aws.greengrass.lifecyclemanager.GreengrassService) Context(com.aws.greengrass.dependency.Context) ComponentRecipe(com.amazon.aws.iot.greengrass.component.common.ComponentRecipe) JobStatus(software.amazon.awssdk.iot.iotjobs.model.JobStatus) PlatformResolver(com.aws.greengrass.config.PlatformResolver) ExecutorService(java.util.concurrent.ExecutorService) ServiceLoadException(com.aws.greengrass.lifecyclemanager.exceptions.ServiceLoadException) DeploymentType(com.aws.greengrass.deployment.model.Deployment.DeploymentType) Files(java.nio.file.Files) MissingRequiredCapabilitiesException(com.aws.greengrass.deployment.exceptions.MissingRequiredCapabilitiesException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Topic(com.aws.greengrass.config.Topic) AtomicLong(java.util.concurrent.atomic.AtomicLong) Utils(com.aws.greengrass.util.Utils) DeploymentTaskMetadata(com.aws.greengrass.deployment.model.DeploymentTaskMetadata) Paths(java.nio.file.Paths) KernelAlternatives(com.aws.greengrass.lifecyclemanager.KernelAlternatives) FAILED_ROLLBACK_NOT_REQUESTED(com.aws.greengrass.deployment.model.DeploymentResult.DeploymentStatus.FAILED_ROLLBACK_NOT_REQUESTED) DeploymentStatus(com.aws.greengrass.deployment.model.DeploymentResult.DeploymentStatus) KernelConfigResolver(com.aws.greengrass.componentmanager.KernelConfigResolver) Logger(com.aws.greengrass.logging.api.Logger) Topics(com.aws.greengrass.config.Topics) Topic(com.aws.greengrass.config.Topic) HashSet(java.util.HashSet)

Example 3 with Node

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()));
            }
        });
    }
}
Also used : Topics(com.aws.greengrass.config.Topics) Node(com.aws.greengrass.config.Node) Topic(com.aws.greengrass.config.Topic)

Example 4 with Node

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"));
}
Also used : Topics(com.aws.greengrass.config.Topics) InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) Node(com.aws.greengrass.config.Node) Deployment(com.aws.greengrass.deployment.model.Deployment) CaseInsensitiveString(com.aws.greengrass.config.CaseInsensitiveString) DeploymentResult(com.aws.greengrass.deployment.model.DeploymentResult) CaseInsensitiveString(com.aws.greengrass.config.CaseInsensitiveString) BufferedReader(java.io.BufferedReader) Topic(com.aws.greengrass.config.Topic) Map(java.util.Map) HashMap(java.util.HashMap) Collections.emptyMap(java.util.Collections.emptyMap) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with Node

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;
}
Also used : Node(com.aws.greengrass.config.Node) CONFIGURATION_CONFIG_KEY(com.aws.greengrass.componentmanager.KernelConfigResolver.CONFIGURATION_CONFIG_KEY) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) HashMap(java.util.HashMap) Coerce(com.aws.greengrass.util.Coerce) JsonMapper(com.fasterxml.jackson.databind.json.JsonMapper) SERVICES_NAMESPACE_TOPIC(com.aws.greengrass.lifecyclemanager.GreengrassService.SERVICES_NAMESPACE_TOPIC) ArrayList(java.util.ArrayList) Topic(com.aws.greengrass.config.Topic) Topics(com.aws.greengrass.config.Topics) Kernel(com.aws.greengrass.lifecyclemanager.Kernel) MapperFeature(com.fasterxml.jackson.databind.MapperFeature) List(java.util.List) Utils(com.aws.greengrass.util.Utils) ACCESS_CONTROL_NAMESPACE_TOPIC(com.aws.greengrass.lifecyclemanager.GreengrassService.ACCESS_CONTROL_NAMESPACE_TOPIC) Map(java.util.Map) LogManager(com.aws.greengrass.logging.impl.LogManager) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Logger(com.aws.greengrass.logging.api.Logger) Topics(com.aws.greengrass.config.Topics) HashMap(java.util.HashMap) Node(com.aws.greengrass.config.Node) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Node (com.aws.greengrass.config.Node)11 Topic (com.aws.greengrass.config.Topic)7 Topics (com.aws.greengrass.config.Topics)7 HashMap (java.util.HashMap)5 CaseInsensitiveString (com.aws.greengrass.config.CaseInsensitiveString)4 Map (java.util.Map)4 Coerce (com.aws.greengrass.util.Coerce)3 Utils (com.aws.greengrass.util.Utils)3 Test (org.junit.jupiter.api.Test)3 ChildChanged (com.aws.greengrass.config.ChildChanged)2 State (com.aws.greengrass.dependency.State)2 DeviceConfigurationException (com.aws.greengrass.deployment.exceptions.DeviceConfigurationException)2 Deployment (com.aws.greengrass.deployment.model.Deployment)2 DeploymentResult (com.aws.greengrass.deployment.model.DeploymentResult)2 GreengrassService (com.aws.greengrass.lifecyclemanager.GreengrassService)2 Kernel (com.aws.greengrass.lifecyclemanager.Kernel)2 Logger (com.aws.greengrass.logging.api.Logger)2 IOException (java.io.IOException)2 List (java.util.List)2 ComponentRecipe (com.amazon.aws.iot.greengrass.component.common.ComponentRecipe)1