Skip to content

Commit

Permalink
fix: reject invalid thing group names (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcosentino11 authored Aug 22, 2024
1 parent 40a78a4 commit 21bc20c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,6 @@ public CreateLocalDeploymentResponse handleRequest(CreateLocalDeploymentRequest
.resource(AuthorizationHandler.ANY_REGEX)
.operation(CREATE_LOCAL_DEPLOYMENT)
.build());
String deploymentId = UUID.randomUUID().toString();
//All inputs are valid. If all inputs are empty, then user might just want to retrigger the deployment
// with new recipes set using the updateRecipesAndArtifacts API.
Map<String, ConfigurationUpdateOperation> configUpdate = null;
Expand All @@ -489,6 +488,9 @@ public CreateLocalDeploymentResponse handleRequest(CreateLocalDeploymentRequest
}
}

validateThingGroupName(request);

String deploymentId = UUID.randomUUID().toString();
LocalOverrideRequest localOverrideRequest = LocalOverrideRequest.builder().requestId(deploymentId)
.componentsToMerge(request.getRootComponentVersionsToAdd())
.componentsToRemove(request.getRootComponentsToRemove())
Expand Down Expand Up @@ -537,6 +539,11 @@ public CreateLocalDeploymentResponse handleRequest(CreateLocalDeploymentRequest
});
}

private void validateThingGroupName(CreateLocalDeploymentRequest request) {
if (!Utils.isEmpty(request.getGroupName()) && request.getGroupName().contains(":")) {
throw new InvalidArgumentsError("Thing group name cannot contain colon characters");
}
}

@Override
public void handleStreamEvent(EventStreamJsonMessage streamRequestEvent) {
Expand Down
24 changes: 24 additions & 0 deletions server/src/test/java/com/aws/greengrass/cli/IPCCliTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import software.amazon.awssdk.aws.greengrass.GreengrassCoreIPCClient;
import software.amazon.awssdk.aws.greengrass.GreengrassCoreIPCClientV2;
import software.amazon.awssdk.aws.greengrass.model.ComponentDetails;
Expand Down Expand Up @@ -451,6 +453,28 @@ void GIVEN_kernel_running_WHEN_CLI_authorized_groups_updated_THEN_old_token_revo
}
}

@Order(11)
@ParameterizedTest
@ValueSource(strings = {"group:", "group:1", "group:1:1"})
void GIVEN_kernel_running_WHEN_local_deployment_with_invalid_thing_group_THEN_deployment_fails(String invalidThingGroupName, ExtensionContext context) throws Exception {
ignoreExceptionOfType(context, SdkClientException.class);
ignoreExceptionOfType(context, PackageDownloadException.class);
ignoreExceptionOfType(context, ComponentVersionNegotiationException.class);

Path recipesPath = Paths.get(this.getClass().getResource("recipes").toURI());
Path artifactsPath = Paths.get(this.getClass().getResource("artifacts").toURI());

CreateLocalDeploymentRequest createLocalDeploymentRequest = new CreateLocalDeploymentRequest();
createLocalDeploymentRequest.setGroupName(invalidThingGroupName);
createLocalDeploymentRequest.setRootComponentVersionsToAdd(Collections.singletonMap("Component1", "1.0.0"));
createLocalDeploymentRequest.setRecipeDirectoryPath(recipesPath.toAbsolutePath().toString());
createLocalDeploymentRequest.setArtifactsDirectoryPath(artifactsPath.toAbsolutePath().toString());

ExecutionException executionException = assertThrows(ExecutionException.class, () ->
clientConnection.createLocalDeployment(createLocalDeploymentRequest, Optional.empty())
.getResponse().get(DEFAULT_TIMEOUT_IN_SEC, TimeUnit.SECONDS));
assertEquals(InvalidArgumentsError.class, executionException.getCause().getClass());
}

private String getAuthTokenFromInfoFile() throws IOException {
File[] authFiles = kernel.getNucleusPaths().cliIpcInfoPath().toFile().listFiles();
Expand Down

0 comments on commit 21bc20c

Please sign in to comment.