ldap: tests: add LDAP integration tests

This commit adds integration tests to ensure that the crate works as intended.
The tests are executed against a real LDAP server, namely `glauth`. `glauth` was
chosen because it ships as a single, statically compiled binary and can
be configured with a single configuration file.

The tests are written as off-the-shelf unit tests. However, they are
 #[ignored] by default, as they have some special requirements:
   * They required the GLAUTH_BIN environment variable to be set,
     pointing to the location of the `glauth` binary. `glauth` will be
     started and stopped automatically by the test suite.
   * Tests have to be executed sequentially (`--test-threads 1`),
     otherwise multiple instances of the glauth server might bind to the
     same port.

The `run_integration_tests.sh` checks whether GLAUTH_BIN is set, or if
not, attempts to find `glauth` on PATH. The script also ensures that the
tests are run sequentially.

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
This commit is contained in:
Lukas Wagner 2023-01-24 11:03:36 +01:00 committed by Wolfgang Bumiller
parent 4488256cb1
commit 582e994cca
9 changed files with 419 additions and 1 deletions

View File

@ -14,3 +14,5 @@ ldap3 = { workspace = true, default_features = false, features = ["tls"] }
serde = { workspace = true, features = ["derive"] }
native-tls.workspace = true
[dev_dependencies]
proxmox-async.workspace = true

View File

@ -0,0 +1,31 @@
#!/bin/bash
#
# Run integration tests for the proxmox_ldap crate.
# At this time, the tests require `glauth` to be available,
# either explicitly passed via $GLAUTH_PATH, or somewhere
# on $PATH.
#
# Tested with glauth v2.1.0
function run_tests {
# All tests that need glauth running are ignored, so
# that we can run `cargo test` without caring about them
# Also, only run on 1 thread, because otherwise
# glauth would need a separate port for each rurnning test
exec cargo test -- --ignored --test-threads 1
}
if [ -z ${GLAUTH_BIN+x} ];
then
GLAUTH_BIN=$(command -v glauth)
if [ $? -eq 0 ] ;
then
export GLAUTH_BIN
else
echo "glauth not found in PATH"
exit 1
fi
fi
run_tests

View File

@ -62,7 +62,7 @@ pub struct SearchParameters {
pub user_filter: Option<String>,
}
#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Debug)]
/// Single LDAP user search result
pub struct SearchResult {
/// The full user's domain

View File

@ -0,0 +1,4 @@
#!/bin/bash
openssl req -x509 -newkey rsa:4096 -keyout glauth.key -out glauth.crt -days 36500 -nodes -subj '/CN=localhost'

View File

@ -0,0 +1,67 @@
debug = true
[ldap]
enabled = true
listen = "0.0.0.0:3893"
[ldaps]
enabled = true
listen = "0.0.0.0:3894"
cert = "tests/assets/glauth.crt"
key = "tests/assets/glauth.key"
[backend]
datastore = "config"
baseDN = "dc=example,dc=com"
nameformat = "cn"
groupformat = "ou"
# to create a passSHA256: echo -n "mysecret" | openssl dgst -sha256
[[users]]
name = "test1"
givenname="Test 1"
sn="User"
mail = "test1@example.com"
uidnumber = 1001
primarygroup = 1000
passsha256 = "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8" # password
[[users]]
name = "test2"
givenname="Test 2"
sn="User"
mail = "test2@example.com"
uidnumber = 1002
primarygroup = 1000
passsha256 = "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8" # password
[[users]]
name = "test3"
givenname="Test 3"
sn="User"
mail = "test3@example.com"
uidnumber = 1003
primarygroup = 1000
passsha256 = "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8" # password
[[users]]
name = "serviceuser"
mail = "serviceuser@example.com"
uidnumber = 1111
primarygroup = 1001
passsha256 = "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8" # password
[[users.capabilities]]
action = "search"
object = "*"
[[groups]]
name = "testgroup"
gidnumber = 1000
[[groups]]
name = "svcaccts"
gidnumber = 1001

View File

@ -0,0 +1,29 @@
-----BEGIN CERTIFICATE-----
MIIFCzCCAvOgAwIBAgIUREnN1wK1O6wTcVxSk6d5o0yHwjswDQYJKoZIhvcNAQEL
BQAwFDESMBAGA1UEAwwJbG9jYWxob3N0MCAXDTIzMDExNzEwMjEwNFoYDzIxMjIx
MjI0MTAyMTA0WjAUMRIwEAYDVQQDDAlsb2NhbGhvc3QwggIiMA0GCSqGSIb3DQEB
AQUAA4ICDwAwggIKAoICAQC/6A+m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RoYzRY9HaPEzeFyunG9
EB70mvZRpEOs6AembbBuag1ykVjSGqifBzJd8vHVo9AoBXW4owq3+LSINlGko2Wh
Y5jyaWgSvAx3vfVxZaAzkKB7dQqsrl8drS4AwkJ40KNmqVm8T7DEBYX20aQKNYWS
sMxRAoIBAQD3ycMDd0CP8aAbOwLVwHboNzeZNpLCx8oXV16/qID0PO7M8kT1d+0x
V38FxL3UGafWSwsRZhU4GrwNDJVK6LA40m5v3CiZF1tsUJLlwSR7If4MXTU6g9uC
x35EXdr+kcKSw6QCVRc1CcPRUIwRisqn67SDeREDzZUtAoCyZex0dOj9mzVc+HID
Wh4SeBmHbeToCFRhIANSsAj0psO0nuvAgVDEekGiQ/m9DrZriEiZ/adniUd0gSHi
lpgXcqNlVS4/Pp56GkdEOvHleVr5R+y0P1NCNcdl+iRxmfdIMhEwrSZnqfjntXMU
yWQL2XczMt+NRQn+v23VKpQVK/R9PMEV
-----END PRIVATE KEY-----

View File

@ -0,0 +1,67 @@
debug = true
[ldap]
enabled = true
listen = "[::]:3893"
[ldaps]
enabled = true
listen = "[::]:3894"
cert = "tests/assets/glauth.crt"
key = "tests/assets/glauth.key"
[backend]
datastore = "config"
baseDN = "dc=example,dc=com"
nameformat = "cn"
groupformat = "ou"
# to create a passSHA256: echo -n "mysecret" | openssl dgst -sha256
[[users]]
name = "test1"
givenname="Test 1"
sn="User"
mail = "test1@example.com"
uidnumber = 1001
primarygroup = 1000
passsha256 = "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8" # password
[[users]]
name = "test2"
givenname="Test 2"
sn="User"
mail = "test2@example.com"
uidnumber = 1002
primarygroup = 1000
passsha256 = "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8" # password
[[users]]
name = "test3"
givenname="Test 3"
sn="User"
mail = "test3@example.com"
uidnumber = 1003
primarygroup = 1000
passsha256 = "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8" # password
[[users]]
name = "serviceuser"
mail = "serviceuser@example.com"
uidnumber = 1111
primarygroup = 1001
passsha256 = "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8" # password
[[users.capabilities]]
action = "search"
object = "*"
[[groups]]
name = "testgroup"
gidnumber = 1000
[[groups]]
name = "svcaccts"
gidnumber = 1001

View File

@ -0,0 +1,166 @@
use std::{
process::{Child, Command, Stdio},
thread::sleep,
time::Duration,
};
use anyhow::{Context, Error};
use proxmox_ldap::*;
struct GlauthServer {
handle: Child,
}
impl GlauthServer {
fn new(path: &str) -> Result<Self, Error> {
let glauth_bin = std::env::var("GLAUTH_BIN").context("GLAUTH_BIN is not set")?;
let handle = Command::new(&glauth_bin)
.arg("-c")
.arg(path)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
.context("Could not start glauth process")?;
// Make 'sure' that glauth is up
sleep(Duration::from_secs(1));
Ok(Self { handle })
}
}
impl Drop for GlauthServer {
fn drop(&mut self) {
self.handle.kill().ok();
}
}
fn authenticate(con: &LdapConnection, user: &str, pass: &str) -> Result<(), Error> {
proxmox_async::runtime::block_on(con.authenticate_user(user, pass))
}
fn default_config() -> LdapConfig {
LdapConfig {
servers: vec!["localhost".into()],
port: Some(3893),
user_attr: "cn".into(),
base_dn: "dc=example,dc=com".into(),
bind_dn: Some("cn=serviceuser,ou=svcaccts,dc=example,dc=com".into()),
bind_password: Some("password".into()),
tls_mode: LdapConnectionMode::Ldap,
verify_certificate: false,
additional_trusted_certificates: None,
certificate_store_path: Some("/etc/ssl/certs".into()),
}
}
#[test]
#[ignore]
fn test_authentication() -> Result<(), Error> {
let _glauth = GlauthServer::new("tests/assets/glauth.cfg")?;
let connection = LdapConnection::new(default_config());
assert!(authenticate(&connection, "test1", "password").is_ok());
assert!(authenticate(&connection, "test2", "password").is_ok());
assert!(authenticate(&connection, "test3", "password").is_ok());
assert!(authenticate(&connection, "test1", "invalid").is_err());
assert!(authenticate(&connection, "invalid", "password").is_err());
Ok(())
}
#[test]
#[ignore]
fn test_authentication_via_ipv6() -> Result<(), Error> {
let _glauth = GlauthServer::new("tests/assets/glauth_v6.cfg")?;
let settings = LdapConfig {
servers: vec!["[::1]".into()],
..default_config()
};
let connection = LdapConnection::new(settings);
assert!(authenticate(&connection, "test1", "password").is_ok());
Ok(())
}
#[test]
#[ignore]
fn test_authentication_via_ldaps() -> Result<(), Error> {
let settings = LdapConfig {
port: Some(3894),
tls_mode: LdapConnectionMode::Ldaps,
verify_certificate: true,
additional_trusted_certificates: Some(vec!["tests/assets/glauth.crt".into()]),
..default_config()
};
let _glauth = GlauthServer::new("tests/assets/glauth.cfg")?;
let connection = LdapConnection::new(settings);
assert!(authenticate(&connection, "test1", "password").is_ok());
assert!(authenticate(&connection, "test1", "invalid").is_err());
Ok(())
}
#[test]
#[ignore]
fn test_fallback() -> Result<(), Error> {
let settings = LdapConfig {
servers: vec!["invalid.host".into(), "localhost".into()],
..default_config()
};
let _glauth = GlauthServer::new("tests/assets/glauth.cfg")?;
let connection = LdapConnection::new(settings);
assert!(authenticate(&connection, "test1", "password").is_ok());
Ok(())
}
#[test]
#[ignore]
fn test_search() -> Result<(), Error> {
let _glauth = GlauthServer::new("tests/assets/glauth.cfg")?;
let connection = LdapConnection::new(default_config());
let params = SearchParameters {
attributes: vec!["cn".into(), "mail".into(), "sn".into()],
user_classes: vec!["posixAccount".into()],
user_filter: Some("(cn=test*)".into()),
};
let search_results = proxmox_async::runtime::block_on(connection.search_entities(&params))?;
assert_eq!(search_results.len(), 3);
for a in search_results {
assert!(a.dn.starts_with("cn=test"));
assert!(a.dn.ends_with("ou=testgroup,ou=users,dc=example,dc=com"));
assert!(a
.attributes
.get("mail")
.unwrap()
.get(0)
.unwrap()
.ends_with("@example.com"));
assert!(a
.attributes
.get("sn")
.unwrap()
.get(0)
.unwrap()
.eq("User".into()));
}
Ok(())
}