Skip to main content

bitwarden_access_connector/resolver/
env.rs

1//! Environment variable naming for per-target credentials.
2
3use bitwarden_pam_sealed::SealedTargetSystemKind;
4use uuid::Uuid;
5
6pub(crate) const LDAP_HOST_SUFFIX: &str = "LDAP_HOST";
7pub(crate) const BIND_DN_SUFFIX: &str = "BIND_DN";
8pub(crate) const BASE_DN_SUFFIX: &str = "BASE_DN";
9pub(crate) const BIND_PASSWORD_SUFFIX: &str = "BIND_PASSWORD";
10pub(crate) const CA_CERTIFICATE_SUFFIX: &str = "CA_CERTIFICATE";
11
12/// Environment variable suffixes required per target kind.
13pub(crate) fn required_suffixes(kind: SealedTargetSystemKind) -> &'static [&'static str] {
14    match kind {
15        SealedTargetSystemKind::Entra => &["TENANT_ID", "CLIENT_ID", "CLIENT_SECRET"],
16        SealedTargetSystemKind::CustomScript => &["SCRIPT"],
17        SealedTargetSystemKind::ActiveDirectory => &[
18            LDAP_HOST_SUFFIX,
19            BIND_DN_SUFFIX,
20            BASE_DN_SUFFIX,
21            BIND_PASSWORD_SUFFIX,
22        ],
23    }
24}
25
26/// Env var prefix for a target, e.g. `EC2C1D46_6A4B_4751_A310_AF9601317F2D_`.
27pub(crate) fn prefix_for(id: Uuid) -> String {
28    let mut s = id.to_string().to_uppercase();
29    s = s.replace('-', "_");
30    s.push('_');
31    s
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37
38    #[test]
39    fn prefix_plain_uuid() {
40        let id: Uuid = "ec2c1d46-6a4b-4751-a310-af9601317f2d".parse().unwrap();
41        assert_eq!(prefix_for(id), "EC2C1D46_6A4B_4751_A310_AF9601317F2D_");
42    }
43
44    #[test]
45    fn prefix_hyphens_replaced_by_underscores() {
46        let id: Uuid = "00000000-0000-0000-0000-000000000001".parse().unwrap();
47        let p = prefix_for(id);
48        assert!(!p.contains('-'), "hyphens must be replaced: {p}");
49        assert!(p.ends_with('_'), "must have trailing underscore: {p}");
50    }
51
52    #[test]
53    fn prefix_fully_uppercased() {
54        let id: Uuid = "aabbccdd-eeff-1122-3344-556677889900".parse().unwrap();
55        let p = prefix_for(id);
56        assert_eq!(p, p.to_uppercase(), "prefix must be all-uppercase: {p}");
57    }
58}