Skip to content

Commit 01e18e6

Browse files
committed
Adds hyperdbaas bindings
1 parent bd1b5ad commit 01e18e6

5 files changed

Lines changed: 169 additions & 1 deletion

File tree

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright IBM Corporation 2017
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import Foundation
18+
19+
/// HDBaaSCredentials class
20+
///
21+
/// Contains the credentials for a HDBaaS service instance.
22+
public class HyperSecureDBaaSCredentials {
23+
public let uri: String
24+
public let host: String
25+
public let cert: String
26+
public let username: String
27+
public let password: String
28+
public let port: Int
29+
30+
public init(
31+
uri: String,
32+
host: String,
33+
cert: String,
34+
username: String,
35+
password: String,
36+
port: Int) {
37+
38+
self.uri = uri
39+
self.host = host
40+
self.cert = cert
41+
self.username = username
42+
self.password = password
43+
self.port = port
44+
}
45+
}
46+
47+
extension CloudEnv {
48+
49+
/// Returns a HDBaaSCredentials object with the corresponding credentials.
50+
///
51+
/// - Parameter name: The key to lookup the credentials object.
52+
public func getHyperSecureDBaaSCredentials(name: String) -> HyperSecureDBaaSCredentials? {
53+
54+
guard let credentials = getDictionary(name: name) else {
55+
return nil
56+
}
57+
58+
// For detail on the format for the URI connection string that MongoDB supports,
59+
// see: https://docs.mongodb.com/manual/reference/connection-string/
60+
// It is possible to specify more than one host in the URI connection string
61+
62+
guard let uri = credentials["url"] as? String,
63+
let cert = credentials["cert"] as? String else {
64+
return nil
65+
}
66+
67+
let uriItems = uri.components(separatedBy: ",")
68+
let filtered = uriItems.filter({ $0.contains("ssl=true") })
69+
let uriValue: String?
70+
if filtered.count == 1, let dbInfo = filtered.first, var credentialInfo = uriItems.first,
71+
let atRange = credentialInfo.range(of: "@") {
72+
// Substitute non-ssl hostname:port with correct hostname:port
73+
credentialInfo.removeSubrange(atRange.upperBound..<credentialInfo.endIndex)
74+
uriValue = credentialInfo + dbInfo
75+
} else {
76+
uriValue = uriItems.first
77+
}
78+
79+
guard let stringURL = uriValue, stringURL.count > 0,
80+
let url = URL(string: stringURL),
81+
let host = url.host,
82+
let username = url.user,
83+
let password = url.password,
84+
let port = url.port else {
85+
86+
return nil
87+
}
88+
89+
return HyperSecureDBaaSCredentials(
90+
uri: uri,
91+
host: host,
92+
cert: cert,
93+
username: username,
94+
password: password,
95+
port: port)
96+
}
97+
98+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright IBM Corporation 2017
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import XCTest
18+
import Configuration
19+
@testable import CloudEnvironment
20+
21+
class HyperSecureDBaaSTests: XCTestCase {
22+
23+
static var allTests : [(String, (HyperSecureDBaaSTests) -> () throws -> Void)] {
24+
return [
25+
("testGetCredentials", testGetCredentials),
26+
]
27+
}
28+
29+
func testGetCredentials() {
30+
31+
// Load test mappings.json file and Cloud Foundry test credentials-- VCAP_SERVICES and VCAP_APPLICATION
32+
let cloudEnv = CloudEnv(mappingsFilePath: "Tests/CloudEnvironmentTests/resources", cloudFoundryFile: "Tests/CloudEnvironmentTests/resources/config_cf_example.json")
33+
34+
guard let credentials = cloudEnv.getHyperSecureDBaaSCredentials(name: "hypersecuredbaas") else {
35+
XCTFail("Could not load HypersercureDBaaS credentials.")
36+
return
37+
}
38+
39+
XCTAssertEqual(credentials.password, "199b3db8be4c4b2baeb0b460f6e3fa20", "HypersercureDBaaS service password should match.")
40+
XCTAssertEqual(credentials.cert, "-----BEGIN CERTIFICATE-----\nMIIDrzCCApegAwIBAgIQCDvgVpBCRrGhdWrJWZHHSjANBgkqhkiG9w0BAQUFADBh\n-----END CERTIFICATE-----\n", "HypersercureDBaaS service cert should match.")
41+
XCTAssertEqual(credentials.username, "7f49ed06-d822-426f-bda6-76a515884450", "HypersercureDBaaS service port should match.")
42+
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.")
43+
44+
}
45+
46+
}

Tests/CloudEnvironmentTests/resources/config_cf_example.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,20 @@
264264
]
265265
}
266266
],
267+
"hypersecuredbaas": [
268+
{
269+
"label": "hypersecuredbaas",
270+
"name": "hypersecuredbaas",
271+
"plan": "Basic Plan",
272+
"credentials": {
273+
"apikey": "hj4v2352",
274+
"cert": "-----BEGIN CERTIFICATE-----\nMIIDrzCCApegAwIBAgIQCDvgVpBCRrGhdWrJWZHHSjANBgkqhkiG9w0BAQUFADBh\n-----END CERTIFICATE-----\n",
275+
"password": "199b3db8be4c4b2baeb0b460f6e3fa20",
276+
"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",
277+
"userid": "7f49ed06-d822-426f-bda6-76a515884450"
278+
}
279+
}
280+
],
267281
"Object-Storage": [
268282
{
269283
"credentials": {

Tests/CloudEnvironmentTests/resources/mappings.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,5 +158,14 @@
158158
"file:Tests/CloudEnvironmentTests/resources/config_file_with_key.json:CloudFunctions"
159159
]
160160
}
161-
}
161+
},
162+
"hypersecuredbaas": {
163+
"credentials": {
164+
"searchPatterns": [
165+
"cloudfoundry:hypersecuredbaas",
166+
"env:service_hypersecuredbaas",
167+
"file:Tests/CloudEnvironmentTests/resources/config_cf_example.json"
168+
]
169+
}
170+
}
162171
}

Tests/LinuxMain.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ XCTMain([
2424
testCase(CloudantTests.allTests),
2525
testCase(DB2Tests.allTests),
2626
testCase(MongoDBTests.allTests),
27+
testCase(HyperSecureDBaaSTests.allTests),
2728
testCase(MySQLTests.allTests),
2829
testCase(ObjectStorageTests.allTests),
2930
testCase(PostgreSQLTests.allTests),

0 commit comments

Comments
 (0)