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
Test recent Couchbase Server versions as well as the old ones.

Fixes "Couchbase Community Edition 7.6 fails to start" (#8989)
  • Loading branch information
dnault committed Jul 25, 2024
1 parent 5bd94ab commit ba1b6c5
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 39 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,24 @@ 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 +524,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 @@ -33,50 +33,39 @@

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));

Collection collection = bucket.defaultCollection();
private static final String COUCHBASE_IMAGE_COMMUNITY = "couchbase/server:community-7.0.2";

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) {
BucketDefinition bucketDefinition = new BucketDefinition("mybucket");

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

0 comments on commit ba1b6c5

Please sign in to comment.