Skip to content

Commit

Permalink
Merge pull request #287 from coopernetes/feat/runnable
Browse files Browse the repository at this point in the history
feat: Make git-proxy executable, custom config file arg & validation, bump to 1.1.0
  • Loading branch information
JamieSlome authored Oct 13, 2023
2 parents c309692 + 84207ae commit c3c66b8
Show file tree
Hide file tree
Showing 11 changed files with 293 additions and 75 deletions.
79 changes: 79 additions & 0 deletions config.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://git-proxy.finos.org/config.schema.json",
"title": "Git Proxy",
"description": "Configuration file for modifying the behaviour of git-proxy",
"type": "object",
"properties": {
"authorisedList": {
"description": "List of repositories that are authorised to be pushed to through the proxy.",
"type": "array",
"items": {
"$ref": "#/$defs/authorisedRepo"
}
},
"sink": {
"description": "List of database sources. The first source in the configuration with enabled=true will be used.",
"type": "array",
"items": {
"$ref": "#/$defs/database"
}
},
"authentication": {
"description": "List of authentication sources. The first source in the configuration with enabled=true will be used.",
"type": "array",
"items": {
"$ref": "#/$defs/authentication"
}
},
"tempPassword": {
"description": "Toggle the generation of temporary password for git-proxy admin user",
"type": "object",
"properties": {
"sendEmail": { "type": "boolean" },
"emailConfig": {
"description": "Generic object to configure nodemailer. For full type information, please see https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/nodemailer",
"type": "object"
}
}
}
},
"additionalProperties": false,
"anyOf": [
{ "required": "authorisedList" },
{ "required": "sink" },
{ "required": "authentication" },
{ "required": "tempPassword" }
],
"$defs": {
"authorisedRepo": {
"type": "object",
"properties": {
"project": { "type": "string" },
"name": { "type": "string" },
"url": { "type": "string" }
},
"required": [ "project", "name", "url" ]
},
"database": {
"type": "object",
"properties": {
"type": { "type": "string" },
"enabled": { "type": "boolean" },
"connectionString": { "type": "string" },
"options": { "type": "object" },
"params": { "type": "object" }
},
"required": [ "type", "enabled" ]
},
"authentication": {
"type": "object",
"properties": {
"type": { "type": "string" },
"enabled": { "type": "boolean" },
"options": { "type": "object" }
},
"required": [ "type", "enabled" ]
}
}
}
45 changes: 45 additions & 0 deletions index.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,48 @@
#!/usr/bin/env node
/* eslint-disable max-len */
const argv = require('yargs/yargs')(process.argv.slice(2))
.usage('Usage: $0 [options]')
.options({
validate: {
description:
'Check the proxy.config.json file in the current working directory for validation errors.',
required: false,
alias: 'v',
},
config: {
description: 'Path to custom git-proxy configuration file.',
default: 'proxy.config.json',
required: false,
alias: 'c',
},
}).argv;

require('./src/config/file').configFile = argv.c ? argv.c : undefined;

if (argv.v) {
const fs = require('fs');
const path = require('path');
const validate = require('jsonschema').validate;
const configFile = require('./src/config/file').configFile;

if (!fs.existsSync(configFile)) {
console.error(
`Config file ${configFile} doesn't exist, nothing to validate! Did you forget -c/--config?`,
);
process.exit(1);
}

const config = JSON.parse(fs.readFileSync(configFile));
const schema = JSON.parse(
fs.readFileSync(path.join(__dirname, 'config.schema.json')),
);

validate(config, schema, { required: true, throwError: true });

console.log(`${configFile} is valid`);
process.exit(0);
}

const proxy = require('./src/proxy');
const service = require('./src/service');

Expand Down
19 changes: 16 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions 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.1.0",
"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 All @@ -37,6 +38,7 @@
"generate-password": "^1.5.1",
"history": "5.3.0",
"load-plugin": "^5.1.0",
"jsonschema": "^1.4.1",
"lodash": "^4.17.21",
"moment": "^2.29.4",
"mongodb": "^5.0.0",
Expand All @@ -51,7 +53,8 @@
"react-dom": "^16.13.1",
"react-html-parser": "^2.0.2",
"react-router-dom": "6.16.0",
"uuid": "^9.0.0"
"uuid": "^9.0.0",
"yargs": "^17.7.2"
},
"devDependencies": {
"@babel/eslint-parser": "^7.22.9",
Expand Down
File renamed without changes.
21 changes: 0 additions & 21 deletions resources/server.cert

This file was deleted.

28 changes: 0 additions & 28 deletions resources/server.key

This file was deleted.

14 changes: 14 additions & 0 deletions src/config/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const path = require('path');
// eslint-disable-next-line prefer-const
let configFile = undefined;

module.exports = {
get configFile() {
return configFile
? configFile
: path.join(process.cwd(), 'proxy.config.json');
},
set configFile(file) {
configFile = file;
},
};
39 changes: 18 additions & 21 deletions src/config/index.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,39 @@
const fs = require('fs');
const proxySettings = JSON.parse(fs.readFileSync('./resources/config.json'));

let _userSettings = null;
let _authorisedList = proxySettings.authorisedList;
let _database = proxySettings.sink;
let _authentication = proxySettings.authentication;
let _tempPassword = proxySettings.tempPassword;
const defaultSettings = require('../../proxy.config.json');
const userSettingsPath = require('./file').configFile;

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 +49,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
Loading

0 comments on commit c3c66b8

Please sign in to comment.