use crate::error::{PackageError, Result};
use crate::package::{DeployDescriptor, IamStatement};
const ALLOWED_TIERS: &[&str] = &["foundation", "domain"];
const ALLOWED_TARGET_TYPES: &[&str] = &["pmcp-run", "google-cloud-run"];
const ALLOWED_IAM_ACTION_PREFIXES: &[&str] = &[
"dynamodb:",
"s3:",
"glue:",
"athena:",
"secretsmanager:",
"ssm:",
"logs:",
];
pub fn validate(descriptor: &DeployDescriptor) -> Result<()> {
check_target(descriptor)?;
check_tier(descriptor)?;
check_iam(descriptor)?;
Ok(())
}
fn check_target(descriptor: &DeployDescriptor) -> Result<()> {
if !ALLOWED_TARGET_TYPES.contains(&descriptor.target.target_type.as_str()) {
return Err(PackageError::AllowlistViolation {
resource: format!("target.type:{}", descriptor.target.target_type),
});
}
Ok(())
}
fn check_tier(descriptor: &DeployDescriptor) -> Result<()> {
if let Some(composition) = &descriptor.composition {
if !ALLOWED_TIERS.contains(&composition.tier.as_str()) {
return Err(PackageError::AllowlistViolation {
resource: format!("composition.tier:{}", composition.tier),
});
}
}
Ok(())
}
fn check_iam(descriptor: &DeployDescriptor) -> Result<()> {
if let Some(iam) = &descriptor.iam {
for statement in &iam.statements {
check_statement(statement)?;
}
}
Ok(())
}
fn check_statement(statement: &IamStatement) -> Result<()> {
if statement.effect != "Allow" {
return Err(PackageError::AllowlistViolation {
resource: format!("iam.statements.effect:{}", statement.effect),
});
}
for action in &statement.actions {
let allowed = action != "*"
&& ALLOWED_IAM_ACTION_PREFIXES
.iter()
.any(|prefix| action.starts_with(prefix));
if !allowed {
return Err(PackageError::AllowlistViolation {
resource: format!("iam.statements.actions:{action}"),
});
}
}
for resource in &statement.resources {
if resource == "*" {
return Err(PackageError::AllowlistViolation {
resource: "iam.statements.resources:*".to_string(),
});
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::package::{
AuthDcrSection, AuthSection, AwsSection, CompositionSection, DeployDescriptor, IamSection,
IamStatement, ObservabilitySection, ServerSection, TargetSection,
};
use std::collections::BTreeMap;
fn allowed_descriptor() -> DeployDescriptor {
DeployDescriptor {
target: TargetSection {
target_type: "pmcp-run".to_string(),
version: "1.0.0".to_string(),
},
metadata: None,
aws: AwsSection {
region: "us-east-1".to_string(),
},
server: ServerSection {
name: "team-fs".to_string(),
memory_mb: Some(1024),
timeout_seconds: 30,
memory: None,
cpu: None,
ingress: None,
allow_unauthenticated: None,
binary: None,
},
environment: BTreeMap::new(),
secrets: BTreeMap::new(),
auth: AuthSection {
enabled: false,
provider: "none".to_string(),
callback_urls: vec![],
cognito: None,
dcr: Some(AuthDcrSection {
enabled: true,
public_client_patterns: vec!["claude".to_string()],
default_scopes: vec!["openid".to_string()],
allowed_scopes: vec!["openid".to_string()],
}),
groups: None,
scopes: None,
},
observability: ObservabilitySection {
log_retention_days: 30,
enable_xray: true,
create_dashboard: true,
alarms: None,
},
composition: Some(CompositionSection {
tier: "foundation".to_string(),
allow_composition: true,
internal_only: false,
}),
assets: None,
iam: Some(IamSection {
statements: vec![IamStatement {
effect: "Allow".to_string(),
actions: vec!["dynamodb:Scan".to_string()],
resources: vec!["arn:aws:dynamodb:us-east-1:*:table/AgentTeam-*".to_string()],
}],
}),
gcp: None,
layout: None,
}
}
#[test]
fn validate_accepts_an_allowed_descriptor() {
assert!(validate(&allowed_descriptor()).is_ok());
}
#[test]
fn validate_rejects_a_disallowed_tier() {
let mut descriptor = allowed_descriptor();
descriptor.composition.as_mut().unwrap().tier = "enterprise".to_string();
let err = validate(&descriptor).unwrap_err();
assert!(matches!(err, PackageError::AllowlistViolation { .. }));
}
#[test]
fn validate_rejects_a_disallowed_target_type() {
let mut descriptor = allowed_descriptor();
descriptor.target.target_type = "bare-metal".to_string();
let err = validate(&descriptor).unwrap_err();
assert!(matches!(err, PackageError::AllowlistViolation { .. }));
}
#[test]
fn validate_rejects_a_wildcard_iam_action() {
let mut descriptor = allowed_descriptor();
descriptor.iam = Some(IamSection {
statements: vec![IamStatement {
effect: "Allow".to_string(),
actions: vec!["*".to_string()],
resources: vec!["arn:aws:dynamodb:us-east-1:*:table/AgentTeam-*".to_string()],
}],
});
let err = validate(&descriptor).unwrap_err();
assert!(matches!(err, PackageError::AllowlistViolation { .. }));
}
#[test]
fn validate_rejects_a_wildcard_iam_resource() {
let mut descriptor = allowed_descriptor();
descriptor.iam = Some(IamSection {
statements: vec![IamStatement {
effect: "Allow".to_string(),
actions: vec!["dynamodb:Scan".to_string()],
resources: vec!["*".to_string()],
}],
});
let err = validate(&descriptor).unwrap_err();
assert!(matches!(err, PackageError::AllowlistViolation { .. }));
}
#[test]
fn validate_rejects_a_disallowed_iam_action_prefix() {
let mut descriptor = allowed_descriptor();
descriptor.iam = Some(IamSection {
statements: vec![IamStatement {
effect: "Allow".to_string(),
actions: vec!["iam:CreateRole".to_string()],
resources: vec!["arn:aws:iam::*:role/*".to_string()],
}],
});
let err = validate(&descriptor).unwrap_err();
assert!(matches!(err, PackageError::AllowlistViolation { .. }));
}
#[test]
fn validate_is_deterministic_across_repeated_calls() {
let descriptor = allowed_descriptor();
let first = validate(&descriptor);
let second = validate(&descriptor);
assert_eq!(first.is_ok(), second.is_ok());
let mut disallowed = descriptor;
disallowed.composition.as_mut().unwrap().tier = "enterprise".to_string();
let first_err = validate(&disallowed).unwrap_err().to_string();
let second_err = validate(&disallowed).unwrap_err().to_string();
assert_eq!(first_err, second_err);
}
}