Skip to content

Commit

Permalink
feat: bump to 1.0.1, add exec, merge user config
Browse files Browse the repository at this point in the history
- add "bin" to package.json to support running git-proxy as an
  executable. Largely taken from #184 and fixes #183
- refactor config to read in `user-settings.json` from local directory
  to support custom user settings outside the repo.
- load default settings from a module instead of explicit file path
- add test for default & user setting merging
- bump @finos/git-proxy to 1.0.1
  • Loading branch information
coopernetes committed Sep 16, 2023
1 parent a7929cc commit fd7bc16
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 22 deletions.
1 change: 1 addition & 0 deletions index.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env node
const proxy = require('./src/proxy');
const service = require('./src/service');

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@finos/git-proxy",
"version": "1.0.0",
"version": "1.0.1",
"description": "Deploy custom push protections and policies on top of Git.",
"scripts": {
"client": "vite --config vite.config.js",
Expand All @@ -15,6 +15,7 @@
"prepare": "node ./scripts/prepare.js",
"lint": "eslint --fix . --ext .js,.jsx"
},
"bin": "./index.js",
"author": "Paul Groves",
"license": "Apache-2.0",
"dependencies": {
Expand Down
40 changes: 19 additions & 21 deletions src/config/index.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,40 @@
const fs = require('fs');
const proxySettings = JSON.parse(fs.readFileSync('./resources/config.json'));
const path = require('path');

let _userSettings = null;
let _authorisedList = proxySettings.authorisedList;
let _database = proxySettings.sink;
let _authentication = proxySettings.authentication;
let _tempPassword = proxySettings.tempPassword;
const defaultSettings = require('../../resources/config.json');
const userSettingsPath = path.join(process.cwd(), './user-settings.json');

const userSettings = () => {
const path = './user-settings.json';
if (_userSettings === null && fs.existsSync(path)) {
_userSettings = JSON.parse(fs.readFileSync(path));
}
return _userSettings;
};
let _userSettings = null;
if (fs.existsSync(userSettingsPath)) {
_userSettings = JSON.parse(fs.readFileSync(userSettingsPath));
}
let _authorisedList = defaultSettings.authorisedList;
let _database = defaultSettings.sink;
let _authentication = defaultSettings.authentication;
let _tempPassword = defaultSettings.tempPassword;

// Gets a list of authorised repositories
const getAuthorisedList = () => {
if (userSettings !== null && userSettings.authorisedList) {
_authorisedList = userSettings.authorisedList;
if (_userSettings !== null && _userSettings.authorisedList) {
_authorisedList = _userSettings.authorisedList;
}

return _authorisedList;
};

// Gets a list of authorised repositories
const getTempPasswordConfig = () => {
if (userSettings !== null && userSettings.tempPassword) {
_tempPassword = userSettings.tempPassword;
if (_userSettings !== null && _userSettings.tempPassword) {
_tempPassword = _userSettings.tempPassword;
}

return _tempPassword;
};

// Gets the configuared data sink, defaults to filesystem
const getDatabase = () => {
if (userSettings !== null && userSettings.sink) {
_database = userSettings.database;
if (_userSettings !== null && _userSettings.sink) {
_database = _userSettings.sink;
}
for (const ix in _database) {
if (ix) {
Expand All @@ -52,8 +50,8 @@ const getDatabase = () => {

// Gets the configuared data sink, defaults to filesystem
const getAuthentication = () => {
if (userSettings !== null && userSettings.sink) {
_authentication = userSettings.authentication;
if (_userSettings !== null && _userSettings.authentication) {
_authentication = _userSettings.authentication;
}
for (const ix in _authentication) {
if (!ix) continue;
Expand Down
118 changes: 118 additions & 0 deletions test/testConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/* eslint-disable max-len */
const chai = require('chai');
const fs = require('fs');
const path = require('path');
const defaultSettings = require('../resources/config.json');

chai.should();
const expect = chai.expect;

describe('default configuration', function () {
it('should use default values if no user-settings.json file exists', function () {
const config = require('../src/config');

expect(config.getAuthentication()).to.be.eql(
defaultSettings.authentication[0],
);
expect(config.getDatabase()).to.be.eql(defaultSettings.sink[0]);
expect(config.getTempPasswordConfig()).to.be.eql(
defaultSettings.tempPassword,
);
expect(config.getAuthorisedList()).to.be.eql(
defaultSettings.authorisedList,
);
});
after(function () {
delete require.cache[require.resolve('../src/config')];
});
});

describe('user configuration', function () {
let tempDir;
let tempUserFile;
let curDirUserFile;

beforeEach(function () {
tempDir = fs.mkdtempSync('gitproxy-test');
tempUserFile = path.join(tempDir, 'user-settings.json');
curDirUserFile = path.join(process.cwd(), 'user-settings.json');
fs.symlinkSync(tempUserFile, curDirUserFile);
});

it('should override default settings for authorisedList', function () {
const user = {
authorisedList: [
{
project: 'foo',
name: 'bar',
url: 'https://github.com/foo/bar.git',
},
],
};
fs.writeFileSync(tempUserFile, JSON.stringify(user));

const config = require('../src/config');

expect(config.getAuthorisedList()).to.be.eql(user.authorisedList);
expect(config.getAuthentication()).to.be.eql(
defaultSettings.authentication[0],
);
expect(config.getDatabase()).to.be.eql(defaultSettings.sink[0]);
expect(config.getTempPasswordConfig()).to.be.eql(
defaultSettings.tempPassword,
);
});

it('should override default settings for authentication', function () {
const user = {
authentication: [
{
type: 'google',
enabled: true,
},
],
};
fs.writeFileSync(tempUserFile, JSON.stringify(user));

const config = require('../src/config');

expect(config.getAuthentication()).to.be.eql(user.authentication[0]);
expect(config.getAuthentication()).to.not.be.eql(
defaultSettings.authentication[0],
);
expect(config.getDatabase()).to.be.eql(defaultSettings.sink[0]);
expect(config.getTempPasswordConfig()).to.be.eql(
defaultSettings.tempPassword,
);
});

it('should override default settings for database', function () {
const user = {
sink: [
{
type: 'postgres',
enabled: true,
},
],
};
fs.writeFileSync(tempUserFile, JSON.stringify(user));

const config = require('../src/config');

expect(config.getDatabase()).to.be.eql(user.sink[0]);
expect(config.getDatabase()).to.not.be.eql(defaultSettings.sink[0]);
expect(config.getAuthentication()).to.be.eql(
defaultSettings.authentication[0],
);
expect(config.getTempPasswordConfig()).to.be.eql(
defaultSettings.tempPassword,
);
});

afterEach(function () {
fs.unlinkSync(curDirUserFile);
fs.rmSync(tempUserFile);
fs.rmdirSync(tempDir);
delete require.cache[require.resolve('../src/config')];
});
});

0 comments on commit fd7bc16

Please sign in to comment.