forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvpc-utils.ts
More file actions
84 lines (73 loc) · 3.02 KB
/
Copy pathvpc-utils.ts
File metadata and controls
84 lines (73 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
export interface VpcOptions {
networkMode?: string;
subnets?: string;
securityGroups?: string;
}
export interface VpcValidationResult {
valid: boolean;
error?: string;
}
/**
* Warning shown when an agent is configured with VPC network mode.
* Used in CLI output, TUI completion screens, and exit messages.
*/
export const VPC_ENDPOINT_WARNING =
'VPC mode may require VPC endpoints for CloudWatch, X-Ray, ECR, and Bedrock depending on your agent configuration. If your agent calls public APIs or uses an API-key-based provider, a NAT gateway or additional endpoints may also be needed.';
const SUBNET_PATTERN = /^subnet-[0-9a-zA-Z]{8,17}$/;
const SECURITY_GROUP_PATTERN = /^sg-[0-9a-zA-Z]{8,17}$/;
export function parseCommaSeparatedList(value: string | undefined): string[] | undefined {
if (!value) return undefined;
return value
.split(',')
.map(s => s.trim())
.filter(Boolean);
}
/**
* Validate a comma-separated list of subnet IDs.
* Returns true if valid, or an error message string if invalid.
*/
export function validateSubnetIds(value: string): true | string {
const ids = value
.split(',')
.map(s => s.trim())
.filter(Boolean);
if (ids.length === 0) return 'At least one subnet ID is required';
const invalid = ids.filter(id => !SUBNET_PATTERN.test(id));
if (invalid.length > 0) return `Invalid subnet ID format: ${invalid[0]}. Expected subnet-xxxxxxxx`;
return true;
}
/**
* Validate a comma-separated list of security group IDs.
* Returns true if valid, or an error message string if invalid.
*/
export function validateSecurityGroupIds(value: string): true | string {
const ids = value
.split(',')
.map(s => s.trim())
.filter(Boolean);
if (ids.length === 0) return 'At least one security group ID is required';
const invalid = ids.filter(id => !SECURITY_GROUP_PATTERN.test(id));
if (invalid.length > 0) return `Invalid security group ID format: ${invalid[0]}. Expected sg-xxxxxxxx`;
return true;
}
export function validateVpcOptions(options: VpcOptions): VpcValidationResult {
if (options.networkMode && options.networkMode !== 'PUBLIC' && options.networkMode !== 'VPC') {
return { valid: false, error: `Invalid network mode: ${options.networkMode}. Use PUBLIC or VPC` };
}
if (options.networkMode === 'VPC') {
if (!options.subnets) {
return { valid: false, error: '--subnets is required when network mode is VPC' };
}
if (!options.securityGroups) {
return { valid: false, error: '--security-groups is required when network mode is VPC' };
}
const subnetResult = validateSubnetIds(options.subnets);
if (subnetResult !== true) return { valid: false, error: subnetResult };
const sgResult = validateSecurityGroupIds(options.securityGroups);
if (sgResult !== true) return { valid: false, error: sgResult };
}
if (options.networkMode !== 'VPC' && (options.subnets || options.securityGroups)) {
return { valid: false, error: '--subnets and --security-groups are only valid with --network-mode VPC' };
}
return { valid: true };
}