remote-acl: list (dirty)

Signed-off-by: dzdidi <deniszalessky@gmail.com>
This commit is contained in:
dzdidi
2024-02-14 17:43:18 +00:00
parent 4ef1ad74c4
commit 20599d854f
4 changed files with 41 additions and 56 deletions

View File

@@ -94,36 +94,24 @@ program
throw new Error('Cannot perform both user and branch action at the same time') 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 (!options.user && !options.branch) {
if (a !== 'list') throw new Error('Need either user or branch option') throw new Error('Either user or branch option is required')
const repoACL = await retreiveACL(a, n, p)
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 (options.user) {
if (p.startsWith('pear://')) { 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) await remoteACL(a, n, p, options)
} else { } else {
localACL(a, n, p, options) localACL(a, n, p, options)
} }
} else if (options.branch) { } else if (options.branch) {
if (p.startsWith('pear://')) { 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) await remoteBranchProtectionRules(a, n, p, options)
} else { } else {
localBranchProtectionRules(a, n, p, options) localBranchProtectionRules(a, n, p, options)
@@ -260,6 +248,7 @@ function localACL(a, u, p, options) {
if (a === 'list' && !u) { if (a === 'list' && !u) {
console.log('Repo Visibility:', '\t', repoACL.visibility) console.log('Repo Visibility:', '\t', repoACL.visibility)
console.log('Protected Branch(s):', '\t', repoACL.protectedBranches.join(', '))
console.log('User:', '\t', 'Role:') console.log('User:', '\t', 'Role:')
for (const user in repoACL.ACL) { for (const user in repoACL.ACL) {
console.log(user, '\t', repoACL.ACL[user]) console.log(user, '\t', repoACL.ACL[user])
@@ -309,12 +298,24 @@ function localACL(a, u, p, options) {
} }
async function remoteBranchProtectionRules(a, b, p, options) { async function remoteBranchProtectionRules(a, b, p, options) {
// TODO
if (a === 'list') { 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) { 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) { function checkIfGitRepo(p) {
@@ -340,32 +341,8 @@ async function share(name, branchToShare, options) {
function logBranches(name) { function logBranches(name) {
const repoACL = acl.getACL(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(', ')) 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() program.parse()

View File

@@ -5,21 +5,21 @@ async function getACLHandler (publicKey, req) {
const { repoName, userId, acl } = await parseACLRequest.bind(this)(publicKey, req) const { repoName, userId, acl } = await parseACLRequest.bind(this)(publicKey, req)
const repoACL = ACL.getACL(repoName) const repoACL = ACL.getACL(repoName)
return JSON.stringify(repoACL) return Buffer.from(JSON.stringify(repoACL))
} }
async function addACLHandler (publicKey, req) { async function addACLHandler (publicKey, req) {
const { repoName, userId, acl } = await parseACLRequest.bind(this)(publicKey, req) const { repoName, userId, acl } = await parseACLRequest.bind(this)(publicKey, req)
const { protectedBranches } = ACL.getACL(repoName) const repoACL = ACL.getACL(repoName)
return JSON.stringify({ protectedBranches }) return Buffer.from(JSON.stringify(repoACL))
} }
async function delACLHandler (publicKey, req) { async function delACLHandler (publicKey, req) {
const { repoName, userId, acl } = await parseACLRequest.bind(this)(publicKey, req) const { repoName, userId, acl } = await parseACLRequest.bind(this)(publicKey, req)
const { protectedBranches } = ACL.getACL(repoName) const repoACL = ACL.getACL(repoName)
return JSON.stringify({ protectedBranches }) return Buffer.from(JSON.stringify(repoACL))
} }
async function parseACLRequest(publicKey, req) { async function parseACLRequest(publicKey, req) {
@@ -45,6 +45,3 @@ module.exports = {
addACLHandler, addACLHandler,
delACLHandler, delACLHandler,
} }

View File

@@ -6,7 +6,7 @@ const crypto = require('hypercore-crypto')
const home = require('../home') const home = require('../home')
const auth = require('../auth') const auth = require('../auth')
async function list (url) { async function list (url, name) {
const matches = url.match(/pear:\/\/([a-f0-9]{64})/) const matches = url.match(/pear:\/\/([a-f0-9]{64})/)
if (!matches || matches.length < 2) { 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') { if (process.env.GIT_PEAR_AUTH && process.env.GIT_PEAR_AUTH !== 'native') {
payload.header = await auth.getToken(payload.body) payload.header = await auth.getToken(payload.body)
} }
const repoACL = await rpc.request('get-acl', Buffer.from(JSON.stringify(payload))) const repoACLres = await rpc.request('get-acl', Buffer.from(JSON.stringify(payload)))
console.log('REPO ACL:', JSON.parse(repoACL.toString())) 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) process.exit(0)
}) })

View File

@@ -40,7 +40,6 @@ module.exports = class RPC {
/* -- ACL HANDLERS -- */ /* -- ACL HANDLERS -- */
rpc.respond('get-acl', async req => await acl.getACLHandler.bind(this)(socket.remotePublicKey, req)) 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('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)) rpc.respond('del-acl', async req => await acl.delACLHandler.bind(this)(socket.remotePublicKey, req))
} }