Skip to content

Commit 9637dea

Browse files
authored
Add support for disabling TLS in OpenSearch module (#1193)
1 parent 4c25cd1 commit 9637dea

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

docs/modules/opensearch.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,9 @@ Choose an image from the [container registry](https://hub.docker.com/r/opensearc
3333
<!--codeinclude-->
3434
[](../../packages/modules/opensearch/src/opensearch-container.test.ts) inside_block:opensearchCustomPassword
3535
<!--/codeinclude-->
36+
37+
### With security disabled
38+
39+
<!--codeinclude-->
40+
[](../../packages/modules/opensearch/src/opensearch-container.test.ts) inside_block:opensearchDisableSecurity
41+
<!--/codeinclude-->

packages/modules/opensearch/src/opensearch-container.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,23 @@ describe("OpenSearchContainer", { timeout: 180_000 }, () => {
102102
const { body } = await client.indices.exists({ index: "people" });
103103
expect(body).toBe(true);
104104
});
105+
106+
it("should be reachable with security disabled", async () => {
107+
// opensearchDisableSecurity {
108+
await using container = await new OpenSearchContainer(IMAGE).withSecurityEnabled(false).start();
109+
110+
const client = new Client({
111+
node: container.getHttpUrl(),
112+
// no auth, or ssl required
113+
});
114+
// }
115+
116+
// Url should start with http not https.
117+
expect(container.getHttpUrl()).toMatch(/^http:\/\/.*/);
118+
119+
await client.indices.create({ index: "people" });
120+
121+
const { body } = await client.indices.exists({ index: "people" });
122+
expect(body).toBe(true);
123+
});
105124
});

packages/modules/opensearch/src/opensearch-container.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ export class OpenSearchContainer extends GenericContainer {
1212
private readonly username = "admin";
1313

1414
// HTTPS + Basic Auth wait strategy
15-
private readonly defaultWaitStrategy = Wait.forHttp("/", OPENSEARCH_HTTP_PORT)
16-
.usingTls()
17-
.allowInsecure()
18-
.withBasicCredentials(this.username, this.password);
15+
private readonly defaultWaitStrategy = Wait.forHttp("/", OPENSEARCH_HTTP_PORT).withBasicCredentials(
16+
this.username,
17+
this.password
18+
);
1919

2020
constructor(image: string) {
2121
super(image);
@@ -67,16 +67,21 @@ export class OpenSearchContainer extends GenericContainer {
6767
OPENSEARCH_INITIAL_ADMIN_PASSWORD: this.password,
6868
});
6969

70+
if (this.securityEnabled) {
71+
this.defaultWaitStrategy.usingTls().allowInsecure();
72+
}
73+
7074
const started = await super.start();
71-
return new StartedOpenSearchContainer(started, this.username, this.password);
75+
return new StartedOpenSearchContainer(started, this.username, this.password, this.securityEnabled);
7276
}
7377
}
7478

7579
export class StartedOpenSearchContainer extends AbstractStartedContainer {
7680
constructor(
7781
override readonly startedTestContainer: StartedTestContainer,
7882
private readonly username: string,
79-
private readonly password: string
83+
private readonly password: string,
84+
private readonly securityEnabled: boolean
8085
) {
8186
super(startedTestContainer);
8287
}
@@ -86,9 +91,14 @@ export class StartedOpenSearchContainer extends AbstractStartedContainer {
8691
return this.getMappedPort(OPENSEARCH_HTTP_PORT);
8792
}
8893

94+
/** Get the URL schema needed for connecting to this container */
95+
public getSchema(): string {
96+
return this.securityEnabled ? "https" : "http";
97+
}
98+
8999
/** HTTPS endpoint URL */
90100
public getHttpUrl(): string {
91-
return `https://${this.getHost()}:${this.getPort()}`;
101+
return `${this.getSchema()}://${this.getHost()}:${this.getPort()}`;
92102
}
93103

94104
/** Admin username (always 'admin' by default) */

0 commit comments

Comments
 (0)