-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds HyperSecure DBaaS Support (#49)
* Adds hyperdbaas bindings * Rename
- Loading branch information
Showing
5 changed files
with
164 additions
and
1 deletion.
There are no files selected for viewing
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,93 @@ | ||
/* | ||
* Copyright IBM Corporation 2018 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import Foundation | ||
|
||
/// HyperSecureDBaaSCredentials class | ||
/// | ||
/// Contains the credentials for a HyperSecureDBaaS service instance. | ||
public class HyperSecureDBaaSCredentials { | ||
public let uri: String | ||
public let host: String | ||
public let cert: String | ||
public let username: String | ||
public let password: String | ||
public let port: Int | ||
|
||
public init( | ||
uri: String, | ||
host: String, | ||
cert: String, | ||
username: String, | ||
password: String, | ||
port: Int) { | ||
|
||
self.uri = uri | ||
self.host = host | ||
self.cert = cert | ||
self.username = username | ||
self.password = password | ||
self.port = port | ||
} | ||
} | ||
|
||
extension CloudEnv { | ||
|
||
/// Returns a HyperSecureDBaaSCredentials object with the corresponding credentials. | ||
/// | ||
/// - Parameter name: The key to lookup the credentials object. | ||
public func getHyperSecureDBaaSCredentials(name: String) -> HyperSecureDBaaSCredentials? { | ||
|
||
guard let credentials = getDictionary(name: name) else { | ||
return nil | ||
} | ||
|
||
guard let uri = credentials["url"] as? String, | ||
let cert = credentials["cert"] as? String else { | ||
return nil | ||
} | ||
|
||
let uriItems = uri.components(separatedBy: ",") | ||
let filtered = uriItems.filter({ $0.contains("ssl=true") }) | ||
let uriValue: String? | ||
if filtered.count == 1, let dbInfo = filtered.first, var credentialInfo = uriItems.first, | ||
let atRange = credentialInfo.range(of: "@") { | ||
// Substitute non-ssl hostname:port with correct hostname:port | ||
credentialInfo.removeSubrange(atRange.upperBound..<credentialInfo.endIndex) | ||
uriValue = credentialInfo + dbInfo | ||
} else { | ||
uriValue = uriItems.first | ||
} | ||
|
||
guard let stringURL = uriValue, stringURL.count > 0, | ||
let url = URL(string: stringURL), | ||
let host = url.host, | ||
let username = url.user, | ||
let password = url.password, | ||
let port = url.port else { | ||
|
||
return nil | ||
} | ||
|
||
return HyperSecureDBaaSCredentials( | ||
uri: uri, | ||
host: host, | ||
cert: cert, | ||
username: username, | ||
password: password, | ||
port: port) | ||
} | ||
} |
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 IBM Corporation 2018 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import XCTest | ||
import Configuration | ||
@testable import CloudEnvironment | ||
|
||
class HyperSecureDBaaSTests: XCTestCase { | ||
|
||
static var allTests : [(String, (HyperSecureDBaaSTests) -> () throws -> Void)] { | ||
return [ | ||
("testGetCredentials", testGetCredentials), | ||
] | ||
} | ||
|
||
func testGetCredentials() { | ||
|
||
// Load test mappings.json file and Cloud Foundry test credentials-- VCAP_SERVICES and VCAP_APPLICATION | ||
let cloudEnv = CloudEnv(mappingsFilePath: "Tests/CloudEnvironmentTests/resources", cloudFoundryFile: "Tests/CloudEnvironmentTests/resources/config_cf_example.json") | ||
|
||
guard let credentials = cloudEnv.getHyperSecureDBaaSCredentials(name: "hypersecuredbaas") else { | ||
XCTFail("Could not load HyperSercureDBaaS credentials.") | ||
return | ||
} | ||
|
||
XCTAssertEqual(credentials.password, "199b3db8be4c4b2baeb0b460f6e3fa20", "HypersercureDBaaS service password should match.") | ||
XCTAssertEqual(credentials.cert, "-----BEGIN CERTIFICATE-----\nMIIDrzCCApegAwIBAgIQCDvgVpBCRrGhdWrJWZHHSjANBgkqhkiG9w0BAQUFADBh\n-----END CERTIFICATE-----\n", "HypersercureDBaaS service cert should match.") | ||
XCTAssertEqual(credentials.username, "7f49ed06-d822-426f-bda6-76a515884450", "HypersercureDBaaS service port should match.") | ||
XCTAssertEqual(credentials.uri, "mongodb://7f49ed06-d822-426f-bda6-76a515884450:199b3db8be4c4b2baeb0b460f6e3fa20@dbaas08.hypersecuredbaas.ibm.com:20489,dbaas10.hypersecuredbaas.ibm.com:20553,dbaas09.hypersecuredbaas.ibm.com:20229/7f49ed06-d822-426f-bda6-76a515884450?replicaSet=testerclus", "HypersercureDBaaS service uri should match.") | ||
|
||
} | ||
|
||
} |
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