use of com.aws.greengrass.dependency.EZPlugins in project aws-greengrass-nucleus by aws-greengrass.
the class Kernel method locateExternalPlugin.
@SuppressWarnings({ "PMD.AvoidCatchingThrowable", "PMD.CloseResource" })
private Class<?> locateExternalPlugin(String name, Topics serviceRootTopics) throws ServiceLoadException {
ComponentIdentifier componentId = ComponentIdentifier.fromServiceTopics(serviceRootTopics);
Path pluginJar;
try {
pluginJar = nucleusPaths.artifactPath(componentId).resolve(componentId.getName() + JAR_FILE_EXTENSION);
} catch (IOException e) {
throw new ServiceLoadException(e);
}
if (!pluginJar.toFile().exists() || !pluginJar.toFile().isFile()) {
throw new ServiceLoadException(String.format("Unable to find %s because %s does not exist", name, pluginJar));
}
Topic storedDigest = config.find(SERVICES_NAMESPACE_TOPIC, MAIN_SERVICE_NAME, GreengrassService.RUNTIME_STORE_NAMESPACE_TOPIC, SERVICE_DIGEST_TOPIC_KEY, componentId.toString());
if (storedDigest == null || storedDigest.getOnce() == null) {
logger.atError("plugin-load-error").kv(GreengrassService.SERVICE_NAME_KEY, name).log("Local external plugin is not supported by this greengrass version");
throw new ServiceLoadException("Custom plugins is not supported by this greengrass version");
}
ComponentStore componentStore = context.get(ComponentStore.class);
if (!componentStore.validateComponentRecipeDigest(componentId, Coerce.toString(storedDigest))) {
logger.atError("plugin-load-error").kv(GreengrassService.SERVICE_NAME_KEY, name).log("Local plugin does not match the version in cloud!!");
throw new ServiceLoadException("Plugin has been modified after it was downloaded");
}
Class<?> clazz;
try {
AtomicReference<Class<?>> classReference = new AtomicReference<>();
EZPlugins ezPlugins = context.get(EZPlugins.class);
ezPlugins.loadPlugin(pluginJar, (sc) -> sc.matchClassesWithAnnotation(ImplementsService.class, (c) -> {
// Only use the class whose name matches what we want
ImplementsService serviceImplementation = c.getAnnotation(ImplementsService.class);
if (serviceImplementation.name().equals(name)) {
if (classReference.get() != null) {
logger.atWarn().log("Multiple classes implementing service found in {} " + "for component {}. Using the first one found: {}", pluginJar, name, classReference.get());
return;
}
classReference.set(c);
}
}));
clazz = classReference.get();
} catch (Throwable e) {
throw new ServiceLoadException(String.format("Unable to load %s as a plugin", name), e);
}
if (clazz == null) {
throw new ServiceLoadException(String.format("Unable to find %s. Could not find any ImplementsService annotation with the same name.", name));
}
return clazz;
}
use of com.aws.greengrass.dependency.EZPlugins in project aws-greengrass-nucleus by aws-greengrass.
the class KernelLifecycleTest method GIVEN_kernel_WHEN_launch_with_autostart_services_THEN_autostarts_added_as_dependencies_of_main.
@SuppressWarnings("PMD.CloseResource")
@Test
void GIVEN_kernel_WHEN_launch_with_autostart_services_THEN_autostarts_added_as_dependencies_of_main() throws Exception {
GreengrassService mockMain = mock(GreengrassService.class);
GreengrassService mockOthers = mock(GreengrassService.class);
doReturn(mockMain).when(mockKernel).locateIgnoreError(eq("main"));
doReturn(mockOthers).when(mockKernel).locate(not(eq("main")));
// Mock out EZPlugins so I can return a deterministic set of services to be added as auto-start
EZPlugins pluginMock = mock(EZPlugins.class);
kernelLifecycle.setStartables(new ArrayList<>());
when(mockContext.get(EZPlugins.class)).thenReturn(pluginMock);
doAnswer((i) -> {
ClassAnnotationMatchProcessor func = i.getArgument(1);
func.processMatch(UpdateSystemPolicyService.class);
func.processMatch(DeploymentService.class);
return null;
}).when(pluginMock).annotated(eq(ImplementsService.class), any());
kernelLifecycle.launch();
// Expect 2 times because I returned 2 plugins from above: SafeUpdate and Deployment
verify(mockMain, times(2)).addOrUpdateDependency(eq(mockOthers), eq(DependencyType.HARD), eq(true));
}
use of com.aws.greengrass.dependency.EZPlugins in project aws-greengrass-nucleus by aws-greengrass.
the class KernelLifecycleTest method GIVEN_kernel_WHEN_launch_with_provisioning_plugin_AND_plugin_methods_throw_runtime_Exception_THEN_offline_mode.
@SuppressWarnings("PMD.CloseResource")
@Test
void GIVEN_kernel_WHEN_launch_with_provisioning_plugin_AND_plugin_methods_throw_runtime_Exception_THEN_offline_mode(ExtensionContext context) throws Exception {
ignoreExceptionOfType(context, RuntimeException.class);
mockProvisioning();
when(mockProvisioningPlugin.updateIdentityConfiguration(any())).thenThrow(new RuntimeException("Error provisioning"));
EZPlugins pluginMock = mock(EZPlugins.class);
when(mockContext.get(EZPlugins.class)).thenReturn(pluginMock);
doAnswer((i) -> {
ImplementingClassMatchProcessor func = i.getArgument(1);
func.processMatch(mockPluginClass);
return null;
}).when(pluginMock).implementing(eq(DeviceIdentityInterface.class), any());
kernelLifecycle.launch();
verify(mockProvisioningPlugin, timeout(1000).times(1)).updateIdentityConfiguration(any(ProvisionContext.class));
verify(mockProvisioningConfigUpdateHelper, times(0)).updateNucleusConfiguration(any(NucleusConfiguration.class), eq(UpdateBehaviorTree.UpdateBehavior.MERGE));
verify(mockProvisioningConfigUpdateHelper, times(0)).updateSystemConfiguration(any(SystemConfiguration.class), eq(UpdateBehaviorTree.UpdateBehavior.MERGE));
}
use of com.aws.greengrass.dependency.EZPlugins in project aws-greengrass-nucleus by aws-greengrass.
the class KernelLifecycleTest method GIVEN_kernel_WHEN_launch_with_provisioning_plugin_THEN_plugin_methods_are_invoked.
@SuppressWarnings("PMD.CloseResource")
@Test
void GIVEN_kernel_WHEN_launch_with_provisioning_plugin_THEN_plugin_methods_are_invoked() throws Exception {
mockProvisioning();
when(mockProvisioningPlugin.updateIdentityConfiguration(any())).thenReturn(mock(ProvisionConfiguration.class));
EZPlugins pluginMock = mock(EZPlugins.class);
when(mockContext.get(EZPlugins.class)).thenReturn(pluginMock);
doAnswer((i) -> {
ImplementingClassMatchProcessor func = i.getArgument(1);
func.processMatch(mockPluginClass);
return null;
}).when(pluginMock).implementing(eq(DeviceIdentityInterface.class), any());
kernelLifecycle.launch();
verify(mockProvisioningPlugin, timeout(1000).times(1)).updateIdentityConfiguration(any(ProvisionContext.class));
}
use of com.aws.greengrass.dependency.EZPlugins in project aws-greengrass-nucleus by aws-greengrass.
the class GreengrassSetupTest method GIVEN_setup_script_WHEN_trusted_plugin_provided_THEN_jar_copied_to_trusted_plugin_path.
@Test
@SuppressWarnings("PMD.CloseResource")
void GIVEN_setup_script_WHEN_trusted_plugin_provided_THEN_jar_copied_to_trusted_plugin_path() throws Exception {
Path pluginJarPath = Files.createTempFile(null, ".jar");
Path mockTrustedDirectory = Files.createTempDirectory(null);
GreengrassSetup greengrassSetup = new GreengrassSetup(System.out, System.err, deviceProvisioningHelper, platform, kernel, "-i", "mock_config_path", "-r", "mock_root", "-tn", "mock_thing_name", "-trn", "mock_tes_role_name", "-ss", "false", "--aws-region", "us-east-1", "--trusted-plugin", pluginJarPath.toString());
NucleusPaths mockNucleusPaths = mock(NucleusPaths.class);
Path mockPluginPath = mock(Path.class);
when(mockNucleusPaths.pluginPath()).thenReturn(mockPluginPath);
when(kernel.getNucleusPaths()).thenReturn(mockNucleusPaths);
EZPlugins mockEZPlugin = mock(EZPlugins.class);
when(mockEZPlugin.withCacheDirectory(eq(mockPluginPath))).thenReturn(mockEZPlugin);
when(mockEZPlugin.getTrustedCacheDirectory()).thenReturn(mockTrustedDirectory);
when(context.get(eq(EZPlugins.class))).thenReturn(mockEZPlugin);
greengrassSetup.parseArgs();
greengrassSetup.performSetup();
assertTrue(Files.exists(mockTrustedDirectory.resolve(Utils.namePart(pluginJarPath.toString()))));
}
Aggregations