Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/modules/opensearch.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ Choose an image from the [container registry](https://hub.docker.com/r/opensearc
<!--codeinclude-->
[](../../packages/modules/opensearch/src/opensearch-container.test.ts) inside_block:opensearchCustomPassword
<!--/codeinclude-->

### With security disabled

<!--codeinclude-->
[](../../packages/modules/opensearch/src/opensearch-container.test.ts) inside_block:opensearchDisableSecurity
<!--/codeinclude-->
19 changes: 19 additions & 0 deletions packages/modules/opensearch/src/opensearch-container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,23 @@ describe("OpenSearchContainer", { timeout: 180_000 }, () => {
const { body } = await client.indices.exists({ index: "people" });
expect(body).toBe(true);
});

it("should be reachable with security disabled", async () => {
// opensearchDisableSecurity {
await using container = await new OpenSearchContainer(IMAGE).withSecurityEnabled(false).start();

const client = new Client({
node: container.getHttpUrl(),
auth: {
username: container.getUsername(),
password: container.getPassword(),
},
});
// }

await client.indices.create({ index: "people" });

const { body } = await client.indices.exists({ index: "people" });
expect(body).toBe(true);
});
});
24 changes: 17 additions & 7 deletions packages/modules/opensearch/src/opensearch-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ export class OpenSearchContainer extends GenericContainer {
private readonly username = "admin";

// HTTPS + Basic Auth wait strategy
private readonly defaultWaitStrategy = Wait.forHttp("/", OPENSEARCH_HTTP_PORT)
.usingTls()
.allowInsecure()
.withBasicCredentials(this.username, this.password);
private readonly defaultWaitStrategy = Wait.forHttp("/", OPENSEARCH_HTTP_PORT).withBasicCredentials(
this.username,
this.password
);

constructor(image: string) {
super(image);
Expand Down Expand Up @@ -67,16 +67,21 @@ export class OpenSearchContainer extends GenericContainer {
OPENSEARCH_INITIAL_ADMIN_PASSWORD: this.password,
});

if (this.securityEnabled) {
this.defaultWaitStrategy.usingTls().allowInsecure();
}

const started = await super.start();
return new StartedOpenSearchContainer(started, this.username, this.password);
return new StartedOpenSearchContainer(started, this.username, this.password, this.securityEnabled);
}
}

export class StartedOpenSearchContainer extends AbstractStartedContainer {
constructor(
override readonly startedTestContainer: StartedTestContainer,
private readonly username: string,
private readonly password: string
private readonly password: string,
private readonly securityEnabled: boolean
) {
super(startedTestContainer);
}
Expand All @@ -86,9 +91,14 @@ export class StartedOpenSearchContainer extends AbstractStartedContainer {
return this.getMappedPort(OPENSEARCH_HTTP_PORT);
}

/** Get the URL schema needed for connecting to this container */
public getSchema(): string {
return this.securityEnabled ? "https" : "http";
}

/** HTTPS endpoint URL */
public getHttpUrl(): string {
return `https://${this.getHost()}:${this.getPort()}`;
return `${this.getSchema()}://${this.getHost()}:${this.getPort()}`;
}

/** Admin username (always 'admin' by default) */
Expand Down
Loading