From 86301ca530cf123a132c021cba8cd5e9b728e46d Mon Sep 17 00:00:00 2001 From: Fabian Fett Date: Wed, 20 May 2020 14:25:47 +0200 Subject: [PATCH] Reading environment variable from C (#257) --- Sources/AWSSDKSwiftCore/AWSClient.swift | 1 - .../Credential/Credential.swift | 22 +++++++++++++++-- Sources/AWSSDKSwiftCore/Doc/Environment.swift | 14 +++++++++++ Sources/AWSSDKSwiftCore/Doc/Mirror.swift | 3 +-- Sources/AWSSDKSwiftCore/MetaDataService.swift | 3 +-- Sources/AWSSDKSwiftCore/ServiceConfig.swift | 4 +--- Sources/AWSSignerV4/credentials.swift | 22 ----------------- .../Doc/EnvironmentTests.swift | 24 +++++++++++++++++++ .../MetaDataServiceTests.swift | 2 +- Tests/AWSSDKSwiftCoreTests/TestUtils.swift | 4 ++-- 10 files changed, 64 insertions(+), 35 deletions(-) create mode 100644 Sources/AWSSDKSwiftCore/Doc/Environment.swift create mode 100644 Tests/AWSSDKSwiftCoreTests/Doc/EnvironmentTests.swift diff --git a/Sources/AWSSDKSwiftCore/AWSClient.swift b/Sources/AWSSDKSwiftCore/AWSClient.swift index 7d9f5157c..cd78c1192 100644 --- a/Sources/AWSSDKSwiftCore/AWSClient.swift +++ b/Sources/AWSSDKSwiftCore/AWSClient.swift @@ -18,7 +18,6 @@ import Metrics import NIO import NIOHTTP1 import NIOTransportServices -import class Foundation.ProcessInfo import class Foundation.JSONSerialization import class Foundation.JSONDecoder import struct Foundation.Data diff --git a/Sources/AWSSDKSwiftCore/Credential/Credential.swift b/Sources/AWSSDKSwiftCore/Credential/Credential.swift index e9ffc49cc..c349fb2b5 100644 --- a/Sources/AWSSDKSwiftCore/Credential/Credential.swift +++ b/Sources/AWSSDKSwiftCore/Credential/Credential.swift @@ -14,7 +14,6 @@ import INIParser import struct Foundation.Date -import class Foundation.ProcessInfo import class Foundation.NSString extension Credential { @@ -33,7 +32,7 @@ public struct ExpiringCredential: Credential { public init(accessKeyId: String, secretAccessKey: String, sessionToken: String? = nil, expiration: Date? = nil) { self.accessKeyId = accessKeyId self.secretAccessKey = secretAccessKey - self.sessionToken = sessionToken ?? ProcessInfo.processInfo.environment["AWS_SESSION_TOKEN"] + self.sessionToken = sessionToken ?? Environment["AWS_SESSION_TOKEN"] self.expiration = expiration } @@ -111,3 +110,22 @@ public struct SharedCredential: Credential { self.sessionToken = config["aws_session_token"] } } + +/// environment variable version of credential that uses system environment variables to get credential details +public struct EnvironmentCredential: Credential { + public let accessKeyId: String + public let secretAccessKey: String + public let sessionToken: String? + + public init?() { + guard let accessKeyId = Environment["AWS_ACCESS_KEY_ID"] else { + return nil + } + guard let secretAccessKey = Environment["AWS_SECRET_ACCESS_KEY"] else { + return nil + } + self.accessKeyId = accessKeyId + self.secretAccessKey = secretAccessKey + self.sessionToken = Environment["AWS_SESSION_TOKEN"] + } +} diff --git a/Sources/AWSSDKSwiftCore/Doc/Environment.swift b/Sources/AWSSDKSwiftCore/Doc/Environment.swift new file mode 100644 index 000000000..6132cc1ee --- /dev/null +++ b/Sources/AWSSDKSwiftCore/Doc/Environment.swift @@ -0,0 +1,14 @@ +#if os(Linux) +import Glibc +#else +import Darwin.C +#endif + +internal enum Environment { + internal static subscript(_ name: String) -> String? { + guard let value = getenv(name) else { + return nil + } + return String(cString: value) + } +} diff --git a/Sources/AWSSDKSwiftCore/Doc/Mirror.swift b/Sources/AWSSDKSwiftCore/Doc/Mirror.swift index a640c81c2..e49537a5f 100644 --- a/Sources/AWSSDKSwiftCore/Doc/Mirror.swift +++ b/Sources/AWSSDKSwiftCore/Doc/Mirror.swift @@ -12,8 +12,7 @@ // //===----------------------------------------------------------------------===// -func unwrap(_ any: Any) -> Any? -{ +func unwrap(_ any: Any) -> Any? { let mirror = Mirror(reflecting: any) guard mirror.displayStyle == .optional else { return any } guard let first = mirror.children.first else { return nil } diff --git a/Sources/AWSSDKSwiftCore/MetaDataService.swift b/Sources/AWSSDKSwiftCore/MetaDataService.swift index 682ee099b..9fdcfac98 100644 --- a/Sources/AWSSDKSwiftCore/MetaDataService.swift +++ b/Sources/AWSSDKSwiftCore/MetaDataService.swift @@ -25,7 +25,6 @@ import struct Foundation.TimeInterval import struct Foundation.TimeZone import struct Foundation.Locale import class Foundation.JSONDecoder -import class Foundation.ProcessInfo /// errors returned by metadata service enum MetaDataServiceError: Error { @@ -120,7 +119,7 @@ struct ECSMetaDataServiceProvider: MetaDataServiceProvider { typealias MetaData = ECSMetaData - static var containerCredentialsUri = ProcessInfo.processInfo.environment["AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"] + static var containerCredentialsUri = Environment["AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"] static var host = "169.254.170.2" var url: String diff --git a/Sources/AWSSDKSwiftCore/ServiceConfig.swift b/Sources/AWSSDKSwiftCore/ServiceConfig.swift index 3f48cd650..389a7b684 100644 --- a/Sources/AWSSDKSwiftCore/ServiceConfig.swift +++ b/Sources/AWSSDKSwiftCore/ServiceConfig.swift @@ -12,8 +12,6 @@ // //===----------------------------------------------------------------------===// -import class Foundation.ProcessInfo - /// Configuration class defining an AWS service public class ServiceConfig { @@ -72,7 +70,7 @@ public class ServiceConfig { partition = region.partition } else if let partitionEndpoint = partitionEndpoints[partition] { self.region = partitionEndpoint.region - } else if let defaultRegion = ProcessInfo.processInfo.environment["AWS_DEFAULT_REGION"] { + } else if let defaultRegion = Environment["AWS_DEFAULT_REGION"] { self.region = Region(rawValue: defaultRegion) } else { self.region = .useast1 diff --git a/Sources/AWSSignerV4/credentials.swift b/Sources/AWSSignerV4/credentials.swift index 49c9e908e..cfee2a107 100644 --- a/Sources/AWSSignerV4/credentials.swift +++ b/Sources/AWSSignerV4/credentials.swift @@ -12,8 +12,6 @@ // //===----------------------------------------------------------------------===// -import class Foundation.ProcessInfo - /// Protocol for providing credential details for accessing AWS services public protocol Credential { var accessKeyId: String {get} @@ -33,23 +31,3 @@ public struct StaticCredential: Credential { self.sessionToken = sessionToken } } - -/// environment variable version of credential that uses system environment variables to get credential details -public struct EnvironmentCredential: Credential { - public let accessKeyId: String - public let secretAccessKey: String - public let sessionToken: String? - - public init?() { - guard let accessKeyId = ProcessInfo.processInfo.environment["AWS_ACCESS_KEY_ID"] else { - return nil - } - guard let secretAccessKey = ProcessInfo.processInfo.environment["AWS_SECRET_ACCESS_KEY"] else { - return nil - } - self.accessKeyId = accessKeyId - self.secretAccessKey = secretAccessKey - self.sessionToken = ProcessInfo.processInfo.environment["AWS_SESSION_TOKEN"] - } -} - diff --git a/Tests/AWSSDKSwiftCoreTests/Doc/EnvironmentTests.swift b/Tests/AWSSDKSwiftCoreTests/Doc/EnvironmentTests.swift new file mode 100644 index 000000000..fcd89c151 --- /dev/null +++ b/Tests/AWSSDKSwiftCoreTests/Doc/EnvironmentTests.swift @@ -0,0 +1,24 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the AWSSDKSwift open source project +// +// Copyright (c) 2017-2020 the AWSSDKSwift project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of AWSSDKSwift project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +import XCTest +@testable import AWSSDKSwiftCore + +class EnvironmentTests: XCTestCase { + func testGetPathFromEnvironment() { + let path = Environment["PATH"] + XCTAssertNotNil(path) + XCTAssertNotEqual(path, "") + } +} diff --git a/Tests/AWSSDKSwiftCoreTests/MetaDataServiceTests.swift b/Tests/AWSSDKSwiftCoreTests/MetaDataServiceTests.swift index 7881809a8..bce01eed6 100644 --- a/Tests/AWSSDKSwiftCoreTests/MetaDataServiceTests.swift +++ b/Tests/AWSSDKSwiftCoreTests/MetaDataServiceTests.swift @@ -64,7 +64,7 @@ class MetaDataServiceTests: XCTestCase { // Disabling cannot guarantee that 169.254.169.254 is not a valid IP on another network func testMetaDataGetCredential() { - if ProcessInfo.processInfo.environment["TEST_EC2_METADATA"] != nil { + if Environment["TEST_EC2_METADATA"] != nil { do { let httpClient = HTTPClient(eventLoopGroupProvider: .createNew) _ = try MetaDataService.getCredential(httpClient: httpClient, on: httpClient.eventLoopGroup.next()).wait() diff --git a/Tests/AWSSDKSwiftCoreTests/TestUtils.swift b/Tests/AWSSDKSwiftCoreTests/TestUtils.swift index b441194a8..e01a56b44 100644 --- a/Tests/AWSSDKSwiftCoreTests/TestUtils.swift +++ b/Tests/AWSSDKSwiftCoreTests/TestUtils.swift @@ -12,8 +12,8 @@ // //===----------------------------------------------------------------------===// -import AWSSDKSwiftCore import Foundation +@testable import AWSSDKSwiftCore @propertyWrapper struct EnvironmentVariable { var defaultValue: Value @@ -26,7 +26,7 @@ import Foundation public var wrappedValue: Value { get { - guard let value = ProcessInfo.processInfo.environment[variableName] else { return defaultValue } + guard let value = Environment[variableName] else { return defaultValue } return Value(value) ?? defaultValue } }