-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add process credential provider (#1076)
- Loading branch information
Showing
12 changed files
with
179 additions
and
43 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
IntegrationTests/Services/AWSS3IntegrationTests/ProcessCredentialsProviderTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Foundation | ||
import XCTest | ||
import AWSS3 | ||
import AWSClientRuntime | ||
|
||
// Please provide your-access-key and your-secret-key in Resources/credenitals | ||
class ProcessCredentialProviderTests: XCTestCase { | ||
|
||
var client: S3Client! | ||
|
||
override func setUp() async throws { | ||
// Setup ProcessCredentialsProvider | ||
let processCredentialsProvider = try ProcessCredentialsProvider( | ||
configFilePath: Bundle.module.path(forResource: "config", ofType: nil)!, | ||
credentialsFilePath: Bundle.module.path(forResource: "credentials", ofType: nil)! | ||
) | ||
|
||
// Setup S3ClientConfiguration to use ProcessCredentialsProvider | ||
let testConfig = try await S3Client.S3ClientConfiguration() | ||
testConfig.credentialsProvider = processCredentialsProvider | ||
|
||
// Initialize our S3 client with the specified configuration | ||
client = S3Client(config: testConfig) | ||
} | ||
|
||
// This test calls listBuckets() and forces S3Client to use ProcessCredentialsProvider | ||
func test_listBuckets() async throws { | ||
_ = try await client.listBuckets(input: ListBucketsInput()) | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
IntegrationTests/Services/AWSS3IntegrationTests/Resources/config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[default] | ||
region = us-east-1 |
2 changes: 2 additions & 0 deletions
2
IntegrationTests/Services/AWSS3IntegrationTests/Resources/credentials
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[default] | ||
credential_process = echo '{"Version": 1, "AccessKeyId": "your-access-key", "SecretAccessKey": "your-secret-key"}' |
51 changes: 51 additions & 0 deletions
51
Sources/Core/AWSClientRuntime/Auth/CredentialsProviders/ProcessCredentialsProvider.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import AwsCommonRuntimeKit | ||
import ClientRuntime | ||
import Foundation | ||
|
||
/// The process credentials provider sources credentials from running a command or process. | ||
/// The command to run is sourced from a profile in the AWS config file, using the standard | ||
/// profile selection rules. The profile key the command is read from is "credential_process." | ||
/// E.g.: | ||
/// [default] | ||
/// credential_process=/opt/amazon/bin/my-credential-fetcher --argsA=abc | ||
/// On successfully running the command, the output should be a json data with the following | ||
/// format: | ||
/// { | ||
/// "Version": 1, | ||
/// "AccessKeyId": "accesskey", | ||
/// "SecretAccessKey": "secretAccessKey" | ||
/// "SessionToken": "....", | ||
/// "Expiration": "2019-05-29T00:21:43Z" | ||
/// } | ||
/// Version here identifies the command output format version. | ||
public struct ProcessCredentialsProvider: CredentialsSourcedByCRT { | ||
let crtCredentialsProvider: CRTCredentialsProvider | ||
|
||
/// Creates a credentials provider that gets credentials from running a command or process. | ||
/// | ||
/// - Parameters: | ||
/// - profileName: The profile name to use. If not provided it will be resolved internally via the `AWS_PROFILE` environment variable or defaulted to `default` if not configured. | ||
/// - configFilePath: The path to the configuration file to use. If not provided it will be resolved internally via the `AWS_CONFIG_FILE` environment variable or defaulted to `~/.aws/config` if not configured. | ||
/// - credentialsFilePath: The path to the shared credentials file to use. If not provided it will be resolved internally via the `AWS_SHARED_CREDENTIALS_FILE` environment variable or defaulted `~/.aws/credentials` if not configured. | ||
public init( | ||
profileName: String? = nil, | ||
configFilePath: String? = nil, | ||
credentialsFilePath: String? = nil | ||
) throws { | ||
let fileBasedConfig = try CRTFileBasedConfiguration( | ||
configFilePath: configFilePath, | ||
credentialsFilePath: credentialsFilePath | ||
) | ||
self.crtCredentialsProvider = try CRTCredentialsProvider(source: .process( | ||
fileBasedConfiguration: fileBasedConfig, | ||
profileFileNameOverride: profileName | ||
)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...WSClientRuntimeTests/Auth/CredentialsProvidersTests/ProcessCredentialsProviderTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import ClientRuntime | ||
import Foundation | ||
import XCTest | ||
|
||
@_spi(FileBasedConfig) @testable import AWSClientRuntime | ||
|
||
// Test fails on CI build with macos-11, Xcode_13.2.1, platform=iOS Simulator but not on later versions | ||
// ProcessCredentialsProvider is not useful on iOS platform so this test will remain disabled for now | ||
#if !os(iOS) | ||
class ProcessCredentialsProviderTests: XCTestCase { | ||
let configPath = Bundle.module.path(forResource: "config", ofType: nil)! | ||
let credentialsPath = Bundle.module.path(forResource: "credentials", ofType: nil)! | ||
|
||
func testGetCredentialsWithDefaultProfile() async throws { | ||
let subject = try ProcessCredentialsProvider( | ||
configFilePath: configPath, | ||
credentialsFilePath: credentialsPath | ||
) | ||
let credentials = try await subject.getCredentials() | ||
|
||
XCTAssertEqual("AccessKey123", credentials.accessKey) | ||
XCTAssertEqual("SecretAccessKey123", credentials.secret) | ||
XCTAssertEqual("SessionToken123", credentials.sessionToken) | ||
} | ||
|
||
func testGetCredentialsWithNamedProfileFromConfigFile() async throws { | ||
let subject = try ProcessCredentialsProvider( | ||
profileName: "credentials-process-config-tests-profile", | ||
configFilePath: configPath, | ||
credentialsFilePath: credentialsPath | ||
) | ||
let credentials = try await subject.getCredentials() | ||
|
||
XCTAssertEqual("AccessKey123", credentials.accessKey) | ||
XCTAssertEqual("SecretAccessKey123", credentials.secret) | ||
XCTAssertEqual("SessionToken123", credentials.sessionToken) | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
[default] | ||
aws_access_key_id = access_key_default_config | ||
aws_secret_access_key = secret_default_config | ||
credential_process = echo '{"Version": 1, "AccessKeyId": "AccessKey123", "SecretAccessKey": "SecretAccessKey123", "SessionToken": "SessionToken123","Expiration":"2020-02-25T06:03:31Z"}' | ||
|
||
[profile credentials-provider-config-tests-profile] | ||
aws_access_key_id = access_key_profile_config | ||
aws_secret_access_key = secret_profile_config | ||
|
||
[profile credentials-process-config-tests-profile] | ||
aws_access_key_id = access_key_profile_config | ||
aws_secret_access_key = secret_profile_config | ||
credential_process = echo '{"Version": 1, "AccessKeyId": "AccessKey123", "SecretAccessKey": "SecretAccessKey123", "SessionToken": "SessionToken123","Expiration":"2020-02-25T06:03:31Z"}' |