Microsoft Azure Confidential Ledger Client Library for Python
Project description
Azure Confidential Ledger client library for Python
Azure Confidential Ledger provides a service for logging to an immutable, tamper-proof ledger. As part of the Azure Confidential Computing portfolio, Azure Confidential Ledger runs in secure, hardware-based trusted execution environments, also known as enclaves. It is built on Microsoft Research's Confidential Consortium Framework.
Source code | Package (PyPI) | API reference documentation | Product documentation
Getting started
Install packages
Install azure-confidentialledger and azure-identity with pip:
pip install azure-identity azure-confidentialledger
azure-identity is used for Azure Active Directory authentication as demonstrated below.
Prerequisites
- An Azure subscription
- Python 2.7, 3.5.3, or later
- A running instance of Azure Confidential Ledger.
- A registered user in the Confidential Ledger, typically assigned during ARM resource creation, with
Administrator
privileges.
Authenticate the client
Using Azure Active Directory
This document demonstrates using DefaultAzureCredential to authenticate to the Confidential Ledger via Azure Active Directory. However, ConfidentialLedgerClient
accepts any azure-identity credential. See the azure-identity documentation for more information about other credentials.
Using a client certificate
As an alternative to Azure Active Directory, clients may choose to use a client certificate to authenticate via mutual TLS. azure.confidentialledger.ConfidentialLedgerCertificateCredential
may be used for this purpose.
Create a client
DefaultAzureCredential
will automatically handle most Azure SDK client scenarios. To get started, set environment variables for the AAD identity registered with your Confidential Ledger.
export AZURE_CLIENT_ID="generated app id"
export AZURE_CLIENT_SECRET="random password"
export AZURE_TENANT_ID="tenant id"
Then, DefaultAzureCredential
will be able to authenticate the ConfidentialLedgerClient
.
Constructing the client also requires your Confidential Ledger's URL and id, which you can get from the Azure CLI or the Azure Portal. When you have retrieved those values, please replace instances of "my-ledger-id"
and "https://my-ledger-url.confidential-ledger.azure.com"
in the examples below. You may also need to replace "https://identity.accledger.azure.com"
with the hostname from the identityServiceUri
in the ARM description of your ledger.
Because Confidential Ledgers use self-signed certificates securely generated and stored in an enclave, the signing certificate for each Confidential Ledger must first be retrieved from the Confidential Ledger Identity Service.
from azure.identity import DefaultAzureCredential
from azure.confidentialledger import ConfidentialLedgerClient
from azure.confidentialledger.identity_service import ConfidentialLedgerIdentityServiceClient
identity_client = ConfidentialLedgerIdentityServiceClient("https://identity.accledger.azure.com")
network_identity = identity_client.get_ledger_identity(
ledger_id="my-ledger-id"
)
ledger_tls_cert_file_name = "ledger_certificate.pem"
with open(ledger_tls_cert_file_name, "w") as cert_file:
cert_file.write(network_identity.ledger_tls_certificate)
credential = DefaultAzureCredential()
ledger_client = ConfidentialLedgerClient(
endpoint="https://my-ledger-url.confidential-ledger.azure.com",
credential=credential,
ledger_certificate_path=ledger_tls_cert_file_name
)
Key concepts
Ledger entries and transactions
Every write to Azure Confidential Ledger generates an immutable ledger entry in the service. Writes, also referred to as transactions, are uniquely identified by transaction ids that increment with each write. Once written, ledger entries may be retrieved at any time.
Receipts
State changes to the Confidential Ledger are saved in a data structure called a Merkle tree. To cryptographically verify that writes were correctly saved, a Merkle proof, or receipt, can be retrieved for any transaction id.
Sub-ledgers
While most use cases will involve one ledger, we provide the sub-ledger feature in case semantically or logically different groups of data need to be stored in the same Confidential Ledger.
Ledger entries are retrieved by their sub-ledger identifier. The Confidential Ledger will always assume a constant, service-determined sub-ledger id for entries submitted without a sub-ledger specified.
Users
Users are managed directly with the Confidential Ledger instead of through Azure. Users may be AAD-based, identified by their AAD object id, or certificate-based, identified by their PEM certificate fingerprint.
Confidential computing
Azure Confidential Computing allows you to isolate and protect your data while it is being processed in the cloud. Azure Confidential Ledger runs on Azure Confidential Computing virtual machines, thus providing stronger data protection with encryption of data in use.
Confidential Consortium Framework
Azure Confidential Ledger is built on Microsoft Research's open-source Confidential Consortium Framework (CCF). Under CCF, applications are managed by a consortium of members with the ability to submit proposals to modify and govern application operation. In Azure Confidential Ledger, Microsoft Azure owns a member identity, allowing it to perform governance actions like replacing unhealthy nodes in the Confidential Ledger, or upgrading the enclave code.
Examples
This section contains code snippets covering common tasks:
- Append a ledger entry
- Get a receipt
- Using sub-ledgers
- Retrieving ledger entries
- Making a ranged query
- Managing users
- Using certificate authentication
- Verifying service details
- Asynchronously get a ledger entry
- Asynchronously get a range of ledger entries
Append entry
Data that needs to be stored immutably in a tamper-proof manner can be saved to Azure Confidential Ledger by appending an entry to the ledger.
first_append_result = ledger_client.append_to_ledger(entry_contents="Hello world!")
print(first_append_result.transaction_id)
Since Confidential Ledger is a distributed system, rare transient failures may cause writes to be lost. For entries that must be preserved, it is advisable to verify that the write became durable. Waits are blocking calls.
from azure.confidentialledger import TransactionState
ledger_client.wait_until_durable(transaction_id=first_append_result.transaction_id)
assert ledger_client.get_transaction_status(
transaction_id=first_append_result.transaction_id
).state is TransactionState.COMMITTED
# Alternatively, a client may wait when appending.
append_result = ledger_client.append_to_ledger(
entry_contents="Hello world, again!", wait_for_commit=True
)
assert ledger_client.get_transaction_status(
transaction_id=append_result.transaction_id
).state is TransactionState.COMMITTED
Get receipt
A receipt can be retrieved for any transaction id to provide cryptographic proof of the contents of the transaction.
receipt = ledger_client.get_transaction_receipt(
transaction_id=append_result.transaction_id
)
print(receipt.contents)
Using sub-ledgers
Clients can write to different sub-ledgers to separate logically-distinct data.
ledger_client.append_to_ledger(
entry_contents="Hello from Alice", sub_ledger_id="Alice"
)
ledger_client.append_to_ledger(
entry_contents="Hello from Bob", sub_ledger_id="Bob"
)
When no sub-ledger id is specified on method calls, the Confidential Ledger service will assume a constant, service-determined sub-ledger id.
append_result = ledger_client.append_to_ledger(entry_contents="Hello world?", wait_for_commit=True)
# The append result contains the sub-ledger id assigned.
entry_by_subledger = ledger_client.get_ledger_entry(
transaction_id=append_result.transaction_id,
sub_ledger_id=append_result.sub_ledger_id
)
assert entry_by_subledger.contents == "Hello world?"
# When a ledger entry is retrieved without a sub-ledger specified,
# the service default is used.
entry = ledger_client.get_ledger_entry(transaction_id=append_result.transaction_id)
assert entry.contents == entry_by_subledger.contents
assert entry.sub_ledger_id == entry_by_subledger.sub_ledger_id
Retrieving ledger entries
Ledger entries are retrieved from sub-ledgers. When a transaction id is specified, the returned value is the value contained in the specified sub-ledger at the point in time identified by the transaction id. If no transaction id is specified, the latest available value is returned.
append_result = ledger_client.append_to_ledger(entry_contents="Hello world 0")
ledger_client.append_to_ledger(entry_contents="Hello world 1")
subledger_append_result = ledger_client.append_to_ledger(
entry_contents="Hello world sub-ledger 0",
sub_ledger_id="sub-ledger"
)
ledger_client.append_to_ledger(
entry_contents="Hello world sub-ledger 1",
sub_ledger_id="sub-ledger",
wait_for_commit=True
)
# The ledger entry written at 'append_result.transaction_id'
# is retrieved from the default sub-ledger.
entry = ledger_client.get_ledger_entry(transaction_id=append_result.transaction_id)
assert entry.contents == "Hello world 0"
# This is the latest entry available in the default sub-ledger.
latest_entry = ledger_client.get_ledger_entry()
assert latest_entry.contents == "Hello world 1"
# The ledger entry written at 'subledger_append_result.transaction_id'
# is retrieved from the sub-ledger 'sub-ledger'.
subledger_entry = ledger_client.get_ledger_entry(
transaction_id=subledger_append_result.transaction_id,
sub_ledger_id="sub-ledger"
)
assert subledger_entry.contents == "Hello world sub-ledger 0"
# This is the latest entry available in the sub-ledger 'sub-ledger'.
subledger_latest_entry = ledger_client.get_ledger_entry(
sub_ledger_id="sub-ledger"
)
assert subledger_latest_entry.contents == "Hello world sub-ledger 1"
Making a ranged query
Ledger entries in a sub-ledger may be retrieved over a range of transaction ids.
ranged_result = ledger_client.get_ledger_entries(
from_transaction_id=first_append_result.transaction_id
)
for entry in ranged_result:
print(f"Transaction id {entry.transaction_id} contents: {entry.contents}")
Managing users
Users with Administrator
privileges can manage users of the Confidential Ledger directly with the Confidential Ledger itself. Available roles are Reader
(read-only), Contributor
(read and write), and Administrator
(read, write, and add or remove users).
from azure.confidentialledger import LedgerUserRole
user_id = "some AAD object id"
user = ledger_client.create_or_update_user(
user_id, LedgerUserRole.CONTRIBUTOR
)
# A client may now be created and used with AAD credentials for the user identified by `user_id`.
user = ledger_client.get_user(user_id)
assert user.id == user_id
assert user.role == LedgerUserRole.CONTRIBUTOR
ledger_client.delete_user(user_id)
# For a certificate-based user, their user ID is the fingerprint for their PEM certificate.
user_id = "PEM certificate fingerprint"
user = ledger_client.create_or_update_user(
user_id, LedgerUserRole.READER
)
Using certificate authentication
Clients may authenticate with a client certificate in mutual TLS instead of via an Azure Active Directory token. ConfidentialLedgerCertificateCredential
is provided for such clients.
from azure.confidentialledger import ConfidentialLedgerClient, ConfidentialLedgerCertificateCredential
from azure.confidentialledger.identity_service import ConfidentialLedgerIdentityServiceClient
identity_client = ConfidentialLedgerIdentityServiceClient("https://identity.accledger.azure.com")
network_identity = identity_client.get_ledger_identity(
ledger_id="my-ledger-id"
)
ledger_tls_cert_file_name = "ledger_certificate.pem"
with open(ledger_tls_cert_file_name, "w") as cert_file:
cert_file.write(network_identity.ledger_tls_certificate)
credential = ConfidentialLedgerCertificateCredential("path to user certificate PEM")
ledger_client = ConfidentialLedgerClient(
endpoint="https://my-ledger-url.confidential-ledger.azure.com",
credential=credential,
ledger_certificate_path=ledger_tls_cert_file_name
)
Verifying service details
One may want to validate details about the Confidential Ledger for a variety of reasons. For example, you may want to view details about how Microsoft may manage your Confidential Ledger as part of Confidential Consortium Framework governance, or verify that your Confidential Ledger is indeed running in a secure enclave. A number of client methods are provided for these use cases.
consortium = ledger_client.get_consortium()
# Consortium members can manage and alter the Confidential Ledger,
# such as by replacing unhealthy nodes.
for member in consortium.members:
print(member.certificate)
print(member.id)
import hashlib
# The constitution is a collection of JavaScript code that
# defines actions available to members,
# and vets proposals by members to execute those actions.
constitution = ledger_client.get_constitution()
assert constitution.digest.lower() == \
hashlib.sha256(constitution.contents.encode()).hexdigest().lower()
print(constitution.contents)
print(constitution.digest)
# Enclave quotes contain material that can be used to
# cryptographically verify the validity and contents
# of an enclave.
ledger_enclaves = ledger_client.get_enclave_quotes()
assert ledger_enclaves.source_node in ledger_enclaves.quotes
for node_id, quote in ledger_enclaves.quotes.items():
assert node_id == quote.node_id
print(quote.node_id)
print(quote.mrenclave)
print(quote.raw_quote)
print(quote.version)
Microsoft Azure Attestation Service is one provider of enclave quotes.
Async API
This library includes a complete async API supported on Python 3.5+. To use it, you must first install an async transport, such as aiohttp. See the azure-core documentation for more information.
An async client is obtained from azure.confidentialledger.aio
. Methods have the same names and signatures as the synchronous client.
Async clients should be closed when they're no longer needed. These objects are async context managers and define async close
methods. For example:
from azure.identity.aio import DefaultAzureCredential
from azure.confidentialledger.aio import ConfidentialLedgerClient
from azure.confidentialledger.identity_service.aio import ConfidentialLedgerIdentityServiceClient
identity_client = ConfidentialLedgerIdentityServiceClient("https://identity.accledger.azure.com")
network_identity = await identity_client.get_ledger_identity(
ledger_id="my-ledger-id"
)
ledger_tls_cert_file_name = "ledger_certificate.pem"
with open(ledger_tls_cert_file_name, "w") as cert_file:
cert_file.write(network_identity.ledger_tls_certificate)
credential = DefaultAzureCredential()
ledger_client = ConfidentialLedgerClient(
endpoint="https://my-ledger-url.confidential-ledger.azure.com",
credential=credential,
ledger_certificate_path=ledger_tls_cert_file_name
)
# Call close when the client and credential are no longer needed.
await client.close()
await credential.close()
# Alternatively, use them as async context managers (contextlib.AsyncExitStack can help).
ledger_client = ConfidentialLedgerClient(
endpoint="https://my-ledger-url.confidential-ledger.azure.com",
credential=credential,
ledger_certificate_path=ledger_tls_cert_file_name
)
async with client:
async with credential:
pass
Asynchronously get a ledger entry
Ledger entries may be retrieved with the async client.
entry = await self.client.get_ledger_entry()
print(entry.contents)
print(entry.sub_ledger_id)
Asynchronously get a range of ledger entries
Ledger entries may be retrieved over a range with the async client.
query_result = client.get_ledger_entries(
from_transaction_id="12.3"
)
async for entry in query_result:
print(entry.transaction_id)
print(entry.contents)
Troubleshooting
General
Confidential Ledger clients raise exceptions defined in azure-core. For example, if you try to get a transaction that doesn't exist, ConfidentialLedgerClient
raises ResourceNotFoundError:
from azure.core.exceptions import ResourceNotFoundError
from azure.identity import DefaultAzureCredential
from azure.confidentialledger import ConfidentialLedgerClient
from azure.confidentialledger.identity_service import ConfidentialLedgerIdentityServiceClient
identity_client = ConfidentialLedgerIdentityServiceClient("https://identity.accledger.azure.com")
network_identity = identity_client.get_ledger_identity(
ledger_id="my-ledger-id"
)
ledger_tls_cert_file_name = "ledger_certificate.pem"
with open(ledger_tls_cert_file_name, "w") as cert_file:
cert_file.write(network_identity.ledger_tls_certificate)
credential = DefaultAzureCredential()
ledger_client = ConfidentialLedgerClient(
endpoint="https://my-ledger-url.confidential-ledger.azure.com",
credential=credential,
ledger_certificate_path=ledger_tls_cert_file_name
)
try:
ledger_client.get_ledger_entry(transaction_id="10000.100000")
except ResourceNotFoundError as e:
print(e.message)
Logging
This library uses the standard logging library for logging. Basic information about HTTP sessions (URLs, headers, etc.) is logged at INFO level.
Detailed DEBUG level logging, including request/response bodies and unredacted headers, can be enabled on a client with the logging_enable
argument:
import logging
import sys
from azure.identity import DefaultAzureCredential
from azure.confidentialledger import ConfidentialLedgerClient
from azure.confidentialledger.identity_service import ConfidentialLedgerIdentityServiceClient
# Create a logger for the 'azure' SDK
logger = logging.getLogger('azure')
logger.setLevel(logging.DEBUG)
# Configure a console output
handler = logging.StreamHandler(stream=sys.stdout)
logger.addHandler(handler)
identity_client = ConfidentialLedgerIdentityServiceClient("https://identity.accledger.azure.com")
network_identity = identity_client.get_ledger_identity(
ledger_id="my-ledger-id"
)
ledger_tls_cert_file_name = "ledger_certificate.pem"
with open(ledger_tls_cert_file_name, "w") as cert_file:
cert_file.write(network_identity.ledger_tls_certificate)
credential = DefaultAzureCredential()
# This client will log detailed information about its HTTP sessions, at DEBUG level
ledger_client = ConfidentialLedgerClient(
endpoint="https://my-ledger-url.confidential-ledger.azure.com",
credential=credential,
ledger_certificate_path=ledger_tls_cert_file_name,
logging_enable=True
)
Similarly, logging_enable
can enable detailed logging for a single operation, even when it isn't enabled for the client:
ledger_client.get_ledger_entry(transaction_id="12.3", logging_enable=True)
Next steps
Additional Documentation
For more extensive documentation on Azure Confidential Ledger, see the API reference documentation. You may also read more about Microsoft Research's open-source Confidential Consortium Framework.
Contributing
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
Release History
1.0.0b1 (2021-05-12)
- This is the initial release of the Azure Confidential Ledger library.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file azure-confidentialledger-1.0.0b1.zip
.
File metadata
- Download URL: azure-confidentialledger-1.0.0b1.zip
- Upload date:
- Size: 104.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b5ede5302602e326e4d74f1a468c4e18338666250750e0c1f857fcbccf11f2e6 |
|
MD5 | 852757bad14ca605e861996e183ef0de |
|
BLAKE2b-256 | ca1b4615140f7be3853002e0fd379873d7612fcd6b9d4dedb9bce163069a751a |
File details
Details for the file azure_confidentialledger-1.0.0b1-py2.py3-none-any.whl
.
File metadata
- Download URL: azure_confidentialledger-1.0.0b1-py2.py3-none-any.whl
- Upload date:
- Size: 69.3 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cf4ed794d1035652c804df4bcbe5ab3b5e336c090a11283e02a6424ac5d41ba7 |
|
MD5 | 1f40db33e28e697c3ab2303528c390d4 |
|
BLAKE2b-256 | bf573c7c5a4f6eca3734fc00eae54624e9aa8f185ba06229e1581ee1e8ae57fe |