Skip to content

Commit

Permalink
Merge pull request #29 from runk/key-file
Browse files Browse the repository at this point in the history
feat(core): key file
  • Loading branch information
Dmitry Shirokov authored Jun 13, 2020
2 parents d19d2c1 + ad1b2dd commit 12b2de0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 17 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,21 @@ Maxmind's GeoLite2 Free Databases download helper.

If you don't have access to the environment variables during installation, you can provide license key via `package.json`:

```json
```jsonc
{
...
"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 12b2de0

Please sign in to comment.