From 20599d854fd81c98eb9b485b09c60d54dd524764 Mon Sep 17 00:00:00 2001 From: dzdidi Date: Wed, 14 Feb 2024 17:43:18 +0000 Subject: [PATCH] remote-acl: list (dirty) Signed-off-by: dzdidi --- src/cli.js | 65 +++++++++++----------------------- src/rpc-handlers/acl.js | 13 +++---- src/rpc-requests/acl-remote.js | 18 ++++++++-- src/rpc.js | 1 - 4 files changed, 41 insertions(+), 56 deletions(-) diff --git a/src/cli.js b/src/cli.js index 401c240..1d454cd 100755 --- a/src/cli.js +++ b/src/cli.js @@ -94,36 +94,24 @@ program throw new Error('Cannot perform both user and branch action at the same time') } - console.log("INPUT", a, n, p, options) - if (!options.user && !options.branch) { - if (a !== 'list') throw new Error('Need either user or branch option') - const repoACL = await retreiveACL(a, n, p) + throw new Error('Either user or branch option is required') + } - console.log('Repo Visibility:', '\t', repoACL.visibility) - console.log('Protected Branch(s):', '\t', repoACL.protectedBranches.join(', ')) - for (const u in repoACL.ACL) { - console.log('User:', u, '\t', repoACL.ACL[u]) - } - return + if (n.startsWith('pear://')) { + let swap = n + n = p + p = swap } if (options.user) { if (p.startsWith('pear://')) { - console.log('a', a, 'n', n, 'p', p, 'options', options) - await remoteACL(a, n, p, options) - } else if (n.startsWith('pear://')) { - console.log('a', a, 'n', n, 'p', p, 'options', options) await remoteACL(a, n, p, options) } else { localACL(a, n, p, options) } } else if (options.branch) { if (p.startsWith('pear://')) { - console.log('a', a, 'n', n, 'p', p, 'options', options) - await remoteBranchProtectionRules(a, n, p, options) - } else if (n.startsWith('pear://')) { - console.log('a', a, 'n', n, 'p', p, 'options', options) await remoteBranchProtectionRules(a, n, p, options) } else { localBranchProtectionRules(a, n, p, options) @@ -260,6 +248,7 @@ function localACL(a, u, p, options) { if (a === 'list' && !u) { console.log('Repo Visibility:', '\t', repoACL.visibility) + console.log('Protected Branch(s):', '\t', repoACL.protectedBranches.join(', ')) console.log('User:', '\t', 'Role:') for (const user in repoACL.ACL) { console.log(user, '\t', repoACL.ACL[user]) @@ -309,12 +298,24 @@ function localACL(a, u, p, options) { } async function remoteBranchProtectionRules(a, b, p, options) { + // TODO if (a === 'list') { - return await acl.list(p) + await aclRemote.list(p) + } else if (a === 'add') { + } else if (a === 'remove') { + } else { + throw new Error('Invalid action') } } async function remoteACL(a, b, p, options) { + if (a === 'list') { + await aclRemote.list(p) + } else if (a === 'add') { + } else if (a === 'remove') { + } else { + throw new Error('Invalid action') + } } function checkIfGitRepo(p) { @@ -340,32 +341,8 @@ async function share(name, branchToShare, options) { function logBranches(name) { const repoACL = acl.getACL(name) - console.log('Visibility:', '\t', repoACL.visibility) + console.log('Repo Visibility:', '\t', repoACL.visibility) console.log('Protected Branch(s):', '\t', repoACL.protectedBranches.join(', ')) } -async function retreiveACL(a, n, p) { - console.log('getting acl', 'a', a) - console.log('getting acl', 'n', n) - console.log('getting acl', 'p', p) - return (p.startsWith('pear://')) ? (await getRemoteACL(p)) : (await getLocalACL(p)) -} - -async function getLocalACL(p) { - const fullPath = path.resolve(p) - checkIfGitRepo(fullPath) - - const name = fullPath.split(path.sep).pop() - if (!home.isInitialized(name)) { - console.error(`${name} is not initialized`) - process.exit(1) - } - - return acl.getACL(name) - -} - -async function getRemoteACL(p) { - console.log('getting remote acl') -} program.parse() diff --git a/src/rpc-handlers/acl.js b/src/rpc-handlers/acl.js index 4a88b02..d5c1463 100644 --- a/src/rpc-handlers/acl.js +++ b/src/rpc-handlers/acl.js @@ -5,21 +5,21 @@ async function getACLHandler (publicKey, req) { const { repoName, userId, acl } = await parseACLRequest.bind(this)(publicKey, req) const repoACL = ACL.getACL(repoName) - return JSON.stringify(repoACL) + return Buffer.from(JSON.stringify(repoACL)) } async function addACLHandler (publicKey, req) { const { repoName, userId, acl } = await parseACLRequest.bind(this)(publicKey, req) - const { protectedBranches } = ACL.getACL(repoName) - return JSON.stringify({ protectedBranches }) + const repoACL = ACL.getACL(repoName) + return Buffer.from(JSON.stringify(repoACL)) } async function delACLHandler (publicKey, req) { const { repoName, userId, acl } = await parseACLRequest.bind(this)(publicKey, req) - const { protectedBranches } = ACL.getACL(repoName) - return JSON.stringify({ protectedBranches }) + const repoACL = ACL.getACL(repoName) + return Buffer.from(JSON.stringify(repoACL)) } async function parseACLRequest(publicKey, req) { @@ -45,6 +45,3 @@ module.exports = { addACLHandler, delACLHandler, } - - - diff --git a/src/rpc-requests/acl-remote.js b/src/rpc-requests/acl-remote.js index 6d5dbaf..91b3c29 100644 --- a/src/rpc-requests/acl-remote.js +++ b/src/rpc-requests/acl-remote.js @@ -6,7 +6,7 @@ const crypto = require('hypercore-crypto') const home = require('../home') const auth = require('../auth') -async function list (url) { +async function list (url, name) { const matches = url.match(/pear:\/\/([a-f0-9]{64})/) if (!matches || matches.length < 2) { @@ -49,8 +49,20 @@ async function list (url) { if (process.env.GIT_PEAR_AUTH && process.env.GIT_PEAR_AUTH !== 'native') { payload.header = await auth.getToken(payload.body) } - const repoACL = await rpc.request('get-acl', Buffer.from(JSON.stringify(payload))) - console.log('REPO ACL:', JSON.parse(repoACL.toString())) + const repoACLres = await rpc.request('get-acl', Buffer.from(JSON.stringify(payload))) + const repoACL = JSON.parse(repoACLres.toString()) + + console.log('Repo Visibility:', '\t', repoACL.visibility) + console.log('Protected Branch(s):', '\t', repoACL.protectedBranches.join(', ')) + console.log('User:', '\t', 'Role:') + if (name) { + console.log(name, '\t', repoACL.ACL[name]) + process.exit(0) + } + + for (const user in repoACL.ACL) { + console.log(user, '\t', repoACL.ACL[user]) + } process.exit(0) }) diff --git a/src/rpc.js b/src/rpc.js index 6241255..7a58453 100755 --- a/src/rpc.js +++ b/src/rpc.js @@ -40,7 +40,6 @@ module.exports = class RPC { /* -- ACL HANDLERS -- */ rpc.respond('get-acl', async req => await acl.getACLHandler.bind(this)(socket.remotePublicKey, req)) rpc.respond('add-acl', async req => await acl.addACLHandler.bind(this)(socket.remotePublicKey, req)) - rpc.respond('chg-acl', async req => await acl.chgCLHandler.bind(this)(socket.remotePublicKey, req)) rpc.respond('del-acl', async req => await acl.delACLHandler.bind(this)(socket.remotePublicKey, req)) }