Skip to content

Commit

Permalink
Couchbase: Don't configure external TLS ports if they're not supported (
Browse files Browse the repository at this point in the history
#8990)

Fixes #8989

---------

Co-authored-by: Eddú Meléndez <eddu.melendez@gmail.com>
  • Loading branch information
dnault and eddumelendez authored Jul 25, 2024
1 parent daf44c4 commit 835ae9b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ public class CouchbaseContainer extends GenericContainer<CouchbaseContainer> {

private boolean isEnterprise = false;

private boolean hasTlsPorts = false;

/**
* Creates a new couchbase container with the default image and version.
* @deprecated use {@link #CouchbaseContainer(DockerImageName)} instead
Expand Down Expand Up @@ -345,6 +347,7 @@ protected void containerIsStarting(final InspectContainerResponse containerInfo)

timePhase("waitUntilNodeIsOnline", this::waitUntilNodeIsOnline);
timePhase("initializeIsEnterprise", this::initializeIsEnterprise);
timePhase("initializeHasTlsPorts", this::initializeHasTlsPorts);
timePhase("renameNode", this::renameNode);
timePhase("initializeServices", this::initializeServices);
timePhase("setMemoryQuotas", this::setMemoryQuotas);
Expand Down Expand Up @@ -394,6 +397,31 @@ private void initializeIsEnterprise() {
}
}

/**
* Initializes the {@link #hasTlsPorts} flag.
* <p>
* Community Edition might support TLS one happy day, so use a "supports TLS" flag separate from
* the "enterprise edition" flag.
*/
private void initializeHasTlsPorts() {
@Cleanup
Response response = doHttpRequest(MGMT_PORT, "/pools/default/nodeServices", "GET", null, true);

try {
String clusterTopology = response.body().string();
hasTlsPorts =
!MAPPER
.readTree(clusterTopology)
.path("nodesExt")
.path(0)
.path("services")
.path("mgmtSSL")
.isMissingNode();
} catch (IOException e) {
throw new IllegalStateException("Couchbase /pools/default/nodeServices did not return valid JSON");
}
}

/**
* Rebinds/renames the internal hostname.
* <p>
Expand Down Expand Up @@ -503,33 +531,45 @@ private void configureExternalPorts() {
final FormBody.Builder builder = new FormBody.Builder();
builder.add("hostname", getHost());
builder.add("mgmt", Integer.toString(getMappedPort(MGMT_PORT)));
builder.add("mgmtSSL", Integer.toString(getMappedPort(MGMT_SSL_PORT)));
if (hasTlsPorts) {
builder.add("mgmtSSL", Integer.toString(getMappedPort(MGMT_SSL_PORT)));
}

if (enabledServices.contains(CouchbaseService.KV)) {
builder.add("kv", Integer.toString(getMappedPort(KV_PORT)));
builder.add("kvSSL", Integer.toString(getMappedPort(KV_SSL_PORT)));
builder.add("capi", Integer.toString(getMappedPort(VIEW_PORT)));
builder.add("capiSSL", Integer.toString(getMappedPort(VIEW_SSL_PORT)));
if (hasTlsPorts) {
builder.add("kvSSL", Integer.toString(getMappedPort(KV_SSL_PORT)));
builder.add("capiSSL", Integer.toString(getMappedPort(VIEW_SSL_PORT)));
}
}

if (enabledServices.contains(CouchbaseService.QUERY)) {
builder.add("n1ql", Integer.toString(getMappedPort(QUERY_PORT)));
builder.add("n1qlSSL", Integer.toString(getMappedPort(QUERY_SSL_PORT)));
if (hasTlsPorts) {
builder.add("n1qlSSL", Integer.toString(getMappedPort(QUERY_SSL_PORT)));
}
}

if (enabledServices.contains(CouchbaseService.SEARCH)) {
builder.add("fts", Integer.toString(getMappedPort(SEARCH_PORT)));
builder.add("ftsSSL", Integer.toString(getMappedPort(SEARCH_SSL_PORT)));
if (hasTlsPorts) {
builder.add("ftsSSL", Integer.toString(getMappedPort(SEARCH_SSL_PORT)));
}
}

if (enabledServices.contains(CouchbaseService.ANALYTICS)) {
builder.add("cbas", Integer.toString(getMappedPort(ANALYTICS_PORT)));
builder.add("cbasSSL", Integer.toString(getMappedPort(ANALYTICS_SSL_PORT)));
if (hasTlsPorts) {
builder.add("cbasSSL", Integer.toString(getMappedPort(ANALYTICS_SSL_PORT)));
}
}

if (enabledServices.contains(CouchbaseService.EVENTING)) {
builder.add("eventingAdminPort", Integer.toString(getMappedPort(EVENTING_PORT)));
builder.add("eventingSSL", Integer.toString(getMappedPort(EVENTING_SSL_PORT)));
if (hasTlsPorts) {
builder.add("eventingSSL", Integer.toString(getMappedPort(EVENTING_SSL_PORT)));
}
}

@Cleanup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.couchbase.client.java.json.JsonObject;
import org.junit.Test;
import org.testcontainers.containers.ContainerLaunchException;
import org.testcontainers.utility.DockerImageName;

import java.time.Duration;
import java.util.function.Consumer;
Expand All @@ -33,51 +32,43 @@

public class CouchbaseContainerTest {

private static final DockerImageName COUCHBASE_IMAGE_ENTERPRISE = DockerImageName.parse(
"couchbase/server:enterprise-7.0.3"
);
private static final String COUCHBASE_IMAGE_ENTERPRISE = "couchbase/server:enterprise-7.0.3";

private static final DockerImageName COUCHBASE_IMAGE_COMMUNITY = DockerImageName.parse(
"couchbase/server:community-7.0.2"
);
private static final String COUCHBASE_IMAGE_ENTERPRISE_RECENT = "couchbase/server:enterprise-7.6.2";

@Test
public void testBasicContainerUsageForEnterpriseContainer() {
// bucket_definition {
BucketDefinition bucketDefinition = new BucketDefinition("mybucket");
// }

try (
// container_definition {
CouchbaseContainer container = new CouchbaseContainer(COUCHBASE_IMAGE_ENTERPRISE)
.withBucket(bucketDefinition)
// }
) {
setUpClient(
container,
cluster -> {
Bucket bucket = cluster.bucket(bucketDefinition.getName());
bucket.waitUntilReady(Duration.ofSeconds(10L));
private static final String COUCHBASE_IMAGE_COMMUNITY = "couchbase/server:community-7.0.2";

Collection collection = bucket.defaultCollection();

collection.upsert("foo", JsonObject.create().put("key", "value"));
private static final String COUCHBASE_IMAGE_COMMUNITY_RECENT = "couchbase/server:community-7.6.2";

JsonObject fooObject = collection.get("foo").contentAsObject();
@Test
public void testBasicContainerUsageForEnterpriseContainer() {
testBasicContainerUsage(COUCHBASE_IMAGE_ENTERPRISE);
}

assertThat(fooObject.getString("key")).isEqualTo("value");
}
);
}
@Test
public void testBasicContainerUsageForEnterpriseContainerRecent() {
testBasicContainerUsage(COUCHBASE_IMAGE_ENTERPRISE_RECENT);
}

@Test
public void testBasicContainerUsageForCommunityContainer() {
testBasicContainerUsage(COUCHBASE_IMAGE_COMMUNITY);
}

@Test
public void testBasicContainerUsageForCommunityContainerRecent() {
testBasicContainerUsage(COUCHBASE_IMAGE_COMMUNITY_RECENT);
}

private void testBasicContainerUsage(String couchbaseImage) {
// bucket_definition {
BucketDefinition bucketDefinition = new BucketDefinition("mybucket");
// }

try (
CouchbaseContainer container = new CouchbaseContainer(COUCHBASE_IMAGE_COMMUNITY)
.withBucket(bucketDefinition)
// container_definition {
CouchbaseContainer container = new CouchbaseContainer(couchbaseImage).withBucket(bucketDefinition)
// }
) {
setUpClient(
container,
Expand Down

0 comments on commit 835ae9b

Please sign in to comment.