暗物质叉 联 合 国 驼峰/高龄,作为
@drkmttr/sops-age。 。 。 . 添加dataKey选项(解密您自己解开的数据密钥的文件,例如通过AWS KMS)并接受没有年龄收件人的文件。 提供上游 驼峰/羊龄#22;这个分叉之所以存在,是因为上游已经安静了一年了. 其他一切都没有改变.
索普斯年龄
sops-age 是一个 TypeScript 库,旨在解密已加密的文件 目标 (保密程序) 年龄 加密工具。 这个库为解密环境变量,配置文件,以及其它在您的应用程序中用SOPS和年龄加密的敏感数据提供了简单的方法. 它在大多数的JavaScript运行时间(node.js,浏览器,Deno,Bun等)中工作.
特征
- 支持加密 SOPS 文件的解密
age - 遵循 SOPS 常规自动时代密钥发现
- SSH 密钥支持, 自动将 Ed25519 和 RSA SSH 密钥转换为年龄格式
- 兼容各种文件格式,包括
.env, (中文(简体) ),.json,以及.yaml - 支持多种输入类型( E)
string, (中文(简体) ),Buffer, (中文(简体) ),File, (中文(简体) ),Blob,溪流等) - 跨不同 JavaScript 运行时间的工作( Node.js, Deno, Bun, 浏览器)
- 从文件、 URL 或原始内容中解密 OPS 数据的简单、统一的 API
- 带有可选手动覆盖的文件类型自动检测
安装
安装 sops-age 使用您首选的软件包管理器 :
# npm
npm install sops-age
# pnpm
pnpm add sops-age
# yarn
yarn add sops-age
使用量
库可用于各种 JavaScript 环境并支持多个模块格式:
无害环境管理(建议)
import { decryptSops } from "sops-age";
// Decrypt from a local file (keys auto-discovered from env and file system)
const config = await decryptSops({
path: "./config.enc.json",
});
// Or with explicit age key
const config = await decryptSops({
path: "./config.enc.json",
secretKey: "AGE-SECRET-KEY-1qgdy...",
});
常见JS
const { decryptSops } = require("sops-age");
// Decrypt from a URL
const config = await decryptSops({
url: "https://example.com/config.enc.yaml",
secretKey: "AGE-SECRET-KEY-1qgdy...",
});
浏览器( CDN)
<!-- Add to your HTML -->
<script src="https://unpkg.com/sops-age/dist/index.global.js"></script>
<script>
// The library is available as window.decryptSops
async function loadConfig() {
const config = await decryptSops({
url: "https://example.com/config.enc.json",
secretKey: "AGE-SECRET-KEY-1qgdy...",
});
console.log(config);
}
</script>
类型脚本
库包括 TypeScript 类型定义:
import { decryptSops, type DecryptSopsOptions } from "sops-age";
const options: DecryptSopsOptions = {
secretKey: "AGE-SECRET-KEY-1qgdy...",
fileType: "json",
};
const config = await decryptSops(jsonString, options);
基本用途
图书馆提供统一的 decryptSops 可处理各种输入类型的函数:
import { decryptSops } from "sops-age";
// Decrypt from a local file, auto-discovering age keys
const config = await decryptSops({
path: "./config.enc.json",
});
// Decrypt from a URL with explicit age key
const remoteConfig = await decryptSops({
url: "https://example.com/config.enc.yaml",
secretKey: "AGE-SECRET-KEY-1qgdy...",
});
// Decrypt from string content
const content = '{"sops": {...}}';
const data = await decryptSops(content, {
secretKey: "AGE-SECRET-KEY-1qgdy...",
fileType: "json",
});
支持的文件类型
sops-age 支持以下文件类型:
.env.json.yaml编号 :.yml
库根据文件扩展名或内容自动检测文件类型. 您也可以使用手动指定文件类型 fileType 选项.
输入类型
sops-age 支持 SOPS 加密内容的各种输入类型 :
string: SOPS 文件的原始字符串内容File: 文件对象( 在浏览器环境中)Blob: 二进制数据ArrayBuffer: 原始二进制数据Uint8Array: 键入的字节数组Buffer:Node.js Buffer (在Node.js环境中) 互联网档案馆的存檔,存档日期2013-12-21ReadableStream<Uint8Array>: 二进制数据流
年龄密钥发现
sops-age 使用与SOPS本身相同的逻辑自动发现年龄键. 否时 secretKey 图书馆将按以下顺序搜索密钥:
1. SSH 密钥(转换为年龄格式)
库可以自动将 SSH 私钥转换为年龄格式 :
- 环境变量数字 :
SOPS_AGE_SSH_PRIVATE_KEY_FILE- SSH 私钥路径 - 默认位置数字 :
~/.ssh/id_ed25519财务报告和审定财务报表~/.ssh/id_rsa(按此顺序排列) - 支持类型: Ed25519 和 RSA 密钥
// Set environment variable to use specific SSH key
process.env.SOPS_AGE_SSH_PRIVATE_KEY_FILE = "/path/to/my/ssh/key";
// Keys will be auto-discovered and converted
const config = await decryptSops({ path: "./config.enc.json" });
2. 环境变量的年龄键
SOPS_AGE_KEY: 直接年龄的私钥SOPS_AGE_KEY_FILE: 包含年龄密钥的文件路径SOPS_AGE_KEY_CMD: 输出时代键的命令
// Direct key
process.env.SOPS_AGE_KEY = "AGE-SECRET-KEY-1qgdy...";
// Key file
process.env.SOPS_AGE_KEY_FILE = "/path/to/keys.txt";
// Command that outputs keys
process.env.SOPS_AGE_KEY_CMD = "my-key-manager get-age-key";
3. 默认配置文件
默认 SOPS 配置目录中的年龄密钥库检查 :
- Linux/ 团结数字 :
~/.config/sops/age/keys.txt(或$XDG_CONFIG_HOME/sops/age/keys.txt(中文(简体) ) - 马科斯数字 :
~/Library/Application Support/sops/age/keys.txt(或$XDG_CONFIG_HOME/sops/age/keys.txt如果设置) - 窗口数字 :
%APPDATA%\sops\age\keys.txt
示例 keys.txt 文件 :
# Created: 2024-01-15T10:30:00Z
# Public key: age1je6kjhzuhdjy3fqptpttxjh5k8q46vygzlgtpuq3030c947pc5tqz9dqvr
AGE-SECRET-KEY-1QGDY7NWZDM5HG2QMSKQHQZPQF2QJLTQHQZPQF2QJLTQHQZPQF2QJLTQHQZ
# Another key
AGE-SECRET-KEY-1ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOP
密钥发现优先级
当不明确时 secretKey 按此顺序查找密钥 :
- SSH 密钥数字 :
SOPS_AGE_SSH_PRIVATE_KEY_FILE→~/.ssh/id_ed25519→~/.ssh/id_rsa - 年龄键数字 :
SOPS_AGE_KEY→SOPS_AGE_KEY_FILE→SOPS_AGE_KEY_CMD - 默认配置: 特定平台
sops/age/keys.txt文件
库将尝试所有已发现的密钥, 直到一个成功解密文件 .
年龄关键公用事业
库包括用于发现年龄密钥和将SSH密钥转换为年龄格式的公用设备:
import { sshKeyToAge, sshKeyFileToAge, findAllAgeKeys } from "sops-age";
// Convert SSH key content to age format
const sshKeyContent = "-----BEGIN OPENSSH PRIVATE KEY-----\n...";
const ageKey = sshKeyToAge(sshKeyContent);
// Convert SSH key file to age format
const ageKey = await sshKeyFileToAge("/path/to/ssh/key");
// Discover all available age keys
const allKeys = await findAllAgeKeys();
console.log("Found keys:", allKeys);
API 参考
decryptSops(input, options?)
直接从字符串,缓冲或者其他被支持的输入类型解密 OPS-加密内容.
// With auto-discovered keys
const decrypted = await decryptSops(jsonString, {
fileType: "json",
});
// With explicit key
const decrypted = await decryptSops(jsonString, {
secretKey: "AGE-SECRET-KEY-1qgdy...",
fileType: "json",
});
decryptSops({ path: "...", ... })
从本地文件系统解密一个 SOPS 加密的文件 .
const decrypted = await decryptSops({
path: "/path/to/config.enc.json",
// secretKey optional - will auto-discover
});
decryptSops({ url: "https://...", ... })
从 URL 解密一个 SOPS 加密文件 .
const decrypted = await decryptSops({
url: "https://example.com/config.enc.json",
secretKey: "AGE-SECRET-KEY-1qgdy...", // or auto-discover
});
decryptSops(sopsObject, options?)
直接从预分解的 SOPS 对象解密 SOPS 加密内容.
const sopsObject = {
secret:
"ENC[AES256_GCM,data:trrpgezXug4Dq9T/inwkMA==,iv:glPwxoY2UuHO91vlJRaqYtFkPY1VsWvkJtfkEKZJdns=,tag:v7DbOYl7C5gdQRdW6BVoLw==,type:str]",
sops: {
// ... SOPS metadata
},
};
// Auto-discovers keys from environment/config
const decrypted = await decryptSops(sopsObject);
DecryptSopsOptions
这个 decryptSops 函数接受以下选项:
secretKey: 解密的年龄密钥( 可选的 - 如果不提供会自动发现)fileType:可选文件类型 ("env","json",或"yaml"). 未指定时自动检测keyPath: 只解密特定值的可选路径path: 本地 SOPS 文件的路径( 当使用基于文件的解密时)url: SOPS 文件的 URL( 当使用基于 URL 的解密时)
工具函数
findAllAgeKeys()
使用 SOPS 逻辑发现所有可用的时代密钥(包括已转换的 SSH 密钥):
import { findAllAgeKeys } from "sops-age";
const keys = await findAllAgeKeys();
console.log("Available age keys:", keys);
sshKeyToAge(keyContent, filePath)
将 SSH 私有密钥内容转换为年龄格式 :
import { sshKeyToAge } from "sops-age";
const sshKey = "-----BEGIN OPENSSH PRIVATE KEY-----\n...";
const ageKey = sshKeyToAge(sshKey, "id_ed25519");
// Returns: "AGE-SECRET-KEY-1..." or null for unsupported keys
sshKeyFileToAge(filePath)
将 SSH 私有密钥文件转换为年龄格式 :
import { sshKeyFileToAge } from "sops-age";
const ageKey = await sshKeyFileToAge("~/.ssh/id_ed25519");
// Returns: "AGE-SECRET-KEY-1..." or null
许可证
sops-age 以麻省理工学院的许可证发放。 见 自由主义 以备详细文件.
文件加密到其他主密钥( AWS KMS,...)
sops-age 只打开 SOPS 数据密钥 年龄。如果一个文件被加密
到另一个主密钥, 自己解开数据密钥并交给它
dataKey; 值解密( AES-256- GCM 有 SOPS 路径附加)
每个主密钥都是一样的:
import { decryptSops } from "sops-age";
// e.g. kms:Decrypt of sops.kms[0].enc → 32-byte data key
const dataKey: Uint8Array = await unwrapWithKms(encrypted.sops.kms[0]);
const secrets = await decryptSops(encryptedJson, { dataKey, fileType: "json" });
何时 dataKey 设置, 没有查找年龄键, 并且 secretKey 被忽略.
文件为 "age": null (完全没有年龄领取者)
dataKey。 。 。 .