-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Python: samples: hdp_provenance - cryptographic delegation audit trail for agent-framework agents #5727
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
Open
asiridalugoda
wants to merge
4
commits into
microsoft:main
Choose a base branch
from
asiridalugoda:feat/hdp-provenance-sample
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+167
−6
Open
Python: samples: hdp_provenance - cryptographic delegation audit trail for agent-framework agents #5727
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9ba26f1
samples: add hdp_provenance - cryptographic delegation audit trail fo…
asiridalugoda 181cb51
fix: correct install deps, base64 padding, and use sync AzureCliCrede…
asiridalugoda a3b3625
docs: add hdp_provenance entry to security samples README
asiridalugoda 99423de
samples(hdp_provenance): add tamper-resistance demo and mode annotations
asiridalugoda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| # Copyright (c) Microsoft. All rights reserved. | ||
| # SPDX-License-Identifier: MIT | ||
| # | ||
| # This sample requires: | ||
| # pip install "agent-framework-foundry" "hdp-agent-framework" "azure-identity" python-dotenv | ||
| # | ||
| # Generate an Ed25519 signing key once: | ||
| # python -c " | ||
| # from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey | ||
| # import base64; k = Ed25519PrivateKey.generate() | ||
| # print('HDP_SIGNING_KEY=' + base64.urlsafe_b64encode(k.private_bytes_raw()).decode()) | ||
| # " | ||
| # export HDP_SIGNING_KEY=<value> | ||
| # | ||
| # Reference: https://helixar.ai/about/labs/hdp/ | ||
| # Package: https://pypi.org/project/hdp-agent-framework/ | ||
|
|
||
| """ | ||
| HDP Delegation Provenance - agent-framework integration | ||
|
|
||
| Attaches a cryptographic audit trail to an agent-framework Agent. | ||
| Every chat call is recorded as a signed delegation hop verifiable | ||
| offline with a single public key. | ||
| """ | ||
|
|
||
| import asyncio | ||
| import base64 | ||
| import os | ||
| import sys | ||
|
|
||
| from agent_framework import Agent | ||
| from agent_framework.foundry import FoundryChatClient | ||
| from azure.identity import AzureCliCredential | ||
| from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey | ||
| from dotenv import load_dotenv | ||
|
|
||
| # HDP middleware - pip install hdp-agent-framework | ||
| # Docs: https://helixar.ai/about/labs/hdp/ | ||
| from hdp_agent_framework import HdpMiddleware, HdpPrincipal, ScopePolicy, verify_chain | ||
|
|
||
| load_dotenv() | ||
|
|
||
|
|
||
| def _load_signing_key() -> Ed25519PrivateKey: | ||
| raw_b64 = os.getenv("HDP_SIGNING_KEY") | ||
| if not raw_b64: | ||
| print("ERROR: HDP_SIGNING_KEY not set. See comment at top of file.") | ||
| sys.exit(1) | ||
| padding = (4 - len(raw_b64) % 4) % 4 | ||
| return Ed25519PrivateKey.from_private_bytes( | ||
| base64.urlsafe_b64decode(raw_b64 + "=" * padding) | ||
| ) | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| private_key = _load_signing_key() | ||
|
|
||
| # 1. Declare what the human is authorising | ||
| middleware = HdpMiddleware( | ||
| signing_key=private_key.private_bytes_raw(), | ||
| session_id="analysis-session-2026", | ||
| principal=HdpPrincipal(id="analyst@example.com", id_type="email"), | ||
| scope=ScopePolicy( | ||
| intent="Analyse sales data and produce a written summary", | ||
| authorized_tools=["fetch_data", "write_report"], | ||
| max_hops=5, | ||
| ), | ||
| ) | ||
|
|
||
| # 2. Build agent as normal | ||
| credential = AzureCliCredential() | ||
| agent = Agent( | ||
| client=FoundryChatClient(credential=credential), | ||
| name="sales_analyst", | ||
| instructions="You are a sales analyst. Fetch data, then write a summary.", | ||
| ) | ||
|
|
||
| # 3. Attach HDP - one line, zero agent changes | ||
| middleware.configure(agent) | ||
|
|
||
| # 4. Run | ||
| result = await agent.run("Analyse Q1 EMEA sales and write a one-page summary.") | ||
| print(result.text) | ||
|
|
||
| # 5. Verify delegation chain offline | ||
| token = middleware.export_token() | ||
| verification = verify_chain(token, private_key.public_key()) | ||
|
|
||
| print(f"\nHDP chain valid: {verification.valid}") | ||
| print(f"Hops recorded: {verification.hop_count}") | ||
| if verification.violations: | ||
| print(f"Violations: {verification.violations}") | ||
|
|
||
| # --- Tamper resistance --- | ||
| # Mutating any hop signature causes verify_chain to return valid=False. | ||
| # The first invalid hop is flagged; subsequent hops are not re-checked | ||
| # (each is verified against the cumulative chain, so later results are | ||
| # unreliable once an earlier hop is broken). | ||
| if verification.hop_count > 0: | ||
| tampered = dict(token) | ||
| tampered["chain"] = [dict(h) for h in token["chain"]] | ||
| tampered["chain"][0]["hop_signature"] = "AAAA" # simulate attacker modifying chain | ||
| tampered_result = verify_chain(tampered, private_key.public_key()) | ||
| print(f"\nTampered chain valid: {tampered_result.valid}") # False | ||
| print(f"Tampered violations: {tampered_result.violations}") | ||
|
|
||
| # --- max_hops --- | ||
| # ScopePolicy(max_hops=N) caps delegation depth. verify_chain adds a | ||
| # violation when len(chain) > max_hops, returning valid=False. | ||
| # Example: scope=ScopePolicy(intent="...", max_hops=2) | ||
|
|
||
| # --- Strict vs audit mode --- | ||
| # strict=False (default) - scope violations are recorded in the token | ||
| # for post-hoc audit; the agent continues running. | ||
| # strict=True - raises HDPScopeViolationError immediately on | ||
| # any out-of-scope tool call. | ||
| # Example: HdpMiddleware(..., strict=True) | ||
|
|
||
| # Full spec: https://helixar.ai/about/labs/hdp/ | ||
| # arXiv paper: https://arxiv.org/abs/2604.04522 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.