Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add SSO credentials provider #1084

Merged
merged 13 commits into from
Aug 25, 2023
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 AwsCommonRuntimeKit
import ClientRuntime
import Foundation
sichanyoo marked this conversation as resolved.
Show resolved Hide resolved

/// A credentials provider that sources credentials using GetRoleCredentialsRequest to the AWS Single Sign-On Service to maintain short-lived sessions.
/// [Details link](https://docs.aws.amazon.com/sdkref/latest/guide/feature-sso-credentials.html)
public struct SSOCredentialsProvider: CredentialsSourcedByCRT {
let crtCredentialsProvider: CRTCredentialsProvider

/// - 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: .sso(
bootstrap: SDKDefaultIO.shared.clientBootstrap,
tlsContext: SDKDefaultIO.shared.tlsContext,
fileBasedConfiguration: fileBasedConfig,
profileFileNameOverride: profileName
))
}
}
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 ClientRuntime
import Foundation
import XCTest

@_spi(FileBasedConfig) @testable import AWSClientRuntime

class SSOCredentialsProviderTests: XCTestCase {
let configPath = Bundle.module.path(forResource: "sso_tests", ofType: nil)!
let credentialsPath = Bundle.module.path(forResource: "credentials", ofType: nil)!

func testCreateCredentialsProviderSSOLegacyProfile() async throws {
let provider = try SSOCredentialsProvider(
profileName: "user",
configFilePath: configPath,
credentialsFilePath: credentialsPath)
XCTAssertNotNil(provider)
sichanyoo marked this conversation as resolved.
Show resolved Hide resolved
}

func testCreateCredentialsProviderSSOTokenProviderProfile() async throws {
let provider = try SSOCredentialsProvider(
profileName: "dev",
configFilePath: configPath,
credentialsFilePath: credentialsPath)
XCTAssertNotNil(provider)
}


// TODO: add integration tests that automatically test that SSO crednetials provider correctly exchanges SSO token for temporary AWS credentails.
// End-to-end manual testing confirmed SSOCredentialsProvider works as of 081723.
}
17 changes: 17 additions & 0 deletions Tests/Core/AWSClientRuntimeTests/Resources/sso_tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[profile dev]
sso_session = my-sso
sso_account_id = 111122223333
sso_role_name = SampleRole

[sso-session my-sso]
sso_region = us-east-1
sso_start_url = https://my-sso-portal.awsapps.com/start
sso_registration_scopes = sso:account:access

[profile user]
aws_access_key_id = example_access_key_id
aws_secret_access_key = example_secret_access_key
sso_start_url = https://d-test.awsapps.com/start
sso_region = us-west-2
sso_account_id = 12345
sso_role_name = roleName
Loading