Skip to content

Instantly share code, notes, and snippets.

@dbluhm
Last active August 1, 2024 07:50
Show Gist options
  • Save dbluhm/91fe68c26730f4de74d0222d0d0b4737 to your computer and use it in GitHub Desktop.
Save dbluhm/91fe68c26730f4de74d0222d0d0b4737 to your computer and use it in GitHub Desktop.
Askar DB Provision Behavior test

Testing Aries Askar Provision behavior

This is a quick demonstration of the provision behavior of Aries Askar.

To run:

docker-compose build
docker-compose run test
docker-compose logs postgres
docker-compose down -v

This will:

  • build
  • run the test script which connects to a Postgres Askar store and performs a few actions
  • print the logs from the postgres DB which will show all queries executed
  • and then clean up the containers.

As demonstrated, the CREATE DATABASE query in the init.sql prevents Askar from attempting to create a DB itself.

version: '3.8'
services:
postgres:
image: postgres:latest
environment:
POSTGRES_USER: admin
POSTGRES_PASSWORD: admin
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql:z
ports:
- "5432:5432"
command: ["postgres", "-c", "log_statement=all"]
test:
build: .
environment:
SEED: askar-db-test
LOG_LEVEL: WARNING
volumes:
- ./main.py:/app/main.py:z
depends_on:
- postgres
command: python /app/main.py
FROM python:3.9-slim
WORKDIR /app
RUN pip install aries-askar
CMD ["python", "main.py"]
-- Use the default 'postgres' database to create new database and user
\c postgres
-- Create a new database
CREATE DATABASE mydb;
-- Create a new user with a password
CREATE USER myuser WITH PASSWORD 'mypassword';
-- Grant connect privilege to the user
GRANT CONNECT ON DATABASE mydb TO myuser;
-- Change to the new database to set further permissions
\c mydb
-- Grant privileges to create schemas in the new database
GRANT CREATE ON DATABASE mydb TO myuser;
-- Grant usage and create privileges on all schemas that will be created
-- This needs to be executed each time a new schema is added
GRANT USAGE, CREATE ON SCHEMA public TO myuser;
-- Grant all privileges on all tables in the public schema
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO myuser;
-- Set the default privileges for new tables created in public schema
ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO myuser;
-- Make the user an owner of the schema to manage and create new tables and other objects
ALTER SCHEMA public OWNER TO myuser;
"""Test askar db setup."""
import logging
from os import getenv
from secrets import token_hex
from aries_askar import Key, KeyAlg, Store
SEED = getenv("SEED")
LOG_LEVEL = getenv("LOG_LEVEL", "").upper() or None
logging.basicConfig(level=LOG_LEVEL)
async def main():
"""Run test."""
assert SEED
key = Store.generate_raw_key(SEED)
store = await Store.provision(
"postgres://myuser:mypassword@postgres:5432/mydb", key_method="RAW", pass_key=key
)
async with store.session() as session:
sigkey = Key.generate(KeyAlg.ED25519)
id = token_hex()
await session.insert_key(id, sigkey)
entry = await session.fetch_key(id)
print(entry)
if __name__ == "__main__":
import asyncio
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment