envsec 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 David Nussio
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # secenv
2
+
3
+ To install dependencies:
4
+
5
+ ```bash
6
+ bun install
7
+ ```
8
+
9
+ To run:
10
+
11
+ ```bash
12
+ bun run index.ts
13
+ ```
14
+
15
+ This project was created using `bun init` in bun v1.3.10. [Bun](https://bun.com) is a fast all-in-one JavaScript runtime.
@@ -0,0 +1,28 @@
1
+ import { Command, Options, Args } from "@effect/cli";
2
+ import { Effect, Option } from "effect";
3
+ import { SecretStore } from "../services/SecretStore.js";
4
+ import { rootCommand } from "./root.js";
5
+ const key = Args.text({ name: "key" });
6
+ const wordOption = Options.text("word").pipe(Options.withAlias("w"), Options.withDescription("String value to store"), Options.optional);
7
+ const digitOption = Options.text("digit").pipe(Options.withAlias("d"), Options.withDescription("Number value to store"), Options.optional);
8
+ const boolOption = Options.boolean("bool").pipe(Options.withAlias("b"), Options.withDescription("Boolean flag to store (presence = true)"));
9
+ export const addCommand = Command.make("add", { key, word: wordOption, digit: digitOption, bool: boolOption }, ({ key, word, digit, bool }) => Effect.gen(function* () {
10
+ const { env } = yield* rootCommand;
11
+ if (Option.isSome(word)) {
12
+ yield* SecretStore.set(env, key, word.value, "string");
13
+ }
14
+ else if (Option.isSome(digit)) {
15
+ const num = Number(digit.value);
16
+ if (isNaN(num)) {
17
+ return yield* Effect.fail(new Error(`Invalid number: ${digit.value}`));
18
+ }
19
+ yield* SecretStore.set(env, key, digit.value, "number");
20
+ }
21
+ else if (bool) {
22
+ yield* SecretStore.set(env, key, "true", "boolean");
23
+ }
24
+ else {
25
+ yield* SecretStore.set(env, key, "false", "boolean");
26
+ }
27
+ yield* Effect.log(`Secret "${key}" stored in env "${env}"`);
28
+ }));
@@ -0,0 +1,15 @@
1
+ import { Command } from "@effect/cli";
2
+ import { Effect, Console } from "effect";
3
+ import { SecretStore } from "../services/SecretStore.js";
4
+ import { rootCommand } from "./root.js";
5
+ export const listCommand = Command.make("list", {}, () => Effect.gen(function* () {
6
+ const { env } = yield* rootCommand;
7
+ const results = yield* SecretStore.list(env);
8
+ if (results.length === 0) {
9
+ yield* Console.log("No secrets found.");
10
+ return;
11
+ }
12
+ for (const item of results) {
13
+ yield* Console.log(`${item.key} (${item.type}) updated: ${item.updated_at}`);
14
+ }
15
+ }));
@@ -0,0 +1,10 @@
1
+ import { Command, Args } from "@effect/cli";
2
+ import { Effect, Console } from "effect";
3
+ import { SecretStore } from "../services/SecretStore.js";
4
+ import { rootCommand } from "./root.js";
5
+ const key = Args.text({ name: "key" });
6
+ export const readCommand = Command.make("read", { key }, ({ key }) => Effect.gen(function* () {
7
+ const { env } = yield* rootCommand;
8
+ const value = yield* SecretStore.get(env, key);
9
+ yield* Console.log(String(value));
10
+ }));
@@ -0,0 +1,3 @@
1
+ import { Command, Options } from "@effect/cli";
2
+ const env = Options.text("env").pipe(Options.withAlias("e"), Options.withDescription("Environment name (e.g. dev, staging, prod)"));
3
+ export const rootCommand = Command.make("secenv", { env });
@@ -0,0 +1,16 @@
1
+ import { Command, Args } from "@effect/cli";
2
+ import { Effect, Console } from "effect";
3
+ import { SecretStore } from "../services/SecretStore.js";
4
+ import { rootCommand } from "./root.js";
5
+ const pattern = Args.text({ name: "pattern" });
6
+ export const searchCommand = Command.make("search", { pattern }, ({ pattern }) => Effect.gen(function* () {
7
+ const { env } = yield* rootCommand;
8
+ const results = yield* SecretStore.search(env, pattern);
9
+ if (results.length === 0) {
10
+ yield* Console.log("No secrets found.");
11
+ return;
12
+ }
13
+ for (const item of results) {
14
+ yield* Console.log(`${item.key} (${item.type})`);
15
+ }
16
+ }));
@@ -0,0 +1,17 @@
1
+ import { Effect } from "effect";
2
+ import { InvalidKeyError } from "../errors.js";
3
+ export const parse = Effect.fn("SecretKey.parse")(function* (key, env) {
4
+ const parts = key.split(".");
5
+ if (parts.length < 2) {
6
+ return yield* new InvalidKeyError({
7
+ key,
8
+ message: `Key "${key}" must have at least 2 dot-separated parts (e.g. "service.account")`,
9
+ });
10
+ }
11
+ const account = parts[parts.length - 1];
12
+ const serviceParts = parts.slice(0, -1);
13
+ return {
14
+ service: `secenv.${env}.${serviceParts.join(".")}`,
15
+ account,
16
+ };
17
+ });
package/dist/errors.js ADDED
@@ -0,0 +1,23 @@
1
+ import { Schema } from "effect";
2
+ export class SecretNotFoundError extends Schema.TaggedError()("SecretNotFoundError", {
3
+ key: Schema.String,
4
+ env: Schema.String,
5
+ message: Schema.String,
6
+ }) {
7
+ }
8
+ export class KeychainError extends Schema.TaggedError()("KeychainError", {
9
+ command: Schema.String,
10
+ stderr: Schema.String,
11
+ message: Schema.String,
12
+ }) {
13
+ }
14
+ export class MetadataStoreError extends Schema.TaggedError()("MetadataStoreError", {
15
+ operation: Schema.String,
16
+ message: Schema.String,
17
+ }) {
18
+ }
19
+ export class InvalidKeyError extends Schema.TaggedError()("InvalidKeyError", {
20
+ key: Schema.String,
21
+ message: Schema.String,
22
+ }) {
23
+ }
@@ -0,0 +1,84 @@
1
+ import { execFile } from "node:child_process";
2
+ import { Effect, Layer } from "effect";
3
+ import { KeychainAccess } from "../services/KeychainAccess.js";
4
+ import { KeychainError, SecretNotFoundError } from "../errors.js";
5
+ const run = (args) => Effect.async((resume) => {
6
+ execFile("security", args, (error, stdout, stderr) => {
7
+ if (error && typeof error.code === "string") {
8
+ resume(Effect.fail(new KeychainError({
9
+ command: args[0] ?? "unknown",
10
+ stderr: String(error),
11
+ message: `Failed to run security command`,
12
+ })));
13
+ return;
14
+ }
15
+ resume(Effect.succeed({
16
+ exitCode: error ? error.code ?? 1 : 0,
17
+ stdout,
18
+ stderr,
19
+ }));
20
+ });
21
+ });
22
+ const make = KeychainAccess.of({
23
+ set: Effect.fn("MacOsKeychainAccess.set")(function* (service, account, password) {
24
+ const result = yield* run([
25
+ "add-generic-password",
26
+ "-U",
27
+ "-s",
28
+ service,
29
+ "-a",
30
+ account,
31
+ "-w",
32
+ password,
33
+ ]);
34
+ if (result.exitCode !== 0) {
35
+ return yield* new KeychainError({
36
+ command: "add-generic-password",
37
+ stderr: result.stderr,
38
+ message: `Failed to set keychain item: ${service}/${account}`,
39
+ });
40
+ }
41
+ }),
42
+ get: Effect.fn("MacOsKeychainAccess.get")(function* (service, account) {
43
+ const result = yield* run([
44
+ "find-generic-password",
45
+ "-s",
46
+ service,
47
+ "-a",
48
+ account,
49
+ "-w",
50
+ ]);
51
+ if (result.exitCode === 44) {
52
+ return yield* new SecretNotFoundError({
53
+ key: account,
54
+ env: service,
55
+ message: `Secret not found: ${service}/${account}`,
56
+ });
57
+ }
58
+ if (result.exitCode !== 0) {
59
+ return yield* new KeychainError({
60
+ command: "find-generic-password",
61
+ stderr: result.stderr,
62
+ message: `Failed to get keychain item: ${service}/${account}`,
63
+ });
64
+ }
65
+ return result.stdout.trim();
66
+ }),
67
+ remove: Effect.fn("MacOsKeychainAccess.remove")(function* (service, account) {
68
+ const result = yield* run([
69
+ "delete-generic-password",
70
+ "-s",
71
+ service,
72
+ "-a",
73
+ account,
74
+ ]);
75
+ if (result.exitCode !== 0) {
76
+ return yield* new KeychainError({
77
+ command: "delete-generic-password",
78
+ stderr: result.stderr,
79
+ message: `Failed to remove keychain item: ${service}/${account}`,
80
+ });
81
+ }
82
+ }),
83
+ });
84
+ export const MacOsKeychainAccessLive = Layer.succeed(KeychainAccess, make);
@@ -0,0 +1,103 @@
1
+ import { Effect, Layer } from "effect";
2
+ import Database from "better-sqlite3";
3
+ import { mkdirSync } from "node:fs";
4
+ import { homedir } from "node:os";
5
+ import { join } from "node:path";
6
+ import { MetadataStore } from "../services/MetadataStore.js";
7
+ import { MetadataStoreError, SecretNotFoundError } from "../errors.js";
8
+ const dbPath = join(homedir(), ".secenv", "store.sqlite");
9
+ const initDb = () => {
10
+ mkdirSync(join(homedir(), ".secenv"), { recursive: true });
11
+ const db = new Database(dbPath);
12
+ db.exec(`
13
+ CREATE TABLE IF NOT EXISTS secrets (
14
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
15
+ env TEXT NOT NULL,
16
+ key TEXT NOT NULL,
17
+ type TEXT NOT NULL CHECK(type IN ('string', 'number', 'boolean')),
18
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
19
+ updated_at TEXT NOT NULL DEFAULT (datetime('now')),
20
+ UNIQUE(env, key)
21
+ )
22
+ `);
23
+ return db;
24
+ };
25
+ const make = Effect.gen(function* () {
26
+ const db = yield* Effect.try({
27
+ try: () => initDb(),
28
+ catch: (error) => new MetadataStoreError({
29
+ operation: "init",
30
+ message: `Failed to initialize database: ${error}`,
31
+ }),
32
+ });
33
+ return MetadataStore.of({
34
+ upsert: Effect.fn("SqliteMetadataStore.upsert")(function* (env, key, type) {
35
+ yield* Effect.try({
36
+ try: () => {
37
+ db.prepare(`INSERT INTO secrets (env, key, type)
38
+ VALUES (?, ?, ?)
39
+ ON CONFLICT(env, key) DO UPDATE SET
40
+ type = excluded.type,
41
+ updated_at = datetime('now')`).run(env, key, type);
42
+ },
43
+ catch: (error) => new MetadataStoreError({
44
+ operation: "upsert",
45
+ message: `Failed to upsert metadata for ${env}/${key}: ${error}`,
46
+ }),
47
+ });
48
+ }),
49
+ get: Effect.fn("SqliteMetadataStore.get")(function* (env, key) {
50
+ const row = yield* Effect.try({
51
+ try: () => db
52
+ .prepare(`SELECT key, type, created_at, updated_at FROM secrets WHERE env = ? AND key = ?`)
53
+ .get(env, key),
54
+ catch: (error) => new MetadataStoreError({
55
+ operation: "get",
56
+ message: `Failed to get metadata for ${env}/${key}: ${error}`,
57
+ }),
58
+ });
59
+ if (!row) {
60
+ return yield* new SecretNotFoundError({
61
+ key,
62
+ env,
63
+ message: `Secret metadata not found: ${env}/${key}`,
64
+ });
65
+ }
66
+ return row;
67
+ }),
68
+ remove: Effect.fn("SqliteMetadataStore.remove")(function* (env, key) {
69
+ yield* Effect.try({
70
+ try: () => {
71
+ db.prepare(`DELETE FROM secrets WHERE env = ? AND key = ?`).run(env, key);
72
+ },
73
+ catch: (error) => new MetadataStoreError({
74
+ operation: "remove",
75
+ message: `Failed to remove metadata for ${env}/${key}: ${error}`,
76
+ }),
77
+ });
78
+ }),
79
+ search: Effect.fn("SqliteMetadataStore.search")(function* (env, pattern) {
80
+ return yield* Effect.try({
81
+ try: () => db
82
+ .prepare(`SELECT key, type FROM secrets WHERE env = ? AND key GLOB ?`)
83
+ .all(env, pattern),
84
+ catch: (error) => new MetadataStoreError({
85
+ operation: "search",
86
+ message: `Failed to search metadata for ${env}/${pattern}: ${error}`,
87
+ }),
88
+ });
89
+ }),
90
+ list: Effect.fn("SqliteMetadataStore.list")(function* (env) {
91
+ return yield* Effect.try({
92
+ try: () => db
93
+ .prepare(`SELECT key, type, updated_at FROM secrets WHERE env = ? ORDER BY key`)
94
+ .all(env),
95
+ catch: (error) => new MetadataStoreError({
96
+ operation: "list",
97
+ message: `Failed to list metadata for ${env}: ${error}`,
98
+ }),
99
+ });
100
+ }),
101
+ });
102
+ });
103
+ export const SqliteMetadataStoreLive = Layer.effect(MetadataStore, make);
package/dist/main.js ADDED
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env node
2
+ import { Command } from "@effect/cli";
3
+ import { NodeContext, NodeRuntime } from "@effect/platform-node";
4
+ import { Effect } from "effect";
5
+ import { createRequire } from "node:module";
6
+ import { rootCommand } from "./cli/root.js";
7
+ import { addCommand } from "./cli/add.js";
8
+ import { readCommand } from "./cli/read.js";
9
+ import { searchCommand } from "./cli/search.js";
10
+ import { listCommand } from "./cli/list.js";
11
+ import { SecretStore } from "./services/SecretStore.js";
12
+ const require = createRequire(import.meta.url);
13
+ const pkg = require("../package.json");
14
+ const command = rootCommand.pipe(Command.withSubcommands([addCommand, readCommand, searchCommand, listCommand]));
15
+ const cli = Command.run(command, {
16
+ name: "secenv",
17
+ version: pkg.version,
18
+ });
19
+ cli(process.argv).pipe(Effect.provide(SecretStore.Default), Effect.provide(NodeContext.layer), NodeRuntime.runMain);
@@ -0,0 +1,3 @@
1
+ import { Context } from "effect";
2
+ export class KeychainAccess extends Context.Tag("KeychainAccess")() {
3
+ }
@@ -0,0 +1,3 @@
1
+ import { Context } from "effect";
2
+ export class MetadataStore extends Context.Tag("MetadataStore")() {
3
+ }
@@ -0,0 +1,45 @@
1
+ import { Effect } from "effect";
2
+ import { KeychainAccess } from "./KeychainAccess.js";
3
+ import { MetadataStore } from "./MetadataStore.js";
4
+ import * as SecretKey from "../domain/SecretKey.js";
5
+ import { MacOsKeychainAccessLive } from "../implementations/MacOsKeychainAccess.js";
6
+ import { SqliteMetadataStoreLive } from "../implementations/SqliteMetadataStore.js";
7
+ export class SecretStore extends Effect.Service()("SecretStore", {
8
+ accessors: true,
9
+ dependencies: [MacOsKeychainAccessLive, SqliteMetadataStoreLive],
10
+ effect: Effect.gen(function* () {
11
+ const keychain = yield* KeychainAccess;
12
+ const metadata = yield* MetadataStore;
13
+ const set = Effect.fn("SecretStore.set")(function* (env, key, value, type) {
14
+ const parsed = yield* SecretKey.parse(key, env);
15
+ yield* keychain.set(parsed.service, parsed.account, value);
16
+ yield* metadata.upsert(env, key, type);
17
+ });
18
+ const get = Effect.fn("SecretStore.get")(function* (env, key) {
19
+ const meta = yield* metadata.get(env, key);
20
+ const parsed = yield* SecretKey.parse(key, env);
21
+ const raw = yield* keychain.get(parsed.service, parsed.account);
22
+ switch (meta.type) {
23
+ case "number":
24
+ return Number(raw);
25
+ case "boolean":
26
+ return raw === "true";
27
+ default:
28
+ return raw;
29
+ }
30
+ });
31
+ const remove = Effect.fn("SecretStore.remove")(function* (env, key) {
32
+ const parsed = yield* SecretKey.parse(key, env);
33
+ yield* keychain.remove(parsed.service, parsed.account);
34
+ yield* metadata.remove(env, key);
35
+ });
36
+ const search = Effect.fn("SecretStore.search")(function* (env, pattern) {
37
+ return yield* metadata.search(env, pattern);
38
+ });
39
+ const list = Effect.fn("SecretStore.list")(function* (env) {
40
+ return yield* metadata.list(env);
41
+ });
42
+ return { set, get, remove, search, list };
43
+ }),
44
+ }) {
45
+ }
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "envsec",
3
+ "version": "0.1.1",
4
+ "description": "Secure environment secrets management using macOS Keychain",
5
+ "type": "module",
6
+ "bin": {
7
+ "secenv": "./dist/main.js"
8
+ },
9
+ "files": [
10
+ "dist"
11
+ ],
12
+ "engines": {
13
+ "node": ">=18.0.0"
14
+ },
15
+ "os": [
16
+ "darwin"
17
+ ],
18
+ "scripts": {
19
+ "build": "tsc",
20
+ "prepublishOnly": "npm run build"
21
+ },
22
+ "keywords": [
23
+ "secrets",
24
+ "environment-variables",
25
+ "keychain",
26
+ "macos",
27
+ "effect",
28
+ "cli"
29
+ ],
30
+ "author": "David Nussio",
31
+ "license": "MIT",
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "git+https://github.com/davidnussio/secenv.git"
35
+ },
36
+ "publishConfig": {
37
+ "access": "public"
38
+ },
39
+ "dependencies": {
40
+ "@effect/cli": "^0.73.2",
41
+ "@effect/platform": "^0.94.5",
42
+ "@effect/platform-node": "^0.104.1",
43
+ "better-sqlite3": "^12.8.0",
44
+ "effect": "^3.19.19"
45
+ },
46
+ "devDependencies": {
47
+ "@types/better-sqlite3": "^7.6.13",
48
+ "@types/node": "^22",
49
+ "typescript": "^5"
50
+ }
51
+ }