|
| 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 | +} |
0 commit comments