Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.

Commit 3a83a2f

Browse files
committed
Adding support for IAM keys with Cloudant event source.
1 parent 8b9559d commit 3a83a2f

File tree

3 files changed

+77
-21
lines changed

3 files changed

+77
-21
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1153,13 +1153,19 @@ functions:
11531153
index:
11541154
handler: users.main
11551155
events:
1156-
- cloudant:
1156+
- cloudant: // basic auth example
11571157
host: xxx-yyy-zzz-bluemix.cloudant.com
11581158
username: USERNAME
11591159
password: PASSWORD
11601160
db: db_name
1161+
- cloudant: // iam auth example
1162+
host: xxx-yyy-zzz-bluemix.cloudant.com
1163+
iam_api_key: IAM_API_KEY
1164+
db: db_name
11611165
```
11621166

1167+
`username` and `password` or `iam_api_key` parameters can be used for authentication.
1168+
11631169
### Adding Optional Parameters
11641170

11651171
The following optional feed parameters are also supported:

compile/cloudant/index.js

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
const BbPromise = require('bluebird');
44

5-
const config_properties = ['db', 'password', 'username', 'host']
6-
75
class OpenWhiskCompileCloudant {
86
constructor(serverless, options) {
97
this.serverless = serverless;
@@ -40,13 +38,28 @@ class OpenWhiskCompileCloudant {
4038
}
4139

4240
if (!config.package) {
43-
config_properties.forEach(prop => {
44-
if (!config[prop]) {
45-
throw new this.serverless.classes.Error(
46-
`Cloudant event property (${prop}) missing on function: ${fnName}`
47-
)
48-
}
49-
})
41+
const config_properties = ['db', 'password', 'username', 'host']
42+
43+
if (!config.db) {
44+
throw new this.serverless.classes.Error(
45+
`Cloudant event property (db) missing on function: ${fnName}`
46+
)
47+
}
48+
49+
if (!config.host) {
50+
throw new this.serverless.classes.Error(
51+
`Cloudant event property (host) missing on function: ${fnName}`
52+
)
53+
}
54+
55+
const has_manual_auth = !!config.username && !!config.password
56+
const has_iam_auth = !!config.iam_api_key
57+
if (!has_manual_auth && !has_iam_auth) {
58+
throw new this.serverless.classes.Error(
59+
`Cloudant event authentication property (username & password or iam_api_key) missing on function: ${fnName}`
60+
)
61+
}
62+
5063
}
5164
}
5265

@@ -60,8 +73,12 @@ class OpenWhiskCompileCloudant {
6073
}
6174

6275
if (!config.package) {
63-
feed_parameters.username = config.username
64-
feed_parameters.password = config.password
76+
if (config.iam_api_key) {
77+
feed_parameters.iamApiKey = config.iam_api_key
78+
} else {
79+
feed_parameters.username = config.username
80+
feed_parameters.password = config.password
81+
}
6582
feed_parameters.host = config.host
6683
}
6784

compile/cloudant/tests/index.js

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,32 @@ describe('OpenWhiskCompileCloudant', () => {
8282
.to.throw(Error, 'Cloudant event property (db) missing on function: testing');
8383
})
8484

85-
it('should throw errors for missing mandatory parameters without package', () => {
86-
const config = { db: 'dbname', username: 'user', password: 'password', host: 'hostname' }
85+
it('should throw errors for missing host parameters without package', () => {
86+
const config = { db: 'dbname', username: 'user', password: 'password' }
8787

88-
Object.keys(config).forEach(key => {
89-
const cloned = Object.assign({}, config)
90-
cloned[key] = ''
91-
expect(() => openwhiskCompileCloudant.compileCloudantTrigger('testing', cloned))
92-
.to.throw(Error, `Cloudant event property (${key}) missing on function: testing`);
93-
})
88+
expect(() => openwhiskCompileCloudant.compileCloudantTrigger('testing', config))
89+
.to.throw(Error, `Cloudant event property (host) missing on function: testing`);
90+
})
91+
92+
it('should throw errors for missing username parameter without package', () => {
93+
const config = { db: 'dbname', host: 'host.com', password: 'password' }
94+
95+
expect(() => openwhiskCompileCloudant.compileCloudantTrigger('testing', config))
96+
.to.throw(Error, `Cloudant event authentication property (username & password or iam_api_key) missing on function: testing`);
97+
})
98+
99+
it('should throw errors for missing password parameter without package', () => {
100+
const config = { db: 'dbname', host: 'host.com', username: 'username' }
101+
102+
expect(() => openwhiskCompileCloudant.compileCloudantTrigger('testing', config))
103+
.to.throw(Error, `Cloudant event authentication property (username & password or iam_api_key) missing on function: testing`);
104+
})
105+
106+
it('should throw errors for missing authentication parameters without package', () => {
107+
const config = { db: 'dbname', host: 'host.com' }
108+
109+
expect(() => openwhiskCompileCloudant.compileCloudantTrigger('testing', config))
110+
.to.throw(Error, `Cloudant event authentication property (username & password or iam_api_key) missing on function: testing`);
94111
})
95112

96113
it('should return trigger for cloudant provider using package.', () => {
@@ -107,7 +124,7 @@ describe('OpenWhiskCompileCloudant', () => {
107124
})
108125
})
109126

110-
it('should return trigger for cloudant provider with manual configuration.', () => {
127+
it('should return trigger for cloudant provider with manual username & password configuration.', () => {
111128
const config = { db: 'dbname', username: 'user', password: 'password', host: 'hostname' }
112129
const trigger = openwhiskCompileCloudant.compileCloudantTrigger('testing', config)
113130
expect(trigger).to.be.deep.equal({
@@ -124,6 +141,22 @@ describe('OpenWhiskCompileCloudant', () => {
124141
})
125142
})
126143

144+
it('should return trigger for cloudant provider with manual iam api key configuration.', () => {
145+
const config = { db: 'dbname', iam_api_key: 'api_key', host: 'hostname' }
146+
const trigger = openwhiskCompileCloudant.compileCloudantTrigger('testing', config)
147+
expect(trigger).to.be.deep.equal({
148+
name: `${serverless.service.service}_testing_cloudant_${config.db}`,
149+
content: {
150+
feed: `/whisk.system/cloudant/changes`,
151+
feed_parameters: {
152+
iamApiKey: config.iam_api_key,
153+
host: config.host,
154+
dbname: config.db
155+
}
156+
}
157+
})
158+
})
159+
127160
it('should return trigger with optional configuration parameters.', () => {
128161
const config = { db: 'dbname', username: 'user', password: 'password', host: 'hostname', max: 10000, query: { key: 'value' }, filter: 'some/view' }
129162
const trigger = openwhiskCompileCloudant.compileCloudantTrigger('testing', config)

0 commit comments

Comments
 (0)