Skip to content

Commit ba8ead1

Browse files
authored
Merge pull request #110 from jason-fox/feature/dependencies
Feature/dependencies
2 parents 95ea992 + 0a583d7 commit ba8ead1

7 files changed

Lines changed: 34 additions & 84 deletions

File tree

Dockerfile-sample

Lines changed: 0 additions & 35 deletions
This file was deleted.

bin/www

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const fs = require('fs');
77
const https = require('https');
88
const Root = require('../controllers/root').Root;
99
const IDM = require('../lib/idm.js').IDM;
10-
const async = require('async');
1110
const errorhandler = require('errorhandler');
1211

1312
config.azf = config.azf || {};
@@ -77,54 +76,20 @@ for (const p in config.public_paths) {
7776

7877
app.all('/*', Root.pep);
7978

80-
let retries = 0;
81-
let idmConnected = false;
82-
83-
function retryCheck() {
84-
return !idmConnected && retries < 10;
85-
}
8679

8780
function connectIDM(callback) {
8881
IDM.authenticate(
8982
function(token) {
9083
log.info('Success authenticating PEP proxy. Proxy Auth-token: ', token);
91-
idmConnected = true;
9284
callback();
9385
},
9486
function(status, e) {
9587
log.error('Error in IDM communication', e);
96-
callback();
88+
callback(e);
9789
}
9890
);
9991
}
10092

101-
function tryCreateConnection(callback) {
102-
const seconds = 5;
103-
104-
retries++;
105-
106-
if (retries === 1) {
107-
log.info('Starting PEP proxy in port ' + port + '. IdM authentication ...');
108-
connectIDM(callback);
109-
} else {
110-
log.info('Waiting %d seconds before attempting again.', seconds);
111-
setTimeout(() => {
112-
connectIDM(callback);
113-
}, seconds * 1000);
114-
}
115-
}
116-
117-
function createConnectionHandler(error) {
118-
if (idmConnected) {
119-
log.info('Success authenticating PEP proxy.');
120-
} else {
121-
log.error('Error found after [%d] attempts: %s', retries, error);
122-
process.exit(1);
123-
}
124-
}
125-
126-
async.whilst(retryCheck, tryCreateConnection, createConnectionHandler);
127-
12893
if (config.https.enabled === true) {
12994
const options = {
13095
key: fs.readFileSync(config.https.key_file),
@@ -139,3 +104,28 @@ if (config.https.enabled === true) {
139104
} else {
140105
app.listen(app.get('port'));
141106
}
107+
108+
let retry = 20;
109+
function connect() {
110+
const connect_with_retry = () => {
111+
connectIDM((err) => {
112+
if (err) {
113+
retry--;
114+
if (retry === 0) {
115+
log.error('Error found after [%d] attempts: %s', 20, error);
116+
process.exit(1);
117+
} else {
118+
console.log('retry after 5 seconds.');
119+
//eslint-disable-next-line snakecase/snakecase
120+
setTimeout(connect_with_retry, 5000);
121+
}
122+
} else {
123+
log.info('Success authenticating PEP proxy.');
124+
}
125+
});
126+
};
127+
connect_with_retry();
128+
}
129+
130+
log.info('Starting PEP proxy in port ' + port + '. IdM authentication ...');
131+
connect();

controllers/root.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,9 @@ const Root = (function() {
210210
};
211211

212212
const redirRequest = ('auth_for_nginx' in config && config.auth_for_nginx)
213-
? function(req, res, userInfo) {
213+
?
214+
// eslint-disable-next-line no-unused-vars
215+
function(req, res, userInfo) {
214216
log.info('Access-token OK. Response 204');
215217
res.sendStatus(204);
216218
}

doc/admin_guide.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,10 @@ server {
171171

172172
The auth_request directive in the `location /` block specifies the location for checking a token and permissions.
173173
Proxying to a backend-app happens only if the auth_request response is successful (HTTP status 2xx). The proxy_pass
174-
directive is a url of a backend-app.
174+
directive is a URL of a backend-app.
175175

176176
To call a PEP Proxy, the various values of a request are defined in the `/_check_oauth2_token` block. The proxy_pass
177-
directive is a url of a PEP Proxy.
177+
directive is a URL of a PEP Proxy.
178178

179179
Update the values of the two proxy_pass directives to suit your system environment.
180180

lib/config_service.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
const log = require('../lib/logger').logger.getLogger('Server');
22
const path = require('path');
3+
const fs = require('fs');
34

45
let config = {};
5-
6-
const fs = require('fs');
7-
const path = require('path');
86
const SECRETS_DIR = process.env.SECRETS_DIR || '/run/secrets';
97
const secrets = {};
108

119
if (fs.existsSync(SECRETS_DIR)) {
1210
const files = fs.readdirSync(SECRETS_DIR);
11+
// eslint-disable-next-line no-unused-vars
1312
files.forEach(function(file, index) {
1413
const fullPath = path.join(SECRETS_DIR, file);
1514
const key = file;
1615
try {
1716
const data = fs.readFileSync(fullPath, 'utf8').toString().trim();
1817
secrets[key] = data;
1918
} catch (e) {
20-
logger.error(e.message);
19+
log.error(e.message);
2120
}
2221
});
2322
}

package-lock.json

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"description": "PEP oauth2 authentication proxy for FIWARE GE services",
66
"author": "GING DIT UPM",
77
"dependencies": {
8-
"async": "3.2.0",
98
"errorhandler": "1.x",
109
"escape-html": "1.0.3",
1110
"express": "4.x",

0 commit comments

Comments
 (0)