kaji 0.0.1

Steer your Keycloak configuration to a stable, declared state.
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
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
use crate::client::KeycloakClient;
use crate::models::{
    AuthenticationFlowRepresentation, AuthenticatorConfigRepresentation, ClientRepresentation,
    ClientScopeRepresentation, ComponentRepresentation, GroupRepresentation,
    IdentityProviderRepresentation, KeycloakResource, RequiredActionProviderRepresentation,
    ResourceMeta, RoleRepresentation, UserRepresentation,
};
use crate::utils::to_sorted_yaml_with_secrets;
use crate::utils::ui::{CHECK, SEARCH, SUCCESS, WARN};
use anyhow::{Context, Result};
use console::style;
use dialoguer::{Confirm, theme::ColorfulTheme};
use sanitize_filename::sanitize;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tokio::fs;
use tokio::sync::Mutex;

pub async fn run(
    client: &KeycloakClient,
    workspace_dir: PathBuf,
    realms_to_inspect: &[String],
    yes: bool,
) -> Result<()> {
    if !fs::try_exists(&workspace_dir)
        .await
        .context("Failed to check output directory")?
    {
        fs::create_dir_all(&workspace_dir)
            .await
            .context("Failed to create output directory")?;
    }

    let realms = if realms_to_inspect.is_empty() {
        let all_realms = client
            .get_realms()
            .await
            .context("Failed to fetch realms")?;
        all_realms.into_iter().map(|r| r.realm).collect()
    } else {
        realms_to_inspect.to_vec()
    };

    let all_secrets = Arc::new(Mutex::new(BTreeMap::new()));
    let prompt_mutex = Arc::new(Mutex::new(()));

    let mut set = tokio::task::JoinSet::new();

    for realm_name in realms {
        let mut realm_client = client.clone();
        realm_client.set_target_realm(realm_name.clone());
        let realm_dir = workspace_dir.join(&realm_name);
        let all_secrets = Arc::clone(&all_secrets);
        let prompt_mutex = Arc::clone(&prompt_mutex);
        let realm_name_owned = realm_name.clone();

        set.spawn(async move {
            {
                let _lock = prompt_mutex.lock().await;
                println!(
                    "\n{} {}",
                    SEARCH,
                    style(format!("Inspecting realm: {}", realm_name_owned))
                        .cyan()
                        .bold()
                );
            }
            inspect_realm(
                &realm_client,
                &realm_name_owned,
                realm_dir,
                all_secrets,
                yes,
                prompt_mutex,
            )
            .await
        });
    }

    crate::utils::join_all_tasks(set, Some("Task panicked")).await?;

    let secrets_lock = all_secrets.lock().await;
    if !secrets_lock.is_empty() {
        let env_path = workspace_dir.join(".secrets");
        let mut env_content = String::new();
        for (key, value) in secrets_lock.iter() {
            env_content.push_str(&format!("{}={}\n", key, value));
        }

        let mut existing_env = String::new();
        if fs::try_exists(&env_path).await.unwrap_or(false) {
            #[allow(clippy::collapsible_if)]
            if let Ok(content) = fs::read_to_string(&env_path).await {
                existing_env = content;
                if !existing_env.ends_with('\n') && !existing_env.is_empty() {
                    existing_env.push('\n');
                }
            }
        }

        let new_content = format!("{}{}", existing_env, env_content);
        write_if_changed_with_mutex(
            &env_path,
            &new_content,
            yes,
            Arc::clone(&prompt_mutex),
            true,
        )
        .await?;
        println!(
            "{} {}",
            CHECK,
            style("Exported secrets to .secrets").green()
        );
    }

    Ok(())
}

async fn write_if_changed_with_mutex(
    path: &Path,
    content: &str,
    yes: bool,
    prompt_mutex: Arc<Mutex<()>>,
    secure: bool,
) -> Result<()> {
    if fs::try_exists(path).await.unwrap_or(false) {
        let existing = fs::read_to_string(path).await.unwrap_or_default();
        if existing == content {
            return Ok(());
        }

        if !yes {
            let _lock = prompt_mutex.lock().await;
            if !Confirm::with_theme(&ColorfulTheme::default())
                .with_prompt(format!(
                    "File {:?} already exists with different content. Overwrite?",
                    path
                ))
                .default(false)
                .interact()?
            {
                println!(
                    "{} {}",
                    WARN,
                    style(format!("Skipping {:?}", path)).yellow()
                );
                return Ok(());
            }
        }
    }

    if secure {
        crate::utils::write_secure(path, content).await?;
    } else {
        #[cfg(unix)]
        {
            use std::os::unix::fs::OpenOptionsExt;
            use tokio::io::AsyncWriteExt;
            let mut options = std::fs::OpenOptions::new();
            options.write(true).create(true).truncate(true).mode(0o600);
            let mut file = fs::OpenOptions::from(options)
                .open(path)
                .await
                .with_context(|| format!("Failed to open {:?}", path))?;
            file.write_all(content.as_bytes())
                .await
                .with_context(|| format!("Failed to write {:?}", path))?;
            file.flush()
                .await
                .with_context(|| format!("Failed to flush {:?}", path))?;
        }
        #[cfg(not(unix))]
        {
            fs::write(path, content)
                .await
                .with_context(|| format!("Failed to write {:?}", path))?;
        }
    }

    Ok(())
}

async fn inspect_resources<T>(
    client: &KeycloakClient,
    realm_name: &str,
    target_dir: Arc<PathBuf>,
    all_secrets: Arc<Mutex<BTreeMap<String, String>>>,
    yes: bool,
    prompt_mutex: Arc<Mutex<()>>,
) -> Result<()>
where
    T: KeycloakResource
        + ResourceMeta
        + serde::Serialize
        + for<'de> serde::Deserialize<'de>
        + Send
        + Sync
        + 'static,
{
    let resources = client
        .get_resources::<T>()
        .await
        .with_context(|| format!("Failed to fetch {} for realm '{}'", T::LABEL, realm_name))?;

    if !fs::try_exists(&*target_dir)
        .await
        .with_context(|| format!("Failed to check {} directory", T::LABEL))?
    {
        fs::create_dir_all(&*target_dir)
            .await
            .with_context(|| format!("Failed to create {} directory", T::LABEL))?;
    }

    let mut set = tokio::task::JoinSet::new();
    for res in resources {
        let target_dir = Arc::clone(&target_dir);
        let all_secrets = Arc::clone(&all_secrets);
        let realm_name = realm_name.to_string();
        let prompt_mutex = Arc::clone(&prompt_mutex);
        set.spawn(async move {
            let filename = format!("{}.yaml", sanitize(res.get_filename()));
            let path = target_dir.join(filename);
            let mut local_secrets = BTreeMap::new();
            let prefix = format!("realm_{}_{}", realm_name, T::SECRET_PREFIX);
            let yaml = to_sorted_yaml_with_secrets(&res, &prefix, &mut local_secrets).context(
                format!("Failed to serialize {} {}", T::LABEL, res.get_name()),
            )?;
            all_secrets.lock().await.extend(local_secrets);
            write_if_changed_with_mutex(&path, &yaml, yes, prompt_mutex, true).await
        });
    }
    crate::utils::join_all_tasks(set, Some("Task panicked")).await?;
    {
        let _lock = prompt_mutex.lock().await;
        println!(
            "  {} {}",
            SUCCESS,
            style(format!(
                "Exported {} to {}/",
                T::LABEL,
                target_dir
                    .file_name()
                    .and_then(|n| n.to_str())
                    .unwrap_or_default()
            ))
            .green()
        );
    }

    Ok(())
}

async fn inspect_realm(
    client: &KeycloakClient,
    realm_name: &str,
    workspace_dir: PathBuf,
    all_secrets: Arc<Mutex<BTreeMap<String, String>>>,
    yes: bool,
    prompt_mutex: Arc<Mutex<()>>,
) -> Result<()> {
    if !fs::try_exists(&workspace_dir)
        .await
        .context("Failed to check output directory")?
    {
        fs::create_dir_all(&workspace_dir)
            .await
            .context("Failed to create output directory")?;
    }

    let mut set = tokio::task::JoinSet::new();
    let workspace_dir = Arc::new(workspace_dir);

    // Fetch realm configuration in parallel
    {
        let client = client.clone();
        let realm_name = realm_name.to_string();
        let workspace_dir = Arc::clone(&workspace_dir);
        let all_secrets = Arc::clone(&all_secrets);
        let prompt_mutex = Arc::clone(&prompt_mutex);
        set.spawn(async move {
            let realm = client.get_realm().await.context("Failed to fetch realm")?;
            let mut local_secrets = BTreeMap::new();
            let realm_prefix = format!("realm_{}", realm_name);
            let realm_yaml = to_sorted_yaml_with_secrets(&realm, &realm_prefix, &mut local_secrets)
                .context("Failed to serialize realm")?;
            all_secrets.lock().await.extend(local_secrets);

            let realm_path = workspace_dir.join("realm.yaml");
            write_if_changed_with_mutex(
                &realm_path,
                &realm_yaml,
                yes,
                Arc::clone(&prompt_mutex),
                true,
            )
            .await?;
            {
                let _lock = prompt_mutex.lock().await;
                println!(
                    "  {} {}",
                    SUCCESS,
                    style("Exported realm configuration to realm.yaml").green()
                );
            }
            Ok::<(), anyhow::Error>(())
        });
    }

    // Fetch resources in parallel
    spawn_inspect::<ClientRepresentation>(
        &mut set,
        client,
        realm_name,
        &workspace_dir,
        &all_secrets,
        yes,
        &prompt_mutex,
    );
    spawn_inspect::<RoleRepresentation>(
        &mut set,
        client,
        realm_name,
        &workspace_dir,
        &all_secrets,
        yes,
        &prompt_mutex,
    );
    spawn_inspect::<ClientScopeRepresentation>(
        &mut set,
        client,
        realm_name,
        &workspace_dir,
        &all_secrets,
        yes,
        &prompt_mutex,
    );
    spawn_inspect::<IdentityProviderRepresentation>(
        &mut set,
        client,
        realm_name,
        &workspace_dir,
        &all_secrets,
        yes,
        &prompt_mutex,
    );
    spawn_inspect::<GroupRepresentation>(
        &mut set,
        client,
        realm_name,
        &workspace_dir,
        &all_secrets,
        yes,
        &prompt_mutex,
    );
    spawn_inspect::<UserRepresentation>(
        &mut set,
        client,
        realm_name,
        &workspace_dir,
        &all_secrets,
        yes,
        &prompt_mutex,
    );
    spawn_inspect::<AuthenticationFlowRepresentation>(
        &mut set,
        client,
        realm_name,
        &workspace_dir,
        &all_secrets,
        yes,
        &prompt_mutex,
    );
    spawn_inspect::<RequiredActionProviderRepresentation>(
        &mut set,
        client,
        realm_name,
        &workspace_dir,
        &all_secrets,
        yes,
        &prompt_mutex,
    );
    spawn_inspect::<ComponentRepresentation>(
        &mut set,
        client,
        realm_name,
        &workspace_dir,
        &all_secrets,
        yes,
        &prompt_mutex,
    );
    spawn_inspect::<AuthenticatorConfigRepresentation>(
        &mut set,
        client,
        realm_name,
        &workspace_dir,
        &all_secrets,
        yes,
        &prompt_mutex,
    );

    crate::utils::join_all_tasks(set, Some("Task panicked")).await?;

    Ok(())
}

fn spawn_inspect<T>(
    set: &mut tokio::task::JoinSet<Result<()>>,
    client: &KeycloakClient,
    realm_name: &str,
    workspace_dir: &Arc<PathBuf>,
    all_secrets: &Arc<Mutex<BTreeMap<String, String>>>,
    yes: bool,
    prompt_mutex: &Arc<Mutex<()>>,
) where
    T: KeycloakResource
        + ResourceMeta
        + serde::Serialize
        + for<'de> serde::Deserialize<'de>
        + Send
        + Sync
        + 'static,
{
    let client = client.clone();
    let realm_name = realm_name.to_string();
    let target_dir = Arc::new(workspace_dir.join(T::DIR_NAME));
    let all_secrets = Arc::clone(all_secrets);
    let prompt_mutex = Arc::clone(prompt_mutex);

    set.spawn(async move {
        inspect_resources::<T>(
            &client,
            &realm_name,
            target_dir,
            all_secrets,
            yes,
            prompt_mutex,
        )
        .await
    });
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::tempdir;

    #[tokio::test]
    async fn test_write_if_changed_with_mutex_insecure() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("insecure.txt");
        let prompt_mutex = Arc::new(Mutex::new(()));

        // Write new file
        write_if_changed_with_mutex(
            &file_path,
            "test content",
            true,
            Arc::clone(&prompt_mutex),
            false,
        )
        .await
        .unwrap();
        let content = fs::read_to_string(&file_path).await.unwrap();
        assert_eq!(content, "test content");

        // Overwrite file
        write_if_changed_with_mutex(
            &file_path,
            "new content",
            true,
            Arc::clone(&prompt_mutex),
            false,
        )
        .await
        .unwrap();
        let content = fs::read_to_string(&file_path).await.unwrap();
        assert_eq!(content, "new content");
    }
}