1
//! The `ssh-key-add` form and `remove-ssh-key` end to end: nomiscript form ->
2
//! rpc::Session -> server::command::* -> real Postgres. Gated on the `db` feature.
3

            
4
#![cfg(feature = "db")]
5

            
6
use forms::{FormValues, SSH_KEY_ADD};
7
use rpc::{ScriptCtx, Session};
8
use server::db::install_test_pool;
9
use server::ssh_keys::parse_authorized_keys_line;
10
use sqlx::PgPool;
11
use supp_macro::local_db_sqlx_test;
12
use tests_integration::session_support::{insert_test_user, setup};
13
use uuid::Uuid;
14

            
15
const LAPTOP: &str =
16
    "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID4KsQxV3UhkA3wbnZ4C5n4kg6xdInR3sydCk+Hfg6ZF laptop";
17

            
18
10
async fn user_session(pool: &PgPool) -> anyhow::Result<Session> {
19
10
    let user_id = Uuid::new_v4();
20
10
    insert_test_user(pool, user_id).await?;
21
10
    Ok(Session::new(ScriptCtx::new(user_id)).expect("Session::new"))
22
10
}
23

            
24
24
async fn call(session: &mut Session, id: u32, form: &str) -> String {
25
24
    session
26
24
        .handle_form(&format!("(:id {id} :form {form})"))
27
24
        .await
28
24
}
29

            
30
14
fn add_form(key: &str) -> String {
31
14
    let mut values = FormValues::default();
32
14
    values.set("key", key);
33
14
    cli_core::transaction::submit_call(&SSH_KEY_ADD, &values)
34
14
}
35

            
36
3
fn fingerprint_of(key: &str) -> String {
37
3
    parse_authorized_keys_line(key)
38
3
        .expect("a valid key")
39
3
        .fingerprint
40
3
}
41

            
42
#[local_db_sqlx_test]
43
async fn an_added_key_is_listed_with_its_fingerprint(pool: PgPool) -> anyhow::Result<()> {
44
    let mut session = user_session(&pool).await?;
45

            
46
    let added = call(&mut session, 1, &add_form(LAPTOP)).await;
47
    assert!(added.contains(":value \""), "got: {added}");
48

            
49
    let listed = call(&mut session, 2, "(list-ssh-keys)").await;
50
    assert!(listed.contains("ssh-ed25519"), "got: {listed}");
51
    assert!(listed.contains("laptop"), "got: {listed}");
52
    assert!(listed.contains(&fingerprint_of(LAPTOP)), "got: {listed}");
53
}
54

            
55
#[local_db_sqlx_test]
56
async fn adding_a_key_twice_is_refused_at_the_key(pool: PgPool) -> anyhow::Result<()> {
57
    let mut session = user_session(&pool).await?;
58
    call(&mut session, 1, &add_form(LAPTOP)).await;
59

            
60
    let again = call(&mut session, 2, &add_form(LAPTOP)).await;
61

            
62
    assert!(again.contains(":code form-invalid"), "got: {again}");
63
    assert!(again.contains("already registered"), "got: {again}");
64
}
65

            
66
#[local_db_sqlx_test]
67
async fn a_key_held_by_another_account_is_refused(pool: PgPool) -> anyhow::Result<()> {
68
    let mut owner = user_session(&pool).await?;
69
    let mut other = user_session(&pool).await?;
70
    call(&mut owner, 1, &add_form(LAPTOP)).await;
71

            
72
    let stolen = call(&mut other, 2, &add_form(LAPTOP)).await;
73

            
74
    assert!(stolen.contains(":code form-invalid"), "got: {stolen}");
75
    let listed = call(&mut other, 3, "(list-ssh-keys)").await;
76
    assert!(listed.contains(":value nil"), "got: {listed}");
77
}
78

            
79
#[local_db_sqlx_test]
80
async fn invalid_keys_are_refused_and_nothing_is_stored(pool: PgPool) -> anyhow::Result<()> {
81
    let mut session = user_session(&pool).await?;
82
    let two_lines = format!("{LAPTOP}\n{LAPTOP}");
83
    let rejected = [
84
        "not-a-key",
85
        "-----BEGIN OPENSSH PRIVATE KEY-----",
86
        "ssh-unknown AAAAC3NzaC1lZDI1NTE5AAAAID4KsQxV3UhkA3wbnZ4C5n4kg6xdInR3sydCk+Hfg6ZF x",
87
        "ssh-rsa AAAAC3NzaC1lZDI1NTE5AAAAID4KsQxV3UhkA3wbnZ4C5n4kg6xdInR3sydCk+Hfg6ZF x",
88
        &two_lines,
89
        "",
90
    ];
91

            
92
    for (n, key) in (1..).zip(rejected) {
93
        let response = call(&mut session, n, &add_form(key)).await;
94
        assert!(response.contains(":code form-invalid"), "{key}: {response}");
95
    }
96

            
97
    let listed = call(&mut session, 11, "(list-ssh-keys)").await;
98
    assert!(listed.contains(":value nil"), "got: {listed}");
99
}
100

            
101
#[local_db_sqlx_test]
102
async fn a_key_is_removed_by_its_owner_and_not_by_another_user(pool: PgPool) -> anyhow::Result<()> {
103
    let mut owner = user_session(&pool).await?;
104
    let mut other = user_session(&pool).await?;
105
    call(&mut owner, 1, &add_form(LAPTOP)).await;
106
    let fingerprint = fingerprint_of(LAPTOP);
107
    let remove = format!("(remove-ssh-key \"{fingerprint}\")");
108

            
109
    let foreign = call(&mut other, 2, &remove).await;
110
    assert!(foreign.contains(":code not-found"), "got: {foreign}");
111
    let still_listed = call(&mut owner, 3, "(list-ssh-keys)").await;
112
    assert!(still_listed.contains(&fingerprint), "got: {still_listed}");
113

            
114
    let removed = call(&mut owner, 4, &remove).await;
115
    assert!(removed.contains(":value t"), "got: {removed}");
116
    let listed = call(&mut owner, 5, "(list-ssh-keys)").await;
117
    assert!(listed.contains(":value nil"), "got: {listed}");
118
}
119

            
120
#[local_db_sqlx_test]
121
async fn the_same_key_is_removed_once(pool: PgPool) -> anyhow::Result<()> {
122
    let mut session = user_session(&pool).await?;
123
    call(&mut session, 1, &add_form(LAPTOP)).await;
124
    let remove = format!("(remove-ssh-key \"{}\")", fingerprint_of(LAPTOP));
125
    call(&mut session, 2, &remove).await;
126

            
127
    let twice = call(&mut session, 3, &remove).await;
128

            
129
    assert!(twice.contains(":code not-found"), "got: {twice}");
130
}
131

            
132
#[local_db_sqlx_test]
133
async fn add_is_per_user(pool: PgPool) -> anyhow::Result<()> {
134
    let mut first = user_session(&pool).await?;
135
    let mut second = user_session(&pool).await?;
136
    call(&mut first, 1, &add_form(LAPTOP)).await;
137

            
138
    let listed = call(&mut second, 2, "(list-ssh-keys)").await;
139

            
140
    assert!(listed.contains(":value nil"), "got: {listed}");
141
}