Watch
0
0
Fork
You've already forked hyperhive
0

gate ssh signing per agent: ssh permission, signing policy on the login role, ca key endpoint

This commit is contained in:
atlas 2026-11-16 18:29:06 +01:00 • committed by mara
commit 910f9a6f50

View file

@ -474,6 +474,67 @@ impl SecretStore {
Ok(())
}
/// The public key of the SSH secrets engine mounted at `mount`, one
/// `authorized_keys`-style line, or `None` when the mount holds no CA yet.
///
/// # Errors
/// [`Error::Vault`] for anything that is not a 400 or 404: a denial, or an
/// unreachable store.
pub async fn ssh_ca_public_key(&self, mount: &str) -> Result<Option<String>, Error> {
let request = ReadSshCa {
mount: mount.to_owned(),
};
match vaultrs::api::exec_with_result(&self.inner, request).await {
Ok(ca) => Ok(Some(ca.public_key.trim().to_owned()).filter(|k| !k.is_empty())),
Err(e) if is_absent(&e) || is_bad_request(&e) => Ok(None),
Err(e) => Err(e.into()),
}
}
/// The policies the cert-auth role `name` attaches to its tokens, or `None`
/// when the role does not exist.
///
/// # Errors
/// [`Error::Vault`] for anything that is not a 404.
pub async fn cert_role_policies(
&self,
mount: &str,
name: &str,
) -> Result<Option<Vec<String>>, Error> {
let request = ReadCertRolePolicies {
mount: mount.to_owned(),
name: name.to_owned(),
};
match vaultrs::api::exec_with_result(&self.inner, request).await {
Ok(role) => Ok(Some(role.token_policies)),
Err(e) if is_absent(&e) => Ok(None),
Err(e) => Err(e.into()),
}
}
/// Replace the policies the existing cert-auth role `name` attaches, leaving
/// the rest of the role as it is. The write names no other field, for the
/// reason [`Self::set_cert_role_token_ttls`] gives. Tokens already issued
/// keep the policies they were issued with until they expire or are renewed.
///
/// # Errors
/// [`Error::Vault`] when the token's policy does not cover
/// `auth/<mount>/certs/<name>`, or the store refuses the write.
pub async fn set_cert_role_policies(
&self,
mount: &str,
name: &str,
policies: &[&str],
) -> Result<(), Error> {
let request = SetCertRolePolicies {
mount: mount.to_owned(),
name: name.to_owned(),
token_policies: policies.iter().map(|p| (*p).to_owned()).collect(),
};
vaultrs::api::exec_with_empty(&self.inner, request).await?;
Ok(())
}
/// Have the PKI role `role` on `mount` issue a client certificate for
/// `common_name`, returning it with its key and the issuing CA.
///
@ -768,6 +829,11 @@ fn is_absent(e: &vaultrs::error::ClientError) -> bool {
matches!(e, vaultrs::error::ClientError::APIError { code: 404, .. })
}
/// A 400, which the SSH engine answers a CA read with while it holds no CA.
fn is_bad_request(e: &vaultrs::error::ClientError) -> bool {
matches!(e, vaultrs::error::ClientError::APIError { code: 400, .. })
}
/// Write an ACL policy, spelled out here because [`vaultrs`] does not have it:
/// its `sys::policy` module targets `sys/policy/<name>`, the deprecated alias,
/// and the store ACLs that path separately from `sys/policies/acl/<name>`. A
@ -828,6 +894,53 @@ struct SetCertRoleTtls {
token_max_ttl: String,
}
/// Read an SSH engine mount's CA.
#[derive(Endpoint)]
#[endpoint(path = "{self.mount}/config/ca", method = "GET", response = "SshCa")]
struct ReadSshCa {
#[endpoint(skip)]
mount: String,
}
/// The public half of the CA, the one field read.
#[derive(Debug, serde::Deserialize)]
struct SshCa {
public_key: String,
}
/// Read the policies of a cert-auth role.
#[derive(Endpoint)]
#[endpoint(
path = "auth/{self.mount}/certs/{self.name}",
method = "GET",
response = "CertRolePolicies"
)]
struct ReadCertRolePolicies {
#[endpoint(skip)]
mount: String,
#[endpoint(skip)]
name: String,
}
/// The one field of a cert-auth role [`SecretStore::cert_role_policies`] reads.
#[derive(Debug, serde::Deserialize)]
struct CertRolePolicies {
#[serde(default)]
token_policies: Vec<String>,
}
/// Write the policies of a cert-auth role and nothing else, as [`SetCertRoleTtls`]
/// does for the lifetimes.
#[derive(Endpoint)]
#[endpoint(path = "auth/{self.mount}/certs/{self.name}", method = "POST")]
struct SetCertRolePolicies {
#[endpoint(skip)]
mount: String,
#[endpoint(skip)]
name: String,
token_policies: Vec<String>,
}
/// The request [`SecretStore::set_cert_role_token_ttls`] sends.
fn set_cert_role_token_ttls_request(mount: &str, name: &str) -> SetCertRoleTtls {
SetCertRoleTtls {