Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add txId in payload for RBAC check for asset indexing #1067

Merged
merged 4 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions aquarius/events/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ def __init__(
self._chain_id = chain_id
self.metadata_proofs = None

def check_permission(self, publisher_address):
if not os.getenv("RBAC_SERVER_URL") or not publisher_address:
def check_permission(self, publisher_address, tx_id, asset):
if not os.getenv("RBAC_SERVER_URL") or not publisher_address or not tx_id:
return True

event_type = (
Expand All @@ -69,7 +69,7 @@ def check_permission(self, publisher_address):
else "update"
)

return RBAC.check_permission_rbac(event_type, publisher_address)
return RBAC.check_permission_rbac(event_type, publisher_address, tx_id, asset)

def add_aqua_data(self, record):
"""Adds keys that are specific to Aquarius, on top of the DDO structure:
Expand Down Expand Up @@ -300,7 +300,7 @@ def process(self):
except Exception:
pass

permission = self.check_permission(sender_address)
permission = self.check_permission(sender_address, txid, asset)
if not permission:
error = "RBAC permission denied."
logger.info(error)
Expand Down Expand Up @@ -446,7 +446,7 @@ def process(self):
self.did = asset["id"]
did, sender_address = self.did, self.sender_address

permission = self.check_permission(sender_address)
permission = self.check_permission(sender_address, txid, asset)
if not permission:
error = "RBAC permission denied."
logger.info(error)
Expand Down
10 changes: 9 additions & 1 deletion aquarius/rbac.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,19 @@ def validate_ddo_rbac(data):
).json()

@staticmethod
def check_permission_rbac(event_type, address):
def check_permission_rbac(event_type, address, tx_id, asset):
try:
chain_id = asset["chainId"]
nft_address = asset["nftAddress"]
except KeyError:
return False

payload = {
"eventType": event_type,
"component": "metadatacache",
"credentials": {"type": "address", "value": address},
"txid": tx_id,
"asset": {"chainId": chain_id, "nftAddress": nft_address},
}

try:
Expand Down
2 changes: 1 addition & 1 deletion tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def send_create_update_tx(name, ddo, flags, account):
if flags[0] & 2:
headers = {"Content-type": "application/octet-stream"}
response = requests.post(
provider_url + "/api/services/encrypt?chainId={web3.chain_id}",
provider_url + f"/api/services/encrypt?chainId={web3.eth.chain_id}",
data=compressed_document,
headers=headers,
timeout=5,
Expand Down
6 changes: 4 additions & 2 deletions tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ def run_test(client, base_ddo_url, events_instance, flags):
for service in published_ddo["services"]:
assert service["datatokenAddress"] == erc20_address
assert service["name"] in ["dataAssetAccess", "dataAssetComputingService"]
ddo_state = get_did_state(events_instance._es_instance, None, None, None, did)
ddo_state = get_did_state(
events_instance._es_instance, web3.eth.chain_id, None, None, did
)
assert len(ddo_state["hits"]["hits"]) == 1
assert ddo_state["hits"]["hits"][0]["_id"] == did
assert ddo_state["hits"]["hits"][0]["_source"]["valid"] is True
Expand Down Expand Up @@ -122,7 +124,7 @@ def test_publish_unallowed_address(client, base_ddo_url, events_object):


def test_publish_and_update_ddo_rbac(client, base_ddo_url, events_object, monkeypatch):
monkeypatch.setenv("RBAC_SERVER_URL", "http://localhost:3000")
monkeypatch.setenv("RBAC_SERVER_URL", "http://172.15.0.8:3000")
run_test(client, base_ddo_url, events_object, 2)


Expand Down
9 changes: 8 additions & 1 deletion tests/test_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,14 @@ def test_check_permission(monkeypatch):
)
with patch("requests.post") as mock:
mock.side_effect = Exception("Boom!")
assert processor.check_permission("some_address") is False
asset = {"chainId": 8996, "nftAddress": "some_nft_address"}
assert processor.check_permission("some_address", "some tx id", asset) is False

# Test permission with empty asset
with patch("requests.post") as mock:
mock.side_effect = Exception("Boom!")
asset = {}
assert processor.check_permission("some_address", "some tx id", asset) is False

# will affect the process() function too
with pytest.raises(Exception):
Expand Down
Loading