alien-core 1.4.0

Deploy software into your customers' cloud accounts and keep it fully managed
Documentation
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! Defines core permission types and structures used across Alien Infra.

use indexmap::IndexMap;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
pub enum AwsPermissionEffect {
    #[default]
    Allow,
    Deny,
}

impl AwsPermissionEffect {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Allow => "Allow",
            Self::Deny => "Deny",
        }
    }

    pub fn is_allow(&self) -> bool {
        matches!(self, Self::Allow)
    }
}

/// Grant permissions for a specific cloud platform
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct PermissionGrant {
    /// AWS IAM actions (only for AWS)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub actions: Option<Vec<String>>,
    /// GCP permissions that require an exact residual custom role.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub permissions: Option<Vec<String>>,
    /// Provider predefined roles to bind directly.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub predefined_roles: Option<Vec<String>>,
    /// GCP residual custom permissions to pair with predefined roles.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub residual_permissions: Option<Vec<String>>,
    /// Azure actions (only for Azure)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data_actions: Option<Vec<String>>,
}

/// AWS-specific binding specification
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct AwsBindingSpec {
    /// Resource ARNs to bind to
    pub resources: Vec<String>,
    /// Optional condition for additional filtering (rare)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub condition: Option<IndexMap<String, IndexMap<String, String>>>,
}

/// GCP-specific binding specification
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct GcpBindingSpec {
    /// Scope (project/resource level)
    pub scope: String,
    /// Optional condition for filtering resources
    #[serde(skip_serializing_if = "Option::is_none")]
    pub condition: Option<GcpCondition>,
}

/// Azure-specific binding specification
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct AzureBindingSpec {
    /// Scope (subscription/resource group/resource level)
    pub scope: String,
}

/// Generic binding configuration for permissions
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct BindingConfiguration<T> {
    /// Stack-level binding
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stack: Option<T>,
    /// Resource-level binding
    #[serde(skip_serializing_if = "Option::is_none")]
    pub resource: Option<T>,
}

impl<T> BindingConfiguration<T> {
    /// Check if the binding configuration is empty (no stack or resource bindings)
    pub fn is_empty(&self) -> bool {
        self.stack.is_none() && self.resource.is_none()
    }
}

/// GCP IAM condition
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct GcpCondition {
    pub title: String,
    pub expression: String,
}

/// AWS-specific platform permission configuration
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct AwsPlatformPermission {
    /// Stable admin-facing label for this permission entry.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub label: Option<String>,
    /// Short admin-facing description of why this entry exists.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// IAM effect. Defaults to Allow.
    #[serde(default, skip_serializing_if = "AwsPermissionEffect::is_allow")]
    pub effect: AwsPermissionEffect,
    /// What permissions to grant
    pub grant: PermissionGrant,
    /// How to bind the permissions (stack vs resource scope)
    pub binding: BindingConfiguration<AwsBindingSpec>,
}

/// GCP-specific platform permission configuration
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct GcpPlatformPermission {
    /// Stable admin-facing label for this permission entry.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub label: Option<String>,
    /// Short admin-facing description of why this entry exists.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// What permissions to grant
    pub grant: PermissionGrant,
    /// How to bind the permissions (stack vs resource scope)
    pub binding: BindingConfiguration<GcpBindingSpec>,
}

/// Azure-specific platform permission configuration
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct AzurePlatformPermission {
    /// Stable admin-facing label for this permission entry.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub label: Option<String>,
    /// Short admin-facing description of why this entry exists.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// What permissions to grant
    pub grant: PermissionGrant,
    /// How to bind the permissions (stack vs resource scope)
    pub binding: BindingConfiguration<AzureBindingSpec>,
}

/// Platform-specific permission configurations
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct PlatformPermissions {
    /// AWS permission configurations
    #[serde(skip_serializing_if = "Option::is_none")]
    pub aws: Option<Vec<AwsPlatformPermission>>,
    /// GCP permission configurations
    #[serde(skip_serializing_if = "Option::is_none")]
    pub gcp: Option<Vec<GcpPlatformPermission>>,
    /// Azure permission configurations
    #[serde(skip_serializing_if = "Option::is_none")]
    pub azure: Option<Vec<AzurePlatformPermission>>,
}

/// A permission set that can be applied across different cloud platforms
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct PermissionSet {
    /// Unique identifier for the permission set (e.g., "storage/data-read")
    pub id: String,
    /// Human-readable description of what this permission set allows
    pub description: String,
    /// Platform-specific permission configurations
    pub platforms: PlatformPermissions,
}

/// Reference to a permission set - either by name or inline definition
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(untagged)]
pub enum PermissionSetReference {
    /// Reference to a built-in permission set by name (e.g., "storage/data-read")
    Name(String),
    /// Inline permission set definition
    Inline(PermissionSet),
}

impl PermissionSetReference {
    /// Get the ID of the permission set, whether it's a reference or inline
    pub fn id(&self) -> &str {
        match self {
            PermissionSetReference::Name(name) => name,
            PermissionSetReference::Inline(permission_set) => &permission_set.id,
        }
    }

    /// Create a permission set reference from a name
    pub fn from_name(name: impl Into<String>) -> Self {
        PermissionSetReference::Name(name.into())
    }

    /// Create a permission set reference from an inline permission set
    pub fn from_inline(permission_set: PermissionSet) -> Self {
        PermissionSetReference::Inline(permission_set)
    }

    /// Resolve this reference to a concrete PermissionSet
    /// Takes a resolver function for built-in permission sets
    pub fn resolve(
        &self,
        resolver: impl Fn(&str) -> Option<PermissionSet>,
    ) -> Option<PermissionSet> {
        match self {
            PermissionSetReference::Name(name) => resolver(name),
            PermissionSetReference::Inline(permission_set) => Some(permission_set.clone()),
        }
    }
}

/// Permission profile that maps resources to permission sets
/// Key can be "*" for all resources or resource name for specific resource
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(transparent)]
pub struct PermissionProfile(pub IndexMap<String, Vec<PermissionSetReference>>);

impl PermissionProfile {
    /// Create a new permission profile
    pub fn new() -> Self {
        Self(IndexMap::new())
    }

    /// Add global permissions (applies to all resources)
    pub fn global<I>(mut self, permission_sets: I) -> Self
    where
        I: IntoIterator,
        I::Item: Into<PermissionSetReference>,
    {
        let permission_list: Vec<PermissionSetReference> =
            permission_sets.into_iter().map(|s| s.into()).collect();
        self.0.insert("*".to_string(), permission_list);
        self
    }

    /// Add resource-scoped permissions
    pub fn resource<I>(mut self, resource_name: impl Into<String>, permission_sets: I) -> Self
    where
        I: IntoIterator,
        I::Item: Into<PermissionSetReference>,
    {
        let permission_list: Vec<PermissionSetReference> =
            permission_sets.into_iter().map(|s| s.into()).collect();
        self.0.insert(resource_name.into(), permission_list);
        self
    }
}

impl Default for PermissionProfile {
    fn default() -> Self {
        Self::new()
    }
}

impl From<String> for PermissionSetReference {
    fn from(name: String) -> Self {
        PermissionSetReference::Name(name)
    }
}

impl From<&str> for PermissionSetReference {
    fn from(name: &str) -> Self {
        PermissionSetReference::Name(name.to_string())
    }
}

impl From<PermissionSet> for PermissionSetReference {
    fn from(permission_set: PermissionSet) -> Self {
        PermissionSetReference::Inline(permission_set)
    }
}

/// Management permissions configuration for stack management access
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub enum ManagementPermissions {
    /// Auto-derived permissions only (default)
    /// Uses resource lifecycles to determine management permissions:
    /// - Frozen resources: `<type>/management`
    /// - Live resources: `<type>/provision`
    Auto,

    /// Add permissions to auto-derived baseline
    Extend(PermissionProfile),

    /// Replace auto-derived permissions entirely
    Override(PermissionProfile),
}

impl Default for ManagementPermissions {
    fn default() -> Self {
        ManagementPermissions::Auto
    }
}

/// Combined permissions configuration that contains both profiles and management
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct PermissionsConfig {
    /// Permission profiles that define access control for compute services
    /// Key is the profile name, value is the permission configuration
    pub profiles: IndexMap<String, PermissionProfile>,
    /// Management permissions configuration for stack management access
    #[serde(default)]
    pub management: ManagementPermissions,
}

impl PermissionsConfig {
    /// Create a new permissions config with auto management
    pub fn new() -> Self {
        Self {
            profiles: IndexMap::new(),
            management: ManagementPermissions::Auto,
        }
    }

    /// Add a permission profile
    pub fn with_profile(mut self, name: impl Into<String>, profile: PermissionProfile) -> Self {
        self.profiles.insert(name.into(), profile);
        self
    }

    /// Set management permissions
    pub fn with_management(mut self, management: ManagementPermissions) -> Self {
        self.management = management;
        self
    }
}

impl Default for PermissionsConfig {
    fn default() -> Self {
        Self::new()
    }
}

impl ManagementPermissions {
    /// Create auto-derived management permissions
    pub fn auto() -> Self {
        ManagementPermissions::Auto
    }

    /// Create management permissions that extend auto-derived baseline
    pub fn extend(profile: PermissionProfile) -> Self {
        ManagementPermissions::Extend(profile)
    }

    /// Create management permissions that override auto-derived permissions
    pub fn override_(profile: PermissionProfile) -> Self {
        ManagementPermissions::Override(profile)
    }

    /// Get the permission profile if present (for Extend/Override variants)
    pub fn profile(&self) -> Option<&PermissionProfile> {
        match self {
            ManagementPermissions::Auto => None,
            ManagementPermissions::Extend(profile) => Some(profile),
            ManagementPermissions::Override(profile) => Some(profile),
        }
    }

    /// Check if this is the auto variant
    pub fn is_auto(&self) -> bool {
        matches!(self, ManagementPermissions::Auto)
    }

    /// Check if this extends auto-derived permissions
    pub fn is_extend(&self) -> bool {
        matches!(self, ManagementPermissions::Extend(_))
    }

    /// Check if this overrides auto-derived permissions
    pub fn is_override(&self) -> bool {
        matches!(self, ManagementPermissions::Override(_))
    }
}