Signed-off-by: dzdidi <deniszalessky@gmail.com>
This commit is contained in:
dzdidi
2024-02-12 15:15:09 +00:00
parent 7d3884eb9a
commit 4ef1ad74c4
6 changed files with 83 additions and 76 deletions

View File

@@ -12,7 +12,7 @@ const home = require('./home')
const acl = require('./acl') const acl = require('./acl')
const git = require('./git') const git = require('./git')
const { listRemote, bpr } = require('./rpc-requests') const { listRemote, aclRemote } = require('./rpc-requests')
const pkg = require('../package.json') const pkg = require('../package.json')
program program
@@ -94,11 +94,11 @@ 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(a, n, p, options) 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') if (a !== 'list') throw new Error('Need either user or branch option')
const repoACL = await getACL(p) const repoACL = await retreiveACL(a, n, p)
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('Protected Branch(s):', '\t', repoACL.protectedBranches.join(', '))
@@ -108,17 +108,22 @@ program
return return
} }
if (options.user) { if (options.user) {
if (p.startsWith('pear://') || n.startsWith('pear://')) { if (p.startsWith('pear://')) {
// XXX 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://') || n.startsWith('pear://')) { if (p.startsWith('pear://')) {
// XXX 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)
@@ -305,7 +310,7 @@ function localACL(a, u, p, options) {
async function remoteBranchProtectionRules(a, b, p, options) { async function remoteBranchProtectionRules(a, b, p, options) {
if (a === 'list') { if (a === 'list') {
return await bpr.list(p) return await acl.list(p)
} }
} }
@@ -339,7 +344,10 @@ function logBranches(name) {
console.log('Protected Branch(s):', '\t', repoACL.protectedBranches.join(', ')) console.log('Protected Branch(s):', '\t', repoACL.protectedBranches.join(', '))
} }
async function getACL(p) { 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)) return (p.startsWith('pear://')) ? (await getRemoteACL(p)) : (await getLocalACL(p))
} }
@@ -358,5 +366,6 @@ async function getLocalACL(p) {
} }
async function getRemoteACL(p) { async function getRemoteACL(p) {
console.log('getting remote acl')
} }
program.parse() program.parse()

View File

@@ -1,9 +1,7 @@
const git = require('./git') const git = require('./git')
const acl = require('./acl') const acl = require('./acl')
const bpr = require('./bpr')
module.exports = { module.exports = {
git, git,
acl, acl,
bpr,
} }

View File

@@ -0,0 +1,61 @@
const ProtomuxRPC = require('protomux-rpc')
const Hyperswarm = require('hyperswarm')
const crypto = require('hypercore-crypto')
const home = require('../home')
const auth = require('../auth')
async function list (url) {
const matches = url.match(/pear:\/\/([a-f0-9]{64})/)
if (!matches || matches.length < 2) {
console.error('Invalid URL')
process.exit(1)
}
const targetKey = matches[1]
console.log('Connecting to:', targetKey)
const swarmOpts = {}
if (process.env.GIT_PEAR_AUTH === 'native') {
swarmOpts.keyPair = home.getKeyPair()
}
const swarm = new Hyperswarm(swarmOpts)
swarm.join(crypto.discoveryKey(Buffer.from(targetKey, 'hex')), { server: false })
swarm.on('connection', async (socket) => {
const rpc = new ProtomuxRPC(socket)
let payload = { body: { url, method: 'get-repos' } }
if (!process.env.GIT_PEAR_AUTH) {
console.debug('Retreiving data using un-authenticated access')
} else {
console.debug('Retreiving data using authenticated access')
}
if (process.env.GIT_PEAR_AUTH && process.env.GIT_PEAR_AUTH !== 'native') {
payload.header = await auth.getToken(payload.body)
}
const reposRes = await rpc.request('get-repos', Buffer.from(JSON.stringify(payload)))
const repositories = JSON.parse(reposRes.toString())
if (!repositories) {
console.error('Failed to retrieve repositories')
process.exit(1)
}
paylod = { body: { url, method: 'get-acl' } }
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()))
process.exit(0)
})
}
module.exports = {
list,
}

View File

@@ -1,61 +0,0 @@
const ProtomuxRPC = require('protomux-rpc')
const Hyperswarm = require('hyperswarm')
const crypto = require('hypercore-crypto')
const home = require('../home')
const auth = require('../auth')
async function list (url) {
const matches = url.match(/pear:\/\/([a-f0-9]{64})/)
if (!matches || matches.length < 2) {
console.error('Invalid URL')
process.exit(1)
}
const targetKey = matches[1]
console.log('Connecting to:', targetKey)
const swarmOpts = {}
if (process.env.GIT_PEAR_AUTH === 'native') {
swarmOpts.keyPair = home.getKeyPair()
}
const swarm = new Hyperswarm(swarmOpts)
swarm.join(crypto.discoveryKey(Buffer.from(targetKey, 'hex')), { server: false })
swarm.on('connection', async (socket) => {
const rpc = new ProtomuxRPC(socket)
let payload = { body: { url, method: 'get-repos' } }
if (!process.env.GIT_PEAR_AUTH) {
console.debug('Retreiving data using un-authenticated access')
} else {
console.debug('Retreiving data using authenticated access')
}
if (process.env.GIT_PEAR_AUTH && process.env.GIT_PEAR_AUTH !== 'native') {
payload.header = await auth.getToken(payload.body)
}
const reposRes = await rpc.request('get-repos', Buffer.from(JSON.stringify(payload)))
const repositories = JSON.parse(reposRes.toString())
if (!repositories) {
console.error('Failed to retrieve repositories')
process.exit(1)
}
paylod = { body: { url, method: 'get-bpr' } }
if (process.env.GIT_PEAR_AUTH && process.env.GIT_PEAR_AUTH !== 'native') {
payload.header = await auth.getToken(payload.body)
}
const bpr = await rpc.request('get-bpr', Buffer.from(JSON.stringify(payload)))
console.log('BPR:', JSON.parse(bpr.toString()))
process.exit(0)
})
}
module.exports = {
list,
}

View File

@@ -1,7 +1,7 @@
const listRemote = require('./list-remote') const listRemote = require('./list-remote')
const bpr = require('./bpr-remote') const aclRemote = require('./acl-remote')
module.exports = { module.exports = {
listRemote, listRemote,
bpr, aclRemote,
} }

View File

@@ -3,7 +3,7 @@ const SecretStream = require('@hyperswarm/secret-stream')
const { spawn } = require('child_process') const { spawn } = require('child_process')
const home = require('./home') const home = require('./home')
const auth = require('./auth') const auth = require('./auth')
const { git, acl, bpr } = require('./rpc-handlers') const { git, acl } = require('./rpc-handlers')
module.exports = class RPC { module.exports = class RPC {
constructor (announcedRefs, repositories, drives) { constructor (announcedRefs, repositories, drives) {