diff --git a/src/common/atlas/apiClient.ts b/src/common/atlas/apiClient.ts index e87e7048..0c6615d7 100644 --- a/src/common/atlas/apiClient.ts +++ b/src/common/atlas/apiClient.ts @@ -64,7 +64,7 @@ export class ApiClient { return undefined; } if (await apiClient.validateToken()) { - request.headers.set("Authorization", `Bearer ${apiClient.token?.access_token}`); + request.headers.set("Authorization", `Bearer ${apiClient.token!.access_token}`); return request; } }, @@ -252,6 +252,32 @@ export class ApiClient { } } + async getIpInfo() { + if (!(await this.validateToken())) { + throw new Error("Not Authenticated"); + } + + 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}`, + "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) { const { data } = await this.client.GET(`/api/atlas/v2/groups`, options); return data; diff --git a/src/tools/atlas/createAccessList.ts b/src/tools/atlas/createAccessList.ts index fad14a16..7bcc4979 100644 --- a/src/tools/atlas/createAccessList.ts +++ b/src/tools/atlas/createAccessList.ts @@ -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(), }; @@ -23,11 +24,12 @@ export class CreateAccessListTool extends AtlasToolBase { ipAddresses, cidrBlocks, comment, + currentIpAddress, }: ToolArgs): Promise { 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) => ({ @@ -36,6 +38,16 @@ export class CreateAccessListTool extends AtlasToolBase { comment: comment || DEFAULT_COMMENT, })); + if (currentIpAddress) { + 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,