Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .agents/skills/helm-dev-environment/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,35 @@ Envoy Gateway is already installed by Skaffold (the `envoy-gateway` Helm release
service for the proxy; klipper-lb binds it to hostPort 80, reachable via the
`8080:80` load balancer port mapping.

### BackendTLSPolicy (end-to-end TLS)

To enable end-to-end TLS between the Gateway proxy and the gateway pod, add
BackendTLSPolicy values to the Helm install:

```bash
helm upgrade --install openshell deploy/helm/openshell \
--set grpcRoute.enabled=true \
--set grpcRoute.backendTLSPolicy.enabled=true \
--set server.tls.enableMtls=false \
...
```

This requires `server.tls.enableMtls=false` because ingress proxies cannot
present client certificates to the backend. The certgen hook creates a backend
CA ConfigMap from the server Secret's `ca.crt` key. With cert-manager, a
separate post-install Job polls for the cert-manager-issued certificate (up to
`pkiInitJob.timeoutSeconds`); with built-in PKI the ConfigMap is created in the
same pre-install hook. The ConfigMap is reconciled on every upgrade so CA
rotations propagate automatically.

Key Helm values:
- `grpcRoute.backendTLSPolicy.enabled`: create the BackendTLSPolicy resource
- `grpcRoute.backendTLSPolicy.caCertificateConfigMapName`: override ConfigMap name
- `grpcRoute.backendTLSPolicy.hostname`: override backend validation hostname
- `server.tls.enableMtls`: must be `false` for BackendTLSPolicy
- `pkiInitJob.timeoutSeconds`: polling duration for cert-manager mode
- `pkiInitJob.failOnTimeout`: fail install if cert-manager times out

### Keycloak OIDC

One-time setup — only needed once per cluster lifetime:
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions architecture/gateway.md
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,25 @@ requested present -> generate and write. This guards continuity across restarts
and upgrades while still recovering cleanly if an operator deletes everything
and starts over.

When `grpcRoute.backendTLSPolicy.enabled=true`, the certgen hook also creates a
`ConfigMap` containing the CA certificate (`ca.crt`) used by the Gateway proxy
to validate the backend pod's TLS certificate. The CA is always read from the
authoritative server Secret (not the in-memory bundle) so that enabling
BackendTLSPolicy on an existing release uses the CA that actually signed the
server certificate. The ConfigMap is reconciled on every hook run: if the CA
changes (rotation, re-issue), the ConfigMap is updated in place. In built-in PKI
mode the ConfigMap is created in the same pre-install hook. In cert-manager
mode, a separate post-install/post-upgrade hook Job polls for the cert-manager-
issued server Secret and then creates or updates the ConfigMap, because
cert-manager Certificate resources are regular release objects applied after
pre-install hooks.

The `server.tls.enableMtls` value controls whether the gateway requires client
certificates. When `enableMtls` is `false`, the gateway runs HTTPS-only without
client certificate verification (use OIDC for identity instead). BackendTLSPolicy
requires `enableMtls=false` because the ingress proxy cannot present client
certificates to the backend.

Operators who manage TLS PKI with cert-manager enable `certManager.enabled`;
cert-manager takes precedence over built-in TLS generation and the chart still
renders the JWT-only hook. Operators who pre-create all TLS and JWT Secrets can
Expand Down
1 change: 1 addition & 0 deletions crates/openshell-bootstrap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ tokio = { workspace = true }
tracing = { workspace = true }

[dev-dependencies]
x509-parser = "0.16"

[lints]
workspace = true
59 changes: 58 additions & 1 deletion crates/openshell-bootstrap/src/pki.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

use crate::jwt::{JwtKeyMaterial, generate_jwt_key};
use miette::{IntoDiagnostic, Result, WrapErr};
use rcgen::{BasicConstraints, CertificateParams, DnType, Ia5String, IsCa, KeyPair, SanType};
use rcgen::{
BasicConstraints, CertificateParams, DnType, Ia5String, IsCa, KeyPair, KeyUsagePurpose, SanType,
};
use std::net::IpAddr;

/// All PEM-encoded materials produced by [`generate_pki`].
Expand Down Expand Up @@ -59,6 +61,7 @@ pub fn generate_pki(extra_sans: &[String]) -> Result<PkiBundle> {
.into_diagnostic()
.wrap_err("failed to create CA params")?;
ca_params.is_ca = IsCa::Ca(BasicConstraints::Unconstrained);
ca_params.key_usages = vec![KeyUsagePurpose::KeyCertSign, KeyUsagePurpose::CrlSign];
ca_params
.distinguished_name
.push(DnType::OrganizationName, "openshell");
Expand All @@ -80,6 +83,7 @@ pub fn generate_pki(extra_sans: &[String]) -> Result<PkiBundle> {
.into_diagnostic()
.wrap_err("failed to create server cert params")?;
server_params.subject_alt_names = server_sans;
server_params.use_authority_key_identifier_extension = true;
server_params
.distinguished_name
.push(DnType::CommonName, "openshell-server");
Expand All @@ -96,6 +100,7 @@ pub fn generate_pki(extra_sans: &[String]) -> Result<PkiBundle> {
let mut client_params = CertificateParams::new(Vec::<String>::new())
.into_diagnostic()
.wrap_err("failed to create client cert params")?;
client_params.use_authority_key_identifier_extension = true;
client_params
.distinguished_name
.push(DnType::CommonName, "openshell-client");
Expand Down Expand Up @@ -178,6 +183,58 @@ mod tests {
assert!(bundle.server_cert_pem.contains("BEGIN CERTIFICATE"));
}

#[test]
fn generate_pki_emits_strict_verifier_extensions() {
use x509_parser::pem::parse_x509_pem;
use x509_parser::prelude::{FromDer, ParsedExtension, X509Certificate};

let bundle = generate_pki(&[]).expect("generate_pki failed");
let parse = |pem: &str| -> Vec<u8> {
parse_x509_pem(pem.as_bytes())
.expect("valid PEM")
.1
.contents
};
let ca_der = parse(&bundle.ca_cert_pem);
let ca = X509Certificate::from_der(&ca_der).expect("valid CA cert").1;
let ca_key_usage = ca
.key_usage()
.expect("readable key usage")
.expect("CA has a key usage extension");
assert!(ca_key_usage.value.key_cert_sign());
assert!(ca_key_usage.value.crl_sign());
let ca_ski = ca
.get_extension_unique(&x509_parser::oid_registry::OID_X509_EXT_SUBJECT_KEY_IDENTIFIER)
.expect("readable SKI")
.expect("CA has a Subject Key Identifier");

for (name, pem) in [
("server", &bundle.server_cert_pem),
("client", &bundle.client_cert_pem),
] {
let der = parse(pem);
let cert = X509Certificate::from_der(&der).expect("valid leaf cert").1;
let aki = cert
.get_extension_unique(
&x509_parser::oid_registry::OID_X509_EXT_AUTHORITY_KEY_IDENTIFIER,
)
.expect("readable AKI")
.unwrap_or_else(|| panic!("{name} cert has no Authority Key Identifier"));
let (
ParsedExtension::AuthorityKeyIdentifier(aki),
ParsedExtension::SubjectKeyIdentifier(ski),
) = (aki.parsed_extension(), ca_ski.parsed_extension())
else {
panic!("{name}: unexpected extension shapes");
};
let key_id = aki
.key_identifier
.as_ref()
.unwrap_or_else(|| panic!("{name} AKI has no key identifier"));
assert_eq!(key_id.0, ski.0, "{name} AKI must match the CA SKI");
}
}

#[test]
fn build_server_sans_includes_defaults_and_extras() {
let extras = vec!["192.168.1.100".to_string(), "remote.host".to_string()];
Expand Down
196 changes: 194 additions & 2 deletions crates/openshell-server/src/certgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

use clap::Args;
use k8s_openapi::ByteString;
use k8s_openapi::api::core::v1::Secret;
use k8s_openapi::api::core::v1::{ConfigMap, Secret};
use kube::Client;
use kube::api::{Api, ObjectMeta, PostParams};
use miette::{IntoDiagnostic, Result, WrapErr};
Expand Down Expand Up @@ -78,6 +78,33 @@ pub struct CertgenArgs {
/// For local debugging.
#[arg(long)]
dry_run: bool,

/// Name of a `ConfigMap` to create containing the CA certificate (key: ca.crt)
/// for `BackendTLSPolicy` backend validation. The CA is always read from the
/// authoritative server Secret: --server-secret-name in full PKI mode,
/// --backend-ca-source-secret in --jwt-only mode.
#[arg(long, value_name = "NAME")]
backend_ca_configmap_name: Option<String>,

/// Name of an existing `Secret` containing a ca.crt key to populate the
/// backend CA `ConfigMap` from. Required with --jwt-only when
/// --backend-ca-configmap-name is set (typically the server TLS `Secret`
/// created by cert-manager).
#[arg(long, value_name = "NAME", requires = "backend_ca_configmap_name")]
backend_ca_source_secret: Option<String>,

/// Maximum time in seconds to poll for the backend CA source `Secret` when
/// using cert-manager. Defaults to 90 seconds. The Helm chart sets this to
/// (Job activeDeadlineSeconds - 30) to leave margin for `ConfigMap` creation.
#[arg(long, value_name = "SECONDS", default_value = "90")]
backend_ca_poll_timeout_seconds: u64,

/// Fail with an error if the backend CA source `Secret` is not found within
/// the polling timeout. When false (default), the hook succeeds with a warning
/// and the `ConfigMap` is not created. The Helm chart sets this based on
/// pkiInitJob.failOnTimeout.
#[arg(long)]
backend_ca_fail_on_timeout: bool,
}

pub async fn run(args: CertgenArgs) -> Result<()> {
Expand All @@ -97,7 +124,13 @@ pub async fn run(args: CertgenArgs) -> Result<()> {
run_local(dir, &args.server_sans)
} else {
let bundle = generate_pki(&args.server_sans)?;
run_kubernetes(&args, &bundle).await
run_kubernetes(&args, &bundle).await?;

if let Some(ref cm_name) = args.backend_ca_configmap_name {
create_backend_ca_configmap_if_needed(&args, cm_name).await?;
}

Ok(())
}
}

Expand Down Expand Up @@ -293,6 +326,165 @@ async fn create_tls_secrets(
Ok(())
}

fn extract_ca_from_secret(secret: &Secret, name: &str) -> Result<String> {
let data = secret
.data
.as_ref()
.ok_or_else(|| miette::miette!("secret {name} has no data"))?;
let ca = data
.get("ca.crt")
.ok_or_else(|| miette::miette!("secret {name} has no ca.crt key"))?;
String::from_utf8(ca.0.clone())
.into_diagnostic()
.wrap_err("ca.crt is not valid UTF-8")
}

async fn create_backend_ca_configmap_if_needed(
args: &CertgenArgs,
configmap_name: &str,
) -> Result<()> {
let namespace = args
.namespace
.as_deref()
.ok_or_else(|| miette::miette!("--namespace is required (or set POD_NAMESPACE)"))?;

let client = Client::try_default()
.await
.into_diagnostic()
.wrap_err("failed to construct Kubernetes client for backend CA ConfigMap")?;
let secret_api: Api<Secret> = Api::namespaced(client.clone(), namespace);
let cm_api: Api<ConfigMap> = Api::namespaced(client, namespace);

// Resolve the CA from the authoritative server Secret rather than the
// in-memory bundle so upgrades that enable BackendTLSPolicy on an
// existing release use the CA that actually signed the server cert.
let ca_pem = if let Some(source_secret) = &args.backend_ca_source_secret {
let poll_timeout = std::time::Duration::from_secs(args.backend_ca_poll_timeout_seconds);
let poll_interval = std::time::Duration::from_secs(2);
let start = std::time::Instant::now();

let secret = loop {
match secret_api
.get_opt(source_secret)
.await
.into_diagnostic()
.wrap_err_with(|| format!("failed to read secret {source_secret}"))?
{
Some(secret) => break secret,
None if start.elapsed() >= poll_timeout => {
let msg = format!(
"Backend CA source secret {source_secret} not found after {timeout_secs}s; \
ConfigMap {configmap_name} not created. This is expected if cert-manager \
is still issuing the certificate.",
timeout_secs = poll_timeout.as_secs()
);
if args.backend_ca_fail_on_timeout {
return Err(miette::miette!(
"{msg} Install failed due to --backend-ca-fail-on-timeout."
));
}
warn!(
secret = %source_secret,
configmap = %configmap_name,
timeout_secs = poll_timeout.as_secs(),
"{msg} Run helm upgrade after the TLS secret exists or the BackendTLSPolicy \
will remain non-functional until the ConfigMap is created manually."
);
return Ok(());
}
None => {
if start.elapsed().as_secs().is_multiple_of(10) {
info!(
secret = %source_secret,
elapsed_secs = start.elapsed().as_secs(),
"Waiting for cert-manager to issue TLS certificate..."
);
}
tokio::time::sleep(poll_interval).await;
}
}
};

info!(
secret = %source_secret,
elapsed_secs = start.elapsed().as_secs(),
"cert-manager TLS certificate found."
);
extract_ca_from_secret(&secret, source_secret)?
} else if let Some(server_secret) = &args.server_secret_name {
let secret = secret_api
.get(server_secret)
.await
.into_diagnostic()
.wrap_err_with(|| format!("failed to read server secret {server_secret}"))?;
extract_ca_from_secret(&secret, server_secret)?
} else {
return Err(miette::miette!(
"--backend-ca-source-secret or --server-secret-name is required \
with --backend-ca-configmap-name"
));
};

// Reconcile: create or update so the backend CA stays current across
// CA rotations and upgrades.
if let Some(existing) = cm_api
.get_opt(configmap_name)
.await
.into_diagnostic()
.wrap_err_with(|| format!("failed to read configmap {configmap_name}"))?
{
let up_to_date = existing
.data
.as_ref()
.and_then(|d| d.get("ca.crt"))
.map(String::as_str)
== Some(&ca_pem);
if up_to_date {
info!(
namespace = %namespace,
configmap = %configmap_name,
"Backend CA ConfigMap is up-to-date, skipping."
);
return Ok(());
}
let mut updated = existing;
updated.data = Some(BTreeMap::from([("ca.crt".to_string(), ca_pem)]));
cm_api
.replace(configmap_name, &PostParams::default(), &updated)
.await
.into_diagnostic()
.wrap_err_with(|| format!("failed to update configmap {configmap_name}"))?;
info!(
namespace = %namespace,
configmap = %configmap_name,
"Backend CA ConfigMap updated with current CA."
);
return Ok(());
}

let configmap = ConfigMap {
metadata: ObjectMeta {
name: Some(configmap_name.to_string()),
..Default::default()
},
data: Some(BTreeMap::from([("ca.crt".to_string(), ca_pem)])),
..Default::default()
};

cm_api
.create(&PostParams::default(), &configmap)
.await
.into_diagnostic()
.wrap_err_with(|| format!("failed to create configmap {configmap_name}"))?;

info!(
namespace = %namespace,
configmap = %configmap_name,
"Backend CA ConfigMap created."
);
Ok(())
}

fn tls_secret(name: &str, crt_pem: &str, key_pem: &str, ca_pem: &str) -> Secret {
let mut data = BTreeMap::new();
data.insert(
Expand Down
Loading
Loading