Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix promises and surface IAP errors in Composer GCF Dag triggering #776

Merged
merged 1 commit into from
Oct 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 17 additions & 26 deletions functions/composer-storage-trigger/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ const FormData = require('form-data');
* https://cloud.google.com/iap/docs/authentication-howto
*
* @param {!Object} event The Cloud Functions event.
* @param {!Function} callback The callback function.
* @returns {Promise}
*/
exports.triggerDag = function triggerDag (event, callback) {
exports.triggerDag = function triggerDag (event) {
// Fill in your Composer environment information here.

// The project that holds your function
Expand All @@ -50,12 +50,10 @@ exports.triggerDag = function triggerDag (event, callback) {
const BODY = {'conf': JSON.stringify(event.data)};

// Make the request
authorizeIap(CLIENT_ID, PROJECT_ID, USER_AGENT)
return authorizeIap(CLIENT_ID, PROJECT_ID, USER_AGENT)
.then(function iapAuthorizationCallback (iap) {
makeIapPostRequest(WEBSERVER_URL, BODY, iap.idToken, USER_AGENT, iap.jwt);
})
.then(_ => callback(null))
.catch(callback);
return makeIapPostRequest(WEBSERVER_URL, BODY, iap.idToken, USER_AGENT, iap.jwt);
});
};

/**
Expand Down Expand Up @@ -142,24 +140,17 @@ function authorizeIap (clientId, projectId, userAgent) {
* @param {string} jwt A Json web token used to authenticate the request.
*/
function makeIapPostRequest (url, body, idToken, userAgent, jwt) {
var form = new FormData();
form.append('grant_type', 'urn:ietf:params:oauth:grant-type:jwt-bearer');
form.append('assertion', jwt);

return fetch(
url, {
method: 'POST',
body: form
})
.then(function makeIapPostRequestCallback () {
return fetch(url, {
method: 'POST',
headers: {
'User-Agent': userAgent,
'Authorization': `Bearer ${idToken}`
},
body: JSON.stringify(body)
});
});
return fetch(url, {
method: 'POST',
headers: {
'User-Agent': userAgent,
'Authorization': `Bearer ${idToken}`
},
body: JSON.stringify(body)
}).then(function checkIapRequestStatus (res) {
if (!res.ok) {
return res.text().then(body => Promise.reject(body));
}
});
}
// [END composer_trigger]
51 changes: 36 additions & 15 deletions functions/composer-storage-trigger/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,13 @@ const proxyquire = require(`proxyquire`).noCallThru();
const sinon = require(`sinon`);
const test = require(`ava`);

function getSample () {
const bodyJson = {};
const body = {
json: sinon.stub().resolves(bodyJson)
};
const FetchMock = sinon.stub().resolves(body);

function getSample (FetchStub) {
return {
program: proxyquire(`../`, {
'node-fetch': FetchMock
'node-fetch': FetchStub
}),
mocks: {
fetch: FetchMock,
body: body,
bodyJson: bodyJson
fetch: FetchStub
}
};
}
Expand All @@ -45,12 +37,41 @@ test.cb(`Handles error in JSON body`, (t) => {
}
};
const expectedMsg = `Something bad happened.`;
const sample = getSample();
sample.mocks.bodyJson.error = expectedMsg;
const bodyJson = {'error': expectedMsg};
const body = {
json: sinon.stub().resolves(bodyJson)
};
const sample = getSample(sinon.stub().resolves(body));

sample.program.triggerDag(event, (err, message) => {
sample.program.triggerDag(event).catch(function (err) {
t.regex(err, /Something bad happened/);
t.is(message, undefined);
t.end();
});
});

test.cb(`Handles error in IAP response.`, (t) => {
const event = {
data: {
file: `some-file`
}
};
const expectedMsg = 'Default IAP Error Message.';

const serviceAccountAccessTokenRes = {
json: sinon.stub().resolves({'access_token': 'default-access-token'})
};
const signJsonClaimRes = {json: sinon.stub().resolves({'signature': 'default-jwt-signature'})};
const getTokenRes = {json: sinon.stub().resolves({'id_token': 'default-id-token'})};
const makeIapPostRequestRes = {ok: false, text: sinon.stub().resolves(expectedMsg)};
const FetchStub = sinon.stub()
.onCall(0).resolves(serviceAccountAccessTokenRes)
.onCall(1).resolves(signJsonClaimRes)
.onCall(2).resolves(getTokenRes)
.onCall(3).resolves(makeIapPostRequestRes);
const sample = getSample(FetchStub);

sample.program.triggerDag(event).catch(function (err) {
t.is(err, expectedMsg);
t.end();
});
});