aws-config 1.1.9

AWS SDK config and credential provider implementations.
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
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

use crate::profile::parser::parse::to_ascii_lowercase;
use std::collections::HashMap;
use std::fmt;

/// Key-Value property pair
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Property {
    key: String,
    value: String,
}

impl Property {
    /// Value of this property
    pub fn value(&self) -> &str {
        &self.value
    }

    /// Name of this property
    pub fn key(&self) -> &str {
        &self.key
    }

    /// Creates a new property
    pub fn new(key: String, value: String) -> Self {
        Property { key, value }
    }
}

type SectionKey = String;
type SectionName = String;
type PropertyName = String;
type SubPropertyName = String;
type PropertyValue = String;

// [section-key section-name]
// property-name = property-value
// property-name =
//   sub-property-name = property-value
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub(crate) struct PropertiesKey {
    section_key: SectionKey,
    section_name: SectionName,
    property_name: PropertyName,
    sub_property_name: Option<SubPropertyName>,
}

impl PropertiesKey {
    pub(crate) fn builder() -> PropertiesKeyBuilder {
        Default::default()
    }
}

impl fmt::Display for PropertiesKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let PropertiesKey {
            section_key,
            section_name,
            property_name,
            sub_property_name,
        } = self;
        match sub_property_name {
            Some(sub_property_name) => {
                write!(
                    f,
                    "[{section_key} {section_name}].{property_name}.{sub_property_name}"
                )
            }
            None => {
                write!(f, "[{section_key} {section_name}].{property_name}")
            }
        }
    }
}

#[derive(Default)]
pub(crate) struct PropertiesKeyBuilder {
    section_key: Option<SectionKey>,
    section_name: Option<SectionName>,
    property_name: Option<PropertyName>,
    sub_property_name: Option<SubPropertyName>,
}

impl PropertiesKeyBuilder {
    pub(crate) fn section_key(mut self, section_key: impl Into<String>) -> Self {
        self.section_key = Some(section_key.into());
        self
    }

    pub(crate) fn section_name(mut self, section_name: impl Into<String>) -> Self {
        self.section_name = Some(section_name.into());
        self
    }

    pub(crate) fn property_name(mut self, property_name: impl Into<String>) -> Self {
        self.property_name = Some(property_name.into());
        self
    }

    pub(crate) fn sub_property_name(mut self, sub_property_name: impl Into<String>) -> Self {
        self.sub_property_name = Some(sub_property_name.into());
        self
    }

    pub(crate) fn build(self) -> Result<PropertiesKey, String> {
        Ok(PropertiesKey {
            section_key: self
                .section_key
                .ok_or("A section_key is required".to_owned())?,
            section_name: self
                .section_name
                .ok_or("A section_name is required".to_owned())?,
            property_name: self
                .property_name
                .ok_or("A property_name is required".to_owned())?,
            sub_property_name: self.sub_property_name,
        })
    }
}

#[allow(clippy::type_complexity)]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(crate) struct Properties {
    inner: HashMap<PropertiesKey, PropertyValue>,
}

#[allow(dead_code)]
impl Properties {
    pub(crate) fn new() -> Self {
        Default::default()
    }

    pub(crate) fn insert(&mut self, properties_key: PropertiesKey, value: PropertyValue) {
        let _ = self
            .inner
            // If we don't clone then we don't get to log a useful warning for a value getting overwritten.
            .entry(properties_key.clone())
            .and_modify(|v| {
                tracing::trace!("overwriting {properties_key}: was {v}, now {value}");
                *v = value.clone();
            })
            .or_insert(value);
    }

    pub(crate) fn get(&self, properties_key: &PropertiesKey) -> Option<&PropertyValue> {
        self.inner.get(properties_key)
    }
}

/// Represents a top-level section (e.g., `[profile name]`) in a config file.
pub(crate) trait Section {
    /// The name of this section
    fn name(&self) -> &str;

    /// Returns all the properties in this section
    fn properties(&self) -> &HashMap<String, Property>;

    /// Returns a reference to the property named `name`
    fn get(&self, name: &str) -> Option<&str>;

    /// True if there are no properties in this section.
    fn is_empty(&self) -> bool;

    /// Insert a property into a section
    fn insert(&mut self, name: String, value: Property);
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub(super) struct SectionInner {
    pub(super) name: String,
    pub(super) properties: HashMap<String, Property>,
}

impl Section for SectionInner {
    fn name(&self) -> &str {
        &self.name
    }

    fn properties(&self) -> &HashMap<String, Property> {
        &self.properties
    }

    fn get(&self, name: &str) -> Option<&str> {
        self.properties
            .get(to_ascii_lowercase(name).as_ref())
            .map(|prop| prop.value())
    }

    fn is_empty(&self) -> bool {
        self.properties.is_empty()
    }

    fn insert(&mut self, name: String, value: Property) {
        self.properties
            .insert(to_ascii_lowercase(&name).into(), value);
    }
}

/// An individual configuration profile
///
/// An AWS config may be composed of a multiple named profiles within a [`ProfileSet`](crate::profile::ProfileSet).
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Profile(SectionInner);

impl Profile {
    /// Create a new profile
    pub fn new(name: impl Into<String>, properties: HashMap<String, Property>) -> Self {
        Self(SectionInner {
            name: name.into(),
            properties,
        })
    }

    /// The name of this profile
    pub fn name(&self) -> &str {
        self.0.name()
    }

    /// Returns a reference to the property named `name`
    pub fn get(&self, name: &str) -> Option<&str> {
        self.0.get(name)
    }
}

impl Section for Profile {
    fn name(&self) -> &str {
        self.0.name()
    }

    fn properties(&self) -> &HashMap<String, Property> {
        self.0.properties()
    }

    fn get(&self, name: &str) -> Option<&str> {
        self.0.get(name)
    }

    fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    fn insert(&mut self, name: String, value: Property) {
        self.0.insert(name, value)
    }
}

/// A `[sso-session name]` section in the config.
#[derive(Debug, Clone, Eq, PartialEq)]
pub(crate) struct SsoSession(SectionInner);

impl SsoSession {
    /// Create a new SSO session section.
    pub(super) fn new(name: impl Into<String>, properties: HashMap<String, Property>) -> Self {
        Self(SectionInner {
            name: name.into(),
            properties,
        })
    }

    /// Returns a reference to the property named `name`
    pub(crate) fn get(&self, name: &str) -> Option<&str> {
        self.0.get(name)
    }
}

impl Section for SsoSession {
    fn name(&self) -> &str {
        self.0.name()
    }

    fn properties(&self) -> &HashMap<String, Property> {
        self.0.properties()
    }

    fn get(&self, name: &str) -> Option<&str> {
        self.0.get(name)
    }

    fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    fn insert(&mut self, name: String, value: Property) {
        self.0.insert(name, value)
    }
}

#[cfg(test)]
mod test {
    use super::PropertiesKey;
    use crate::provider_config::ProviderConfig;
    use aws_types::os_shim_internal::{Env, Fs};

    #[tokio::test]
    async fn test_other_properties_path_get() {
        let _ = tracing_subscriber::fmt::try_init();
        const CFG: &str = r#"[default]
services = foo

[services foo]
s3 =
  endpoint_url = http://localhost:3000
  setting_a = foo
  setting_b = bar

ec2 =
  endpoint_url = http://localhost:2000
  setting_a = foo

[services bar]
ec2 =
  endpoint_url = http://localhost:3000
  setting_b = bar
"#;
        let env = Env::from_slice(&[("AWS_CONFIG_FILE", "config")]);
        let fs = Fs::from_slice(&[("config", CFG)]);

        let provider_config = ProviderConfig::no_configuration().with_env(env).with_fs(fs);

        let p = provider_config.try_profile().await.unwrap();
        let other_sections = p.other_sections();

        assert_eq!(
            "http://localhost:3000",
            other_sections
                .get(&PropertiesKey {
                    section_key: "services".to_owned(),
                    section_name: "foo".to_owned(),
                    property_name: "s3".to_owned(),
                    sub_property_name: Some("endpoint_url".to_owned())
                })
                .expect("setting exists at path")
        );
        assert_eq!(
            "foo",
            other_sections
                .get(&PropertiesKey {
                    section_key: "services".to_owned(),
                    section_name: "foo".to_owned(),
                    property_name: "s3".to_owned(),
                    sub_property_name: Some("setting_a".to_owned())
                })
                .expect("setting exists at path")
        );
        assert_eq!(
            "bar",
            other_sections
                .get(&PropertiesKey {
                    section_key: "services".to_owned(),
                    section_name: "foo".to_owned(),
                    property_name: "s3".to_owned(),
                    sub_property_name: Some("setting_b".to_owned())
                })
                .expect("setting exists at path")
        );

        assert_eq!(
            "http://localhost:2000",
            other_sections
                .get(&PropertiesKey {
                    section_key: "services".to_owned(),
                    section_name: "foo".to_owned(),
                    property_name: "ec2".to_owned(),
                    sub_property_name: Some("endpoint_url".to_owned())
                })
                .expect("setting exists at path")
        );
        assert_eq!(
            "foo",
            other_sections
                .get(&PropertiesKey {
                    section_key: "services".to_owned(),
                    section_name: "foo".to_owned(),
                    property_name: "ec2".to_owned(),
                    sub_property_name: Some("setting_a".to_owned())
                })
                .expect("setting exists at path")
        );

        assert_eq!(
            "http://localhost:3000",
            other_sections
                .get(&PropertiesKey {
                    section_key: "services".to_owned(),
                    section_name: "bar".to_owned(),
                    property_name: "ec2".to_owned(),
                    sub_property_name: Some("endpoint_url".to_owned())
                })
                .expect("setting exists at path")
        );
        assert_eq!(
            "bar",
            other_sections
                .get(&PropertiesKey {
                    section_key: "services".to_owned(),
                    section_name: "bar".to_owned(),
                    property_name: "ec2".to_owned(),
                    sub_property_name: Some("setting_b".to_owned())
                })
                .expect("setting exists at path")
        );
    }
}