Skip to content

Commit

Permalink
feat(core): key file
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Shirokov committed Jun 13, 2020
1 parent 16b6bc6 commit 5be9a14
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ If you don't have access to the environment variables during installation, you c
{
...
"geolite2": {
"license-key": "<your license key>"
// specify the key
"license-key": "<your license key>",
// ... or specify the file where key is located:
"license-file": "maxmind-licence.key"
}
...
}
```

Beware of security risks of adding keys and secrets to your repository!

```javascript
var geolite2 = require('geolite2');
var maxmind = require('maxmind');
Expand Down
52 changes: 37 additions & 15 deletions scripts/postinstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,40 @@ const zlib = require('zlib');
const path = require('path');
const tar = require('tar');

let licenseKey = process.env.MAXMIND_LICENSE_KEY;
if (!licenseKey) {
try {
const packageJsonFilename = path.join(
process.env['INIT_CWD'],
'package.json'
);
const packageJson = JSON.parse(
fs.readFileSync(packageJsonFilename, 'utf8')
);
licenseKey = packageJson['node-geolite2']['license-key'];
} catch (e) {
console.error("Error reading Maxmind license key from 'package.json'");
console.error(e.message);
const getConfig = () => {
const packageJsonFilename = path.join(
process.env['INIT_CWD'],
'package.json'
);
const packageJson = JSON.parse(fs.readFileSync(packageJsonFilename, 'utf8'));
return packageJson['geolite2'] || {};
};

const keyLoaders = [
() => process.env.MAXMIND_LICENSE_KEY,
() => getConfig()['license-key'],
() => {
const configFile = getConfig()['license-file'];
if (!configFile) return;

const filepath = path.join(process.env['INIT_CWD'], configFile);
return fs.existsSync(filepath)
? fs.readFileSync(filepath, 'utf8').trim()
: undefined;
},
];

let licenseKey;

try {
let i = 0;
while (i < keyLoaders.length) {
licenseKey = keyLoaders[i++]();
if (licenseKey) break;
}
} catch (e) {
console.error('geolite2: Error retrieving Maxmind License Key');
console.error(e.message);
}

if (!licenseKey) {
Expand All @@ -31,7 +50,10 @@ if (!licenseKey) {
file (at the root level) like this:
"geolite2": {
"license-key": "<your license key>"
// specify the key
"license-key": "<your license key>",
// ... or specify the file where key is located:
"license-file": "maxmind-licence.key"
}
`);
process.exit(1);
Expand Down

0 comments on commit 5be9a14

Please sign in to comment.