Skip to content

Commit 3a063d7

Browse files
committed
fix: support mTLS in 1.0 Binary and Structured emitters
This commit modifies both of the 1.0 emitters so that they may accept typed objects as a part of the configuration. When using mTLS in Node, you need to provide an `Agent` to the underlying HTTP handler. In this case, Axios will pass this object along to Node.js when it is provided. Fixes: cloudevents#48 Signed-off-by: Lance Ball <lball@redhat.com>
1 parent 4a199d4 commit 3a063d7

File tree

4 files changed

+72
-57
lines changed

4 files changed

+72
-57
lines changed

Diff for: lib/bindings/http/emitter_binary.js

+18-28
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,48 @@ var axios = require("axios");
22
var empty = require("is-empty");
33

44
const Constants = require("./constants.js");
5+
const defaults = {};
6+
defaults[Constants.HEADERS] = {};
7+
defaults[Constants.HEADERS][Constants.HEADER_CONTENT_TYPE] = Constants.DEFAULT_CONTENT_TYPE;
58

69
function BinaryHTTPEmitter(config, headerByGetter, extensionPrefix){
7-
this.config = JSON.parse(JSON.stringify(config));
10+
this.config = Object.assign({}, defaults, config);
811
this.headerByGetter = headerByGetter;
912
this.extensionPrefix = extensionPrefix;
10-
11-
this.config[Constants.HEADERS] =
12-
(!this.config[Constants.HEADERS]
13-
? {}
14-
: this.config[Constants.HEADERS]);
15-
16-
// default is json
17-
if(!this.config[Constants.HEADERS][Constants.HEADER_CONTENT_TYPE]){
18-
this.config[Constants.HEADERS][Constants.HEADER_CONTENT_TYPE] =
19-
Constants.DEFAULT_CONTENT_TYPE;
20-
}
2113
}
2214

23-
BinaryHTTPEmitter.prototype.emit = function(cloudevent) {
24-
// Create new request object
25-
var _config = JSON.parse(JSON.stringify(this.config));
26-
27-
// Always set stuff in _config
28-
var _headers = _config[Constants.HEADERS];
15+
BinaryHTTPEmitter.prototype.emit = function (cloudevent) {
16+
const config = Object.assign({}, this.config);
17+
const headers = Object.assign({}, this.config[Constants.HEADERS]);
2918

3019
Object.keys(this.headerByGetter)
3120
.filter((getter) => cloudevent[getter]())
3221
.forEach((getter) => {
33-
let header = this.headerByGetter[getter];
34-
_headers[header.name] =
22+
const header = this.headerByGetter[getter];
23+
headers[header.name] =
3524
header.parser(
3625
cloudevent[getter]()
3726
);
3827
});
3928

4029
// Set the cloudevent payload
41-
let formatted = cloudevent.format();
30+
const formatted = cloudevent.format();
4231
let data = formatted.data;
4332
data = (formatted.data_base64 ? formatted.data_base64: data);
4433

45-
_config[Constants.DATA_ATTRIBUTE] = data;
46-
4734
// Have extensions?
48-
var exts = cloudevent.getExtensions();
35+
const exts = cloudevent.getExtensions();
4936
Object.keys(exts)
5037
.filter((ext) => Object.hasOwnProperty.call(exts, ext))
5138
.forEach((ext) => {
52-
_headers[this.extensionPrefix + ext] = exts[ext];
39+
headers[this.extensionPrefix + ext] = exts[ext];
5340
});
5441

55-
// Return the Promise
56-
return axios.request(_config);
57-
};
42+
config[Constants.DATA_ATTRIBUTE] = data;
43+
config.headers = headers;
44+
45+
// Return the Promise
46+
return axios.request(config);
47+
};
5848

5949
module.exports = BinaryHTTPEmitter;

Diff for: lib/bindings/http/emitter_structured.js

+10-17
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,23 @@
11
var axios = require("axios");
22

33
const Constants = require("./constants.js");
4+
const defaults = {};
5+
defaults[Constants.HEADERS] = {};
6+
defaults[Constants.HEADERS][Constants.HEADER_CONTENT_TYPE] = Constants.DEFAULT_CE_CONTENT_TYPE;
47

58
function StructuredHTTPEmitter(configuration){
6-
this.config = JSON.parse(JSON.stringify(configuration));
7-
8-
this.config[Constants.HEADERS] =
9-
(!this.config[Constants.HEADERS]
10-
? {}
11-
: this.config[Constants.HEADERS]);
12-
13-
if(!this.config[Constants.HEADERS][Constants.HEADER_CONTENT_TYPE]){
14-
this.config[Constants.HEADERS][Constants.HEADER_CONTENT_TYPE] =
15-
Constants.DEFAULT_CE_CONTENT_TYPE;
16-
}
9+
this.config = Object.assign({}, defaults, configuration);
1710
}
1811

19-
StructuredHTTPEmitter.prototype.emit = function(cloudevent) {
20-
// Create new request object
21-
var _config = JSON.parse(JSON.stringify(this.config));
22-
12+
StructuredHTTPEmitter.prototype.emit = function (cloudevent) {
2313
// Set the cloudevent payload
24-
_config[Constants.DATA_ATTRIBUTE] = cloudevent.format();
14+
this.config[Constants.DATA_ATTRIBUTE] = cloudevent.format();
2515

2616
// Return the Promise
27-
return axios.request(_config);
17+
return axios.request(this.config).then(response => {
18+
delete this.config[Constants.DATA_ATTRIBUTE];
19+
return response;
20+
});
2821
};
2922

3023
module.exports = StructuredHTTPEmitter;

Diff for: test/http_binding_1.js

+42-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
const expect = require("chai").expect;
22
const nock = require("nock");
3-
const http = require("http");
4-
const request = require("request");
3+
const https = require("https");
54
const {asBase64} = require("../lib/utils/fun.js");
65

7-
const BinaryHTTPEmitter =
8-
require("../lib/bindings/http/emitter_binary_1.js");
9-
const Cloudevent = require("../lib/cloudevent.js");
10-
11-
const v1 = require("../v1/index.js");
6+
const {
7+
Spec,
8+
BinaryHTTPEmitter,
9+
StructuredHTTPEmitter,
10+
Cloudevent
11+
} = require("../v1/index.js");
1212

1313
const type = "com.github.pull.create";
1414
const source = "urn:event:from:myapi/resourse/123";
@@ -28,7 +28,7 @@ const ext2Name = "extension2";
2828
const ext2Value = "acme";
2929

3030
const cloudevent =
31-
new Cloudevent(v1.Spec)
31+
new Cloudevent(Spec)
3232
.type(type)
3333
.source(source)
3434
.dataContentType(ceContentType)
@@ -48,7 +48,7 @@ const httpcfg = {
4848
};
4949

5050
const binary = new BinaryHTTPEmitter(httpcfg);
51-
const structured = new v1.StructuredHTTPEmitter(httpcfg);
51+
const structured = new StructuredHTTPEmitter(httpcfg);
5252

5353
describe("HTTP Transport Binding - Version 1.0", () => {
5454
beforeEach(() => {
@@ -59,6 +59,21 @@ describe("HTTP Transport Binding - Version 1.0", () => {
5959
});
6060

6161
describe("Structured", () => {
62+
it('works with mTLS authentication', () => {
63+
const event = new StructuredHTTPEmitter({
64+
method: 'POST',
65+
url: `${webhook}/json`,
66+
httpsAgent: new https.Agent({
67+
cert: 'some value',
68+
key: 'other value'
69+
})
70+
})
71+
return event.emit(cloudevent).then(response => {
72+
expect(response.config.headers['Content-Type'])
73+
.to.equal(contentType);
74+
});
75+
});
76+
6277
describe("JSON Format", () => {
6378
it("requires '" + contentType + "' Content-Type in the header", () => {
6479
return structured.emit(cloudevent)
@@ -81,7 +96,7 @@ describe("HTTP Transport Binding - Version 1.0", () => {
8196
let bindata = Uint32Array.from(dataString, (c) => c.codePointAt(0));
8297
let expected = asBase64(bindata);
8398
let binevent =
84-
new Cloudevent(v1.Spec)
99+
new Cloudevent(Spec)
85100
.type(type)
86101
.source(source)
87102
.dataContentType("text/plain")
@@ -98,7 +113,7 @@ describe("HTTP Transport Binding - Version 1.0", () => {
98113

99114
it("the payload must have 'data_base64' when data is binary", () => {
100115
let binevent =
101-
new Cloudevent(v1.Spec)
116+
new Cloudevent(Spec)
102117
.type(type)
103118
.source(source)
104119
.dataContentType("text/plain")
@@ -117,6 +132,21 @@ describe("HTTP Transport Binding - Version 1.0", () => {
117132
});
118133

119134
describe("Binary", () => {
135+
it('works with mTLS authentication', () => {
136+
const event = new BinaryHTTPEmitter({
137+
method: 'POST',
138+
url: `${webhook}/json`,
139+
httpsAgent: new https.Agent({
140+
cert: 'some value',
141+
key: 'other value'
142+
})
143+
})
144+
return event.emit(cloudevent).then(response => {
145+
expect(response.config.headers['Content-Type'])
146+
.to.equal(cloudevent.getDataContentType());
147+
});
148+
});
149+
120150
describe("JSON Format", () => {
121151
it("requires '" + cloudevent.getDataContentType() + "' Content-Type in the header", () => {
122152
return binary.emit(cloudevent)
@@ -138,7 +168,7 @@ describe("HTTP Transport Binding - Version 1.0", () => {
138168
let bindata = Uint32Array.from(dataString, (c) => c.codePointAt(0));
139169
let expected = asBase64(bindata);
140170
let binevent =
141-
new Cloudevent(v1.Spec)
171+
new Cloudevent(Spec)
142172
.type(type)
143173
.source(source)
144174
.dataContentType("text/plain")

Diff for: v1/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@ module.exports = {
2222
BinaryHTTPEmitter,
2323
StructuredHTTPReceiver,
2424
BinaryHTTPReceiver,
25+
Cloudevent: event,
26+
CloudEvent: event,
2527
event
2628
};

0 commit comments

Comments
 (0)