Skip to content

Commit

Permalink
Merge branch 'main' into reconcile
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieSlome committed Apr 29, 2024
2 parents 3b51e5c + 30271d1 commit 62c17e4
Show file tree
Hide file tree
Showing 14 changed files with 143 additions and 66 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
mongodb-version: [4.4]

steps:
- uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4
- uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4
with:
fetch-depth: 0

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/cve-scanning-node.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
matrix:
node-version: [18.x]
steps:
- uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4
- uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
node-version: ${{ env.NODE_VERSION }}

- name: Code Checkout
uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4
with:
fetch-depth: 0

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/npm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f # v4
- uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4
# Setup .npmrc file to publish to npm
- uses: actions/setup-node@v4
with:
Expand Down
64 changes: 30 additions & 34 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,40 @@
"title": "Git Proxy configuration file",
"description": "Configuration for customizing git-proxy",
"type": "object",
"oneOf": [
{
"properties": {
"authorisedList": {
"description": "List of repositories that are authorised to be pushed to through the proxy.",
"type": "array",
"items": {
"$ref": "#/definitions/authorisedRepo"
}
},
"sink": {
"description": "List of database sources. The first source in the configuration with enabled=true will be used.",
"type": "array",
"items": {
"$ref": "#/definitions/database"
}
},
"authentication": {
"description": "List of authentication sources. The first source in the configuration with enabled=true will be used.",
"type": "array",
"items": {
"$ref": "#/definitions/authentication"
}
},
"tempPassword": {
"description": "Toggle the generation of temporary password for git-proxy admin user",
"type": "object",
"properties": {
"authorisedList": {
"description": "List of repositories that are authorised to be pushed to through the proxy.",
"type": "array",
"items": {
"$ref": "#/definitions/authorisedRepo"
}
},
"sink": {
"description": "List of database sources. The first source in the configuration with enabled=true will be used.",
"type": "array",
"items": {
"$ref": "#/definitions/database"
}
},
"authentication": {
"description": "List of authentication sources. The first source in the configuration with enabled=true will be used.",
"type": "array",
"items": {
"$ref": "#/definitions/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"
}
}
"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"
}
}
}
],
},
"definitions": {
"authorisedRepo": {
"type": "object",
Expand Down
22 changes: 8 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,26 @@ const argv = require('yargs/yargs')(process.argv.slice(2))
})
.strict().argv;

require('./src/config/file').configFile = argv.c ? argv.c : undefined;
const config = require('./src/config/file');
config.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)) {
if (!fs.existsSync(config.configFile)) {
console.error(
`Config file ${configFile} doesn't exist, nothing to validate! Did you forget -c/--config?`,
`Config file ${config.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`);
config.validate();
console.log(`${config.configFile} is valid`);
process.exit(0);
}

config.validate();

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

Expand Down
26 changes: 13 additions & 13 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-html-parser": "^2.0.2",
"react-router-dom": "6.22.3",
"react-router-dom": "6.23.0",
"uuid": "^9.0.0",
"yargs": "^17.7.2"
},
Expand Down
20 changes: 20 additions & 0 deletions src/config/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,31 @@ const path = require('path');
// eslint-disable-next-line prefer-const
let configFile = undefined;

/**
* Validate config file.
* @param {string} configFilePath - The path to the config file.
* @return {boolean} - Returns true if validation is successful.
* @throws Will throw an error if the validation fails.
*/
function validate(configFilePath = configFile) {
const fs = require('fs');
const path = require('path');
const validate = require('jsonschema').validate;

const config = JSON.parse(fs.readFileSync(configFilePath));
const schema = JSON.parse(
fs.readFileSync(path.join(__dirname, '..', '..', 'config.schema.json')),
);
validate(config, schema, { required: true, throwError: true });
return true;
}

module.exports = {
get configFile() {
return configFile ? configFile : path.join(process.cwd(), 'proxy.config.json');
},
set configFile(file) {
configFile = file;
},
validate,
};
9 changes: 9 additions & 0 deletions test/fixtures/proxy.config.invalid-1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"invalidProperty": [
{
"project": "finos",
"name": "git-proxy",
"url": "https://www.github.com/finos/git-proxy.git"
}
]
}
9 changes: 9 additions & 0 deletions test/fixtures/proxy.config.invalid-2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"authorisedList": [
{
"project": "finos",
"name": "git-proxy",
"link": "https://www.github.com/finos/git-proxy"
}
]
}
1 change: 1 addition & 0 deletions test/fixtures/proxy.config.valid-1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}

Check failure on line 1 in test/fixtures/proxy.config.valid-1.json

View workflow job for this annotation

GitHub Actions / Linting

Empty block statement
14 changes: 14 additions & 0 deletions test/fixtures/proxy.config.valid-2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"authorisedList": [
{
"project": "finos",
"name": "git-proxy",
"url": "https://github.com/finos/git-proxy.git"
},
{
"project": "finos",
"name": "git-proxy-test",
"url": "[email protected]:finos/git-proxy-test.git"
}
]
}
34 changes: 34 additions & 0 deletions test/testConfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ const chai = require('chai');
const fs = require('fs');
const path = require('path');
const defaultSettings = require('../proxy.config.json');
const fixtures = 'fixtures';

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');
config.logConfiguration();

expect(config.getAuthentication()).to.be.eql(defaultSettings.authentication[0]);
expect(config.getDatabase()).to.be.eql(defaultSettings.sink[0]);
Expand Down Expand Up @@ -94,3 +96,35 @@ describe('user configuration', function () {
delete require.cache[require.resolve('../src/config')];
});
});

describe('validate config files', function () {
const config = require('../src/config/file');

it('all valid config files should pass validation', function () {
const validConfigFiles = [
'proxy.config.valid-1.json',
'proxy.config.valid-2.json',
];
for (const testConfigFile of validConfigFiles) {
expect(config.validate(path.join(__dirname, fixtures, testConfigFile))).to
.be.true;
}
});

it('all invalid config files should fail validation', function () {
const invalidConfigFiles = [
'proxy.config.invalid-1.json',
'proxy.config.invalid-2.json',
];
for (const testConfigFile of invalidConfigFiles) {
const test = function () {
config.validate(path.join(__dirname, fixtures, testConfigFile));
};
expect(test).to.throw();
}
});

after(function () {
delete require.cache[require.resolve('../src/config')];
});
});

0 comments on commit 62c17e4

Please sign in to comment.