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: supporting custom endpoints for onprem instances in addition to … #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
79 changes: 52 additions & 27 deletions src/js/background.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const snykurl = 'snyk.io';
var snykurl = 'snyk.io';
var apiToken = '';

const browser = window.msBrowser || window.browser || window.chrome;

Expand Down Expand Up @@ -53,55 +54,79 @@ browser.tabs.onUpdated.addListener((tabId, changeInfo) => {

browser.runtime.onMessage.addListener( (request, sender, sendResponse) => {
if (request.source === 'getsnykurl') {
sendResponse({ url: snykurl });
sendResponse({ url: snykurl, apiToken });
console.log('getting '+snykurl);
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's try to avoid committing any further console.logs into the codebase

} else if (request.source === 'snykurl') {
snykurl = request.url;
console.log('saving '+request.url);
snykurl = request.url || 'snyk.io';

var connectionTimeout = setTimeout(() => {
sendResponse({ status: 'fail' });
sendResponse({ status: 'Fail to connect to '+snykurl + '. Defaulting to snyk.io.' });
snykurl = 'snyk.io';
return;
}, 3000);

fetch('https://'+snykurl +'/')
.then(
(response) => {
if (response.status !== 200) {
sendResponse({ status: 'fail' });
snykurl = 'snyk.io';
if (!request.url || !request.apiToken) {
snykurl = 'snyk.io';
apiToken = '';
sendResponse({ ok: true, status: 'Cleared !' });
} else {
fetch('https://'+snykurl +'/api/v1/',
{ headers: {
'Authorization': 'token '+ request.apiToken,
},
})
.then(
(response) => {
if (response.status !== 200) {
sendResponse({ ok: false, status: 'Fail to connect '+response.status });
snykurl = 'snyk.io';
return;
}
// console.log("success");
clearTimeout(connectionTimeout);
snykurl = snykurl;
apiToken = request.apiToken;
sendResponse({ ok: true, status: 'success' });
return;
}
// console.log("success");
clearTimeout(connectionTimeout);
snykurl = snykurl;
sendResponse({ status: 'success' });

return;

}
)
.catch((err) => {
console.log(err);
// sendResponse({status: "fail"});
});
}
)
.catch((err) => {
console.log(err);
sendResponse({ ok: false, status: 'fail' });
});
}
return true;

} else {
const badgeRequest = fetch('https://us-central1-snyk-browser-extension.cloudfunctions.net/badge', {
var endpoint = 'https://us-central1-snyk-browser-extension.cloudfunctions.net/badge';
var url = 'https://'+snykurl + request.testPath + '/badge.svg';
var options = {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
body: JSON.stringify({
url: request.testPath + '/badge.svg',
url,
}),
});
};

if (snykurl !== 'snyk.io') {
endpoint = url; // Endpoint is same as url in onprem scenarios
options = {
headers: {
'access-control-allow-origin': '*',
},
};
}
const badgeRequest = fetch(endpoint, options);

badgeRequest
.then((response) => {
return response.text();
})
.then((response) => {
console.log(response);
const parse = new DOMParser();
const doc = parse.parseFromString(response, 'image/svg+xml');
const nbOfVuln = parseInt(doc.querySelectorAll('text')[3].innerHTML, 10);
Expand Down
2 changes: 1 addition & 1 deletion src/js/content/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ if (parsedUrl && parsedUrl[1] && parsedUrl[2] && parsedUrl[3] === '') {
const githubOwner = parsedUrl[1];
const githubRepo = parsedUrl[2];
const packageName = githubOwner + '/' + githubRepo;
const testPath = `https://snyk.io/test/github/${packageName}`;
const testPath = `/test/github/${packageName}`;

chrome.runtime.sendMessage({
source: 'github',
Expand Down
2 changes: 1 addition & 1 deletion src/js/content/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
function processNpmPackage() {
const packageName = document.location.pathname.split('/')[2];
const packageVersion = document.location.pathname.split('/')[4] || 'latest';
const testPath = `https://snyk.io/test/npm/${packageName}/${packageVersion}`;
const testPath = `/test/npm/${packageName}/${packageVersion}`;

chrome.runtime.sendMessage({
source: 'npm',
Expand Down
2 changes: 1 addition & 1 deletion src/js/content/yarn.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const packageName = document.location.pathname.split('/package/')[1];
const $readme = document.getElementById('readme');
const $anchor = document.createElement('a');
const testPath = `https://snyk.io/test/npm/${packageName}`;
const testPath = `/test/npm/${packageName}`;

$anchor.setAttribute('href', testPath);
$anchor.innerHTML = `<div class="m-2"><img
Expand Down
17 changes: 14 additions & 3 deletions src/js/popup.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
// Copyright (c) 2018 Antoine Arlaud, Snyk Ltd. All rights reserved.
var snykurl = 'snyk.io';
var token = '';
document.addEventListener('DOMContentLoaded', () => {

chrome.runtime.sendMessage({ source: 'getsnykurl' }, (response) => {
document.getElementById('url').value = response.url;
if (response && response.url && response.apiToken) {
snykurl = response.url;
token = response.apiToken? response.apiToken : '';
}
document.getElementById('url').value = snykurl;
document.getElementById('token').value = token;
});

document.getElementById('save').addEventListener('click', () => {
var url = document.getElementById('url').value;
chrome.runtime.sendMessage({ source: 'snykurl', url }, (response) => {
var apiToken = document.getElementById('token').value;
chrome.runtime.sendMessage({ source: 'snykurl', url, apiToken }, (response) => {
document.getElementById('status').textContent = response.status;
if (response.ok) {
snykurl = url;
token = apiToken;
}
});
});
});
4 changes: 3 additions & 1 deletion src/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ <h1>Snyk Extension</h1>
<div id="snykurl">
<div class="input-label" >Snyk instance url</div>
<div><input id="url" type="text" placeholder="snyk.io" value="snyk.io"></a></div>
<div class="input-label" >Snyk API Token</div>
<div><input id="token" type="password" value=""></a></div>
<div><button id="save">save</button></div>
<div id="status"></div>
</div>
<div id="status"></div>
</div>
</body>
</html>