Skip to content

Commit ddff723

Browse files
authored
Merge pull request #973 from kriswest/946-associate-commits-by-email-rebase
fix: 946 associate commits by email
2 parents 9913250 + 5b9fe88 commit ddff723

30 files changed

+780
-1062
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
"source.fixAll.eslint": "explicit"
99
},
1010
"editor.defaultFormatter": "esbenp.prettier-vscode",
11-
"editor.formatOnSave": true
11+
"editor.formatOnSave": true,
12+
"cSpell.words": ["Deltafied"]
1213
}

packages/git-proxy-cli/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ async function authoriseGitPush(id) {
176176
if (error.response) {
177177
switch (error.response.status) {
178178
case 401:
179-
errorMessage = 'Error: Authorise: Authentication required';
179+
errorMessage =
180+
'Error: Authorise: Authentication required (401): ' + error?.response?.data?.message;
180181
process.exitCode = 3;
181182
break;
182183
case 404:
@@ -223,7 +224,8 @@ async function rejectGitPush(id) {
223224
if (error.response) {
224225
switch (error.response.status) {
225226
case 401:
226-
errorMessage = 'Error: Reject: Authentication required';
227+
errorMessage =
228+
'Error: Reject: Authentication required (401): ' + error?.response?.data?.message;
227229
process.exitCode = 3;
228230
break;
229231
case 404:
@@ -270,7 +272,8 @@ async function cancelGitPush(id) {
270272
if (error.response) {
271273
switch (error.response.status) {
272274
case 401:
273-
errorMessage = 'Error: Cancel: Authentication required';
275+
errorMessage =
276+
'Error: Cancel: Authentication required (401): ' + error?.response?.data?.message;
274277
process.exitCode = 3;
275278
break;
276279
case 404:

packages/git-proxy-cli/test/testCli.test.js

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ const TEST_REPO_CONFIG = {
2222
url: 'https://github.com/finos/git-proxy-test.git',
2323
};
2424
const TEST_REPO = 'finos/git-proxy-test.git';
25+
// user for test cases
26+
const TEST_USER = 'testuser';
27+
const TEST_PASSWORD = 'testpassword';
28+
const TEST_EMAIL = '[email protected]';
29+
const TEST_GIT_ACCOUNT = 'testGitAccount';
2530

2631
describe('test git-proxy-cli', function () {
2732
// *** help ***
@@ -87,16 +92,12 @@ describe('test git-proxy-cli', function () {
8792
// *** login ***
8893

8994
describe('test git-proxy-cli :: login', function () {
90-
const testUser = 'testuser';
91-
const testPassword = 'testpassword';
92-
const testEmail = '[email protected]';
93-
9495
before(async function () {
95-
await helper.addUserToDb(testUser, testPassword, testEmail, 'testGitAccount');
96+
await helper.addUserToDb(TEST_USER, TEST_PASSWORD, TEST_EMAIL, TEST_GIT_ACCOUNT);
9697
});
9798

9899
after(async function () {
99-
await helper.removeUserFromDb(testUser);
100+
await helper.removeUserFromDb(TEST_USER);
100101
});
101102

102103
it('login should fail when server is down', async function () {
@@ -140,9 +141,9 @@ describe('test git-proxy-cli', function () {
140141
});
141142

142143
it('login shoud be successful with valid credentials (non-admin)', async function () {
143-
const cli = `npx -- @finos/git-proxy-cli login --username ${testUser} --password ${testPassword}`;
144+
const cli = `npx -- @finos/git-proxy-cli login --username ${TEST_USER} --password ${TEST_PASSWORD}`;
144145
const expectedExitCode = 0;
145-
const expectedMessages = [`Login "${testUser}" <${testEmail}>: OK`];
146+
const expectedMessages = [`Login "${TEST_USER}" <${TEST_EMAIL}>: OK`];
146147
const expectedErrorMessages = null;
147148
try {
148149
await helper.startServer(service);
@@ -219,11 +220,13 @@ describe('test git-proxy-cli', function () {
219220

220221
before(async function () {
221222
await helper.addRepoToDb(TEST_REPO_CONFIG);
222-
await helper.addGitPushToDb(pushId, TEST_REPO);
223+
await helper.addUserToDb(TEST_USER, TEST_PASSWORD, TEST_EMAIL, TEST_GIT_ACCOUNT);
224+
await helper.addGitPushToDb(pushId, TEST_USER, TEST_EMAIL, TEST_REPO);
223225
});
224226

225227
after(async function () {
226228
await helper.removeGitPushFromDb(pushId);
229+
await helper.removeUserFromDb(TEST_USER);
227230
await helper.removeRepoFromDb(TEST_REPO_CONFIG.name);
228231
});
229232

@@ -294,11 +297,13 @@ describe('test git-proxy-cli', function () {
294297

295298
before(async function () {
296299
await helper.addRepoToDb(TEST_REPO_CONFIG);
297-
await helper.addGitPushToDb(pushId, TEST_REPO);
300+
await helper.addUserToDb(TEST_USER, TEST_PASSWORD, TEST_EMAIL, TEST_GIT_ACCOUNT);
301+
await helper.addGitPushToDb(pushId, TEST_USER, TEST_EMAIL, TEST_REPO);
298302
});
299303

300304
after(async function () {
301305
await helper.removeGitPushFromDb(pushId);
306+
await helper.removeUserFromDb(TEST_USER);
302307
await helper.removeRepoFromDb(TEST_REPO_CONFIG.name);
303308
});
304309

@@ -415,11 +420,13 @@ describe('test git-proxy-cli', function () {
415420

416421
before(async function () {
417422
await helper.addRepoToDb(TEST_REPO_CONFIG);
418-
await helper.addGitPushToDb(pushId, TEST_REPO);
423+
await helper.addUserToDb(TEST_USER, TEST_PASSWORD, TEST_EMAIL, TEST_GIT_ACCOUNT);
424+
await helper.addGitPushToDb(pushId, TEST_USER, TEST_EMAIL, TEST_REPO);
419425
});
420426

421427
after(async function () {
422428
await helper.removeGitPushFromDb(pushId);
429+
await helper.removeUserFromDb(TEST_USER);
423430
await helper.removeRepoFromDb(TEST_REPO_CONFIG.name);
424431
});
425432

@@ -487,12 +494,17 @@ describe('test git-proxy-cli', function () {
487494

488495
describe('test git-proxy-cli :: git push administration', function () {
489496
const pushId = `0000000000000000000000000000000000000000__${Date.now()}`;
490-
const gitAccount = 'testGitAccount1';
491497

492498
before(async function () {
493499
await helper.addRepoToDb(TEST_REPO_CONFIG);
494-
await helper.addUserToDb('testuser1', 'testpassword', '[email protected]', gitAccount);
495-
await helper.addGitPushToDb(pushId, TEST_REPO, gitAccount);
500+
await helper.addUserToDb(TEST_USER, TEST_PASSWORD, TEST_EMAIL, TEST_GIT_ACCOUNT);
501+
await helper.addGitPushToDb(pushId, TEST_REPO, TEST_USER, TEST_EMAIL);
502+
});
503+
504+
after(async function () {
505+
await helper.removeGitPushFromDb(pushId);
506+
await helper.removeUserFromDb(TEST_USER);
507+
await helper.removeRepoFromDb(TEST_REPO_CONFIG.name);
496508
});
497509

498510
after(async function () {

packages/git-proxy-cli/test/testCliUtils.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,10 @@ async function removeRepoFromDb(repoName) {
177177
* @param {string} id The ID of the git push.
178178
* @param {string} repo The repository of the git push.
179179
* @param {string} user The user who pushed the git push.
180+
* @param {string} userEmail The email of the user who pushed the git push.
180181
* @param {boolean} debug Flag to enable logging for debugging.
181182
*/
182-
async function addGitPushToDb(id, repo, user = null, debug = false) {
183+
async function addGitPushToDb(id, repo, user = null, userEmail = null, debug = false) {
183184
const action = new actions.Action(
184185
id,
185186
'push', // type
@@ -188,6 +189,7 @@ async function addGitPushToDb(id, repo, user = null, debug = false) {
188189
repo,
189190
);
190191
action.user = user;
192+
action.userEmail = userEmail;
191193
const step = new steps.Step(
192194
'authBlock', // stepName
193195
false, // error

src/proxy/actions/Action.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Step } from './Step';
77
export interface Commit {
88
message: string;
99
committer: string;
10+
committerEmail: string;
1011
tree: string;
1112
parent: string;
1213
author: string;
@@ -45,6 +46,7 @@ class Action {
4546
message?: string;
4647
author?: string;
4748
user?: string;
49+
userEmail?: string;
4850
attestation?: string;
4951
lastStep?: Step;
5052
proxyGitPath?: string;

src/proxy/processors/push-action/checkUserPushPermission.ts

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,29 @@ import { trimTrailingDotGit } from '../../../db/helper';
55
// Execute if the repo is approved
66
const exec = async (req: any, action: Action): Promise<Action> => {
77
const step = new Step('checkUserPushPermission');
8-
const user = action.user;
8+
const userEmail = action.userEmail;
99

10-
if (!user) {
10+
if (!userEmail) {
1111
step.setError('Push blocked: User not found. Please contact an administrator for support.');
1212
action.addStep(step);
1313
step.error = true;
1414
return action;
1515
}
1616

17-
return await validateUser(user, action, step);
17+
return await validateUser(userEmail, action, step);
1818
};
1919

2020
/**
2121
* Helper that validates the user's push permission.
2222
* This can be used by other actions that need it.
23-
* @param {string} user The user to validate
23+
* @param {string} userEmail The user to validate
2424
* @param {Action} action The action object
2525
* @param {Step} step The step object
2626
* @return {Promise<Action>} The action object
2727
*/
28-
const validateUser = async (user: string, action: Action, step: Step): Promise<Action> => {
28+
const validateUser = async (userEmail: string, action: Action, step: Step): Promise<Action> => {
2929
const repoSplit = trimTrailingDotGit(action.repo.toLowerCase()).split('/');
30+
3031
// we expect there to be exactly one / separating org/repoName
3132
if (repoSplit.length != 2) {
3233
step.setError('Server-side issue extracting repoName');
@@ -37,35 +38,44 @@ const validateUser = async (user: string, action: Action, step: Step): Promise<A
3738
const repoName = repoSplit[1];
3839
let isUserAllowed = false;
3940

40-
// Find the user associated with this Git Account
41-
const list = await getUsers({ gitAccount: action.user });
41+
// Find the user associated with this email address
42+
const list = await getUsers({ email: userEmail });
4243

43-
console.log(`Users for this git account: ${JSON.stringify(list)}`);
44+
if (list.length > 1) {
45+
console.error(`Multiple users found with email address ${userEmail}, ending`);
46+
step.error = true;
47+
step.log(
48+
`Multiple Users have email <${userEmail}> so we cannot uniquely identify the user, ending`,
49+
);
4450

45-
if (list.length == 1) {
46-
user = list[0].username;
47-
isUserAllowed = await isUserPushAllowed(repoName, user);
51+
step.setError(
52+
`Your push has been blocked (there are multiple users with email ${action.userEmail})`,
53+
);
54+
action.addStep(step);
55+
return action;
56+
} else if (list.length == 0) {
57+
console.error(`No user with email address ${userEmail} found`);
58+
} else {
59+
isUserAllowed = await isUserPushAllowed(repoName, list[0].username);
4860
}
4961

50-
console.log(`User ${user} permission on Repo ${repoName} : ${isUserAllowed}`);
62+
console.log(`User ${userEmail} permission on Repo ${repoName} : ${isUserAllowed}`);
5163

5264
if (!isUserAllowed) {
5365
console.log('User not allowed to Push');
5466
step.error = true;
55-
step.log(`User ${user} is not allowed to push on repo ${action.repo}, ending`);
56-
57-
console.log('setting error');
67+
step.log(`User ${userEmail} is not allowed to push on repo ${action.repo}, ending`);
5868

5969
step.setError(
60-
`Rejecting push as user ${action.user} ` +
70+
`Your push has been blocked (${action.userEmail} ` +
6171
`is not allowed to push on repo ` +
62-
`${action.repo}`,
72+
`${action.repo})`,
6373
);
6474
action.addStep(step);
6575
return action;
6676
}
6777

68-
step.log(`User ${user} is allowed to push on repo ${action.repo}`);
78+
step.log(`User ${userEmail} is allowed to push on repo ${action.repo}`);
6979
action.addStep(step);
7080
return action;
7181
};

0 commit comments

Comments
 (0)