Search in sources :

Example 1 with CaseInsensitiveString

use of com.aws.greengrass.config.CaseInsensitiveString in project aws-greengrass-nucleus by aws-greengrass.

the class DeviceConfiguration method initNucleusComponentName.

/**
 * Get the Nucleus component name to lookup the configuration in the right place. If no component of type Nucleus
 * exists, create service config for the default Nucleus component.
 */
private String initNucleusComponentName() {
    Optional<CaseInsensitiveString> nucleusComponent = kernel.getConfig().lookupTopics(SERVICES_NAMESPACE_TOPIC).children.keySet().stream().filter(s -> ComponentType.NUCLEUS.name().equals(getComponentType(s.toString()))).findAny();
    String nucleusComponentName = nucleusComponent.isPresent() ? nucleusComponent.get().toString() : DEFAULT_NUCLEUS_COMPONENT_NAME;
    // Initialize default/inferred required config if it doesn't exist
    initializeNucleusComponentConfig(nucleusComponentName);
    return nucleusComponentName;
}
Also used : PackageLoadingException(com.aws.greengrass.componentmanager.exceptions.PackageLoadingException) SerializerFactory.getRecipeSerializer(com.amazon.aws.iot.greengrass.component.common.SerializerFactory.getRecipeSerializer) Arrays(java.util.Arrays) URISyntaxException(java.net.URISyntaxException) LogStore(com.aws.greengrass.logging.impl.config.LogStore) Validator(com.aws.greengrass.config.Validator) ComponentConfigurationValidationException(com.aws.greengrass.deployment.exceptions.ComponentConfigurationValidationException) SERVICE_LIFECYCLE_NAMESPACE_TOPIC(com.aws.greengrass.lifecyclemanager.GreengrassService.SERVICE_LIFECYCLE_NAMESPACE_TOPIC) LogFormat(com.aws.greengrass.logging.impl.config.LogFormat) Level(org.slf4j.event.Level) Map(java.util.Map) SETENV_CONFIG_NAMESPACE(com.aws.greengrass.lifecyclemanager.GreengrassService.SETENV_CONFIG_NAMESPACE) LogManager(com.aws.greengrass.logging.impl.LogManager) Path(java.nio.file.Path) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) Node(com.aws.greengrass.config.Node) SERVICE_TYPE_TOPIC_KEY(com.aws.greengrass.lifecyclemanager.Kernel.SERVICE_TYPE_TOPIC_KEY) TLSAuthException(com.aws.greengrass.util.exceptions.TLSAuthException) Collectors(java.util.stream.Collectors) SdkClientException(software.amazon.awssdk.core.exception.SdkClientException) StandardCharsets(java.nio.charset.StandardCharsets) SERVICES_NAMESPACE_TOPIC(com.aws.greengrass.lifecyclemanager.GreengrassService.SERVICES_NAMESPACE_TOPIC) ChildChanged(com.aws.greengrass.config.ChildChanged) Objects(java.util.Objects) Topics(com.aws.greengrass.config.Topics) Kernel(com.aws.greengrass.lifecyclemanager.Kernel) NucleusPaths(com.aws.greengrass.util.NucleusPaths) FileVisitResult(java.nio.file.FileVisitResult) List(java.util.List) CaseInsensitiveString(com.aws.greengrass.config.CaseInsensitiveString) Optional(java.util.Optional) SecurityService(com.aws.greengrass.security.SecurityService) DefaultAwsRegionProviderChain(software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain) ComponentStore(com.aws.greengrass.componentmanager.ComponentStore) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) ComponentIdentifier(com.aws.greengrass.componentmanager.models.ComponentIdentifier) VERSION_CONFIG_KEY(com.aws.greengrass.componentmanager.KernelConfigResolver.VERSION_CONFIG_KEY) COPY_ATTRIBUTES(java.nio.file.StandardCopyOption.COPY_ATTRIBUTES) CONFIGURATION_CONFIG_KEY(com.aws.greengrass.componentmanager.KernelConfigResolver.CONFIGURATION_CONFIG_KEY) Coerce(com.aws.greengrass.util.Coerce) AtomicReference(java.util.concurrent.atomic.AtomicReference) LogConfigUpdate(com.aws.greengrass.logging.impl.config.model.LogConfigUpdate) ArrayList(java.util.ArrayList) Platform(com.aws.greengrass.util.platforms.Platform) Inject(javax.inject.Inject) MAIN_SERVICE_NAME(com.aws.greengrass.lifecyclemanager.KernelCommandLine.MAIN_SERVICE_NAME) GreengrassService(com.aws.greengrass.lifecyclemanager.GreengrassService) NOFOLLOW_LINKS(java.nio.file.LinkOption.NOFOLLOW_LINKS) Semver(com.vdurmont.semver4j.Semver) ManagementFactory(java.lang.management.ManagementFactory) Region(software.amazon.awssdk.regions.Region) DependencyProperties(com.amazon.aws.iot.greengrass.component.common.DependencyProperties) SERVICE_DEPENDENCIES_NAMESPACE_TOPIC(com.aws.greengrass.lifecyclemanager.GreengrassService.SERVICE_DEPENDENCIES_NAMESPACE_TOPIC) REPLACE_EXISTING(java.nio.file.StandardCopyOption.REPLACE_EXISTING) FileSystemPermission(com.aws.greengrass.util.FileSystemPermission) Files(java.nio.file.Files) Permissions(com.aws.greengrass.util.Permissions) IOException(java.io.IOException) ComponentRecipe(com.aws.greengrass.componentmanager.models.ComponentRecipe) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) KeyManager(javax.net.ssl.KeyManager) Topic(com.aws.greengrass.config.Topic) ComponentType(com.amazon.aws.iot.greengrass.component.common.ComponentType) Utils(com.aws.greengrass.util.Utils) RecipeLoader(com.aws.greengrass.componentmanager.converter.RecipeLoader) WhatHappened(com.aws.greengrass.config.WhatHappened) Paths(java.nio.file.Paths) KernelAlternatives(com.aws.greengrass.lifecyclemanager.KernelAlternatives) KernelAlternatives.locateCurrentKernelUnpackDir(com.aws.greengrass.lifecyclemanager.KernelAlternatives.locateCurrentKernelUnpackDir) DeviceConfigurationException(com.aws.greengrass.deployment.exceptions.DeviceConfigurationException) KernelConfigResolver(com.aws.greengrass.componentmanager.KernelConfigResolver) Collections(java.util.Collections) Logger(com.aws.greengrass.logging.api.Logger) DEFAULT_VALUE_TIMESTAMP(com.aws.greengrass.config.Topic.DEFAULT_VALUE_TIMESTAMP) CaseInsensitiveString(com.aws.greengrass.config.CaseInsensitiveString) CaseInsensitiveString(com.aws.greengrass.config.CaseInsensitiveString)

Example 2 with CaseInsensitiveString

use of com.aws.greengrass.config.CaseInsensitiveString 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 3 with CaseInsensitiveString

use of com.aws.greengrass.config.CaseInsensitiveString in project aws-greengrass-nucleus by aws-greengrass.

the class SecurityService method deregisterCryptoKeyProvider.

/**
 * Deregister crypto key provider for the key type.
 *
 * @param keyProvider Crypto key provider
 */
public void deregisterCryptoKeyProvider(CryptoKeySpi keyProvider) {
    CaseInsensitiveString keyType = new CaseInsensitiveString(keyProvider.supportedKeyType());
    boolean removed = cryptoKeyProviderMap.remove(keyType, keyProvider);
    if (!removed) {
        logger.atInfo().kv(KEY_TYPE, keyType).log("Crypto key service provider is either already removed or " + "unregistered");
    }
}
Also used : CaseInsensitiveString(com.aws.greengrass.config.CaseInsensitiveString)

Example 4 with CaseInsensitiveString

use of com.aws.greengrass.config.CaseInsensitiveString in project aws-greengrass-nucleus by aws-greengrass.

the class SecurityService method selectCryptoKeyProvider.

private CryptoKeySpi selectCryptoKeyProvider(URI uri) throws ServiceUnavailableException {
    CaseInsensitiveString keyType = new CaseInsensitiveString(uri.getScheme());
    CryptoKeySpi provider = cryptoKeyProviderMap.get(keyType);
    if (provider == null) {
        throw new ServiceUnavailableException(String.format("Crypto key service for %s is unavailable", keyType));
    }
    return provider;
}
Also used : ServiceUnavailableException(com.aws.greengrass.security.exceptions.ServiceUnavailableException) CaseInsensitiveString(com.aws.greengrass.config.CaseInsensitiveString)

Example 5 with CaseInsensitiveString

use of com.aws.greengrass.config.CaseInsensitiveString in project aws-greengrass-nucleus by aws-greengrass.

the class SecurityService method registerCryptoKeyProvider.

/**
 * Register crypto key provider for the key type.
 *
 * @param keyProvider Crypto key provider
 * @throws ServiceProviderConflictException if key type is already registered
 */
public void registerCryptoKeyProvider(CryptoKeySpi keyProvider) throws ServiceProviderConflictException {
    CaseInsensitiveString keyType = new CaseInsensitiveString(keyProvider.supportedKeyType());
    logger.atInfo().kv(KEY_TYPE, keyType).log("Register crypto key service provider");
    CryptoKeySpi provider = cryptoKeyProviderMap.computeIfAbsent(keyType, k -> keyProvider);
    if (!provider.equals(keyProvider)) {
        throw new ServiceProviderConflictException(String.format("Key type %s crypto key provider is registered", keyType));
    }
}
Also used : ServiceProviderConflictException(com.aws.greengrass.security.exceptions.ServiceProviderConflictException) CaseInsensitiveString(com.aws.greengrass.config.CaseInsensitiveString)

Aggregations

CaseInsensitiveString (com.aws.greengrass.config.CaseInsensitiveString)21 Topics (com.aws.greengrass.config.Topics)14 Topic (com.aws.greengrass.config.Topic)13 Test (org.junit.jupiter.api.Test)12 HashMap (java.util.HashMap)8 PublishRequest (com.aws.greengrass.mqttclient.PublishRequest)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7 Map (java.util.Map)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 Node (com.aws.greengrass.config.Node)3 GreengrassService (com.aws.greengrass.lifecyclemanager.GreengrassService)3 HashSet (java.util.HashSet)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 ServiceProviderConflictException (com.aws.greengrass.security.exceptions.ServiceProviderConflictException)2 ComponentType (com.amazon.aws.iot.greengrass.component.common.ComponentType)1 DependencyProperties (com.amazon.aws.iot.greengrass.component.common.DependencyProperties)1 DependencyType (com.amazon.aws.iot.greengrass.component.common.DependencyType)1 SerializerFactory.getRecipeSerializer (com.amazon.aws.iot.greengrass.component.common.SerializerFactory.getRecipeSerializer)1 ComponentStore (com.aws.greengrass.componentmanager.ComponentStore)1 KernelConfigResolver (com.aws.greengrass.componentmanager.KernelConfigResolver)1