Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
24 changes: 24 additions & 0 deletions src/common/atlas/apiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,30 @@ export class ApiClient {
}
}

async getIpInfo() {
await this.validateToken();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check the return of this call?


const endpoint = "api/private/ipinfo";
const url = new URL(endpoint, config.apiBaseUrl);
const response = await fetch(url, {
method: "GET",
headers: {
Accept: "application/json",
Authorization: `Bearer ${this.token?.access_token}`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] Assuming this.token is never null if this.validateToken returned successfully.

Suggested change
Authorization: `Bearer ${this.token?.access_token}`,
Authorization: `Bearer ${this.token!.access_token}`,

"User-Agent": config.userAgent,
},
});

if (!response.ok) {
throw await ApiClientError.fromResponse(response);
}

const responseBody = await response.json();
return responseBody as {
currentIpv4Address: string;
};
}

async listProjects(options?: FetchOptions<operations["listProjects"]>) {
const { data } = await this.client.GET(`/api/atlas/v2/groups`, options);
return data;
Expand Down
16 changes: 14 additions & 2 deletions src/tools/atlas/createAccessList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class CreateAccessListTool extends AtlasToolBase {
.describe("IP addresses to allow access from")
.optional(),
cidrBlocks: z.array(z.string().cidr()).describe("CIDR blocks to allow access from").optional(),
currentIpAddress: z.boolean().describe("Add the current IP address").default(false),
comment: z.string().describe("Comment for the access list entries").default(DEFAULT_COMMENT).optional(),
};

Expand All @@ -23,11 +24,12 @@ export class CreateAccessListTool extends AtlasToolBase {
ipAddresses,
cidrBlocks,
comment,
currentIpAddress,
}: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
await this.ensureAuthenticated();

if (!ipAddresses?.length && !cidrBlocks?.length) {
throw new Error("Either ipAddresses or cidrBlocks must be provided.");
if (!ipAddresses?.length && !cidrBlocks?.length && !currentIpAddress) {
throw new Error("One of ipAddresses, cidrBlocks, currentIpAddress must be provided.");
}

const ipInputs = (ipAddresses || []).map((ipAddress) => ({
Expand All @@ -36,6 +38,16 @@ export class CreateAccessListTool extends AtlasToolBase {
comment: comment || DEFAULT_COMMENT,
}));

if (currentIpAddress) {
const currentIp = await this.apiClient!.getIpInfo();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const currentIp = await this.apiClient!.getIpInfo();
const currentIp = await this.apiClient.getIpInfo();

const input = {
groupId: projectId,
ipAddress: currentIp.currentIpv4Address,
comment: comment || DEFAULT_COMMENT,
};
ipInputs.push(input);
}

const cidrInputs = (cidrBlocks || []).map((cidrBlock) => ({
groupId: projectId,
cidrBlock,
Expand Down