Skip to main content

InfraHouse Core

Need Help? Contact Us PyPI Version Documentation Status Latest Release AWS Services Python Versions License

Lightweight Python library with AWS and other general purpose classes.

Requirements

  • Python 3.10+

  • Linux or macOS (Windows is not supported)

Installation

pip install infrahouse-core

Features

AWS Classes

  • EC2Instance - Manage EC2 instances with SSM command execution support

  • ASGInstance - EC2 instance within an AutoScaling Group (lifecycle state, protection, health)

  • ASG - AutoScaling Group lifecycle management (instance refresh, lifecycle hooks)

  • DynamoDBTable - DynamoDB operations with distributed locking support

  • Route53 Zone - DNS record management (add/delete A records)

  • Secret - AWS Secrets Manager operations (create, read, update, delete)

  • InstanceIdentity - Verify an EC2 instance identity document with its RSA-2048 PKCS#7 signature and AWS’s certificate for the region (InstanceIdentityCertificates, bundled), and check the instance, account and region it names

  • AWS Session Management - SSO login, role assumption, credential handling via get_session(), get_client(), get_resource() helpers

GitHub Integration

  • GitHubActions - Manage self-hosted GitHub Actions runners

  • GitHubActionsRunner - Query runner status, labels, and metadata

  • GitHubAppTokenSource - GitHub App installation tokens with only the permissions you ask for, with the App JWT signed in KMS (KMSSigner) or with a PEM key in AWS Secrets Manager (PEMSecretSigner)

  • get_tmp_token() - Token generation from GitHub App credentials stored in AWS Secrets Manager

  • GitHub REST API calls retry dropped connections, 429 and 5xx responses with exponential backoff

  • RegistrationTokenSender / RegistrationTokenReceiver - Deliver a runner registration token to a new EC2 instance over SSM, encrypted to a key the instance makes in a ramfs, after verifying its signed identity document

Orchestrator

  • OrchestratorRaftCluster - Reconcile MySQL Orchestrator Raft membership against an ASG

  • OrchestratorRaftNode - Interact with a single Orchestrator node’s Raft API via SSM

Utilities

  • Input Validation - Validators for AWS resource identifiers (instance IDs, ARNs, regions, DNS names)

  • Timeout - Context manager for enforcing execution time limits (POSIX only)

  • Filesystem - Secure permission management (ensure_permissions())

  • Logging - Configurable logging setup with stdout/stderr separation

Usage Examples

EC2 Instance with SSM Command Execution

from infrahouse_core.aws.ec2_instance import EC2Instance

# Connect to an instance (optionally assume a role for cross-account access)
instance = EC2Instance(
    instance_id="i-0123456789abcdef0",
    region="us-east-1",
    role_arn="arn:aws:iam::123456789012:role/MyRole"  # optional
)

# Execute a command via SSM
exit_code, stdout, stderr = instance.execute_command("hostname")

# Access instance properties
print(instance.private_ip)
print(instance.hostname)
print(instance.tags)

DynamoDB Distributed Lock

from infrahouse_core.aws.dynamodb import DynamoDBTable

table = DynamoDBTable("my-lock-table", region="us-east-1")

with table.lock("my-resource", timeout=30):
    # Critical section - only one process can hold this lock
    do_exclusive_work()

Route53 DNS Management

from infrahouse_core.aws.route53.zone import Zone

zone = Zone(zone_name="example.com")

# Add an A record
zone.add_record("myhost", "10.0.0.1", ttl=300)

# Delete an A record
zone.delete_record("myhost", "10.0.0.1")

Secrets Manager

from infrahouse_core.aws.secretsmanager import Secret

# Read a secret
secret = Secret("my-app/api-key", region="us-east-1")
print(secret.value)  # Returns dict if JSON, else string

# Create or update a secret
secret.ensure_present({"username": "admin", "password": "secret123"})

# Cross-account access
secret = Secret(
    "prod/database-creds",
    role_arn="arn:aws:iam::123456789012:role/SecretsReader"
)

Cross-Account AWS Access

from infrahouse_core.aws import get_client, get_session

# Get a client with assumed role
ec2 = get_client(
    "ec2",
    role_arn="arn:aws:iam::123456789012:role/CrossAccountRole",
    region="us-west-2"
)

# Or get a session for multiple clients
session = get_session(role_arn="arn:aws:iam::123456789012:role/MyRole")
ec2 = session.client("ec2")
s3 = session.client("s3")

Orchestrator Raft Reconciliation

from infrahouse_core.orchestrator import OrchestratorRaftCluster

cluster = OrchestratorRaftCluster(
    "my-orchestrator-asg",
    region="us-east-1",
    role_arn="arn:aws:iam::123456789012:role/OrchestratorRole",  # optional
)

# Reconcile Raft peers with live ASG instances
# (removes stale peers, adds missing ones)
cluster.reconcile()

# Inspect current state
for node in cluster.nodes:
    print(f"{node.hostname}: leader={node.is_leader}")

GitHub Actions Runner Management

from infrahouse_core.github import GitHubActions, GitHubAuth

github = GitHubAuth(token="ghp_...", org="my-org")
actions = GitHubActions(github)

# List all runners
for runner in actions.runners:
    print(f"{runner.name}: {runner.status}")

# Find runners by label
runners = actions.find_runners_by_label("self-hosted")

GitHub App Installation Tokens

GitHubAppTokenSource gets an installation token for a GitHub App installed in an organization. It signs the App JWT with a signer. KMSSigner signs with kms:Sign, so the App’s private key never leaves KMS. PEMSecretSigner reads the key from AWS Secrets Manager, as get_tmp_token() does.

from infrahouse_core.github import GitHubActions, GitHubAppTokenSource, GitHubAuth, KMSSigner

# Create the source once per process, for example at module scope in a Lambda,
# so that the KMS client is created once and reused.
source = GitHubAppTokenSource(
    123456,  # the GitHub App ID
    KMSSigner("arn:aws:kms:us-west-2:123456789012:key/1234abcd-12ab-34cd-56ef-1234567890ab"),
)

# Ask only for the permissions you need. Without permissions, the token gets all of the App's permissions.
token = source.installation_token("my-org", permissions={"organization_self_hosted_runners": "write"})
actions = GitHubActions(GitHubAuth(token=token, org="my-org"))

The KMS key must have key spec RSA_2048 and key usage SIGN_VERIFY, with its key material imported from the App’s private key (Origin=EXTERNAL). GitHub generates the App key and doesn’t accept one made elsewhere, so the key can’t be generated in KMS.

The caller needs kms:Sign on the key, limited to the signing algorithm that KMSSigner uses. The key policy must allow it too, for example the default key policy, which delegates to IAM:

{
    "Effect": "Allow",
    "Action": "kms:Sign",
    "Resource": "arn:aws:kms:us-west-2:123456789012:key/1234abcd-12ab-34cd-56ef-1234567890ab",
    "Condition": {
        "StringEquals": {"kms:SigningAlgorithm": "RSASSA_PKCS1_V1_5_SHA_256"}
    }
}

Code that takes a GitHubTokenSource, the interface, works with any source, such as a token broker added later.

Runner Registration Token over SSM

infrahouse_core.registration_token_delivery gives a new runner instance its registration token without the token being in an SSM parameter or output, a secret or a file. The sender (a registration Lambda) has the instance make an RSA key in a ramfs at /run/gha-registration and accepts it only with an identity document that AWS signed for that instance, account and region. It then writes the token encrypted with RSA-OAEP (SHA-256). The receiver, root on the instance, decrypts it in memory, deletes the key and the ciphertext on every exit, and leaves a marker after a successful registration so that no later delivery is accepted.

from infrahouse_core.aws.ec2_instance import EC2Instance
from infrahouse_core.registration_token_delivery import RegistrationTokenSender

# The sender, while the instance waits in its launch lifecycle hook.
sender = RegistrationTokenSender(EC2Instance(instance_id), account_id, region)
sender.request_key(send_timeout=240)
sender.ensure_pending_wait(asg_instance.lifecycle_state)  # no job can have run on it yet
# ... check that GitHub doesn't list the runner, then mint the registration token ...
sender.deliver(registration_token)  # only to the key request_key() verified, once
from infrahouse_core.registration_token_delivery import RegistrationTokenReceiver

# The receiver, as root on the instance. Pass the token in the environment, never in argv.
with RegistrationTokenReceiver().registration_token() as token:
    subprocess.run(
        [config_sh, "--url", url, "--unattended", "--disableupdate"],
        env={"ACTIONS_RUNNER_INPUT_TOKEN": token, "PATH": "/usr/bin:/bin"},
        user="github-runner",
        umask=0o022,
        check=True,
    )

Contributing

Contributions are welcome! Please see CONTRIBUTING.rst for guidelines.

License

Apache Software License 2.0. See LICENSE for details.

Credits

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

Metadata

Release files for infrahouse-core 1.5.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for infrahouse-core 1.5.1
File Size Uploaded Provenance Actions
infrahouse_core-1.5.1.tar.gz 240.8 kB Without provenance Details for infrahouse_core-1.5.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for infrahouse-core 1.5.1
File Interpreter ABI Platform Actions
infrahouse_core-1.5.1-py3-none-any.whl Python 3 none any Details for infrahouse_core-1.5.1-py3-none-any.whl

Total release size: 398.5 kB

Release files / infrahouse_core-1.5.1.tar.gz

Download URL infrahouse_core-1.5.1.tar.gz
Size 240.8 kB
Tags Source
SHA-256 checksum
How to use checksums
6df7523aa854047975dc7b87d5eaf7afdacf637957f5c7c4685471e1db9aa493
BLAKE2b-256 checksum
How to use checksums
77fd9e3314e9820711aa83505ca0dab211f96bd6dad61697475164c8714fbd1c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.11

Release files / infrahouse_core-1.5.1-py3-none-any.whl

Download URL infrahouse_core-1.5.1-py3-none-any.whl
Size 157.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4d655c8c4b4af3d03eea46a93571b96a41fd9f4dbe8f954a0b008f73165d1d04
BLAKE2b-256 checksum
How to use checksums
2870e9e6d7902e0ddd08b5eacf6303903ab7f80fa328af5586a0760cf3df0c8a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.11

Security for infrahouse-core 1.5.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.5.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page