Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Commit

Permalink
Patch lest encrypt integration.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandresobolevski committed Mar 10, 2017
1 parent abf60d3 commit 8cffd79
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/oauth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Status extends Component {
.then(res => res.json().then(json => {
if (res.status >= 200 && res.status < 400) {
this.setState({status: 'success'});
setTimeout(() => window.close(), 1000);
setTimeout(() => window.close(), 500 );
} else if (res.status >= 400) {
this.setState({
status: 'failure',
Expand Down
9 changes: 7 additions & 2 deletions backend/certificates.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export function getCerts() {
}

export function saveCertsLocally({key, cert, subdomain}) {
Logger.log('Saving certificates locally.');
// (renewal) -> fs.writeFile replaces the file if it already exists
const saveCert = new Promise((resolve, reject) => {
fs.writeFile(getSetting('CERT_FILE'), cert, (err) => {
Expand All @@ -61,6 +62,7 @@ export function saveCertsLocally({key, cert, subdomain}) {

export function fetchCertsFromCA() {
const {username, accessToken} = getSetting('USERS')[0];
Logger.log('Sending request to the CA:' + LOCAL_SETTINGS.CA_HOST_URL);
return fetch(
`${LOCAL_SETTINGS.CA_HOST_URL}`, {
method: 'POST',
Expand Down Expand Up @@ -99,6 +101,9 @@ export function fetchAndSaveCerts() {
* When developing and debugging, you may want to quickly use the
* mocked certs (MOCK_CERTS=true) returned as a promise.
*/

Logger.log('Fetching the CA for new certificates.');
Logger.log('Mocking certs is ' + LOCAL_SETTINGS.USE_MOCK_CERTS);
let fetchCerts;
if (LOCAL_SETTINGS.USE_MOCK_CERTS) {
fetchCerts = mockFetchCertsFromCA;
Expand All @@ -109,16 +114,16 @@ export function fetchAndSaveCerts() {
if (!response.key || !response.cert || !response.subdomain) {
throw 'CA did not return one or more of [key, cert, subdomain].';
}
Logger.log('Received a successful response from the CA.');
return saveCertsLocally(response);
});
}

// Wrapper around fetchAndSaveCerts to try again with a sleep period specified.

// Callback is used for renewals as a function that restarts the https server.
export function timeoutFetchAndSaveCerts(callback = () => {}) {
// Increment tries.
LOCAL_SETTINGS.ONGOING_COUNT += 1;

fetchAndSaveCerts()
.then (() => {
Logger.log('Fetched and Saved certificates. Resetting count.');
Expand Down
6 changes: 4 additions & 2 deletions backend/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import QueryScheduler from './persistent/QueryScheduler.js';
import {getSetting, saveSetting} from './settings.js';
import {checkWritePermissions} from './persistent/PlotlyAPI.js';
import {contains, has, keys, isEmpty, merge, pluck} from 'ramda';
import {getCerts, fetchAndSaveCerts, timeoutFetchAndSaveCerts, setRenewalJob} from './certificates';
import {getCerts, timeoutFetchAndSaveCerts, setRenewalJob} from './certificates';
import Logger from './logger';
import fetch from 'node-fetch';

Expand Down Expand Up @@ -64,7 +64,7 @@ export default class Servers {
// Can't create until user was authenticated.
if (!isEmpty(getSetting('USERS'))) {
clearInterval(createCertificates);
fetchAndSaveCerts();
timeoutFetchAndSaveCerts();
}
}, 500);
}
Expand All @@ -87,6 +87,7 @@ export default class Servers {
}

startHttpsServer() {
Logger.log('Starting HTTPS server');
// Reference the new certs into the instance.
this.httpsServer.certs = getCerts();
this.httpsServer.port = parseInt(getSetting('PORT_HTTPS'), 10);
Expand All @@ -101,6 +102,7 @@ export default class Servers {
}

restartHttpsServer() {
Logger.log('Restarting HTTPS server.');
this.httpsServer.close();
setTimeout(() => {
this.httpsServer.start();
Expand Down

0 comments on commit 8cffd79

Please sign in to comment.