Skip to content

Commit dd9437e

Browse files
committed
feat(api keys): Support new credentials config property
This adds support for a `credentials` pelias.json config property, to replace the outdated `mapzen` section where config properties used to be required to live. The new location is preferred, but API keys can still be loaded from the old location. In that case, a warning will be emitted. Closes pelias/acceptance-tests#465
1 parent 786b4a4 commit dd9437e

File tree

3 files changed

+55
-9
lines changed

3 files changed

+55
-9
lines changed

lib/apiKey.js

+21-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
1-
var url = require('url'),
2-
config = require('pelias-config');
1+
const url = require('url');
2+
const logger = require('pelias-logger').get('fuzzy-tester');
3+
4+
const deprecation_message = 'Loading credentials from deprecated `mapzen.api_key` pelias.json property.' +
5+
'Credentials should now live in `acceptance-tests.credentials`';
36

47
module.exports = function( uri ){
8+
const config = require('pelias-config').generate();
9+
const host = url.parse( uri ).host;
10+
11+
const preferred_credentials = config.get('acceptance-tests.credentials');
512

6-
var settings = config.generate();
13+
const old_credentials = config.get('mapzen.api_key');
714

8-
if( settings && settings.hasOwnProperty('mapzen') ){
9-
var host = url.parse( uri ).host;
10-
if( settings.mapzen.hasOwnProperty('api_key') && settings.mapzen.api_key.hasOwnProperty(host) ){
11-
return settings.mapzen.api_key[ host ];
12-
}
15+
if (preferred_credentials && preferred_credentials[host] !== undefined) {
16+
console.log('using new credentials');
17+
return preferred_credentials[host] || null;
18+
} else {
19+
console.log('checking for old credentials');
1320
}
21+
22+
if (old_credentials && old_credentials[host]) {
23+
logger.warn(deprecation_message);
24+
return old_credentials[ host ];
25+
}
26+
1427
return null;
1528
};

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"nodemailer": "^6.3.1",
3535
"nodemailer-ses-transport": "^1.5.1",
3636
"pelias-config": "^4.0.0",
37+
"pelias-logger": "^1.5.0",
3738
"request": "^2.55.0",
3839
"require-dir": "^1.0.0",
3940
"sanitize-filename": "^1.3.0",

test/apiKey.js

+33-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ tape( 'api_key not found in config', function ( test ){
2424
test.end();
2525
});
2626

27-
tape( 'stage api_key imported from pelias config', function ( test ){
27+
tape( 'stage api_key imported from pelias config, old location', function ( test ){
2828

2929
var config = '{ "mapzen": { "api_key": { "pelias.stage.mapzen.com": "my_api_key" } } }';
3030

@@ -46,6 +46,38 @@ tape( 'stage api_key imported from pelias config', function ( test ){
4646
test.end();
4747
});
4848

49+
tape( 'stage api_key imported from preferred config location first', function ( test ){
50+
const custom_config = {
51+
'acceptance-tests': {
52+
credentials: {
53+
'pelias.stage.mapzen.com': 'my_new_api_key'
54+
}
55+
},
56+
mapzen: {
57+
api_key: {
58+
'pelias.stage.mapzen.com': 'my_old_api_key'
59+
}
60+
}
61+
};
62+
63+
// write a temporary pelias config
64+
fs.writeFileSync( '/tmp/pelias_temp3.json', JSON.stringify(custom_config), 'utf8' );
65+
66+
// set the PELIAS_CONFIG env var
67+
process.env.PELIAS_CONFIG = '/tmp/pelias_temp3.json';
68+
69+
// staging
70+
test.equal( apiKey( 'http://pelias.stage.mapzen.com/foo' ), 'my_new_api_key', 'api key loaded' );
71+
72+
// unset the PELIAS_CONFIG env var
73+
delete process.env.PELIAS_CONFIG;
74+
75+
// delete temp file
76+
fs.unlinkSync( '/tmp/pelias_temp3.json' );
77+
78+
test.end();
79+
});
80+
4981
tape( 'avoid matching partial urls', function ( test ){
5082

5183
var config = '{ "mapzen": { "api_key": { "pelias.stage.mapzen.com": "my_api_key" } } }';

0 commit comments

Comments
 (0)