nifi_rust_client/config/
credentials.rs1use std::fmt;
8
9use crate::error::NifiError;
10
11#[derive(Clone)]
16pub struct Redacted<T>(T);
17
18impl<T> Redacted<T> {
19 pub fn new(value: T) -> Self {
21 Self(value)
22 }
23
24 pub fn inner(&self) -> &T {
26 &self.0
27 }
28}
29
30impl<T> fmt::Debug for Redacted<T> {
31 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32 f.write_str("[REDACTED]")
33 }
34}
35
36#[async_trait::async_trait]
41pub trait CredentialProvider: Send + Sync + fmt::Debug {
42 async fn credentials(&self) -> Result<(String, String), NifiError>;
45}
46
47#[derive(Debug, Clone)]
52pub struct StaticCredentials {
53 username: String,
54 password: Redacted<String>,
55}
56
57impl StaticCredentials {
58 pub fn new(username: impl Into<String>, password: impl Into<String>) -> Self {
60 Self {
61 username: username.into(),
62 password: Redacted::new(password.into()),
63 }
64 }
65}
66
67#[async_trait::async_trait]
68impl CredentialProvider for StaticCredentials {
69 async fn credentials(&self) -> Result<(String, String), NifiError> {
70 Ok((self.username.clone(), self.password.inner().clone()))
71 }
72}
73
74#[derive(Debug, Clone)]
79pub struct EnvCredentials {
80 username_var: String,
81 password_var: String,
82}
83
84impl EnvCredentials {
85 pub fn new() -> Self {
88 Self {
89 username_var: "NIFI_USERNAME".to_string(),
90 password_var: "NIFI_PASSWORD".to_string(),
91 }
92 }
93
94 pub fn with_vars(username_var: impl Into<String>, password_var: impl Into<String>) -> Self {
96 Self {
97 username_var: username_var.into(),
98 password_var: password_var.into(),
99 }
100 }
101
102 pub fn username_var(&self) -> &str {
104 &self.username_var
105 }
106
107 pub fn password_var(&self) -> &str {
109 &self.password_var
110 }
111}
112
113impl Default for EnvCredentials {
114 fn default() -> Self {
115 Self::new()
116 }
117}
118
119#[async_trait::async_trait]
120impl CredentialProvider for EnvCredentials {
121 async fn credentials(&self) -> Result<(String, String), NifiError> {
122 let username = std::env::var(&self.username_var).map_err(|_| NifiError::Auth {
123 message: format!("environment variable {} is not set", self.username_var),
124 })?;
125 let password = std::env::var(&self.password_var).map_err(|_| NifiError::Auth {
126 message: format!("environment variable {} is not set", self.password_var),
127 })?;
128 Ok((username, password))
129 }
130}