rise-deploy 0.16.4

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
use anyhow::{Context, Result};
use sqlx::PgPool;
use std::collections::HashMap;
use uuid::Uuid;

use crate::db::models::{Project, ServiceAccount, User};

/// Create a new service account for a project
pub async fn create(
    pool: &PgPool,
    project_id: Uuid,
    issuer_url: &str,
    claims: &HashMap<String, String>,
) -> Result<ServiceAccount> {
    let mut tx = pool.begin().await.context("Failed to begin transaction")?;

    // Get project for email generation
    let project = sqlx::query_as!(
        Project,
        r#"
        SELECT id, name, status as "status: _", access_class,
               owner_user_id, owner_team_id, finalizers,
               created_at, updated_at
        FROM projects
        WHERE id = $1
        "#,
        project_id
    )
    .fetch_one(&mut *tx)
    .await
    .context("Failed to fetch project")?;

    // Calculate next sequence number
    let sequence: Option<i32> = sqlx::query_scalar!(
        r#"
        SELECT COALESCE(MAX(sequence), 0) + 1 as "sequence"
        FROM service_accounts
        WHERE project_id = $1
        "#,
        project_id
    )
    .fetch_one(&mut *tx)
    .await
    .context("Failed to calculate sequence")?;

    let sequence = sequence.unwrap_or(1);

    // Generate service account email
    let email = format!("{}+{}@sa.rise.local", project.name, sequence);

    // Check if user already exists with this email (from previously deleted SA)
    let existing_user = sqlx::query_as!(
        User,
        r#"
        SELECT id, email, created_at, updated_at
        FROM users
        WHERE email = $1
        "#,
        email
    )
    .fetch_optional(&mut *tx)
    .await
    .context("Failed to check for existing user")?;

    // Create or reuse user
    let user = if let Some(user) = existing_user {
        tracing::debug!("Reusing existing user for service account: {}", email);
        user
    } else {
        sqlx::query_as!(
            User,
            r#"
            INSERT INTO users (email)
            VALUES ($1)
            RETURNING id, email, created_at, updated_at
            "#,
            email
        )
        .fetch_one(&mut *tx)
        .await
        .context("Failed to create user for service account")?
    };

    // Convert claims HashMap to JSONB
    let claims_json = serde_json::to_value(claims).context("Failed to serialize claims")?;

    // Create service account
    let sa = sqlx::query_as!(
        ServiceAccount,
        r#"
        INSERT INTO service_accounts (project_id, user_id, issuer_url, claims, sequence)
        VALUES ($1, $2, $3, $4, $5)
        RETURNING id, project_id, user_id, issuer_url, claims, sequence,
                  deleted_at, created_at, updated_at
        "#,
        project_id,
        user.id,
        issuer_url,
        claims_json,
        sequence
    )
    .fetch_one(&mut *tx)
    .await
    .context("Failed to create service account")?;

    tx.commit().await.context("Failed to commit transaction")?;

    Ok(sa)
}

/// List all active service accounts for a project
pub async fn list_by_project(pool: &PgPool, project_id: Uuid) -> Result<Vec<ServiceAccount>> {
    let sas = sqlx::query_as!(
        ServiceAccount,
        r#"
        SELECT id, project_id, user_id, issuer_url, claims, sequence,
               deleted_at, created_at, updated_at
        FROM service_accounts
        WHERE project_id = $1 AND deleted_at IS NULL
        ORDER BY sequence ASC
        "#,
        project_id
    )
    .fetch_all(pool)
    .await
    .context("Failed to list service accounts")?;

    Ok(sas)
}

/// Get a service account by ID
pub async fn get_by_id(pool: &PgPool, id: Uuid) -> Result<Option<ServiceAccount>> {
    let sa = sqlx::query_as!(
        ServiceAccount,
        r#"
        SELECT id, project_id, user_id, issuer_url, claims, sequence,
               deleted_at, created_at, updated_at
        FROM service_accounts
        WHERE id = $1
        "#,
        id
    )
    .fetch_optional(pool)
    .await
    .context("Failed to get service account by ID")?;

    Ok(sa)
}

/// Get claims for a service account
#[allow(dead_code)]
pub async fn get_claims(
    pool: &PgPool,
    service_account_id: Uuid,
) -> Result<HashMap<String, String>> {
    let sa = sqlx::query_as!(
        ServiceAccount,
        r#"
        SELECT id, project_id, user_id, issuer_url, claims, sequence,
               deleted_at, created_at, updated_at
        FROM service_accounts
        WHERE id = $1
        "#,
        service_account_id
    )
    .fetch_one(pool)
    .await
    .context("Failed to fetch service account")?;

    // Convert JSONB to HashMap<String, String>
    let claims: HashMap<String, String> =
        serde_json::from_value(sa.claims).context("Failed to deserialize claims")?;

    Ok(claims)
}

/// Find all active service accounts with a specific issuer URL (for authentication)
pub async fn find_by_issuer(pool: &PgPool, issuer_url: &str) -> Result<Vec<ServiceAccount>> {
    let sas = sqlx::query_as!(
        ServiceAccount,
        r#"
        SELECT id, project_id, user_id, issuer_url, claims, sequence,
               deleted_at, created_at, updated_at
        FROM service_accounts
        WHERE issuer_url = $1 AND deleted_at IS NULL
        "#,
        issuer_url
    )
    .fetch_all(pool)
    .await
    .context("Failed to find service accounts by issuer")?;

    Ok(sas)
}

/// Find a service account by user ID and project ID (for authorization)
pub async fn find_by_user_and_project(
    pool: &PgPool,
    user_id: Uuid,
    project_id: Uuid,
) -> Result<Option<ServiceAccount>> {
    let sa = sqlx::query_as!(
        ServiceAccount,
        r#"
        SELECT id, project_id, user_id, issuer_url, claims, sequence,
               deleted_at, created_at, updated_at
        FROM service_accounts
        WHERE user_id = $1 AND project_id = $2 AND deleted_at IS NULL
        "#,
        user_id,
        project_id
    )
    .fetch_optional(pool)
    .await
    .context("Failed to find service account by user and project")?;

    Ok(sa)
}

/// Check if a user is a service account
pub async fn is_service_account(pool: &PgPool, user_id: Uuid) -> Result<bool> {
    let exists = sqlx::query_scalar!(
        r#"
        SELECT EXISTS(
            SELECT 1
            FROM service_accounts
            WHERE user_id = $1 AND deleted_at IS NULL
        ) as "exists!"
        "#,
        user_id
    )
    .fetch_one(pool)
    .await
    .context("Failed to check if user is a service account")?;

    Ok(exists)
}

/// Update a service account's issuer_url and/or claims
pub async fn update(
    pool: &PgPool,
    id: Uuid,
    issuer_url: Option<&str>,
    claims: Option<&HashMap<String, String>>,
) -> Result<ServiceAccount> {
    // Convert claims HashMap to JSONB if provided
    let claims_json = if let Some(c) = claims {
        Some(serde_json::to_value(c).context("Failed to serialize claims")?)
    } else {
        None
    };

    let sa = sqlx::query_as!(
        ServiceAccount,
        r#"
        UPDATE service_accounts
        SET
            issuer_url = COALESCE($2, issuer_url),
            claims = COALESCE($3, claims),
            updated_at = NOW()
        WHERE id = $1 AND deleted_at IS NULL
        RETURNING id, project_id, user_id, issuer_url, claims, sequence,
                  deleted_at, created_at, updated_at
        "#,
        id,
        issuer_url,
        claims_json
    )
    .fetch_one(pool)
    .await
    .context("Failed to update service account")?;

    Ok(sa)
}

/// Soft delete a service account
pub async fn soft_delete(pool: &PgPool, id: Uuid) -> Result<()> {
    sqlx::query!(
        r#"
        UPDATE service_accounts
        SET deleted_at = NOW(), updated_at = NOW()
        WHERE id = $1
        "#,
        id
    )
    .execute(pool)
    .await
    .context("Failed to soft delete service account")?;

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::db::{models::ProjectStatus, projects, users};

    #[sqlx::test]
    async fn test_create_service_account(pool: PgPool) -> Result<()> {
        // Create test user and project
        let user = users::create(&pool, "owner@example.com").await?;
        let project = projects::create(
            &pool,
            "test-project",
            ProjectStatus::Stopped,
            "public".to_string(),
            Some(user.id),
            None,
        )
        .await?;

        // Create service account
        let mut claims = HashMap::new();
        claims.insert("sub".to_string(), "test-subject".to_string());
        claims.insert("iss".to_string(), "https://gitlab.com".to_string());

        let sa = create(&pool, project.id, "https://gitlab.com", &claims).await?;

        assert_eq!(sa.project_id, project.id);
        assert_eq!(sa.issuer_url, "https://gitlab.com");
        assert_eq!(sa.sequence, 1);
        assert!(sa.deleted_at.is_none());

        // Verify user was created
        let sa_user = users::find_by_id(&pool, sa.user_id).await?;
        assert!(sa_user.is_some());
        assert_eq!(sa_user.unwrap().email, "test-project+1@sa.rise.local");

        Ok(())
    }

    #[sqlx::test]
    async fn test_sequence_increment(pool: PgPool) -> Result<()> {
        let user = users::create(&pool, "owner@example.com").await?;
        let project = projects::create(
            &pool,
            "test-project",
            ProjectStatus::Stopped,
            "public".to_string(),
            Some(user.id),
            None,
        )
        .await?;

        let claims = HashMap::new();

        // Create first service account
        let sa1 = create(&pool, project.id, "https://gitlab.com", &claims).await?;
        assert_eq!(sa1.sequence, 1);

        // Create second service account
        let sa2 = create(&pool, project.id, "https://github.com", &claims).await?;
        assert_eq!(sa2.sequence, 2);

        Ok(())
    }

    #[sqlx::test]
    async fn test_list_by_project(pool: PgPool) -> Result<()> {
        let user = users::create(&pool, "owner@example.com").await?;
        let project = projects::create(
            &pool,
            "test-project",
            ProjectStatus::Stopped,
            "public".to_string(),
            Some(user.id),
            None,
        )
        .await?;

        let claims = HashMap::new();

        // Create two service accounts
        create(&pool, project.id, "https://gitlab.com", &claims).await?;
        create(&pool, project.id, "https://github.com", &claims).await?;

        // List service accounts
        let sas = list_by_project(&pool, project.id).await?;
        assert_eq!(sas.len(), 2);

        Ok(())
    }

    #[sqlx::test]
    async fn test_find_by_issuer(pool: PgPool) -> Result<()> {
        let user = users::create(&pool, "owner@example.com").await?;
        let project1 = projects::create(
            &pool,
            "project1",
            ProjectStatus::Stopped,
            "public".to_string(),
            Some(user.id),
            None,
        )
        .await?;
        let project2 = projects::create(
            &pool,
            "project2",
            ProjectStatus::Stopped,
            "public".to_string(),
            Some(user.id),
            None,
        )
        .await?;

        let claims = HashMap::new();

        // Create service accounts with same issuer
        create(&pool, project1.id, "https://gitlab.com", &claims).await?;
        create(&pool, project2.id, "https://gitlab.com", &claims).await?;
        create(&pool, project1.id, "https://github.com", &claims).await?;

        // Find by issuer
        let sas = find_by_issuer(&pool, "https://gitlab.com").await?;
        assert_eq!(sas.len(), 2);

        Ok(())
    }

    #[sqlx::test]
    async fn test_soft_delete(pool: PgPool) -> Result<()> {
        let user = users::create(&pool, "owner@example.com").await?;
        let project = projects::create(
            &pool,
            "test-project",
            ProjectStatus::Stopped,
            "public".to_string(),
            Some(user.id),
            None,
        )
        .await?;

        let claims = HashMap::new();
        let sa = create(&pool, project.id, "https://gitlab.com", &claims).await?;

        // Soft delete
        soft_delete(&pool, sa.id).await?;

        // Verify not in list
        let sas = list_by_project(&pool, project.id).await?;
        assert_eq!(sas.len(), 0);

        // Verify still exists in DB
        let sa_deleted = get_by_id(&pool, sa.id).await?;
        assert!(sa_deleted.is_some());
        assert!(sa_deleted.unwrap().deleted_at.is_some());

        Ok(())
    }

    #[sqlx::test]
    async fn test_is_service_account(pool: PgPool) -> Result<()> {
        let regular_user = users::create(&pool, "regular@example.com").await?;
        let project = projects::create(
            &pool,
            "test-project",
            ProjectStatus::Stopped,
            "public".to_string(),
            Some(regular_user.id),
            None,
        )
        .await?;

        let claims = HashMap::new();
        let sa = create(&pool, project.id, "https://gitlab.com", &claims).await?;

        // Regular user should not be a service account
        assert!(!is_service_account(&pool, regular_user.id).await?);

        // SA user should be a service account
        assert!(is_service_account(&pool, sa.user_id).await?);

        Ok(())
    }
}