vaultrs/
aws.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
pub mod config {

    use crate::{
        api::{
            self,
            aws::{
                requests::{
                    ConfigureLeaseRequest, GetConfigurationRequest, ReadLeaseRequest,
                    RotateRootCredentialsRequest, SetConfigurationRequest,
                    SetConfigurationRequestBuilder,
                },
                responses::{
                    GetConfigurationResponse, ReadLeaseResponse, RotateRootCredentialsResponse,
                },
            },
        },
        client::Client,
        error::ClientError,
    };

    /// Configures the root IAM credentials to communicate with AWS.
    ///
    /// See [SetConfigurationRequest]
    pub async fn set(
        client: &impl Client,
        mount: &str,
        access_key: &str,
        secret_key: &str,
        opts: Option<&mut SetConfigurationRequestBuilder>,
    ) -> Result<(), ClientError> {
        let mut t = SetConfigurationRequest::builder();
        let endpoint = opts
            .unwrap_or(&mut t)
            .mount(mount)
            .access_key(access_key)
            .secret_key(secret_key)
            .build()
            .unwrap();

        api::exec_with_empty(client, endpoint).await
    }

    pub async fn get(
        client: &impl Client,
        mount: &str,
    ) -> Result<GetConfigurationResponse, ClientError> {
        let e = GetConfigurationRequest::builder()
            .mount(mount)
            .build()
            .unwrap();

        api::exec_with_result(client, e).await
    }

    pub async fn rotate(
        client: &impl Client,
        mount: &str,
    ) -> Result<RotateRootCredentialsResponse, ClientError> {
        let endpoint = RotateRootCredentialsRequest::builder()
            .mount(mount)
            .build()
            .unwrap();

        api::exec_with_result(client, endpoint).await
    }

    /// Configures the root IAM credentials to communicate with AWS.
    ///
    /// See [SetConfigurationRequest]
    pub async fn set_lease(
        client: &impl Client,
        mount: &str,
        lease: &str,
        lease_max: &str,
    ) -> Result<(), ClientError> {
        let endpoint = ConfigureLeaseRequest::builder()
            .mount(mount)
            .lease(lease)
            .lease_max(lease_max)
            .build()
            .unwrap();

        api::exec_with_empty(client, endpoint).await
    }

    pub async fn read_lease(
        client: &impl Client,
        mount: &str,
    ) -> Result<ReadLeaseResponse, ClientError> {
        let endpoint = ReadLeaseRequest::builder().mount(mount).build().unwrap();

        api::exec_with_result(client, endpoint).await
    }
}

pub mod roles {

    use crate::{
        api::{
            self,
            aws::{
                requests::{
                    CreateUpdateRoleRequest, CreateUpdateRoleRequestBuilder, DeleteRoleRequest,
                    GenerateCredentialsRequest, GenerateCredentialsRequestBuilder,
                    GenerateCredentialsStsRequest, GenerateCredentialsStsRequestBuilder,
                    ListRolesRequest, ReadRoleRequest,
                },
                responses::{GenerateCredentialsResponse, ListRolesResponse, ReadRoleResponse},
            },
        },
        client::Client,
        error::ClientError,
    };

    pub async fn create_update(
        client: &impl Client,
        mount: &str,
        name: &str,
        credential_type: &str,
        opts: Option<&mut CreateUpdateRoleRequestBuilder>,
    ) -> Result<(), ClientError> {
        let mut t = CreateUpdateRoleRequest::builder();
        let endpoint = opts
            .unwrap_or(&mut t)
            .mount(mount)
            .name(name)
            .credential_type(credential_type)
            .build()
            .unwrap();

        api::exec_with_empty(client, endpoint).await
    }

    pub async fn read(
        client: &impl Client,
        mount: &str,
        name: &str,
    ) -> Result<ReadRoleResponse, ClientError> {
        let e = ReadRoleRequest::builder()
            .mount(mount)
            .name(name)
            .build()
            .unwrap();

        api::exec_with_result(client, e).await
    }

    pub async fn delete(client: &impl Client, mount: &str, name: &str) -> Result<(), ClientError> {
        let e = DeleteRoleRequest::builder()
            .mount(mount)
            .name(name)
            .build()
            .unwrap();

        api::exec_with_empty(client, e).await
    }

    pub async fn list(client: &impl Client, mount: &str) -> Result<ListRolesResponse, ClientError> {
        let e = ListRolesRequest::builder().mount(mount).build().unwrap();

        api::exec_with_result(client, e).await
    }

    /// Generate credentials using /aws/creds endpoint
    pub async fn credentials(
        client: &impl Client,
        mount: &str,
        name: &str,
        opts: Option<&mut GenerateCredentialsRequestBuilder>,
    ) -> Result<GenerateCredentialsResponse, ClientError> {
        let mut t = GenerateCredentialsRequest::builder();
        let endpoint = opts
            .unwrap_or(&mut t)
            .mount(mount)
            .name(name)
            .build()
            .unwrap();

        api::exec_with_result(client, endpoint).await
    }

    /// Generate credentials using /aws/sts endpoint
    pub async fn credentials_sts(
        client: &impl Client,
        mount: &str,
        name: &str,
        opts: Option<&mut GenerateCredentialsStsRequestBuilder>,
    ) -> Result<GenerateCredentialsResponse, ClientError> {
        let mut t = GenerateCredentialsStsRequest::builder();
        let endpoint = opts
            .unwrap_or(&mut t)
            .mount(mount)
            .name(name)
            .build()
            .unwrap();

        api::exec_with_result(client, endpoint).await
    }
}