Skip to content

Commit

Permalink
Adds HyperSecure DBaaS Support (#49)
Browse files Browse the repository at this point in the history
* Adds hyperdbaas bindings

* Rename
  • Loading branch information
sandmman authored Mar 14, 2018
1 parent bd1b5ad commit 4b50eee
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 1 deletion.
93 changes: 93 additions & 0 deletions Sources/CloudEnvironment/HDBaaSCredentials.swift
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)
}
}
46 changes: 46 additions & 0 deletions Tests/CloudEnvironmentTests/HyperSecureDBaaSTests.swift
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.")

}

}
14 changes: 14 additions & 0 deletions Tests/CloudEnvironmentTests/resources/config_cf_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,20 @@
]
}
],
"hypersecuredbaas": [
{
"label": "hypersecuredbaas",
"name": "hypersecuredbaas",
"plan": "Basic Plan",
"credentials": {
"apikey": "hj4v2352",
"cert": "-----BEGIN CERTIFICATE-----\nMIIDrzCCApegAwIBAgIQCDvgVpBCRrGhdWrJWZHHSjANBgkqhkiG9w0BAQUFADBh\n-----END CERTIFICATE-----\n",
"password": "199b3db8be4c4b2baeb0b460f6e3fa20",
"url": "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",
"userid": "7f49ed06-d822-426f-bda6-76a515884450"
}
}
],
"Object-Storage": [
{
"credentials": {
Expand Down
11 changes: 10 additions & 1 deletion Tests/CloudEnvironmentTests/resources/mappings.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,14 @@
"file:Tests/CloudEnvironmentTests/resources/config_file_with_key.json:CloudFunctions"
]
}
}
},
"hypersecuredbaas": {
"credentials": {
"searchPatterns": [
"cloudfoundry:hypersecuredbaas",
"env:service_hypersecuredbaas",
"file:Tests/CloudEnvironmentTests/resources/config_cf_example.json"
]
}
}
}
1 change: 1 addition & 0 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ XCTMain([
testCase(CloudantTests.allTests),
testCase(DB2Tests.allTests),
testCase(MongoDBTests.allTests),
testCase(HyperSecureDBaaSTests.allTests),
testCase(MySQLTests.allTests),
testCase(ObjectStorageTests.allTests),
testCase(PostgreSQLTests.allTests),
Expand Down

0 comments on commit 4b50eee

Please sign in to comment.