rise-deploy 0.16.1

A simple and powerful CLI for deploying containerized applications
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
use axum::{
    extract::{Path, State},
    http::StatusCode,
    Extension, Json,
};
use uuid::Uuid;

use crate::db::{projects, service_accounts, users, User};
use crate::server::project::handlers::{check_read_permission, check_write_permission};
use crate::server::state::AppState;
use crate::server::workload_identity::models::{
    CreateWorkloadIdentityRequest, ListWorkloadIdentitiesResponse, UpdateWorkloadIdentityRequest,
    WorkloadIdentityResponse,
};

type Result<T> = std::result::Result<T, (StatusCode, String)>;

/// Verify that an OIDC issuer is reachable and has valid configuration
async fn verify_oidc_issuer(issuer_url: &str) -> Result<()> {
    // Construct the OIDC discovery URL
    let discovery_url = if issuer_url.ends_with('/') {
        format!("{}well-known/openid-configuration", issuer_url)
    } else {
        format!("{}/.well-known/openid-configuration", issuer_url)
    };

    tracing::debug!("Verifying OIDC issuer at: {}", discovery_url);

    // Attempt to fetch the OIDC configuration
    let response = reqwest::get(&discovery_url)
        .await
        .map_err(|e| {
            tracing::warn!("Failed to reach OIDC issuer {}: {:#}", issuer_url, e);
            (
                StatusCode::BAD_REQUEST,
                format!("Failed to reach OIDC issuer: {}. Please verify the issuer URL is correct and accessible.", e),
            )
        })?;

    if !response.status().is_success() {
        tracing::warn!(
            "OIDC issuer {} returned non-success status: {}",
            issuer_url,
            response.status()
        );
        return Err((
            StatusCode::BAD_REQUEST,
            format!(
                "OIDC issuer returned status {}: {}. Please verify the issuer URL points to a valid OIDC provider.",
                response.status(),
                response.status().canonical_reason().unwrap_or("Unknown")
            ),
        ));
    }

    // Try to parse the response as JSON to verify it's valid OIDC configuration
    let config: serde_json::Value = response.json().await.map_err(|e| {
        tracing::warn!("Failed to parse OIDC configuration from {}: {:#}", issuer_url, e);
        (
            StatusCode::BAD_REQUEST,
            format!("Invalid OIDC configuration: {}. The issuer URL does not return valid OIDC discovery metadata.", e),
        )
    })?;

    // Verify required OIDC fields are present
    if config.get("issuer").is_none() {
        return Err((
            StatusCode::BAD_REQUEST,
            "OIDC configuration missing required 'issuer' field".to_string(),
        ));
    }

    if config.get("jwks_uri").is_none() {
        return Err((
            StatusCode::BAD_REQUEST,
            "OIDC configuration missing required 'jwks_uri' field".to_string(),
        ));
    }

    tracing::info!("Successfully verified OIDC issuer: {}", issuer_url);
    Ok(())
}

/// Create a new service account for a project
pub async fn create_workload_identity(
    State(state): State<AppState>,
    Extension(user): Extension<User>,
    Path(project_name): Path<String>,
    Json(req): Json<CreateWorkloadIdentityRequest>,
) -> Result<Json<WorkloadIdentityResponse>> {
    // Get project
    let project = projects::find_by_name(&state.db_pool, &project_name)
        .await
        .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
        .ok_or_else(|| (StatusCode::NOT_FOUND, "Project not found".to_string()))?;

    // Check permission: user must be able to write to project
    if !check_write_permission(&state, &project, &user)
        .await
        .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
    {
        return Err((
            StatusCode::FORBIDDEN,
            "Cannot manage service accounts for this project".to_string(),
        ));
    }

    // Validate issuer URL
    if req.issuer_url.is_empty() {
        return Err((
            StatusCode::BAD_REQUEST,
            "Issuer URL cannot be empty".to_string(),
        ));
    }

    // In production, should validate HTTPS
    // For now, just check it's a valid URL format
    if !req.issuer_url.starts_with("http://") && !req.issuer_url.starts_with("https://") {
        return Err((
            StatusCode::BAD_REQUEST,
            "Issuer URL must be a valid HTTP(S) URL".to_string(),
        ));
    }

    // Validate claims requirements
    if req.claims.is_empty() {
        return Err((
            StatusCode::BAD_REQUEST,
            "At least one claim is required".to_string(),
        ));
    }

    // Require 'aud' claim
    if !req.claims.contains_key("aud") {
        return Err((
            StatusCode::BAD_REQUEST,
            "The 'aud' (audience) claim is required for service accounts".to_string(),
        ));
    }

    // Require at least one additional claim besides 'aud'
    if req.claims.len() < 2 {
        return Err((
            StatusCode::BAD_REQUEST,
            "At least one claim in addition to 'aud' is required (e.g., project_path, ref_protected)".to_string(),
        ));
    }

    // Verify OIDC issuer is reachable and has valid configuration
    verify_oidc_issuer(&req.issuer_url).await?;

    // Create service account
    let sa = service_accounts::create(&state.db_pool, project.id, &req.issuer_url, &req.claims)
        .await
        .map_err(|e| {
            tracing::error!("Failed to create service account: {:#}", e);
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                format!("Failed to create service account: {}", e),
            )
        })?;

    // Get user for response
    let sa_user = users::find_by_id(&state.db_pool, sa.user_id)
        .await
        .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
        .ok_or_else(|| {
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                "Service account user not found".to_string(),
            )
        })?;

    // Convert JSONB claims to HashMap for response
    let claims: std::collections::HashMap<String, String> = serde_json::from_value(sa.claims)
        .map_err(|e| {
            tracing::error!("Failed to deserialize claims: {:#}", e);
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                "Failed to deserialize claims".to_string(),
            )
        })?;

    Ok(Json(WorkloadIdentityResponse {
        id: sa.id.to_string(),
        email: sa_user.email,
        project_name: project.name,
        issuer_url: sa.issuer_url,
        claims,
        created_at: sa.created_at.to_rfc3339(),
    }))
}

/// List all service accounts for a project
pub async fn list_workload_identities(
    State(state): State<AppState>,
    Extension(user): Extension<User>,
    Path(project_name): Path<String>,
) -> Result<Json<ListWorkloadIdentitiesResponse>> {
    // Get project
    let project = projects::find_by_name(&state.db_pool, &project_name)
        .await
        .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
        .ok_or_else(|| (StatusCode::NOT_FOUND, "Project not found".to_string()))?;

    // Check read permission
    if !check_read_permission(&state, &project, &user)
        .await
        .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
    {
        return Err((StatusCode::NOT_FOUND, "Project not found".to_string()));
    }

    // Get active service accounts
    let sas = service_accounts::list_by_project(&state.db_pool, project.id)
        .await
        .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;

    // Convert to response
    let mut workload_identities = Vec::new();
    for sa in sas {
        let sa_user = users::find_by_id(&state.db_pool, sa.user_id)
            .await
            .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
            .ok_or_else(|| {
                (
                    StatusCode::INTERNAL_SERVER_ERROR,
                    "Service account user not found".to_string(),
                )
            })?;

        // Convert JSONB claims to HashMap
        let claims: std::collections::HashMap<String, String> =
            serde_json::from_value(sa.claims.clone()).map_err(|e| {
                tracing::error!("Failed to deserialize claims: {:#}", e);
                (
                    StatusCode::INTERNAL_SERVER_ERROR,
                    "Failed to deserialize claims".to_string(),
                )
            })?;

        workload_identities.push(WorkloadIdentityResponse {
            id: sa.id.to_string(),
            email: sa_user.email,
            project_name: project.name.clone(),
            issuer_url: sa.issuer_url,
            claims,
            created_at: sa.created_at.to_rfc3339(),
        });
    }

    Ok(Json(ListWorkloadIdentitiesResponse {
        workload_identities,
    }))
}

/// Get a specific service account
pub async fn get_workload_identity(
    State(state): State<AppState>,
    Extension(user): Extension<User>,
    Path((project_name, sa_id)): Path<(String, Uuid)>,
) -> Result<Json<WorkloadIdentityResponse>> {
    // Get project
    let project = projects::find_by_name(&state.db_pool, &project_name)
        .await
        .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
        .ok_or_else(|| (StatusCode::NOT_FOUND, "Project not found".to_string()))?;

    // Check read permission
    if !check_read_permission(&state, &project, &user)
        .await
        .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
    {
        return Err((StatusCode::NOT_FOUND, "Project not found".to_string()));
    }

    // Get service account
    let sa = service_accounts::get_by_id(&state.db_pool, sa_id)
        .await
        .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
        .ok_or_else(|| {
            (
                StatusCode::NOT_FOUND,
                "Service account not found".to_string(),
            )
        })?;

    // Verify SA belongs to this project
    if sa.project_id != project.id {
        return Err((
            StatusCode::NOT_FOUND,
            "Service account not found".to_string(),
        ));
    }

    // Check if deleted
    if sa.deleted_at.is_some() {
        return Err((
            StatusCode::NOT_FOUND,
            "Service account not found".to_string(),
        ));
    }

    // Get user
    let sa_user = users::find_by_id(&state.db_pool, sa.user_id)
        .await
        .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
        .ok_or_else(|| {
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                "Service account user not found".to_string(),
            )
        })?;

    // Convert JSONB claims to HashMap
    let claims: std::collections::HashMap<String, String> = serde_json::from_value(sa.claims)
        .map_err(|e| {
            tracing::error!("Failed to deserialize claims: {:#}", e);
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                "Failed to deserialize claims".to_string(),
            )
        })?;

    Ok(Json(WorkloadIdentityResponse {
        id: sa.id.to_string(),
        email: sa_user.email,
        project_name: project.name,
        issuer_url: sa.issuer_url,
        claims,
        created_at: sa.created_at.to_rfc3339(),
    }))
}

/// Update a service account's issuer_url and/or claims
pub async fn update_workload_identity(
    State(state): State<AppState>,
    Extension(user): Extension<User>,
    Path((project_name, sa_id)): Path<(String, Uuid)>,
    Json(req): Json<UpdateWorkloadIdentityRequest>,
) -> Result<Json<WorkloadIdentityResponse>> {
    // Get project
    let project = projects::find_by_name(&state.db_pool, &project_name)
        .await
        .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
        .ok_or_else(|| (StatusCode::NOT_FOUND, "Project not found".to_string()))?;

    // Check write permission
    if !check_write_permission(&state, &project, &user)
        .await
        .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
    {
        return Err((
            StatusCode::FORBIDDEN,
            "Cannot manage service accounts for this project".to_string(),
        ));
    }

    // Get service account
    let sa = service_accounts::get_by_id(&state.db_pool, sa_id)
        .await
        .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
        .ok_or_else(|| {
            (
                StatusCode::NOT_FOUND,
                "Service account not found".to_string(),
            )
        })?;

    // Verify SA belongs to this project
    if sa.project_id != project.id {
        return Err((
            StatusCode::NOT_FOUND,
            "Service account not found".to_string(),
        ));
    }

    // Check if deleted
    if sa.deleted_at.is_some() {
        return Err((
            StatusCode::NOT_FOUND,
            "Service account not found".to_string(),
        ));
    }

    // Validate that at least one field is provided
    if req.issuer_url.is_none() && req.claims.is_none() {
        return Err((
            StatusCode::BAD_REQUEST,
            "At least one field (issuer_url or claims) must be provided for update".to_string(),
        ));
    }

    // Validate issuer URL if provided
    if let Some(ref issuer_url) = req.issuer_url {
        if issuer_url.is_empty() {
            return Err((
                StatusCode::BAD_REQUEST,
                "Issuer URL cannot be empty".to_string(),
            ));
        }

        if !issuer_url.starts_with("http://") && !issuer_url.starts_with("https://") {
            return Err((
                StatusCode::BAD_REQUEST,
                "Issuer URL must be a valid HTTP(S) URL".to_string(),
            ));
        }
    }

    // Validate claims if provided
    if let Some(ref claims) = req.claims {
        if claims.is_empty() {
            return Err((
                StatusCode::BAD_REQUEST,
                "Claims cannot be empty".to_string(),
            ));
        }

        // Require 'aud' claim
        if !claims.contains_key("aud") {
            return Err((
                StatusCode::BAD_REQUEST,
                "The 'aud' (audience) claim is required for service accounts".to_string(),
            ));
        }

        // Require at least one additional claim besides 'aud'
        if claims.len() < 2 {
            return Err((
                StatusCode::BAD_REQUEST,
                "At least one claim in addition to 'aud' is required (e.g., project_path, ref_protected)".to_string(),
            ));
        }
    }

    // Update service account
    let updated_sa = service_accounts::update(
        &state.db_pool,
        sa_id,
        req.issuer_url.as_deref(),
        req.claims.as_ref(),
    )
    .await
    .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;

    // Get user for response
    let sa_user = users::find_by_id(&state.db_pool, updated_sa.user_id)
        .await
        .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
        .ok_or_else(|| {
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                "Service account user not found".to_string(),
            )
        })?;

    // Convert JSONB claims to HashMap for response
    let claims: std::collections::HashMap<String, String> =
        serde_json::from_value(updated_sa.claims).map_err(|e| {
            tracing::error!("Failed to deserialize claims: {:#}", e);
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                "Failed to deserialize claims".to_string(),
            )
        })?;

    Ok(Json(WorkloadIdentityResponse {
        id: updated_sa.id.to_string(),
        email: sa_user.email,
        project_name: project.name,
        issuer_url: updated_sa.issuer_url,
        claims,
        created_at: updated_sa.created_at.to_rfc3339(),
    }))
}

/// Delete a service account (soft delete)
pub async fn delete_workload_identity(
    State(state): State<AppState>,
    Extension(user): Extension<User>,
    Path((project_name, sa_id)): Path<(String, Uuid)>,
) -> Result<StatusCode> {
    // Get project
    let project = projects::find_by_name(&state.db_pool, &project_name)
        .await
        .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
        .ok_or_else(|| (StatusCode::NOT_FOUND, "Project not found".to_string()))?;

    // Check write permission
    if !check_write_permission(&state, &project, &user)
        .await
        .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
    {
        return Err((
            StatusCode::FORBIDDEN,
            "Cannot manage service accounts for this project".to_string(),
        ));
    }

    // Get service account
    let sa = service_accounts::get_by_id(&state.db_pool, sa_id)
        .await
        .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
        .ok_or_else(|| {
            (
                StatusCode::NOT_FOUND,
                "Service account not found".to_string(),
            )
        })?;

    // Verify SA belongs to this project
    if sa.project_id != project.id {
        return Err((
            StatusCode::NOT_FOUND,
            "Service account not found".to_string(),
        ));
    }

    // Check if already deleted
    if sa.deleted_at.is_some() {
        return Err((
            StatusCode::NOT_FOUND,
            "Service account not found".to_string(),
        ));
    }

    // Soft delete
    service_accounts::soft_delete(&state.db_pool, sa_id)
        .await
        .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;

    Ok(StatusCode::NO_CONTENT)
}