Project

opdotenv

0.0
The project is in a healthy, maintained state
Read environment variables from 1Password fields (dotenv/json/yaml format) or all fields using the op CLI or 1Password Connect Server API. Export local .env files back to 1Password.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies
 Project Readme

opdotenv

Gem Version Test Status codecov Quality Gate Status

Load environment variables from 1Password using the op CLI or 1Password Connect Server API. Supports dotenv, JSON, and YAML formats.

Installation

Add to your Gemfile:

gem "opdotenv"

Requirements

Choose one:

  • 1Password CLI (op) - must be installed and authenticated (op signin)
    • By default, op is expected to be in your PATH
    • You can configure a custom path via OP_CLI_PATH or OPDOTENV_CLI_PATH environment variables, or via Rails config (see below)
  • 1Password Connect Server - set OP_CONNECT_URL and OP_CONNECT_TOKEN environment variables

Ruby 3.4+ required.

Rails

Configure in config/application.rb or environment-specific files:

Rails.application.configure do
  config.opdotenv.sources = [
    "op://Vault/.env.development",  # dotenv format (inferred)
    "op://Vault/config.json",        # json format (inferred from .json extension)
    "op://Vault/App"                 # all fields without parsing
  ]
end

Format is inferred from the item or field basename (see Supported formats). Field names with extensions work in the path, for example op://Vault/Item Name/config.json.

1Password Connect

Rails.application.configure do
  config.opdotenv.connect_url = "https://connect.example.com"
  config.opdotenv.connect_token = Rails.application.credentials.dig(:op_connect, :token)
end

Configure op CLI path

If your op CLI command is not in your PATH or you want to use a custom path:

Rails.application.configure do
  config.opdotenv.cli_path = "/usr/local/bin/op"
end

Alternatively, you can set the OP_CLI_PATH or OPDOTENV_CLI_PATH environment variable:

export OP_CLI_PATH=/usr/local/bin/op

Disable automatic loading

Rails.application.configure do
  config.opdotenv.auto_load = false
end

# Load manually when needed
Opdotenv::Loader.load("op://Vault/Item")

Standalone usage

require "opdotenv"

# Load from dotenv format (format inferred from item name)
Opdotenv::Loader.load("op://Vault/.env.development")

# Load from JSON format (any item name ending with .json)
Opdotenv::Loader.load("op://Vault/config.json")
Opdotenv::Loader.load("op://Vault/production.json")

# Load from field with extension in path
Opdotenv::Loader.load("op://Vault/Item Name/config.json")

# Load all fields from an item
Opdotenv::Loader.load("op://Vault/App")

# Don't overwrite existing ENV values
Opdotenv::Loader.load("op://Vault/Item", overwrite: false)

Anyway Config integration

Automatically registers when anyway_config is available:

class AppConfig < Anyway::Config
  attr_config :api_key, :api_secret

  # Format is inferred from item name
  loader_options opdotenv: {
    path: "op://Vault/.env.development"  # dotenv format inferred
  }
end

# Or load all fields from an item
class DatabaseConfig < Anyway::Config
  attr_config :url, :username, :password

  loader_options opdotenv: {
    path: "op://Vault/Database"  # all fields loaded
  }
end

Conditional loading

Load from 1Password only in local or development environments:

class TestConfig < Anyway::Config
  config_name :test
  attr_config :enabled, :sample

  if Rails.env.local?
    loader_options opdotenv: {
      path: "op://Employee/.env.test"
    }
  end
end

See Environment recommendation.

Loading All Fields from an Item

When loading all fields from a 1Password item (not a parsed format), field names are automatically normalized to match the env_prefix:

class TestConfig < Anyway::Config
  config_name :test
  attr_config :enabled, :sample

  loader_options opdotenv: {
    path: "op://Employee/TestConfig"  # Loads all fields from item
  }
end

Field name matching (strict with case-insensitive prefix):

  • Fields in 1Password must be prefixed with the env_prefix (e.g., TEST_ for config_name :test)
  • Matching is case-insensitive: TEST_ENABLED, test_enabled, Test_Enabled all work
  • After prefix stripping, TEST_ENABLED becomes enabled (matching attr_config :enabled)
  • Fields without the prefix (e.g., enabled, ENABLED) are ignored and logged as unmatched

Debugging field matching:

  • Enable debug logging by setting OPDOTENV_DEBUG=true
  • Check Rails logs for messages like:
    [opdotenv] Available fields from 1Password: enabled, ENABLED, sample, SAMPLE
    [opdotenv] Matched fields for TEST: enabled, sample
    [opdotenv] Unmatched fields (must be prefixed with TEST_, case-insensitive): other_field
    

Using with dotenv

Load order determines which values take precedence:

require "dotenv"
require "opdotenv"

# Load local files first, then augment from 1Password (1Password values override by default)
Dotenv.load(".env", ".env.development")
Opdotenv::Loader.load("op://Vault/.env.development")

# Or load from 1Password first, then local files (local values override)
Opdotenv::Loader.load("op://Vault/.env.development", overwrite: false)
Dotenv.load(".env", ".env.development")

Export to 1Password

Use dedicated configuration items for exports through the op CLI. Export values are sent through standard input to keep them out of process arguments. Updating an existing item uses a JSON template and does not support items containing passkeys: 1Password warns that template updates overwrite passkeys. Do not export into a login item that contains a passkey. See the 1Password item edit documentation.

CLI

# Export .env file (format inferred from path)
opdotenv export --path "op://Vault/.env.development" --file .env.development

# Export to item fields
opdotenv export --path "op://Vault/App" --file .env

# Read and print (format inferred from path)
opdotenv read --path "op://Vault/.env.development"

Ruby API

# Export to Secure Note (format inferred from path)
Opdotenv::Exporter.export(
  path: "op://Vault/.env.development",
  data: {"API_KEY" => "secret"}
)

# Export to item fields
Opdotenv::Exporter.export(
  path: "op://Vault/App",
  data: {"API_KEY" => "secret", "API_SECRET" => "another"}
)

Supported formats

Format is inferred from the item or field basename:

  • .env or .env.*: dotenv (KEY=VALUE)
  • *.json: JSON (nested keys flattened with underscores)
  • *.yaml or *.yml: YAML (nested keys flattened with underscores)
  • Other items: load all fields without parsing

Field names with extensions work in the path, for example op://Vault/Item Name/config.json. Callers can also pass field_name and field_type to the API.

Security

Environment recommendation

Use this gem in development and test. For production, prefer the secret store that matches the host:

Provision through infrastructure tools such as Helm, Terraform, or Bicep when that matches the host.

Security considerations

  • CLI mode: Secrets fetched via an authenticated op CLI session.
  • Connect API mode: Secrets fetched via HTTPS. Keep tokens out of logs and process arguments.
  • The library does not persist secrets in memory beyond the load, or on disk.
  • Keep the 1Password CLI or Connect server current and authenticated.

Code Locations for Security Review

For security-sensitive applications, developers should review where this gem reads and writes data from 1Password. The following locations handle secret data:

Reading Secrets (OpClient - CLI mode)

  • lib/opdotenv/op_client.rb:
    • read(path) - Executes op read command to fetch a single field value
    • item_get(item, vault:) - Executes op item get command to fetch all item data as JSON
    • capture(args) - Runs the CLI via Open3.capture2e (array arguments, optional stdin)

Reading Secrets (ConnectApiClient - API mode)

  • lib/opdotenv/connect_api_client.rb:
    • read(path) - Makes HTTP GET request to fetch field or notesPlain content
    • item_get(item_title, vault:) - Searches and fetches item data via API
    • get_item(vault_name, item_title) - Fetches full item details including all fields
    • item_by_title_in_vault(vault_id, item_title) - Filters items by title and fetches full details
    • list_vaults() - Lists all accessible vaults (uses api_request(:get, "/v1/vaults"))
    • vault_name_to_id(vault_name) - Resolves vault names to IDs (cached)
    • api_request(method, path, body) - All HTTP requests go through this method
    • find_field(item, field_name) - Searches item fields by label, ID, or purpose

Main Entry Points

  • lib/opdotenv/loader.rb:
    • load(path, ...) - Main entry point that orchestrates secret fetching
    • load_field(client, path, field_name, field_type) - Loads and parses a single field
    • load_all_fields(client, path) - Loads all fields from an item (skips notesPlain)
    • merge_into_env(env, hash, overwrite:) - Writes secrets to environment hash

Rails Integration

  • lib/opdotenv/railtie.rb:
    • initializer "opdotenv.load" - Automatically loads secrets during Rails initialization
    • Reads from config.opdotenv.sources array
    • Sets OP_CONNECT_URL and OP_CONNECT_TOKEN from Rails config if provided

Anyway Config Integration

  • lib/opdotenv/anyway_loader.rb:
    • Loader#call(...) - Loads secrets for Anyway Config classes
    • Uses Opdotenv::Loader.load() internally

Parsing and Processing

  • lib/opdotenv/parsers/dotenv_parser.rb - Parses dotenv format strings
  • lib/opdotenv/parsers/json_parser.rb - Parses and flattens JSON structures
  • lib/opdotenv/parsers/yaml_parser.rb - Parses YAML (safe_load with aliases: false)

Data flow

  1. Configuration: Rails config or direct API calls
  2. Path parsing: SourceParser.parse() extracts vault/item/field from path
  3. Client selection: ClientFactory.create() chooses CLI or API client
  4. Secret fetching: Client reads from 1Password (CLI command or HTTP request)
  5. Parsing: Format-specific parser converts to key-value pairs
  6. Environment merge: Secrets merged into ENV or provided hash

All secret data flows through these code paths. No secrets are persisted to disk or logged (except explicit error messages).

Development

bundle install
bundle exec polyrun parallel-rspec --workers 5 --merge-failures
bundle exec rbs validate
bundle exec standardrb --fix

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/amkisko/opdotenv.rb.

Contribution policy:

  • New features are not necessarily added to the gem
  • Pull request should have test coverage for affected parts
  • Pull request should have changelog entry

Review policy:

  • It might take up to 2 calendar weeks to review and merge critical fixes
  • It might take up to 6 calendar months to review and merge pull request
  • It might take up to 1 calendar year to review an issue

Security

If you discover a security vulnerability, please report it responsibly. See SECURITY.md for details.

Links

License

The gem is available as open source under the terms of the MIT License.

Sponsors

Sponsored by Kisko Labs.

Sponsored by Kisko Labs