Skip to content

Commit f278e4f

Browse files
authored
feat: add currentIp option atlas-create-access-list tool (#52)
1 parent 38a0f1e commit f278e4f

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

src/common/atlas/apiClient.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export class ApiClient {
6464
return undefined;
6565
}
6666
if (await apiClient.validateToken()) {
67-
request.headers.set("Authorization", `Bearer ${apiClient.token?.access_token}`);
67+
request.headers.set("Authorization", `Bearer ${apiClient.token!.access_token}`);
6868
return request;
6969
}
7070
},
@@ -252,6 +252,32 @@ export class ApiClient {
252252
}
253253
}
254254

255+
async getIpInfo() {
256+
if (!(await this.validateToken())) {
257+
throw new Error("Not Authenticated");
258+
}
259+
260+
const endpoint = "api/private/ipinfo";
261+
const url = new URL(endpoint, config.apiBaseUrl);
262+
const response = await fetch(url, {
263+
method: "GET",
264+
headers: {
265+
Accept: "application/json",
266+
Authorization: `Bearer ${this.token!.access_token}`,
267+
"User-Agent": config.userAgent,
268+
},
269+
});
270+
271+
if (!response.ok) {
272+
throw await ApiClientError.fromResponse(response);
273+
}
274+
275+
const responseBody = await response.json();
276+
return responseBody as {
277+
currentIpv4Address: string;
278+
};
279+
}
280+
255281
async listProjects(options?: FetchOptions<operations["listProjects"]>) {
256282
const { data } = await this.client.GET(`/api/atlas/v2/groups`, options);
257283
return data;

src/tools/atlas/createAccessList.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export class CreateAccessListTool extends AtlasToolBase {
1515
.describe("IP addresses to allow access from")
1616
.optional(),
1717
cidrBlocks: z.array(z.string().cidr()).describe("CIDR blocks to allow access from").optional(),
18+
currentIpAddress: z.boolean().describe("Add the current IP address").default(false),
1819
comment: z.string().describe("Comment for the access list entries").default(DEFAULT_COMMENT).optional(),
1920
};
2021

@@ -23,11 +24,12 @@ export class CreateAccessListTool extends AtlasToolBase {
2324
ipAddresses,
2425
cidrBlocks,
2526
comment,
27+
currentIpAddress,
2628
}: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
2729
await this.ensureAuthenticated();
2830

29-
if (!ipAddresses?.length && !cidrBlocks?.length) {
30-
throw new Error("Either ipAddresses or cidrBlocks must be provided.");
31+
if (!ipAddresses?.length && !cidrBlocks?.length && !currentIpAddress) {
32+
throw new Error("One of ipAddresses, cidrBlocks, currentIpAddress must be provided.");
3133
}
3234

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

41+
if (currentIpAddress) {
42+
const currentIp = await this.apiClient.getIpInfo();
43+
const input = {
44+
groupId: projectId,
45+
ipAddress: currentIp.currentIpv4Address,
46+
comment: comment || DEFAULT_COMMENT,
47+
};
48+
ipInputs.push(input);
49+
}
50+
3951
const cidrInputs = (cidrBlocks || []).map((cidrBlock) => ({
4052
groupId: projectId,
4153
cidrBlock,

0 commit comments

Comments
 (0)