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(): when using s3, parse stream output to buffer for html files #117

Merged
merged 2 commits into from
May 20, 2021
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
2 changes: 1 addition & 1 deletion bin/server
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async function init() {

return logger.info('Server running at %s', server.info.uri);
} catch (err) {
logger.error(err);
logger.error(err.toString());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about JSON.stringify, does JSON.stringify capture many info nosies?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a bug with logger we use winston This is what we should do long term winstonjs/winston#1338 (comment)


return process.exit(1);
}
Expand Down
2 changes: 2 additions & 0 deletions config/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ strategy:
accessKeyId: null
# Amazon secret access key
secretAccessKey: null
# Amazon Session Token from federated credentials
sessionToken: null
# Amazon S3 region
region: YOUR-REGION
# Amazon S3 bucket that you have write access to
Expand Down
5 changes: 5 additions & 0 deletions helpers/aws.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class AwsClient {
* @param {String} config.segment S3 segment
* @param {Integer} config.partSize aws-sdk upload option
* @param {Integer} config.httpTimeout HTTP timeout in ms
* @param {String } config.sessionToken Session token for federated AWS login
*/
constructor(config) {
const options = {
Expand All @@ -31,6 +32,10 @@ class AwsClient {
}
};

if (config.sessionToken) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this added for local debugging only?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, any setup with a federated login, not sure if there is a scope to use this to move away from access key based login.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this, to remove IAM user dependency screwdriver-cd/screwdriver#2454

options.sessionToken = config.sessionToken;
}

if (config.endpoint) {
options.endpoint = config.endpoint;
}
Expand Down
8 changes: 7 additions & 1 deletion helpers/mime.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

const mime = require('mime-types');

const FORCE_EXTENSION_MAPPING = {
yidf: 'txt',
state: 'txt',
diff: 'txt'
};

/**
* getMimeFromFileExtension
* @param {String} fileExtension File extension (e.g. css, txt, html)
* @return {String} text/html
*/
function getMimeFromFileExtension(fileExtension) {
return mime.lookup(fileExtension) || '';
return mime.lookup(FORCE_EXTENSION_MAPPING[fileExtension] || fileExtension) || '';
}

const knownMimes = ['text/css', 'text/javascript', 'image/png', 'image/jpeg', 'application/json',
Expand Down
2 changes: 1 addition & 1 deletion plugins/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ exports.plugin = {
key: pluginOptions.jwtPublicKey,
verifyOptions: {
algorithms: ['RS256'],
maxAge: '12h'
maxAge: '13h'
},
// This function is run once the Token has been decoded with signature
validate
Expand Down
11 changes: 10 additions & 1 deletion plugins/builds.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@ exports.plugin = {

let value;
let response;
let isStreamOutput = false;

// for old json files, the value is hidden in an object, we cannot stream it directly
if (usingS3) {
try {
value = await awsClient.getDownloadStream({ cacheKey: id });
response = h.response(value);
response.headers['content-type'] = 'application/octet-stream';
isStreamOutput = true;
} catch (err) {
request.log([id, 'error'], `Failed to stream the cache: ${err}`);
throw err;
Expand Down Expand Up @@ -104,7 +106,14 @@ exports.plugin = {
`attachment; filename="${encodeURI(fileName)}"`;
} else if (request.query.type === 'preview') {
if (displayableMimes.includes(mime)) {
const $ = cheerio.load(Buffer.from(value));
let htmlContent;

if (isStreamOutput) {
htmlContent = await streamToBuffer(value);
} else {
htmlContent = Buffer.from(value);
}
const $ = cheerio.load(htmlContent);
const scriptNode = `<script>${iframeScript}</script>`;

// inject postMessage into code
Expand Down
8 changes: 8 additions & 0 deletions test/plugins/builds.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ sinon.assert.expose(assert, { prefix: '' });
describe('builds plugin test', () => {
let plugin;
let server;
let configMock;

before(() => {
mockery.enable({
Expand All @@ -23,6 +24,13 @@ describe('builds plugin test', () => {
});

beforeEach(() => {
configMock = {
get: sinon.stub().returns({
plugin: 'memory',
s3: {}
})
};
mockery.registerMock('config', configMock);
// eslint-disable-next-line global-require
plugin = require('../../plugins/builds');

Expand Down