-
Notifications
You must be signed in to change notification settings - Fork 24
security: build keyspace DDL via SchemaBuilder/CqlIdentifier (follow-up to #2472) #2475
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
Open
erichare
wants to merge
8
commits into
main
Choose a base branch
from
security/cql-injection-schemabuilder-followup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e9097e8
security: build keyspace DDL via SchemaBuilder/CqlIdentifier (follow-…
erichare 8733e0f
Merge branch 'main' into security/cql-injection-schemabuilder-followup
erichare 51640a8
fix: Review comments from @amorton
erichare 765e18d
Fix linting
erichare 8b3db8f
Merge branch 'main' into security/cql-injection-schemabuilder-followup
erichare 640905d
fix: Address @amorton review comments
erichare 7a1c4f1
fix: lint issues
erichare 9b0e3b9
Merge branch 'main' into security/cql-injection-schemabuilder-followup
erichare File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 44 additions & 13 deletions
57
...in/java/io/stargate/sgv2/jsonapi/service/operation/keyspaces/CreateKeyspaceOperation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,38 +1,69 @@ | ||
| package io.stargate.sgv2.jsonapi.service.operation.keyspaces; | ||
|
|
||
| import com.datastax.oss.driver.api.core.CqlIdentifier; | ||
| import com.datastax.oss.driver.api.core.cql.SimpleStatement; | ||
| import com.datastax.oss.driver.api.querybuilder.SchemaBuilder; | ||
| import com.datastax.oss.driver.api.querybuilder.schema.CreateKeyspace; | ||
| import com.datastax.oss.driver.api.querybuilder.schema.CreateKeyspaceStart; | ||
| import io.smallrye.mutiny.Uni; | ||
| import io.stargate.sgv2.jsonapi.api.model.command.CommandResult; | ||
| import io.stargate.sgv2.jsonapi.api.request.RequestContext; | ||
| import io.stargate.sgv2.jsonapi.service.cqldriver.executor.QueryExecutor; | ||
| import io.stargate.sgv2.jsonapi.service.operation.Operation; | ||
| import io.stargate.sgv2.jsonapi.service.operation.collections.SchemaChangeResult; | ||
| import java.util.Map; | ||
| import java.util.function.Supplier; | ||
|
|
||
| /** | ||
| * Operation that creates a new Cassandra keyspace that serves as a namespace for the Data API. | ||
| * Operation that creates a Cassandra keyspace for the Data API. | ||
| * | ||
| * @param name Name of the keyspace to create. | ||
| * @param replicationMap A replication json, see | ||
| * https://docs.datastax.com/en/cql-oss/3.3/cql/cql_reference/cqlCreateKeyspace.html#Table2.Replicationstrategyclassandfactorsettings. | ||
| * <p>The keyspace name is already a CQL identifier when it reaches this operation. Replication map | ||
| * keys are still plain strings because that is what the driver accepts, so the resolver validates | ||
| * datacenter names before creating this operation. | ||
| */ | ||
| public record CreateKeyspaceOperation(String name, String replicationMap) implements Operation { | ||
| public record CreateKeyspaceOperation( | ||
| CqlIdentifier name, String strategy, Map<String, Integer> strategyOptions) | ||
| implements Operation { | ||
|
|
||
| // simple pattern for the cql | ||
| private static final String CREATE_KEYSPACE_CQL = | ||
| "CREATE KEYSPACE IF NOT EXISTS \"%s\" WITH REPLICATION = %s;"; | ||
| private static final String NETWORK_TOPOLOGY_STRATEGY = "NetworkTopologyStrategy"; | ||
| private static final int DEFAULT_REPLICATION_FACTOR = 1; | ||
|
|
||
| /** {@inheritDoc} */ | ||
| @Override | ||
| public Uni<Supplier<CommandResult>> execute( | ||
| RequestContext dataApiRequestInfo, QueryExecutor queryExecutor) { | ||
| SimpleStatement createKeyspace = | ||
| SimpleStatement.newInstance(String.format(CREATE_KEYSPACE_CQL, name, replicationMap)); | ||
| // execute | ||
| SimpleStatement createKeyspace = buildStatement(); | ||
| return queryExecutor | ||
| .executeCreateSchemaChange(dataApiRequestInfo, createKeyspace) | ||
|
|
||
| // if we have a result always respond positively | ||
| .map(any -> new SchemaChangeResult(any.wasApplied())); | ||
| } | ||
|
|
||
| private SimpleStatement buildStatement() { | ||
| CreateKeyspaceStart start = SchemaBuilder.createKeyspace(name).ifNotExists(); | ||
| Map<String, Integer> safeStrategyOptions = strategyOptionsOrEmpty(); | ||
|
|
||
| CreateKeyspace withReplication; | ||
| if (NETWORK_TOPOLOGY_STRATEGY.equals(strategy)) { | ||
| withReplication = start.withNetworkTopologyStrategy(safeStrategyOptions); | ||
| } else { | ||
| int replicationFactor = | ||
| safeStrategyOptions.getOrDefault("replication_factor", DEFAULT_REPLICATION_FACTOR); | ||
| withReplication = start.withSimpleStrategy(replicationFactor); | ||
| } | ||
| return withReplication.build(); | ||
| } | ||
|
|
||
| private Map<String, Integer> strategyOptionsOrEmpty() { | ||
| if (strategyOptions == null) { | ||
| return Map.of(); | ||
| } | ||
| strategyOptions.forEach( | ||
| (key, value) -> { | ||
| if (value == null) { | ||
| throw new IllegalArgumentException( | ||
| "Replication strategy option value must not be null for key '%s'".formatted(key)); | ||
| } | ||
| }); | ||
| return strategyOptions; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 0 additions & 44 deletions
44
...ava/io/stargate/sgv2/jsonapi/service/resolver/CreateNamespaceKeyspaceCommandResolver.java
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.