iceberg_catalog_s3tables/
utils.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::collections::HashMap;
19
20use aws_config::{BehaviorVersion, Region, SdkConfig};
21use aws_sdk_s3tables::config::Credentials;
22
23/// Property aws profile name
24pub const AWS_PROFILE_NAME: &str = "profile_name";
25/// Property aws region
26pub const AWS_REGION_NAME: &str = "region_name";
27/// Property aws access key
28pub const AWS_ACCESS_KEY_ID: &str = "aws_access_key_id";
29/// Property aws secret access key
30pub const AWS_SECRET_ACCESS_KEY: &str = "aws_secret_access_key";
31/// Property aws session token
32pub const AWS_SESSION_TOKEN: &str = "aws_session_token";
33
34/// Creates an aws sdk configuration based on
35/// provided properties and an optional endpoint URL.
36pub(crate) async fn create_sdk_config(
37    properties: &HashMap<String, String>,
38    endpoint_url: Option<String>,
39) -> SdkConfig {
40    let mut config = aws_config::defaults(BehaviorVersion::latest());
41
42    if properties.is_empty() {
43        return config.load().await;
44    }
45
46    if let Some(endpoint_url) = endpoint_url {
47        config = config.endpoint_url(endpoint_url);
48    }
49
50    if let (Some(access_key), Some(secret_key)) = (
51        properties.get(AWS_ACCESS_KEY_ID),
52        properties.get(AWS_SECRET_ACCESS_KEY),
53    ) {
54        let session_token = properties.get(AWS_SESSION_TOKEN).cloned();
55        let credentials_provider =
56            Credentials::new(access_key, secret_key, session_token, None, "properties");
57
58        config = config.credentials_provider(credentials_provider)
59    };
60
61    if let Some(profile_name) = properties.get(AWS_PROFILE_NAME) {
62        config = config.profile_name(profile_name);
63    }
64
65    if let Some(region_name) = properties.get(AWS_REGION_NAME) {
66        let region = Region::new(region_name.clone());
67        config = config.region(region);
68    }
69
70    config.load().await
71}