vaultrs_test/
oidc.rs

1use crate::docker::{Server, ServerConfig, TestInstance};
2use dockertest::{waitfor, Composition, DockerOperations, Image, PullPolicy, Source};
3
4#[derive(Clone)]
5pub struct OIDCServerConfig {
6    pub handle: String,
7    pub timeout: u16,
8    pub port: u32,
9    pub version: String,
10}
11
12impl ServerConfig for OIDCServerConfig {
13    fn to_comp(&self) -> Composition {
14        const IMAGE_NAME: &str = "ghcr.io/navikt/mock-oauth2-server";
15        const IMAGE_PORT: u32 = 8080;
16        const WAIT_MESSAGE: &str = "started server on address";
17
18        const PULL_POLICY: PullPolicy = PullPolicy::IfNotPresent;
19        const SOURCE: Source = Source::DockerHub(PULL_POLICY);
20
21        // Build image
22        let image = Image::with_repository(IMAGE_NAME)
23            .source(SOURCE)
24            .tag(&self.version);
25
26        // Build composition
27        let wait = Box::new(waitfor::MessageWait {
28            message: String::from(WAIT_MESSAGE),
29            source: waitfor::MessageSource::Stdout,
30            timeout: self.timeout,
31        });
32
33        let mut comp = Composition::with_image(image);
34        comp.port_map(IMAGE_PORT, self.port);
35        comp.with_wait_for(wait).with_container_name(&self.handle)
36    }
37
38    fn to_instance(&self) -> TestInstance {
39        TestInstance::new(vec![self.to_comp()])
40    }
41}
42
43impl OIDCServerConfig {
44    pub fn new(handle: &str, port: u32, version: &str, timeout: u16) -> OIDCServerConfig {
45        OIDCServerConfig {
46            handle: handle.to_string(),
47            port,
48            timeout,
49            version: version.to_string(),
50        }
51    }
52
53    /// If version is [None], defaults to `latest`.
54    pub fn default(version: Option<&str>) -> OIDCServerConfig {
55        OIDCServerConfig {
56            handle: String::from("vaultrs-oidc"),
57            port: 8080,
58            timeout: 15,
59            version: version
60                .map(|v| v.to_string())
61                .unwrap_or_else(|| String::from("latest")),
62        }
63    }
64}
65
66pub struct OIDCServer {
67    pub address: String,
68    pub address_internal: String,
69    pub config: OIDCServerConfig,
70}
71
72impl Server for OIDCServer {
73    type Config = OIDCServerConfig;
74
75    fn new(opts: &DockerOperations, config: &Self::Config) -> Self {
76        let cont = opts.handle(config.handle.as_str());
77        let address = format!("http://localhost:{}", config.port);
78        let address_internal = format!("http://{}:{}", cont.ip(), config.port);
79        OIDCServer {
80            address,
81            address_internal,
82            config: config.clone(),
83        }
84    }
85}