-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
snakeyaml cve fix #24099
snakeyaml cve fix #24099
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,21 +21,18 @@ | |
import com.google.common.collect.ImmutableList; | ||
import com.google.common.io.Resources; | ||
import io.airlift.units.Duration; | ||
import org.apache.cassandra.service.CassandraDaemon; | ||
|
||
import javax.management.ObjectName; | ||
import org.testcontainers.containers.GenericContainer; | ||
|
||
import java.io.Closeable; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.lang.management.ManagementFactory; | ||
import java.net.InetSocketAddress; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
import static com.datastax.driver.core.ProtocolVersion.V3; | ||
import static com.google.common.base.Preconditions.checkState; | ||
import static com.google.common.io.Files.createTempDir; | ||
import static com.google.common.io.Files.write; | ||
import static com.google.common.io.Resources.getResource; | ||
|
@@ -45,43 +42,38 @@ | |
import static java.util.concurrent.TimeUnit.MINUTES; | ||
import static java.util.concurrent.TimeUnit.NANOSECONDS; | ||
import static java.util.concurrent.TimeUnit.SECONDS; | ||
import static org.testcontainers.utility.MountableFile.forHostPath; | ||
import static org.testng.Assert.assertEquals; | ||
|
||
public final class EmbeddedCassandra | ||
public class CassandraServer | ||
implements Closeable | ||
|
||
{ | ||
private static Logger log = Logger.get(EmbeddedCassandra.class); | ||
private static Logger log = Logger.get(CassandraServer.class); | ||
|
||
private static final String HOST = "127.0.0.1"; | ||
private static final int PORT = 9142; | ||
|
||
private static final Duration REFRESH_SIZE_ESTIMATES_TIMEOUT = new Duration(1, MINUTES); | ||
|
||
private static CassandraSession session; | ||
private static boolean initialized; | ||
private final GenericContainer<?> dockerContainer; | ||
|
||
private EmbeddedCassandra() {} | ||
private final CassandraSession session; | ||
|
||
public static synchronized void start() | ||
public CassandraServer() | ||
throws Exception | ||
{ | ||
if (initialized) { | ||
return; | ||
} | ||
|
||
log.info("Starting cassandra..."); | ||
|
||
System.setProperty("cassandra.config", "file:" + prepareCassandraYaml()); | ||
System.setProperty("cassandra-foreground", "true"); | ||
System.setProperty("cassandra.native.epoll.enabled", "false"); | ||
|
||
CassandraDaemon cassandraDaemon = new CassandraDaemon(); | ||
cassandraDaemon.activate(); | ||
this.dockerContainer = new GenericContainer<>("cassandra:2.1.16") | ||
.withExposedPorts(PORT) | ||
.withCopyFileToContainer(forHostPath(prepareCassandraYaml()), "/etc/cassandra/cassandra.yaml"); | ||
this.dockerContainer.start(); | ||
|
||
Cluster.Builder clusterBuilder = Cluster.builder() | ||
.withProtocolVersion(V3) | ||
.withClusterName("TestCluster") | ||
.addContactPointsWithPorts(ImmutableList.of( | ||
new InetSocketAddress(HOST, PORT))) | ||
new InetSocketAddress(this.dockerContainer.getContainerIpAddress(), this.dockerContainer.getMappedPort(PORT)))) | ||
.withMaxSchemaAgreementWaitSeconds(30); | ||
|
||
ReopeningCluster cluster = new ReopeningCluster(clusterBuilder::build); | ||
|
@@ -96,12 +88,11 @@ public static synchronized void start() | |
} | ||
catch (RuntimeException e) { | ||
cluster.close(); | ||
cassandraDaemon.deactivate(); | ||
this.dockerContainer.stop(); | ||
throw e; | ||
} | ||
|
||
EmbeddedCassandra.session = session; | ||
initialized = true; | ||
this.session = session; | ||
} | ||
|
||
private static String prepareCassandraYaml() | ||
|
@@ -115,35 +106,25 @@ private static String prepareCassandraYaml() | |
Path dataDir = tmpDirPath.resolve("data"); | ||
Files.createDirectory(dataDir); | ||
|
||
String modified = original.replaceAll("\\$\\{data_directory\\}", dataDir.toAbsolutePath().toString()); | ||
|
||
Path yamlLocation = tmpDirPath.resolve("cu-cassandra.yaml"); | ||
write(modified, yamlLocation.toFile(), UTF_8); | ||
write(original, yamlLocation.toFile(), UTF_8); | ||
|
||
return yamlLocation.toAbsolutePath().toString(); | ||
} | ||
|
||
public static synchronized CassandraSession getSession() | ||
public CassandraSession getSession() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For line 109: Since you removed data_directory from cu-cassandra.yaml, this is no longer needed. However we need to make sure the path you specified always exist and won't cause error on different platforms. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure I have removed the line. Also the path is inside cassandra docker container and it will be independent of the platforms, so I believe this won't cause issues. I have updated the cherrypick id in the description of the PR. |
||
{ | ||
checkIsInitialized(); | ||
return requireNonNull(session, "cluster is null"); | ||
} | ||
|
||
public static synchronized String getHost() | ||
public String getHost() | ||
{ | ||
checkIsInitialized(); | ||
return HOST; | ||
return dockerContainer.getContainerIpAddress(); | ||
} | ||
|
||
public static synchronized int getPort() | ||
public int getPort() | ||
{ | ||
checkIsInitialized(); | ||
return PORT; | ||
} | ||
|
||
private static void checkIsInitialized() | ||
{ | ||
checkState(initialized, "EmbeddedCassandra must be started with #start() method before retrieving the cluster retrieval"); | ||
return dockerContainer.getMappedPort(PORT); | ||
} | ||
|
||
private static void checkConnectivity(CassandraSession session) | ||
|
@@ -155,7 +136,7 @@ private static void checkConnectivity(CassandraSession session) | |
log.info("Cassandra version: %s", version); | ||
} | ||
|
||
public static void refreshSizeEstimates(String keyspace, String table) | ||
public void refreshSizeEstimates(String keyspace, String table) | ||
throws Exception | ||
{ | ||
long deadline = System.nanoTime() + REFRESH_SIZE_ESTIMATES_TIMEOUT.roundTo(NANOSECONDS); | ||
|
@@ -173,27 +154,21 @@ public static void refreshSizeEstimates(String keyspace, String table) | |
throw new TimeoutException(format("Attempting to refresh size estimates for table %s.%s has timed out after %s", keyspace, table, REFRESH_SIZE_ESTIMATES_TIMEOUT)); | ||
} | ||
|
||
private static void flushTable(String keyspace, String table) | ||
private void flushTable(String keyspace, String table) | ||
throws Exception | ||
{ | ||
ManagementFactory | ||
.getPlatformMBeanServer() | ||
.invoke( | ||
new ObjectName("org.apache.cassandra.db:type=StorageService"), | ||
"forceKeyspaceFlush", | ||
new Object[] {keyspace, new String[] {table}}, | ||
new String[] {"java.lang.String", "[Ljava.lang.String;"}); | ||
dockerContainer.execInContainer("nodetool", "flush", keyspace, table); | ||
} | ||
|
||
private static void refreshSizeEstimates() | ||
private void refreshSizeEstimates() | ||
throws Exception | ||
{ | ||
ManagementFactory | ||
.getPlatformMBeanServer() | ||
.invoke( | ||
new ObjectName("org.apache.cassandra.db:type=StorageService"), | ||
"refreshSizeEstimates", | ||
new Object[] {}, | ||
new String[] {}); | ||
dockerContainer.execInContainer("nodetool", "refreshsizeestimates"); | ||
} | ||
|
||
@Override | ||
public void close() | ||
{ | ||
dockerContainer.close(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is this bring used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This dependencyManagement section is to fix the Require upper bound dependencies error for slf4j while building the cassandra module.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain a little bit about why the error happens if this one was not added? Is org.testcontainers dependent on slf4j or something else?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes @yingsu00 .
slf4j comes as a transitive dependency in org.testcontainers. If we do not add this dependencymanagement tag
the following error will come.
Inorder to fix this I have added the slf4j version as dependencyManagement tag.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If that's the case, can you please add a comment in the code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yingsu00 Added the comment.