Skip to content

Commit

Permalink
Reading environment variable from C (#257)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianfett authored May 20, 2020
1 parent 1029d42 commit 86301ca
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 35 deletions.
1 change: 0 additions & 1 deletion Sources/AWSSDKSwiftCore/AWSClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 20 additions & 2 deletions Sources/AWSSDKSwiftCore/Credential/Credential.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import INIParser
import struct Foundation.Date
import class Foundation.ProcessInfo
import class Foundation.NSString

extension Credential {
Expand All @@ -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
}

Expand Down Expand Up @@ -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"]
}
}
14 changes: 14 additions & 0 deletions Sources/AWSSDKSwiftCore/Doc/Environment.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
3 changes: 1 addition & 2 deletions Sources/AWSSDKSwiftCore/Doc/Mirror.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
3 changes: 1 addition & 2 deletions Sources/AWSSDKSwiftCore/MetaDataService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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

Expand Down
4 changes: 1 addition & 3 deletions Sources/AWSSDKSwiftCore/ServiceConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
//
//===----------------------------------------------------------------------===//

import class Foundation.ProcessInfo

/// Configuration class defining an AWS service
public class ServiceConfig {

Expand Down Expand Up @@ -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
Expand Down
22 changes: 0 additions & 22 deletions Sources/AWSSignerV4/credentials.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
//
//===----------------------------------------------------------------------===//

import class Foundation.ProcessInfo

/// Protocol for providing credential details for accessing AWS services
public protocol Credential {
var accessKeyId: String {get}
Expand All @@ -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"]
}
}

24 changes: 24 additions & 0 deletions Tests/AWSSDKSwiftCoreTests/Doc/EnvironmentTests.swift
Original file line number Diff line number Diff line change
@@ -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, "")
}
}
2 changes: 1 addition & 1 deletion Tests/AWSSDKSwiftCoreTests/MetaDataServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions Tests/AWSSDKSwiftCoreTests/TestUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
//
//===----------------------------------------------------------------------===//

import AWSSDKSwiftCore
import Foundation
@testable import AWSSDKSwiftCore

@propertyWrapper struct EnvironmentVariable<Value: LosslessStringConvertible> {
var defaultValue: Value
Expand All @@ -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
}
}
Expand Down

0 comments on commit 86301ca

Please sign in to comment.