vaultrs/
ssh.rs

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
use crate::api;
use crate::api::ssh::requests::{GenerateSSHCredsRequest, VerifySSHOTPRequest};
use crate::api::ssh::responses::{GenerateSSHCredsResponse, VerifySSHOTPResponse};
use crate::client::Client;
use crate::error::ClientError;

/// Generates SSH credentials for the given role
///
/// See [GenerateSSHCredsRequest]
pub async fn generate(
    client: &impl Client,
    mount: &str,
    name: &str,
    ip: &str,
    username: Option<String>,
) -> Result<GenerateSSHCredsResponse, ClientError> {
    let mut endpoint = GenerateSSHCredsRequest::builder();
    if let Some(u) = username {
        endpoint.username(u);
    }
    api::exec_with_result(
        client,
        endpoint.mount(mount).name(name).ip(ip).build().unwrap(),
    )
    .await
}

/// Verify SSH OTP details
///
/// See [VerifySSHOTPRequest]
pub async fn verify_otp(
    client: &impl Client,
    mount: &str,
    otp: &str,
) -> Result<VerifySSHOTPResponse, ClientError> {
    let endpoint = VerifySSHOTPRequest::builder()
        .mount(mount)
        .otp(otp)
        .build()
        .unwrap();
    api::exec_with_result(client, endpoint).await
}

pub mod ca {
    use crate::api;
    use crate::api::ssh::requests::{
        DeleteCAInfoRequest, ReadPublicKeyRequest, SignSSHKeyRequest, SignSSHKeyRequestBuilder,
        SubmitCAInfoRequest,
    };
    use crate::api::ssh::responses::{
        ReadPublicKeyResponse, SignSSHKeyResponse, SubmitCAInfoResponse,
    };
    use crate::client::Client;
    use crate::error::ClientError;

    /// Deletes the stored keys for the CA.
    ///
    /// See [DeleteCAInfoRequest]
    pub async fn delete(client: &impl Client, mount: &str) -> Result<(), ClientError> {
        let endpoint = DeleteCAInfoRequest::builder().mount(mount).build().unwrap();
        api::exec_with_empty(client, endpoint).await
    }

    /// Generates CA certificate internally and returns the public key.
    ///
    /// See [SubmitCAInfoRequest]
    pub async fn generate(
        client: &impl Client,
        mount: &str,
    ) -> Result<SubmitCAInfoResponse, ClientError> {
        let endpoint = SubmitCAInfoRequest::builder()
            .mount(mount)
            .generate_signing_key(true)
            .build()
            .unwrap();
        api::exec_with_result(client, endpoint).await
    }

    /// Reads the public key of the CA.
    ///
    /// See [ReadPublicKeyRequest]
    pub async fn read(
        client: &impl Client,
        mount: &str,
    ) -> Result<ReadPublicKeyResponse, ClientError> {
        let endpoint = ReadPublicKeyRequest::builder()
            .mount(mount)
            .build()
            .unwrap();
        api::exec_with_result(client, endpoint).await
    }

    /// Signs a public key using the CA certificate.
    ///
    /// See [SignSSHKeyRequest]
    pub async fn sign(
        client: &impl Client,
        mount: &str,
        name: &str,
        public_key: &str,
        opts: Option<&mut SignSSHKeyRequestBuilder>,
    ) -> Result<SignSSHKeyResponse, ClientError> {
        let mut t = SignSSHKeyRequest::builder();
        let endpoint = opts
            .unwrap_or(&mut t)
            .mount(mount)
            .name(name)
            .public_key(public_key)
            .build()
            .unwrap();
        api::exec_with_result(client, endpoint).await
    }

    /// Sets the private and public key for the CA.
    ///
    /// See [SubmitCAInfoRequest]
    pub async fn set(
        client: &impl Client,
        mount: &str,
        private_key: &str,
        public_key: &str,
    ) -> Result<(), ClientError> {
        let endpoint = SubmitCAInfoRequest::builder()
            .mount(mount)
            .private_key(private_key)
            .public_key(public_key)
            .build()
            .unwrap();
        api::exec_with_empty(client, endpoint).await
    }
}

pub mod key {
    use crate::api;
    use crate::api::ssh::requests::{DeleteKeyRequest, SetKeyRequest};
    use crate::client::Client;
    use crate::error::ClientError;

    /// Creates or updates a SSH key
    ///
    /// See [SetKeyRequest]
    pub async fn set(
        client: &impl Client,
        mount: &str,
        name: &str,
        key: &str,
    ) -> Result<(), ClientError> {
        let endpoint = SetKeyRequest::builder()
            .mount(mount)
            .name(name)
            .key(key)
            .build()
            .unwrap();
        api::exec_with_empty(client, endpoint).await
    }

    /// Deletes a SSH key
    ///
    /// See [DeleteKeyRequest]
    pub async fn delete(client: &impl Client, mount: &str, name: &str) -> Result<(), ClientError> {
        let endpoint = DeleteKeyRequest::builder()
            .mount(mount)
            .name(name)
            .build()
            .unwrap();
        api::exec_with_empty(client, endpoint).await
    }
}

pub mod role {
    use crate::api;
    use crate::api::ssh::requests::ListRolesByIPRequest;
    use crate::api::ssh::responses::ListRolesByIPResponse;
    use crate::api::ssh::{
        requests::{
            DeleteRoleRequest, ListRolesRequest, ReadRoleRequest, SetRoleRequest,
            SetRoleRequestBuilder,
        },
        responses::{ListRolesResponse, ReadRoleResponse},
    };
    use crate::client::Client;
    use crate::error::ClientError;

    /// Deletes a role
    ///
    /// See [DeleteRoleRequest]
    pub async fn delete(client: &impl Client, mount: &str, name: &str) -> Result<(), ClientError> {
        let endpoint = DeleteRoleRequest::builder()
            .mount(mount)
            .name(name)
            .build()
            .unwrap();
        api::exec_with_empty(client, endpoint).await
    }

    /// Lists all roles
    ///
    /// See [ListRolesRequest]
    pub async fn list(client: &impl Client, mount: &str) -> Result<ListRolesResponse, ClientError> {
        let endpoint = ListRolesRequest::builder().mount(mount).build().unwrap();
        api::exec_with_result(client, endpoint).await
    }

    /// Lists all roles by IP
    ///
    /// See [ListRolesByIPRequest]
    pub async fn list_by_ip(
        client: &impl Client,
        mount: &str,
        ip: &str,
    ) -> Result<ListRolesByIPResponse, ClientError> {
        let endpoint = ListRolesByIPRequest::builder()
            .mount(mount)
            .ip(ip)
            .build()
            .unwrap();
        api::exec_with_result(client, endpoint).await
    }

    /// Reads a role
    ///
    /// See [ReadRoleRequest]
    pub async fn read(
        client: &impl Client,
        mount: &str,
        name: &str,
    ) -> Result<ReadRoleResponse, ClientError> {
        let endpoint = ReadRoleRequest::builder()
            .mount(mount)
            .name(name)
            .build()
            .unwrap();
        api::exec_with_result(client, endpoint).await
    }

    /// Creates or updates a role
    ///
    /// See [SetRoleRequest]
    pub async fn set(
        client: &impl Client,
        mount: &str,
        name: &str,
        opts: Option<&mut SetRoleRequestBuilder>,
    ) -> Result<(), ClientError> {
        let mut t = SetRoleRequest::builder();
        let endpoint = opts
            .unwrap_or(&mut t)
            .mount(mount)
            .name(name)
            .build()
            .unwrap();
        api::exec_with_empty(client, endpoint).await
    }
}

pub mod zero {
    use crate::api;
    use crate::api::ssh::requests::{
        ConfigureZeroAddressRolesRequest, DeleteZeroAddressRolesRequest,
        ListZeroAddressRolesRequest,
    };
    use crate::api::ssh::responses::ListZeroAddressRolesResponse;
    use crate::client::Client;
    use crate::error::ClientError;

    /// Deletes all zero-address roles
    ///
    /// See [DeleteZeroAddressRolesRequest]
    pub async fn delete(client: &impl Client, mount: &str) -> Result<(), ClientError> {
        let endpoint = DeleteZeroAddressRolesRequest::builder()
            .mount(mount)
            .build()
            .unwrap();
        api::exec_with_empty(client, endpoint).await
    }

    /// Lists all zero-address roles
    ///
    /// See [ListZeroAddressRolesRequest]
    pub async fn list(
        client: &impl Client,
        mount: &str,
    ) -> Result<ListZeroAddressRolesResponse, ClientError> {
        let endpoint = ListZeroAddressRolesRequest::builder()
            .mount(mount)
            .build()
            .unwrap();
        api::exec_with_result(client, endpoint).await
    }

    /// Sets zero-address roles
    ///
    /// See [ConfigureZeroAddressRolesRequest]
    pub async fn set(
        client: &impl Client,
        mount: &str,
        roles: Vec<String>,
    ) -> Result<(), ClientError> {
        let endpoint = ConfigureZeroAddressRolesRequest::builder()
            .mount(mount)
            .roles(roles)
            .build()
            .unwrap();
        api::exec_with_empty(client, endpoint).await
    }
}