Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jonmattgray committed Jan 29, 2024
1 parent d576696 commit ff92ea8
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 30 deletions.
61 changes: 47 additions & 14 deletions docker-compose-testnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ services:
#- '5003:5003'
- '3000:3000'
# or via command line arguments
command: --endpoint "http://alice:5002" "ws://alice:5003" --ipfs-origin http://ipfs0:5001 --opa-origin http://opa:8181 --config /config.json --persona-title "TA" --persona-color "#eddedf"
networks:
- testnet
command: --endpoint "http://alice:5002" "ws://alice:5003" --ipfs-origin http://ipfs0:5001 --opa-origin http://opa0:8181 --config /config.json --persona-title "TA" --persona-color "#eddedf"
networks: ['alice', 'testnet']

##################################################################################
## Bob ###########################################################################
Expand All @@ -62,9 +61,8 @@ services:
#- '5102:5002'
#- '5103:5003'
- '3001:3000'
command: --endpoint "http://bob:5002" "ws://bob:5003" --ipfs-origin http://ipfs1:5001 --opa-origin http://opa:8181 --config /config.json --persona-title "OEM" --persona-color "#dfedde"
networks:
- testnet
command: --endpoint "http://bob:5002" "ws://bob:5003" --ipfs-origin http://ipfs1:5001 --opa-origin http://opa1:8181 --config /config.json --persona-title "OEM" --persona-color "#dfedde"
networks: ['bob', 'testnet']

##################################################################################
## Charlie #######################################################################
Expand All @@ -86,9 +84,8 @@ services:
#- '5202:5002'
#- '5203:5003'
- '3002:3000'
command: --endpoint "http://charlie:5002" "ws://charlie:5003" --ipfs-origin http://ipfs2:5001 --opa-origin http://opa:8181 --config /config.json --persona-title "Supplier" --persona-color "#dedfed"
networks:
- testnet
command: --endpoint "http://charlie:5002" "ws://charlie:5003" --ipfs-origin http://ipfs2:5001 --opa-origin http://opa2:8181 --config /config.json --persona-title "Supplier" --persona-color "#dedfed"
networks: ['charlie', 'testnet']

##################################################################################
## Cluster PEER 0 ################################################################
Expand Down Expand Up @@ -191,19 +188,46 @@ services:
- testnet

##################################################################################
## OPA ###########################################################################
## OPA 0 #########################################################################
##################################################################################

opa:
container_name: opa
opa0:
container_name: opa0
image: openpolicyagent/opa:0.60.0-static
ports:
- '8181:8181'
volumes:
- ./samples/supplierCustomer.rego:/supplierCustomer.rego
command: run --server --log-level debug supplierCustomer.rego
networks:
- testnet
networks: ['alice']

##################################################################################
## OPA 1 #########################################################################
##################################################################################

opa1:
container_name: opa1
image: openpolicyagent/opa:0.60.0-static
ports:
- '8182:8181'
volumes:
- ./samples/supplierCustomer.rego:/supplierCustomer.rego
command: run --server --log-level debug supplierCustomer.rego
networks: ['bob']

##################################################################################
## OPA 2 #########################################################################
##################################################################################

opa2:
container_name: opa2
image: openpolicyagent/opa:0.60.0-static
ports:
- '8183:8181'
volumes:
- ./samples/supplierCustomer.rego:/supplierCustomer.rego
command: run --server --log-level debug supplierCustomer.rego
networks: ['charlie']

volumes:
ipfs0:
Expand All @@ -214,6 +238,15 @@ volumes:
cluster2:

networks:
alice:
ipam:
driver: default
bob:
ipam:
driver: default
charlie:
ipam:
driver: default
testnet:
name: testnet
driver: bridge
15 changes: 0 additions & 15 deletions samples/supplierCustomer.rego

This file was deleted.

9 changes: 9 additions & 0 deletions samples/suppliers.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package suppliers

default allow = false

allow {
input.method == "query"
input.did != null
input.did == input.suppliers[_]
}
14 changes: 13 additions & 1 deletion src/controllers/access/AccessController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Route, Tags, Get, Response, Path } from 'tsoa'
import { Controller, Route, Tags, Get, Response, Path, Post, Body } from 'tsoa'
import { injectable } from 'tsyringe'

import PolicyAgent from '../../policyAgent'
Expand Down Expand Up @@ -29,4 +29,16 @@ export class AccessController extends Controller {
public async getConnectionById(@Path('policyId') policyId: string) {
return this.policyAgent.getPolicy(policyId)
}

/**
* Evaluate a policy package
* @param packageId package identifier
*/
@Post('data/:packageId/eval')
@Response<NotFound['message']>(404)
public async evaluate(@Path('packageId') packageId: string, @Body() requestBody: object) {
console.log(requestBody)

Check failure on line 40 in src/controllers/access/AccessController.ts

View workflow job for this annotation

GitHub Actions / Run lint

Unexpected console statement
console.log(packageId)

Check failure on line 41 in src/controllers/access/AccessController.ts

View workflow job for this annotation

GitHub Actions / Run lint

Unexpected console statement
return this.policyAgent.evaluatePolicy(packageId, requestBody)
}
}
6 changes: 6 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Response as ExResponse, Request as ExRequest, NextFunction } from 'express'
import { ValidateError } from 'tsoa'
import { isHttpError } from 'http-errors'

import { Logger } from '@aries-framework/core'

Expand Down Expand Up @@ -43,6 +44,11 @@ export const errorHandler =

return res.status(err.code).json(err.message)
}
// capture body parser errors
if (isHttpError(err)) {
logger.warn(`HTTPError in request: ${err.message}`)
return res.status(err.statusCode).json(err.message)
}
if (err instanceof Error) {
logger.error(`Unexpected error thrown in handler: ${err.message}`)
logger.trace(`Stack: ${err.stack}`)
Expand Down
20 changes: 20 additions & 0 deletions src/policyAgent/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,24 @@ export default class PolicyAgent {

throw new HttpResponse({ message: `Error calling Policy Agent` })
}

public async evaluatePolicy(packageId: string, requestBody: object): Promise<boolean> {
const opaEndpoint = `${this.origin}/v1/data/${packageId}/allow`

const response = await fetch(opaEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(requestBody),
})

if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`)
}

const { result } = await response.json()
console.log(result)

Check failure on line 59 in src/policyAgent/index.ts

View workflow job for this annotation

GitHub Actions / Run lint

Unexpected console statement
return result
}
}

0 comments on commit ff92ea8

Please sign in to comment.