aws_config/profile/parser.rs
1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6//! Code for parsing AWS profile config
7
8use aws_runtime::env_config::file::EnvConfigFiles as ProfileFiles;
9use aws_runtime::env_config::source;
10use aws_types::os_shim_internal::{Env, Fs};
11use std::borrow::Cow;
12
13pub use aws_runtime::env_config::error::EnvConfigFileLoadError as ProfileFileLoadError;
14pub use aws_runtime::env_config::parse::EnvConfigParseError as ProfileParseError;
15pub use aws_runtime::env_config::property::Property;
16pub use aws_runtime::env_config::section::{EnvConfigSections as ProfileSet, Profile};
17
18/// Read & parse AWS config files
19///
20/// Loads AWS config file from the filesystem, parses them, and converts them into a [`ProfileSet`](ProfileSet).
21///
22/// Although the basic behavior is straightforward, there are number of nuances to maintain backwards
23/// compatibility with other SDKs enumerated below.
24///
25#[doc = include_str!("location_of_profile_files.md")]
26///
27/// ## Profile file syntax
28///
29/// Profile files have a form similar to `.ini` but with a several edge cases. These behaviors exist
30/// to match existing parser implementations, ensuring consistent behavior across AWS SDKs. These
31/// cases fully enumerated in `test-data/profile-parser-tests.json`.
32///
33/// ### The config file `~/.aws/config`
34/// ```ini
35/// # ~/.aws/config
36/// [profile default]
37/// key = value
38///
39/// # profiles must begin with `profile`
40/// [profile other]
41/// key = value2
42/// ```
43///
44/// ### The credentials file `~/.aws/credentials`
45/// The main difference is that in ~/.aws/credentials, profiles MUST NOT be prefixed with profile:
46/// ```ini
47/// [default]
48/// aws_access_key_id = 123
49///
50/// [other]
51/// aws_access_key_id = 456
52/// ```
53pub async fn load(
54 fs: &Fs,
55 env: &Env,
56 profile_files: &ProfileFiles,
57 selected_profile_override: Option<Cow<'static, str>>,
58) -> Result<ProfileSet, ProfileFileLoadError> {
59 let mut source = source::load(env, fs, profile_files).await?;
60 if let Some(profile) = selected_profile_override {
61 source.profile = profile;
62 }
63
64 Ok(ProfileSet::parse(source)?)
65}