Skip to content

Commit 5f2a213

Browse files
committed
fix(import): validate ARN resource type for online-eval import
import online-eval used a naive regex to extract the config ID from the ARN, skipping resource type, region, and account validation. Now uses parseAndValidateArn like all other import commands. Added an ARN resource type mapping to handle the online-eval vs online-evaluation-config mismatch between ImportableResourceType and the ARN format.
1 parent f5cbb56 commit 5f2a213

2 files changed

Lines changed: 14 additions & 11 deletions

File tree

src/cli/commands/import/import-online-eval.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
listAllOnlineEvaluationConfigs,
77
} from '../../aws/agentcore-control';
88
import { ANSI } from './constants';
9-
import { failResult, findResourceInDeployedState } from './import-utils';
9+
import { failResult, findResourceInDeployedState, parseAndValidateArn } from './import-utils';
1010
import { executeResourceImport } from './resource-import';
1111
import type { ImportResourceOptions, ImportResourceResult, ResourceImportDescriptor } from './types';
1212
import type { Command } from '@commander-js/extra-typings';
@@ -69,13 +69,7 @@ function createOnlineEvalDescriptor(): ResourceImportDescriptor<GetOnlineEvalCon
6969

7070
listResources: region => listAllOnlineEvaluationConfigs({ region }),
7171
getDetail: (region, id) => getOnlineEvaluationConfig({ region, configId: id }),
72-
parseResourceId: arn => {
73-
const match = /\/([^/]+)$/.exec(arn);
74-
if (!match) {
75-
throw new Error(`Could not parse config ID from ARN: ${arn}`);
76-
}
77-
return match[1]!;
78-
},
72+
parseResourceId: (arn, target) => parseAndValidateArn(arn, 'online-eval', target).resourceId,
7973

8074
extractSummaryId: s => s.onlineEvaluationConfigId,
8175
formatListItem: (s, i) =>

src/cli/commands/import/import-utils.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,14 @@ export interface ParsedArn {
212212
const ARN_PATTERN =
213213
/^arn:aws:bedrock-agentcore:([^:]+):([^:]+):(runtime|memory|evaluator|online-evaluation-config)\/(.+)$/;
214214

215+
/** Map from ImportableResourceType to the resource type string used in ARNs. */
216+
const ARN_RESOURCE_TYPE_MAP: Record<ImportableResourceType, string> = {
217+
runtime: 'runtime',
218+
memory: 'memory',
219+
evaluator: 'evaluator',
220+
'online-eval': 'online-evaluation-config',
221+
};
222+
215223
/**
216224
* Parse and validate a BedrockAgentCore ARN.
217225
* Validates format, region, and account against the deployment target.
@@ -222,16 +230,17 @@ export function parseAndValidateArn(
222230
target: { region: string; account: string }
223231
): ParsedArn {
224232
const match = ARN_PATTERN.exec(arn);
233+
const expectedArnType = ARN_RESOURCE_TYPE_MAP[expectedResourceType];
225234
if (!match) {
226235
throw new Error(
227-
`Invalid ARN format: "${arn}". Expected format: arn:aws:bedrock-agentcore:<region>:<account>:${expectedResourceType}/<id>`
236+
`Invalid ARN format: "${arn}". Expected format: arn:aws:bedrock-agentcore:<region>:<account>:${expectedArnType}/<id>`
228237
);
229238
}
230239

231240
const [, region, account, resourceType, resourceId] = match;
232241

233-
if (resourceType !== expectedResourceType) {
234-
throw new Error(`ARN resource type "${resourceType}" does not match expected type "${expectedResourceType}".`);
242+
if (resourceType !== expectedArnType) {
243+
throw new Error(`ARN resource type "${resourceType}" does not match expected type "${expectedArnType}".`);
235244
}
236245

237246
if (region !== target.region) {

0 commit comments

Comments
 (0)