use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use super::command::duration_secs;
pub const MAX_GITHUB_REPOSITORIES: usize = 500;
pub const MIN_AWS_LIFETIME_SECS: u64 = 15 * 60;
pub const MAX_AWS_LIFETIME_SECS: u64 = 12 * 60 * 60;
pub const MIN_GCP_LIFETIME_SECS: u64 = 60;
pub const MAX_GCP_LIFETIME_SECS: u64 = 60 * 60;
pub const DEFAULT_LIFETIME: &str = "15m";
pub const AWS_CREDENTIALS: &str = "~/.aws/credentials";
pub const GCP_CREDENTIALS: &str = "~/.config/gcloud/application_default_credentials.json";
const GCP_SCOPE_PREFIX: &str = "https://www.googleapis.com/auth/";
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub enum MintSource {
GithubApp(GithubApp),
AwsSts(AwsSts),
Gcp(Gcp),
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct GithubApp {
pub app: String,
pub installation: u64,
pub key: String,
pub repositories: Vec<String>,
pub permissions: BTreeMap<String, String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub refresh_before: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AwsSts {
pub role: String,
pub region: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub profile: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub credentials: Option<String>,
pub actions: Vec<String>,
pub resources: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub lifetime: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub refresh_before: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Gcp {
pub service_account: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub credentials: Option<String>,
pub scopes: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub lifetime: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub refresh_before: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase", deny_unknown_fields)]
pub enum Helper {
Git {
username: String,
},
Aws,
}
impl Helper {
#[must_use]
pub fn tool(&self) -> &'static str {
match self {
Self::Git { .. } => "git",
Self::Aws => "aws",
}
}
}
impl std::fmt::Debug for MintSource {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("MintSource")
.field("provider", &self.provider())
.finish_non_exhaustive()
}
}
impl MintSource {
#[must_use]
pub fn provider(&self) -> &'static str {
match self {
Self::GithubApp(_) => "github-app",
Self::AwsSts(_) => "aws-sts",
Self::Gcp(_) => "gcp",
}
}
#[must_use]
pub fn files(&self) -> Vec<&str> {
match self {
Self::GithubApp(g) => vec![g.key.as_str()],
Self::AwsSts(a) => vec![a.credentials.as_deref().unwrap_or(AWS_CREDENTIALS)],
Self::Gcp(g) => vec![g.credentials.as_deref().unwrap_or(GCP_CREDENTIALS)],
}
}
#[must_use]
pub fn lifetime_secs(&self) -> u64 {
let asked = |l: &Option<String>| {
duration_secs(l.as_deref().unwrap_or(DEFAULT_LIFETIME)).unwrap_or(15 * 60)
};
match self {
Self::GithubApp(_) => 60 * 60,
Self::AwsSts(a) => asked(&a.lifetime),
Self::Gcp(g) => asked(&g.lifetime),
}
}
#[must_use]
pub fn refresh_before_secs(&self) -> u64 {
let (written, default) = match self {
Self::GithubApp(g) => (&g.refresh_before, 10 * 60),
Self::AwsSts(a) => (&a.refresh_before, self.lifetime_secs() / 5),
Self::Gcp(g) => (&g.refresh_before, self.lifetime_secs() / 5),
};
written
.as_deref()
.and_then(duration_secs)
.unwrap_or(default)
.max(10)
}
}
pub(super) fn check(source: &MintSource) -> Result<(), String> {
for file in source.files() {
if !(crate::paths::is_absolute(file) || file.starts_with("~/")) {
return Err(format!(
"mint file `{file}` must be absolute or start with `~/`"
));
}
}
match source {
MintSource::GithubApp(g) => check_github(g),
MintSource::AwsSts(a) => check_aws(a),
MintSource::Gcp(g) => check_gcp(g),
}?;
let lifetime = source.lifetime_secs();
let before = source.refresh_before_secs();
if before >= lifetime {
return Err("mint refresh_before must be shorter than the lifetime".into());
}
Ok(())
}
fn check_github(g: &GithubApp) -> Result<(), String> {
if g.app.is_empty()
|| !g
.app
.bytes()
.all(|b| b.is_ascii_alphanumeric() || b == b'.')
{
return Err("github-app `app` must be the App's client ID or numeric App ID".into());
}
if g.installation == 0 {
return Err("github-app `installation` must be the installation's numeric ID".into());
}
if g.repositories.is_empty() || g.repositories.len() > MAX_GITHUB_REPOSITORIES {
return Err(format!(
"github-app `repositories` must name 1 to {MAX_GITHUB_REPOSITORIES} repositories: \
without them the token reaches every repository of the installation"
));
}
let name = |s: &str| {
!s.is_empty()
&& s.len() <= 100
&& s.bytes()
.all(|b| b.is_ascii_alphanumeric() || b"._-".contains(&b))
};
if let Some(bad) = g.repositories.iter().find(|r| !name(r)) {
return Err(format!(
"github-app repository `{bad}` must be a repository name without its owner"
));
}
if g.permissions.is_empty() {
return Err(
"github-app `permissions` must name each permission the token gets: without them \
it gets every permission of the App"
.into(),
);
}
for (permission, level) in &g.permissions {
if permission.is_empty()
|| !permission
.bytes()
.all(|b| b.is_ascii_lowercase() || b == b'_')
{
return Err(format!(
"github-app permission `{permission}` is not a permission name"
));
}
if !matches!(level.as_str(), "read" | "write" | "admin") {
return Err(format!(
"github-app permission `{permission}` must be `read`, `write` or `admin`"
));
}
}
lifetime_text("github-app", None, g.refresh_before.as_deref(), (1, 1))
}
fn check_aws(a: &AwsSts) -> Result<(), String> {
let role = a.role.split(':').collect::<Vec<_>>();
let partition_ok = role.get(1).is_some_and(|p| p.starts_with("aws"));
let account_ok = role
.get(4)
.is_some_and(|n| n.len() == 12 && n.bytes().all(|b| b.is_ascii_digit()));
if role.len() != 6
|| role[0] != "arn"
|| !partition_ok
|| role[2] != "iam"
|| !account_ok
|| !role[5].starts_with("role/")
|| a.role.chars().any(char::is_control)
{
return Err(format!(
"aws-sts `role` `{}` must be a role ARN, arn:aws:iam::<account>:role/<name>",
a.role
));
}
if a.region.is_empty()
|| !a
.region
.bytes()
.all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-')
{
return Err(format!(
"aws-sts `region` `{}` is not a region name",
a.region
));
}
if let Some(profile) = &a.profile
&& (profile.is_empty()
|| profile.contains([']', '['])
|| profile.chars().any(char::is_control))
{
return Err("aws-sts `profile` is not a profile name".into());
}
if a.actions.is_empty() || a.resources.is_empty() {
return Err(
"aws-sts needs `actions` and `resources`: the session policy is what narrows the \
session below the role's own permissions"
.into(),
);
}
for action in &a.actions {
let (service, name) = action.split_once(':').unwrap_or(("", ""));
let ok = |s: &str, star: bool| {
!s.is_empty()
&& s.bytes()
.all(|b| b.is_ascii_alphanumeric() || b == b'-' || (star && b == b'*'))
};
if !ok(service, false) || !ok(name, true) || name == "*" {
return Err(format!(
"aws-sts action `{action}` must be `service:Action` and not every action of a \
service"
));
}
}
for resource in &a.resources {
if !resource.starts_with("arn:")
|| resource == "arn:*"
|| resource
.chars()
.any(|c| c.is_control() || c == '"' || c == '\\')
{
return Err(format!(
"aws-sts resource `{resource}` must be an ARN, not `*`"
));
}
}
lifetime_text(
"aws-sts",
a.lifetime.as_deref(),
a.refresh_before.as_deref(),
(MIN_AWS_LIFETIME_SECS, MAX_AWS_LIFETIME_SECS),
)
}
fn check_gcp(g: &Gcp) -> Result<(), String> {
let account = &g.service_account;
let (name, domain) = account.split_once('@').unwrap_or(("", ""));
if name.is_empty()
|| !domain.ends_with(".gserviceaccount.com")
|| !account
.bytes()
.all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b"@.-_".contains(&b))
{
return Err(format!(
"gcp `service_account` `{account}` must be a service account's email"
));
}
if g.scopes.is_empty() {
return Err("gcp `scopes` must name the OAuth scopes the token gets".into());
}
if let Some(bad) = g.scopes.iter().find(|s| {
s.strip_prefix(GCP_SCOPE_PREFIX).is_none_or(|rest| {
rest.is_empty()
|| !rest
.bytes()
.all(|b| b.is_ascii_alphanumeric() || b"._-".contains(&b))
})
}) {
return Err(format!(
"gcp scope `{bad}` must be a Google OAuth scope, {GCP_SCOPE_PREFIX}…"
));
}
lifetime_text(
"gcp",
g.lifetime.as_deref(),
g.refresh_before.as_deref(),
(MIN_GCP_LIFETIME_SECS, MAX_GCP_LIFETIME_SECS),
)
}
fn lifetime_text(
provider: &str,
lifetime: Option<&str>,
before: Option<&str>,
(min, max): (u64, u64),
) -> Result<(), String> {
if let Some(text) = lifetime {
let secs = duration_secs(text).ok_or_else(|| {
format!("{provider} lifetime `{text}` is not a duration such as `15m`")
})?;
if !(min..=max).contains(&secs) {
return Err(format!(
"{provider} lifetime `{text}` must be from {min}s to {max}s"
));
}
}
if let Some(text) = before
&& duration_secs(text).is_none_or(|s| s < 10)
{
return Err(format!(
"{provider} refresh_before `{text}` must be a duration of at least 10s"
));
}
Ok(())
}
pub(super) fn check_helper(
helper: Option<&Helper>,
source: &super::Source,
host: &str,
) -> Result<(), String> {
let aws_mint = matches!(source, super::Source::Mint(MintSource::AwsSts(_)));
match helper {
None if aws_mint => Err(
"an `aws-sts` mint needs `helper: aws`: AWS requests are signed by the client, so \
the proxy cannot put the credentials in a header"
.into(),
),
Some(Helper::Aws) if !aws_mint => {
Err("`helper: aws` hands out credentials an `aws-sts` mint made, nothing else".into())
}
None | Some(Helper::Aws) => Ok(()),
Some(Helper::Git { username }) => {
if host.parse::<core::net::Ipv4Addr>().is_ok() {
return Err(format!(
"`helper: git` needs a DNS name; {host} is an address"
));
}
if username.is_empty()
|| !username
.bytes()
.all(|b| b.is_ascii_alphanumeric() || b"._-@".contains(&b))
{
return Err(
"`helper: git` needs a user name of letters, digits, `.`, `_`, `-` or `@`"
.into(),
);
}
Ok(())
}
}
}