Skip to content

Commit

Permalink
Load credentials of the databases-for-postgresql format (#60)
Browse files Browse the repository at this point in the history
* Add loading of CloudEnviroment credentials for PostgreSQL through the `databases-for-postgresql` format of Cloud Foundry.
  • Loading branch information
mbarnach authored and ianpartridge committed Jun 21, 2019
1 parent e704e6d commit 91f4da3
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 8 deletions.
28 changes: 28 additions & 0 deletions Sources/CloudEnvironment/PostgreSQLCredentials.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,17 @@ extension CloudEnv {

/// Returns an PostgreSQLCredentials object with the corresponding credentials.
///
/// This function retrieve the credentials for "Compose for PostgreSQL".
/// If you want to retrieve the credentials for "Databases for PostgreSQL",
/// use [getPostgreSQLDatabaseCredentials](x-source-tag://getPostgreSQLDatabaseCredentials).
/// ### Usage Example: ###
/// ```swift
/// let cloudEnv = CloudEnv()
///
/// credentials = cloudEnv.getPostgreSQLCredentials(name: "PostgreSQLKey")
/// ```
/// - Parameter name: The key to lookup the environment variable.
/// - Tag: getPostgreSQLCredentials
public func getPostgreSQLCredentials(name: String) -> PostgreSQLCredentials? {
guard let credentials = getDictionary(name: name),
let uri = credentials["uri"] as? String else {
Expand All @@ -63,4 +67,28 @@ extension CloudEnv {
return PostgreSQLCredentials(uri: uri)
}

/// Returns an PostgreSQLCredentials object with the corresponding credentials.
///
/// This function retrieve the credentials for "Databases for PostgreSQL".
/// If you want to retrieve the credentials for "Compose for PostgreSQL",
/// use [getPostgreSQLCredentials](x-source-tag://getPostgreSQLCredentials).
/// ### Usage Example: ###
/// ```swift
/// let cloudEnv = CloudEnv()
///
/// credentials = cloudEnv.getPostgreSQLDatabaseCredentials(name: "PostgreSQLKey")
/// ```
/// - Parameter name: The key to lookup the environment variable.
/// - Tag: getPostgreSQLDatabaseCredentials
public func getPostgreSQLDatabaseCredentials(name: String) -> PostgreSQLCredentials? {
guard let credentials = getDictionary(name: name),
let connection = credentials["connection"] as? [String: Any],
let postgres = connection["postgres"] as? [String: Any],
let composed = postgres["composed"] as? [String],
let uri = composed.first else {
return nil
}
return PostgreSQLCredentials(uri: uri)
}

}
21 changes: 20 additions & 1 deletion Tests/CloudEnvironmentTests/PostgreSQLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class PostgreSQLTests: XCTestCase {
static var allTests : [(String, (PostgreSQLTests) -> () throws -> Void)] {
return [
("testGetCredentials", testGetCredentials),
("testGetCredentialsDB", testGetCredentialsDB),
]
}

Expand All @@ -43,5 +44,23 @@ class PostgreSQLTests: XCTestCase {
XCTAssertEqual(credentials.database, "compose", "PostgreSQL service database should match.")

}


func testGetCredentialsDB() {

// 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.getPostgreSQLDatabaseCredentials(name: "PostgreSQLKeyDB") else {
XCTFail("Could not load PostgreSQL credentials.")
return
}

XCTAssertEqual(credentials.host, "123456789.123456789.databases.appdomain.cloud", "PostgreSQL service host should match.")
XCTAssertEqual(credentials.username, "ibm_cloud_1234", "PostgreSQL service username should match.")
XCTAssertEqual(credentials.password, "qwertyuiop", "PostgreSQL service password should match.")
XCTAssertEqual(credentials.port, 12345, "PostgreSQL service port should match.")
XCTAssertEqual(credentials.database, "dbname", "PostgreSQL service database should match.")

}

}
74 changes: 74 additions & 0 deletions Tests/CloudEnvironmentTests/resources/config_cf_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,80 @@
]
}
],
"databases-for-postgresql": [
{
"binding_name": null,
"credentials": {
"connection": {
"cli": {
"arguments": [
[
"host=123456789.123456789.databases.appdomain.cloud port=12345 dbname=dbname user=ibm_cloud_1234 sslmode=verify-full"
]
],
"bin": "psql",
"certificate": {
"certificate_base64": "AAAAA",
"name": "aaaa"
},
"composed": [
"PGPASSWORD=qwerty PGSSLROOTCERT=aaaaaaaa-bbbb-cccc-cccc-dddddddddddd psql 'host=123456789.123456789.databases.appdomain.cloud port=12345 dbname=dbname user=ibm_cloud_1234 sslmode=verify-full'"
],
"environment": {
"PGPASSWORD": "qwertyuiop",
"PGSSLROOTCERT": "aaaaaaaa-bbbb-cccc-cccc-dddddddddddd"
},
"type": "cli"
},
"postgres": {
"authentication": {
"method": "direct",
"password": "qwertyuiop",
"username": "ibm_cloud_1234"
},
"certificate": {
"certificate_base64": "AAAA",
"name": "aaaaaaaa-bbbb-cccc-cccc-dddddddddddd"
},
"composed": [
"postgres://ibm_cloud_1234:[email protected]:12345/dbname?sslmode=verify-full"
],
"database": "dbname",
"hosts": [
{
"hostname": "123456789.123456789.databases.appdomain.cloud",
"port": 12345
}
],
"path": "/dbname",
"query_options": {
"sslmode": "verify-full"
},
"scheme": "postgres",
"type": "uri"
}
},
"instance_administration_api": {
"deployment_id": "crn:v1:bluemix:public:databases-for-postgresql:region:c/1234:1234::",
"instance_id": "crn:v1:bluemix:public:databases-for-postgresql:region:c/1234:1234::",
"root": "https://api.region.databases.cloud.ibm.com/v4/ibm"
}
},
"instance_name": "PostgreSQLServiceDB",
"label": "databases-for-postgresql",
"name": "PostgreSQLServiceDB",
"plan": "standard",
"provider": null,
"syslog_drain_url": null,
"tags": [
"data_management",
"ibm_created",
"rc_compatible",
"ibmcloud-alias"
],
"volume_mounts": []
}
],
"cleardb": [
{
"credentials": {
Expand Down
23 changes: 16 additions & 7 deletions Tests/CloudEnvironmentTests/resources/mappings.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,22 @@
}
},
"PostgreSQLKey": {
"credentials": {
"searchPatterns": [
"cloudfoundry:PostgreSQLService",
"env:my-kube-secret-os-credentials",
"file:Tests/CloudEnvironmentTests/resources/config_cf_example.json"
]
}
"credentials": {
"searchPatterns": [
"cloudfoundry:PostgreSQLService",
"env:my-kube-secret-os-credentials",
"file:Tests/CloudEnvironmentTests/resources/config_cf_example.json"
]
}
},
"PostgreSQLKeyDB": {
"credentials": {
"searchPatterns": [
"cloudfoundry:PostgreSQLServiceDB",
"env:my-kube-secret-os-credentials",
"file:Tests/CloudEnvironmentTests/resources/config_cf_example.json"
]
}
},
"MySQLKey": {
"credentials": {
Expand Down

0 comments on commit 91f4da3

Please sign in to comment.