Skip to content

Commit e9ba36e

Browse files
authored
fix: reject invalid thing and group names (#1655)
1 parent 104d579 commit e9ba36e

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

src/main/java/com/aws/greengrass/easysetup/GreengrassSetup.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,13 @@ private String peekArg() {
515515
}
516516

517517
void provision(Kernel kernel) throws IOException, DeviceConfigurationException {
518+
if (thingName.contains(":")) {
519+
throw new RuntimeException("Thing name cannot contain colon characters");
520+
}
521+
if (!Utils.isEmpty(thingGroupName) && thingGroupName.contains(":")) {
522+
throw new RuntimeException("Thing group name cannot contain colon characters");
523+
}
524+
518525
outStream.printf("Provisioning AWS IoT resources for the device with IoT Thing Name: [%s]...%n", thingName);
519526
// handle endpoints provided by external config
520527
String iotDataEndpoint = Coerce.toString(kernel.getConfig().find(SERVICES_NAMESPACE_TOPIC,

src/test/java/com/aws/greengrass/easysetup/GreengrassSetupTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.junit.jupiter.params.provider.Arguments;
2626
import org.junit.jupiter.params.provider.CsvSource;
2727
import org.junit.jupiter.params.provider.MethodSource;
28+
import org.junit.jupiter.params.provider.ValueSource;
2829
import org.mockito.Answers;
2930
import org.mockito.ArgumentCaptor;
3031
import org.mockito.Mock;
@@ -40,6 +41,7 @@
4041
import static org.hamcrest.MatcherAssert.assertThat;
4142
import static org.hamcrest.Matchers.containsString;
4243
import static org.hamcrest.Matchers.hasItems;
44+
import static org.hamcrest.Matchers.is;
4345
import static org.junit.jupiter.api.Assertions.assertThrows;
4446
import static org.junit.jupiter.api.Assertions.assertTrue;
4547
import static org.mockito.ArgumentMatchers.any;
@@ -487,4 +489,42 @@ void GIVEN_setup_script_WHEN_trusted_plugin_provided_THEN_jar_copied_to_trusted_
487489
greengrassSetup.performSetup();
488490
assertTrue(Files.exists(mockTrustedDirectory.resolve(Utils.namePart(pluginJarPath.toString()))));
489491
}
492+
493+
@ParameterizedTest
494+
@ValueSource(strings = {"group:", "group:1", "group:1:"})
495+
void GIVEN_invalid_thing_group_name_WHEN_script_is_used_THEN_error(String groupName, ExtensionContext context) {
496+
ignoreExceptionUltimateCauseOfType(context, IOException.class);
497+
Kernel realKernel = new Kernel();
498+
greengrassSetup =
499+
new GreengrassSetup(System.out, System.err, deviceProvisioningHelper, platform, kernel, "--config",
500+
"mock_config_path", "--root", "mock_root", "--thing-name", "mock_thing_name",
501+
"--thing-group-name", groupName, "--thing-policy-name", "mock_thing_policy_name",
502+
"--tes-role-name", "mock_tes_role_name", "--tes-role-alias-name", "mock_tes_role_alias_name",
503+
"--provision", "--aws-region","us-east-1", "-ss", "false");
504+
Exception e = assertThrows(RuntimeException.class, () -> {
505+
greengrassSetup.parseArgs();
506+
greengrassSetup.performSetup();
507+
});
508+
realKernel.shutdown();
509+
assertThat(e.getMessage(), is("Thing group name cannot contain colon characters"));
510+
}
511+
512+
@ParameterizedTest
513+
@ValueSource(strings = {"thing:", "thing:1", "thing:1:"})
514+
void GIVEN_invalid_thing_name_WHEN_script_is_used_THEN_error(String thingName, ExtensionContext context) {
515+
ignoreExceptionUltimateCauseOfType(context, IOException.class);
516+
Kernel realKernel = new Kernel();
517+
greengrassSetup =
518+
new GreengrassSetup(System.out, System.err, deviceProvisioningHelper, platform, kernel, "--config",
519+
"mock_config_path", "--root", "mock_root", "--thing-name", thingName,
520+
"--thing-group-name", "group", "--thing-policy-name", "mock_thing_policy_name",
521+
"--tes-role-name", "mock_tes_role_name", "--tes-role-alias-name", "mock_tes_role_alias_name",
522+
"--provision", "--aws-region","us-east-1", "-ss", "false");
523+
Exception e = assertThrows(RuntimeException.class, () -> {
524+
greengrassSetup.parseArgs();
525+
greengrassSetup.performSetup();
526+
});
527+
realKernel.shutdown();
528+
assertThat(e.getMessage(), is("Thing name cannot contain colon characters"));
529+
}
490530
}

0 commit comments

Comments
 (0)