use super::cli::AwsCli;
use super::helpers::stack_name;
use anyhow::Result;
use std::collections::HashMap;
pub const S3_TABLES_ROLE_NAME: &str = "S3TablesRoleForLakeFormation";
pub fn s3_tables_trust_policy() -> serde_json::Value {
serde_json::json!({
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": {"Service": "lakeformation.amazonaws.com"},
"Action": ["sts:AssumeRole", "sts:SetSourceIdentity", "sts:SetContext"]
}]
})
}
pub fn s3_tables_data_policy() -> serde_json::Value {
serde_json::json!({
"Version": "2012-10-17",
"Statement": [
{
"Sid": "LakeFormationPermissionsForS3ListTableBucket",
"Effect": "Allow",
"Action": ["s3tables:ListTableBuckets"],
"Resource": ["*"]
},
{
"Sid": "LakeFormationDataAccessPermissionsForS3TableBucket",
"Effect": "Allow",
"Action": [
"s3tables:CreateTableBucket",
"s3tables:GetTableBucket",
"s3tables:CreateNamespace",
"s3tables:GetNamespace",
"s3tables:ListNamespaces",
"s3tables:DeleteNamespace",
"s3tables:DeleteTableBucket",
"s3tables:CreateTable",
"s3tables:DeleteTable",
"s3tables:GetTable",
"s3tables:ListTables",
"s3tables:RenameTable",
"s3tables:UpdateTableMetadataLocation",
"s3tables:GetTableMetadataLocation",
"s3tables:GetTableData",
"s3tables:PutTableData"
],
"Resource": ["*"]
}
]
})
}
#[derive(Debug, Clone)]
pub struct DeployContext {
pub account_id: String,
pub caller_arn: String,
pub region: String,
pub env_name: String,
pub stack_name: String,
pub bucket_name: String,
pub namespace: String,
pub local_build: bool,
pub auth_token: Option<String>,
stack_outputs: HashMap<String, String>,
}
impl DeployContext {
pub fn new(cli: &AwsCli, env_name: &str, namespace: &str, local_build: bool) -> Result<Self> {
let account = cli.sts().get_caller_identity()?;
let stack = stack_name(env_name);
let bucket = stack.clone();
Ok(Self {
account_id: account.account_id,
caller_arn: account.caller_arn,
region: cli.region().to_string(),
env_name: env_name.to_string(),
stack_name: stack,
bucket_name: bucket,
namespace: namespace.to_string(),
local_build,
auth_token: None,
stack_outputs: HashMap::new(),
})
}
pub fn s3_tables_resource_arn(&self) -> String {
format!(
"arn:aws:s3tables:{}:{}:bucket/*",
self.region, self.account_id
)
}
pub fn s3_tables_role_arn(&self) -> String {
format!(
"arn:aws:iam::{}:role/{}",
self.account_id, S3_TABLES_ROLE_NAME
)
}
pub fn glue_catalog_arn(&self) -> String {
format!(
"arn:aws:glue:{}:{}:catalog/s3tablescatalog/{}",
self.region, self.account_id, self.bucket_name
)
}
pub fn table_bucket_arn(&self) -> String {
format!(
"arn:aws:s3tables:{}:{}:bucket/{}",
self.region, self.account_id, self.bucket_name
)
}
pub fn error_bucket_name(&self) -> String {
format!(
"{}-errors-{}-{}",
self.stack_name, self.account_id, self.region
)
}
pub fn artifact_bucket_name(&self) -> String {
format!("{}-artifacts-{}", self.stack_name, self.account_id)
}
pub fn lambda_function_name(&self) -> String {
format!("{}-ingest", self.stack_name)
}
pub fn lambda_role_arn(&self) -> String {
format!(
"arn:aws:iam::{}:role/{}-Lambda-{}",
self.account_id, self.stack_name, self.region
)
}
pub fn firehose_stream_name(&self, signal: &str) -> String {
format!("{}-{}", self.stack_name, signal)
}
pub fn set_stack_outputs(&mut self, outputs: HashMap<String, String>) {
self.stack_outputs = outputs;
}
pub fn get_output(&self, key: &str) -> Option<&String> {
self.stack_outputs.get(key)
}
}