1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//! Extension trait for ClientConfig to provide environment-based configuration and impersonation
use alien_client_core::{ErrorData, Result};
use alien_core::{ClientConfig, ImpersonationConfig, Platform};
use alien_error::AlienError;
#[cfg(any(
feature = "aws",
feature = "gcp",
feature = "azure",
feature = "kubernetes"
))]
use alien_error::Context;
use async_trait::async_trait;
use std::collections::HashMap;
/// Extension trait for ClientConfig providing environment-based configuration and cloud-agnostic impersonation
#[async_trait]
pub trait ClientConfigExt {
/// Create a platform configuration from environment variables based on the specified platform.
///
/// # Examples
///
/// ```rust,no_run
/// use alien_client_config::{ClientConfigExt};
/// use alien_core::{ClientConfig, Platform};
/// use std::collections::HashMap;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let env_vars: HashMap<String, String> = std::env::vars().collect();
/// let aws_config = ClientConfig::from_env(Platform::Aws, &env_vars).await?;
/// let gcp_config = ClientConfig::from_env(Platform::Gcp, &env_vars).await?;
/// let azure_config = ClientConfig::from_env(Platform::Azure, &env_vars).await?;
/// # Ok(())
/// # }
/// ```
async fn from_env(
platform: Platform,
environment_variables: &HashMap<String, String>,
) -> Result<Self>
where
Self: Sized;
/// Create a platform configuration from standard environment variables based on the specified platform.
///
/// # Examples
///
/// ```rust,no_run
/// use alien_client_config::{ClientConfigExt};
/// use alien_core::{ClientConfig, Platform};
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let aws_config = ClientConfig::from_std_env(Platform::Aws).await?;
/// let gcp_config = ClientConfig::from_std_env(Platform::Gcp).await?;
/// let azure_config = ClientConfig::from_std_env(Platform::Azure).await?;
/// # Ok(())
/// # }
/// ```
async fn from_std_env(platform: Platform) -> Result<Self>
where
Self: Sized;
/// Returns the platform enum for this configuration.
fn platform(&self) -> Platform;
/// Cloud-agnostic impersonation method
///
/// # Examples
///
/// ```rust,no_run
/// use alien_client_config::{ClientConfigExt};
/// use alien_core::{ClientConfig, ImpersonationConfig, AwsImpersonationConfig};
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let aws_config = ClientConfig::Test; // placeholder
/// let impersonation = ImpersonationConfig::Aws(AwsImpersonationConfig {
/// role_arn: "arn:aws:iam::123456789012:role/MyRole".to_string(),
/// session_name: None,
/// duration_seconds: Some(3600),
/// external_id: None,
/// target_region: None,
/// });
///
/// let impersonated_config = aws_config.impersonate(impersonation).await?;
/// # Ok(())
/// # }
/// ```
async fn impersonate(&self, config: ImpersonationConfig) -> Result<ClientConfig>;
}
#[async_trait]
impl ClientConfigExt for ClientConfig {
async fn from_env(
platform: Platform,
environment_variables: &HashMap<String, String>,
) -> Result<Self> {
match platform {
#[cfg(feature = "aws")]
Platform::Aws => {
use alien_aws_clients::AwsClientConfigExt;
let config = alien_aws_clients::AwsClientConfig::from_env(environment_variables)
.await
.context(ErrorData::InvalidClientConfig {
message:
"Failed to create AWS client configuration from environment variables"
.to_string(),
errors: None,
})?;
Ok(ClientConfig::Aws(Box::new(config)))
}
#[cfg(not(feature = "aws"))]
Platform::Aws => Err(AlienError::new(ErrorData::InvalidClientConfig {
message: "AWS support is not enabled in this build".to_string(),
errors: None,
})),
#[cfg(feature = "gcp")]
Platform::Gcp => {
use alien_gcp_clients::GcpClientConfigExt;
let config = alien_gcp_clients::GcpClientConfig::from_env(environment_variables)
.await
.context(ErrorData::InvalidClientConfig {
message:
"Failed to create GCP client configuration from environment variables"
.to_string(),
errors: None,
})?;
Ok(ClientConfig::Gcp(Box::new(config)))
}
#[cfg(not(feature = "gcp"))]
Platform::Gcp => Err(AlienError::new(ErrorData::InvalidClientConfig {
message: "GCP support is not enabled in this build".to_string(),
errors: None,
})),
#[cfg(feature = "azure")]
Platform::Azure => {
use alien_azure_clients::AzureClientConfigExt;
let config = alien_azure_clients::AzureClientConfig::from_env(
environment_variables,
)
.await
.context(ErrorData::InvalidClientConfig {
message:
"Failed to create Azure client configuration from environment variables"
.to_string(),
errors: None,
})?;
Ok(ClientConfig::Azure(Box::new(config)))
}
#[cfg(not(feature = "azure"))]
Platform::Azure => Err(AlienError::new(ErrorData::InvalidClientConfig {
message: "Azure support is not enabled in this build".to_string(),
errors: None,
})),
#[cfg(feature = "kubernetes")]
Platform::Kubernetes => {
use alien_k8s_clients::KubernetesClientConfigExt;
let config = alien_k8s_clients::KubernetesClientConfig::from_env(environment_variables)
.await
.context(ErrorData::InvalidClientConfig {
message: "Failed to create Kubernetes client configuration from environment variables".to_string(),
errors: None,
})?;
Ok(ClientConfig::Kubernetes(Box::new(config)))
}
#[cfg(not(feature = "kubernetes"))]
Platform::Kubernetes => Err(AlienError::new(ErrorData::InvalidClientConfig {
message: "Kubernetes support is not enabled in this build".to_string(),
errors: None,
})),
Platform::Test => Ok(ClientConfig::Test),
Platform::Local => {
// Local platform reads state directory from ALIEN_LOCAL_STATE_DIRECTORY
let state_directory = environment_variables
.get("ALIEN_LOCAL_STATE_DIRECTORY")
.cloned()
.unwrap_or_else(|| "/tmp/alien-local".to_string());
Ok(ClientConfig::Local { state_directory })
}
}
}
async fn from_std_env(platform: Platform) -> Result<Self> {
let env_vars: HashMap<String, String> = std::env::vars().collect();
Self::from_env(platform, &env_vars).await
}
fn platform(&self) -> Platform {
match self {
#[cfg(feature = "aws")]
ClientConfig::Aws(_) => Platform::Aws,
#[cfg(feature = "gcp")]
ClientConfig::Gcp(_) => Platform::Gcp,
#[cfg(feature = "azure")]
ClientConfig::Azure(_) => Platform::Azure,
#[cfg(feature = "kubernetes")]
ClientConfig::Kubernetes(_) => Platform::Kubernetes,
#[cfg(feature = "kubernetes")]
ClientConfig::KubernetesCloud { .. } => Platform::Kubernetes,
ClientConfig::Test => Platform::Test,
ClientConfig::Local { .. } => Platform::Local,
// This should never be reached when no features are enabled,
// as the enum would be uninhabitable
#[allow(unreachable_patterns)]
_ => unreachable!("ClientConfig requires at least one platform feature to be enabled"),
}
}
async fn impersonate(&self, config: ImpersonationConfig) -> Result<ClientConfig> {
match (self, config) {
#[cfg(feature = "aws")]
(ClientConfig::Aws(aws_config), ImpersonationConfig::Aws(imp_config)) => {
use alien_aws_clients::AwsClientConfigExt;
let new_config = aws_config.impersonate(imp_config).await.map_err(|e| {
AlienError::new(ErrorData::AuthenticationError {
message: format!("AWS role impersonation failed: {}", e),
})
})?;
Ok(ClientConfig::Aws(Box::new(new_config)))
}
#[cfg(feature = "gcp")]
(ClientConfig::Gcp(gcp_config), ImpersonationConfig::Gcp(imp_config)) => {
use alien_gcp_clients::GcpClientConfigExt;
let new_config = gcp_config.impersonate(imp_config).await.map_err(|e| {
AlienError::new(ErrorData::AuthenticationError {
message: format!("GCP service account impersonation failed: {}", e),
})
})?;
Ok(ClientConfig::Gcp(Box::new(new_config)))
}
#[cfg(feature = "azure")]
(ClientConfig::Azure(azure_config), ImpersonationConfig::Azure(imp_config)) => {
use alien_azure_clients::AzureClientConfigExt;
let new_config = azure_config.impersonate(imp_config).await.map_err(|e| {
AlienError::new(ErrorData::AuthenticationError {
message: format!("Azure managed identity impersonation failed: {}", e),
})
})?;
Ok(ClientConfig::Azure(Box::new(new_config)))
}
// Kubernetes doesn't support impersonation
#[cfg(feature = "kubernetes")]
(ClientConfig::Kubernetes(_), _) => Err(AlienError::new(ErrorData::InvalidInput {
message: "Kubernetes platform does not support impersonation".to_string(),
field_name: Some("impersonation_config".to_string()),
})),
_ => Err(AlienError::new(ErrorData::InvalidInput {
message: "Platform config and impersonation config types must match".to_string(),
field_name: Some("impersonation_config".to_string()),
})),
}
}
}