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

GG-39142 Added cache names validation (8.8.x backport) #3147

Merged
merged 7 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.junit.Test;

import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;

/**
* Verify we are able to escape "_" character in the metadata request.
Expand Down Expand Up @@ -84,7 +83,6 @@ protected List<String> getTableNames(String tabNamePtrn) throws SQLException {
public void createTables() throws Exception {
executeDDl("CREATE TABLE MY_FAV_TABLE (id INT PRIMARY KEY, val VARCHAR)");
executeDDl("CREATE TABLE MY0FAV0TABLE (id INT PRIMARY KEY, val VARCHAR)");
executeDDl("CREATE TABLE \"MY\\FAV\\TABLE\" (id INT PRIMARY KEY, val VARCHAR)");
executeDDl("CREATE TABLE OTHER_TABLE (id INT PRIMARY KEY, val VARCHAR)");
}

Expand All @@ -94,7 +92,6 @@ public void dropTables() throws Exception {
// tables that matched by "TABLE MY_FAV_TABLE" sql pattern:
executeDDl("DROP TABLE MY_FAV_TABLE");
executeDDl("DROP TABLE MY0FAV0TABLE");
executeDDl("DROP TABLE \"MY\\FAV\\TABLE\"");

// and another one that doesn't:
executeDDl("DROP TABLE OTHER_TABLE");
Expand All @@ -105,23 +102,21 @@ public void dropTables() throws Exception {
*/
@Test
public void testTablesMatch() throws SQLException {
assertEqualsCollections(asList("MY0FAV0TABLE", "MY\\FAV\\TABLE", "MY_FAV_TABLE"), getTableNames("MY_FAV_TABLE"));
assertEqualsCollections(singletonList("MY_FAV_TABLE"), getTableNames("MY\\_FAV\\_TABLE"));
assertEqualsCollections(asList("MY0FAV0TABLE", "MY_FAV_TABLE"), getTableNames("MY_FAV_TABLE"));

assertEqualsCollections(Collections.emptyList(), getTableNames("\\%"));
assertEqualsCollections(asList("MY0FAV0TABLE", "MY\\FAV\\TABLE", "MY_FAV_TABLE", "OTHER_TABLE"), getTableNames("%"));
assertEqualsCollections(asList("MY0FAV0TABLE", "MY_FAV_TABLE", "OTHER_TABLE"), getTableNames("%"));

assertEqualsCollections(Collections.emptyList(), getTableNames(""));
assertEqualsCollections(asList("MY0FAV0TABLE", "MY\\FAV\\TABLE", "MY_FAV_TABLE", "OTHER_TABLE"), getTableNames(null));
assertEqualsCollections(asList("MY0FAV0TABLE", "MY_FAV_TABLE", "OTHER_TABLE"), getTableNames(null));
}

/**
* Test to check that "\" character is handling properly in the table metadata request.
*/
@Test
public void testTablesWithBackslashInTheNameMatch() throws SQLException {
assertEqualsCollections(asList("MY0FAV0TABLE", "MY\\FAV\\TABLE", "MY_FAV_TABLE"), getTableNames("MY_FAV_TABLE"));
assertEqualsCollections(singletonList("MY\\FAV\\TABLE"), getTableNames("MY\\FAV\\TABLE"));
assertEqualsCollections(asList("MY0FAV0TABLE", "MY_FAV_TABLE"), getTableNames("MY_FAV_TABLE"));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2159,6 +2159,16 @@ public final class IgniteSystemProperties {
public static final String IGNITE_DEFRAGMENTATION_REGION_SIZE_PERCENTAGE =
"IGNITE_DEFRAGMENTATION_REGION_SIZE_PERCENTAGE";

/**
* If {@code true}, cache names will be validated not to contain characters which cause issues when persistence is used
* ({@code \}, {@code /}, {@code \0}). Default is {@code true}.
*/
@SystemProperty(value = "If true, cache names will be validated not to contain characters " +
"which cause issues when persistence is used ({@code \\}, {@code /}, {@code \\0}). " +
"Default is true",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use SystemProperty#defaults

type = Boolean.class)
public static final String IGNITE_VALIDATE_CACHE_NAMES = "IGNITE_VALIDATE_CACHE_NAMES";

/**
* There can be background tasks that can be interrupted due to node stop, node fail, or cluster deactivation,
* but need to be completed, so they start after node start or cluster activation. If this option is set to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.apache.ignite.IgniteCheckedException;
import org.apache.ignite.IgniteException;
import org.apache.ignite.IgniteLogger;
import org.apache.ignite.IgniteSystemProperties;
import org.apache.ignite.cache.CacheKeyConfiguration;
import org.apache.ignite.cache.CachePartialUpdateException;
import org.apache.ignite.cache.CacheServerNotFoundException;
Expand Down Expand Up @@ -1551,6 +1552,13 @@ public static void validateNewCacheName(String name) throws IllegalArgumentExcep

A.ensure(!isReservedCacheName(name), "Cache name cannot be \"" + name +
"\" because it is reserved for internal purposes.");

if (IgniteSystemProperties.getBoolean(IgniteSystemProperties.IGNITE_VALIDATE_CACHE_NAMES, true)) {
boolean hasIllegalCharacters = name.contains("/") || name.contains("\\") || name.contains("\0")
|| name.contains("\n");
A.ensure(!hasIllegalCharacters, "Cache name cannot contain slashes (/), backslashes (\\), " +
"line separators (\\n), or null characters (\\0). [cacheName=" + name + "]");
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,20 @@ public void testDynamicCacheDirectoryContainsInvalidFileNameChars() throws Excep
cfg.setName(name);

if (persistenceEnabled) {
assertThrows(log, () -> srv.createCache(cfg), IllegalArgumentException.class,
"Cache start failed. Cache or group name contains the characters that are not allowed in file names");
if (checkGroup)
assertThrows(log, () -> srv.createCache(cfg), IllegalArgumentException.class,
"Cache start failed. Cache or group name contains the characters that are not allowed in file names");
else
assertThrows(log, () -> srv.createCache(cfg), IllegalArgumentException.class,
"Ouch! Argument is invalid: Cache name cannot contain slashes (/), backslashes (\\), line separators (\\n), or null characters (\\0)");
}
else {
srv.createCache(cfg);
if (checkGroup)
srv.createCache(cfg);
else
assertThrows(log, () -> srv.createCache(cfg), IllegalArgumentException.class,
"Ouch! Argument is invalid: Cache name cannot contain slashes (/), backslashes (\\), line separators (\\n), or null characters (\\0)");

srv.destroyCache(cfg.getName());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
/*
* Copyright 2019 GridGain Systems, Inc. and Contributors.
*
* Licensed under the GridGain Community Edition License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.gridgain.com/products/software/community-edition/gridgain-community-edition-license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.ignite.internal.processors.cache.persistence;

import org.apache.ignite.IgniteCache;
import org.apache.ignite.IgniteSystemProperties;
import org.apache.ignite.cluster.ClusterState;
import org.apache.ignite.configuration.DataStorageConfiguration;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.testframework.junits.WithSystemProperty;
import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
import org.junit.After;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;

/**
* Tests for persistent caches with tricky names: with special characters, non-ASCII symbols, etc.
*/
@RunWith(Parameterized.class)
public class IgnitePdsExoticCacheNamesTest extends GridCommonAbstractTest {
@Parameterized.Parameters(name = "persistent={0}")
public static Iterable<Boolean> data() {
return Arrays.asList(true, false);
}

@Parameterized.Parameter(0)
public boolean isPersistent;

/** {@inheritDoc} */
@Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);

DataStorageConfiguration storageCfg = new DataStorageConfiguration();
storageCfg.getDefaultDataRegionConfiguration().setPersistenceEnabled(isPersistent);
cfg.setDataStorageConfiguration(storageCfg);

return cfg;
}

/** */
@Before
public void setUp() throws Exception {
cleanPersistenceDir();
}

/** */
@After
public void tearDown() throws Exception {
stopAllGrids();

cleanPersistenceDir();
}

/** */
@Test
public void testNameWithSlashes() throws Exception {
checkBadName("my/cool/cache");
}

/** */
@Test
public void testNameWithBackslashes() throws Exception {
checkBadName("my\\cool\\cache");
}

/** */
@Test
public void testNameWithControlSymbols() throws Exception {
checkBadName("my\0\1\2\3\4\5\6\7cache");
}

/** */
@Test
public void testNameWithNewLines() throws Exception {
checkBadName("my \n cool \n cache");
}

/** */
@Test
@WithSystemProperty(key = IgniteSystemProperties.IGNITE_VALIDATE_CACHE_NAMES, value = "false")
public void testNameWithSlashesNoValidation() throws Exception {
Assume.assumeFalse(isPersistent); // bad characters would break persistence
checkGoodName("my/cool/cache");
}

/** */
@Test
@WithSystemProperty(key = IgniteSystemProperties.IGNITE_VALIDATE_CACHE_NAMES, value = "false")
public void testNameWithBackslashesNoValidation() throws Exception {
Assume.assumeFalse(isPersistent); // bad characters would break persistence
checkGoodName("my\\cool\\cache");
}

/** */
@Test
@WithSystemProperty(key = IgniteSystemProperties.IGNITE_VALIDATE_CACHE_NAMES, value = "false")
public void testNameWithControlSymbolsNoValidation() throws Exception {
Assume.assumeFalse(isPersistent); // bad characters would break persistence
checkGoodName("my\0\1\2\3\4\5\6\7cache");
}

/** */
@Test
public void testNameWithSpecialSymbols() throws Exception {
checkGoodName("my!@#$%^&*()cache");
}

/** */
@Test
public void testNameWithUnicodeSymbols() throws Exception {
checkGoodName("my\u0431\u0430\u0431\u0443\u0448\u043a\u0430cache");
}

/** */
@Test
public void testNameWithWhiteSpaces() throws Exception {
checkGoodName("my \t cool \t cache");
}

/**
* @param cacheName cache name to test
* @throws Exception If failed.
*/
private void checkBadName(String cacheName) throws Exception {
startGrid();

if (isPersistent)
grid().cluster().state(ClusterState.ACTIVE);

try {
grid().createCache(cacheName);

fail("Expected IllegalArgumentException was not thrown");
} catch (IllegalArgumentException e) {
if (e.getMessage().contains(
"Cache name cannot contain slashes (/), backslashes (\\), line separators (\\n), " +
"or null characters (\\0)"))
return; //expected

throw e;
} finally {
stopGrid();
}
}

/**
* @param cacheName cache name to test
* @throws Exception If failed.
*/
private void checkGoodName(String cacheName) throws Exception {
startGrid();

if (isPersistent)
grid().cluster().state(ClusterState.ACTIVE);

IgniteCache<String, String> cache = grid().createCache(cacheName);
cache.put(cacheName + "::foo", cacheName + "::bar");
assertEquals(cacheName + "::bar", cache.get(cacheName + "::foo"));

stopGrid();

if (isPersistent) {
// check that restart works
startGrid();

cache = grid().cache(cacheName);
assertEquals(cacheName + "::bar", cache.get(cacheName + "::foo"));

stopGrid();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.ignite.internal.processors.cache.persistence.IgniteDataStorageMetricsSelfTest;
import org.apache.ignite.internal.processors.cache.persistence.IgnitePdsCorruptedStoreTest;
import org.apache.ignite.internal.processors.cache.persistence.IgnitePdsExchangeDuringCheckpointTest;
import org.apache.ignite.internal.processors.cache.persistence.IgnitePdsExoticCacheNamesTest;
import org.apache.ignite.internal.processors.cache.persistence.IgnitePdsPageSizesTest;
import org.apache.ignite.internal.processors.cache.persistence.IgnitePdsPartitionsStateRecoveryTest;
import org.apache.ignite.internal.processors.cache.persistence.IgnitePersistentStoreDataStructuresTest;
Expand Down Expand Up @@ -98,6 +99,8 @@ public static List<Class<?>> suite(Collection<Class> ignoredTests) {

addRealPageStoreTests(suite, ignoredTests);

GridTestUtils.addTestIfNeeded(suite, IgnitePdsExoticCacheNamesTest.class, ignoredTests);

return suite;
}

Expand Down