1. AI Configuration Management Using Vault KV

    Python

    HashiCorp's Vault is a tool for secrets management, allowing you to securely store and access sensitive data, such as API keys, passwords, and certificates. One of the features of Vault is the Key/Value (KV) secrets engine, which allows you to store and manage arbitrary secrets. It can be used for storing configuration settings and other sensitive information that doesn't fit neatly into the existing secret engines.

    In Pulumi, you can manage Vault secrets by using the Vault provider, which allows you to automate the creation and management of Vault secrets. Below, I'll provide a Pulumi program written in Python that sets up a Vault KV secret, specifically using the vault.kv.SecretV2 resource. This resource manages KV-V2 secrets within Vault, which supports versioning and soft deletion of secrets.

    The following Pulumi program demonstrates how to create a KV secret in Vault:

    import pulumi import pulumi_vault as vault # Ensure you've installed the `pulumi_vault` package using pip to work with Vault. # Also, ensure that you have properly authenticated the `Vault` CLI tool and that # it is configured to interact with your Vault server endpoint. # Deploying a new Vault KV version 2 secret # Here we're creating a secret named 'app-config' within the 'kv' path. # The actual secret content will be the JSON object provided in `data_json`. kv_v2_secret = vault.kv.SecretV2("app-config", name="database-creds", mount="kv", data_json=pulumi.Output.secret("""{ "username": "dbuser", "password": "dbpass" }"""), ) # Optionally, you can export the generated ID of the secret to access it later. # Note: Exporting the actual secret values is generally unsafe, but exporting # identifiers and metadata is fine. pulumi.export("kv_v2_secret_id", kv_v2_secret.id)

    This program does the following:

    • Imports the necessary Pulumi and Pulumi Vault modules.
    • Creates a Vault KV-V2 secret with a given name and data using the vault.kv.SecretV2 resource.
    • The data_json parameter accepts a JSON string of our sensitive data – in this example, database credentials. It's marked with pulumi.Output.secret to ensure that Pulumi treats this as sensitive information and redacts it from plaintext logging.
    • Finally, it exports the ID of the KV secret. This can be useful if you need to reference the secret in other parts of your infrastructure but do not want to expose its value.

    To use this code, you'll need:

    • A Vault server up and running
    • The pulumi_vault Python package installed in your environment (install using pip install pulumi_vault)
    • To have logged in to Vault using the CLI such that your environment can authenticate requests to the Vault server

    This program assumes that the kv secrets engine is enabled at the path kv/. If you have enabled the secrets engine at a different path or not yet enabled it, you’ll need to do so before this code will work.

    To enable the KV-V2 secrets engine at a path in Vault CLI, you can run:

    vault secrets enable -path=kv kv-v2

    Ensure you've set up proper access and authentication policies to interact with the Vault. This will require administrative access to the Vault server.

    Please note that I’ve redacted the actual secrets to avoid exposing sensitive data. Always keep your secrets safe and never expose them in your source code.

    After setting up this Pulumi program, you can run pulumi up to deploy the resources specified in the program. When you want to update or manage these secrets, you can simply update this Pulumi program and run it again to apply the changes.