Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reject invalid thing group names #184

Merged
merged 3 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading