Skip to content

feat: add currentIp option atlas-create-access-list tool #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 27 additions & 1 deletion src/common/atlas/apiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
},
Expand Down Expand Up @@ -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<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();
const input = {
groupId: projectId,
ipAddress: currentIp.currentIpv4Address,
comment: comment || DEFAULT_COMMENT,
};
ipInputs.push(input);
}

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