Skip to content

About

Perl implementation of Mozilla SOPS encrypted file format

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

File::SOPS

Pure Perl implementation of Mozilla SOPS (Secrets OPerationS) encrypted file format.

Synopsis

use File::SOPS;

# Encrypt a data structure
my $encrypted = File::SOPS->encrypt(
    data       => {
        database => {
            password => 'secret123',
            host     => 'db.example.com',
        },
    },
    recipients => ['age1ql3z7hjy54pw3hyww5ayyfg7zqgvc7w3j2elw8zmrj2kg5sfn9aqmcac8p'],
    format     => 'yaml',  # or 'json'
);

# Decrypt
my $data = File::SOPS->decrypt(
    encrypted  => $encrypted,
    identities => ['AGE-SECRET-KEY-1...'],
);

# File operations
File::SOPS->encrypt_file(
    input      => 'secrets.yaml',
    output     => 'secrets.enc.yaml',
    recipients => ['age1...'],
);

File::SOPS->decrypt_file(
    input      => 'secrets.enc.yaml',
    output     => 'secrets.yaml',
    identities => ['AGE-SECRET-KEY-1...'],
);

# Extract single value without decrypting entire file
my $password = File::SOPS->extract(
    file       => 'secrets.enc.yaml',
    path       => 'database.password',
    identities => ['AGE-SECRET-KEY-1...'],
);

# Rotate data key (re-encrypt with new key)
File::SOPS->rotate(
    file       => 'secrets.enc.yaml',
    identities => ['AGE-SECRET-KEY-1...'],
);

Description

SOPS encrypts values in structured files (YAML, JSON) while keeping keys readable. This enables:

  • Git-friendly diffs - see which keys changed without decrypting
  • Partial file inspection without full decryption
  • Multiple encryption backends (currently age, with PGP/KMS planned)
  • MAC verification to detect tampering

Encrypted File Format

database:
    password: ENC[AES256_GCM,data:xyz==,iv:abc==,tag:def==,type:str]
    host: ENC[AES256_GCM,data:xyz==,iv:abc==,tag:def==,type:str]
sops:
    age:
        - recipient: age1ql3z7hjy...
          enc: |
            -----BEGIN AGE ENCRYPTED FILE-----
            <encrypted data key>
            -----END AGE ENCRYPTED FILE-----
    lastmodified: "2025-01-10T12:00:00Z"
    mac: ENC[AES256_GCM,data:...,iv:...,tag:...,type:str]
    version: 3.7.3

Interoperability

Files encrypted with File::SOPS can be decrypted with the reference Go implementation, and vice versa, for YAML and JSON.

Verified against sops v3.13.3 by t/04-interop.t and the interop subtests throughout the suite, which drive the real binary in both directions. They find sops via $SOPS_BIN, then your PATH, then the repo-local .sops-bin/sops, then /tmp/sops (shared logic in t/lib/SopsBin.pm).

Without a binary the suite still reports success — having silently skipped the entire compatibility proof (measured 2026-09-01: it drops a few hundred interop assertions and still prints All tests successful). To install the pinned version where the suite finds it automatically:

maint/fetch-sops .sops-bin   # gitignored, survives a /tmp wipe; needs Go or downloads a pinned build
prove -lr t/                 # auto-detects .sops-bin/sops -- no PATH or $SOPS_BIN needed

Known limitations:

  • Quoted scalars change type on a round trip. sops types a value by what the parser produced; File::SOPS infers the type from the scalar's text. A quoted "true", "false", "1" or "0" therefore comes back as a boolean or an integer rather than the string it went in as. Unquoted values are unaffected.
  • Comments are not preserved. YAML::XS discards them on parse, so they are absent from the output; sops -d keeps them.
  • Multi-document YAML is read and written: decrypt returns an ArrayRef of HashRefs for a stream (a single document stays a HashRef), encrypt accepts one, and extract takes document => $n. One metadata block and one MAC span all documents. Converting a stream to a format with no document stream (JSON, ENV, INI) is refused rather than silently truncated; edit on a stream is not enabled yet.
  • Backends other than age (PGP, KMS, GCP KMS, Azure Key Vault, Vault) are parsed and round-tripped in the metadata but cannot be used for encryption.

Installation

cpanm File::SOPS

Or manually:

perl Makefile.PL
make
make test
make install

Dependencies

Encryption Backends

Currently supported:

  • age - Modern encryption using X25519 + ChaCha20-Poly1305

Planned:

  • PGP
  • AWS KMS
  • GCP KMS
  • Azure Key Vault
  • HashiCorp Vault

How It Works

  1. Generate a random 256-bit data key
  2. Encrypt the data key for each recipient using age
  3. Encrypt each value with AES-256-GCM using the data key
  4. Compute MAC over all values for tamper detection
  5. Store encrypted data keys in sops metadata section

See Also

  • SOPS - Reference implementation
  • Crypt::Age - Perl age encryption
  • age - age encryption specification

Author

Torsten Raudssus [email protected]

License

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

About

Perl implementation of Mozilla SOPS encrypted file format

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages