Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ The backend (`fastapi_backend`) and the frontend (`nextjs-frontend`) are version
`POST /api/v1/secrets/test`), the MCP tools `list_secrets`, `list_vault_items` and `set_secret_reference`, and the
assistant's `list_secrets` and `vault_items` show references and whether they could be read, never a value. The
client (python-vaultwarden, Apache-2.0) is fetched once a Vaultwarden address is set. Secrets saved in Lens stay
the default ([Secrets](docs/secrets.md)).
the default. In the web app, Settings › Secrets sets the vault up and tests it, and every secret field gets From
Vaultwarden, a picker of the vault's items that fills in the reference ([Secrets](docs/secrets.md)).
- **A decide node for judgement calls.** An automation's workflow can ask the decision model a question with a few
options ("Does this need me today?") and go on along the option it picked, or along `unsure` when it isn't sure
enough, none is set up or the budget is spent. The email it reads is marked as outside text, the answer is kept per
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,7 @@ Next, and not in a pull request yet; each links to where it is tracked:
activity ledger ([speech providers](docs/speech-providers.md)).
- **Storage:** uploads and assets' media in the file store, and a storage key to download
([storage](docs/storage.md)).
- **Secrets:** Settings › Secrets in the web app, with a picker for the vault's items, and references in sources'
credentials and MCP servers ([secrets](docs/secrets.md)).
- **Secrets:** references in sources' credentials and MCP servers ([secrets](docs/secrets.md)).
- **Logging:** sending straight to Loki or another HTTP endpoint, and client certificates for TLS
([logging](docs/logging.md)).
- **Proof:** each release tested by upgrading an install of the last one, holding data
Expand Down
2 changes: 1 addition & 1 deletion docs/requirements.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ request, to another check that proves the same thing; loosening a requirement or
| Passkeys instead of passwords | [test_passkeys.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_passkeys.py) `test_first_admin_with_a_passkey_and_no_password`, `test_sign_in_with_a_passkey` |
| A passkey at another of Lens's addresses (remote access, HTTPS) added from a signed-in one: Open signed in, then one fingerprint | [test_passkeys.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_passkeys.py) `test_taking_your_sign_in_to_another_address`, `test_the_addresses_lens_opens_at`; [passkeys-panel.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/passkeys-panel.test.tsx) `opens an address you have no passkey for signed in, to add one there`; [webauthn.test.ts](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/webauthn.test.ts) `points a failed sign-in to the address the passkey was made at` |
| Encryption at rest, with a key per namespace | [test_keyring.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/domain/test_keyring.py) `test_each_namespace_gets_its_own_key_kept_only_wrapped`; [test_encryption.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_encryption.py) `test_a_new_archive_encrypts_and_still_plays_seeks_and_processes` |
| Secret fields that refer to a Vaultwarden or Bitwarden vault, read by Lens and never shown to anyone, agents included | [test_secrets.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_secrets.py) `test_a_secret_field_reads_its_value_from_the_vault`, `test_the_vault_is_read_again_and_what_vanishes_stops_being_used`, `test_the_vault_is_set_up_and_checked_in_the_app`, `test_agents_refer_to_secrets_and_never_read_them` |
| Secret fields that refer to a Vaultwarden or Bitwarden vault, read by Lens and never shown to anyone, agents included | [test_secrets.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_secrets.py) `test_a_secret_field_reads_its_value_from_the_vault`, `test_the_vault_is_read_again_and_what_vanishes_stops_being_used`, `test_the_vault_is_set_up_and_checked_in_the_app`, `test_agents_refer_to_secrets_and_never_read_them`; [settings-secrets.test.tsx](https://github.com/adeelahmad/lense/blob/main/nextjs-frontend/__tests__/settings-secrets.test.tsx) `is picked from the vault's items, and only the reference is saved`, `shows its reference and whether it was read, never a value` |
| Roles per namespace: viewer, editor, owner, or custom roles made of permissions | [test_roles.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_roles.py) `test_builtin_roles_keep_their_meaning`, `test_custom_roles_allow_exactly_their_permissions`; [journeys.mjs](https://github.com/adeelahmad/lense/blob/main/ci/clicks/journeys.mjs) `person-add` |
| Email invitations, and removing someone for good | [test_users.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_users.py) `test_invites_are_emailed_when_mail_is_set_up`, `test_admins_remove_people_for_good` |
| An audit log | [test_settings.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_settings.py) `test_settings_are_editable_and_secret`; [test_files.py](https://github.com/adeelahmad/lense/blob/main/fastapi_backend/tests/api/test_files.py) `test_change_a_file_and_the_audit_log` |
Expand Down
18 changes: 12 additions & 6 deletions docs/secrets.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ and whether it could be read, but never the value.

| | |
|---|---|
| Built | References in Settings' secret fields; Vaultwarden (and Bitwarden) as the first password manager; the API, the assistant's tools and MCP tools for them |
| Planned | Settings › Secrets in the web app, with a picker for the vault's items; references in other stores of secrets (sources, MCP servers, sign-in providers); more password managers |
| Built | References in Settings' secret fields; Vaultwarden (and Bitwarden) as the first password manager; Settings › Secrets in the web app, and a picker for the vault's items beside every secret field; the API, the assistant's tools and MCP tools for them |
| Planned | References in other stores of secrets (sources, MCP servers, sign-in providers); more password managers |

## References

Expand Down Expand Up @@ -43,10 +43,16 @@ of the vault:
2. Make a folder called `Lens` in it. Put the items Lens may read there, or share them into it. Lens reads that folder
only.
3. In that account, open **Settings › Security › Keys › View API key**. Copy the `client_id` and `client_secret`.
4. Give Lens the vault's address, the account's email, the API key and the account's master password with
`PUT /api/v1/settings/secrets`, or ask the admin assistant to (`change_settings`, which asks first). Then
`POST /api/v1/secrets/test` signs in and counts the items in the folder. (Settings › Secrets in the web app is
planned.)
4. In Lens, open **Settings › Secrets** (under Security and access). Set the vault's address, the account's email, the
API key and the account's master password, save, then click **Test**. It signs in and counts the items in the
folder. Agents do the same with `PUT /api/v1/settings/secrets` and `POST /api/v1/secrets/test`, or the admin
assistant's `change_settings` (which asks first).

Then, in any secret field in Settings, **From Vaultwarden** lists the folder's items by name with their fields:
pick one, or type part of a name and press Enter for the first. The field fills with its reference, shown in plain
text since it isn't a secret, and saves like any change. A saved reference shows as **From Vaultwarden ·
vaultwarden://Item/field**, with when it was last read or why it couldn't be. Settings › Secrets lists every field
that refers to the vault, each linked to its place in Settings.

The vault encrypts its items end to end, so the server holds them only encrypted. The master password is what opens
them, so Lens needs it as well as the API key. Both are sealed with the server's key, like any other secret in
Expand Down
226 changes: 226 additions & 0 deletions nextjs-frontend/__tests__/settings-secrets.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
import "@testing-library/jest-dom";

import { Admin } from "@/app/openapi-client";
import { SettingsSection } from "@/components/settings/section";
import type { SectionId, SettingsView } from "@/components/settings/model";
import { TooltipProvider } from "@/components/ui/tooltip";

jest.mock("@/app/openapi-client", () => ({
Admin: {
updateSettings: jest.fn(),
getSecrets: jest.fn(),
listVaultItems: jest.fn(),
testSecrets: jest.fn(),
testMail: jest.fn(),
},
Metadata: {},
}));
jest.mock("next-auth/react", () => ({ useSession: () => ({ data: { accessToken: "t" } }) }));
jest.mock("next/navigation", () => ({ useRouter: () => ({ push: jest.fn() }) }));
jest.mock("@/lib/hooks/session", () => ({ useArchive: () => ({ namespaces: [] }) }));
jest.mock("@/components/ui/toast", () => ({ useToast: () => jest.fn() }));

const ok = (data: unknown) => Promise.resolve({ data, response: { ok: true, status: 200 } });
const REF = "vaultwarden://Mail/password";
const VAULT = {
id: "vaultwarden",
label: "Vaultwarden",
scheme: "vaultwarden",
configured: true,
url: "https://vault.example.com",
folder: "Lens",
};
const SECRETS = {
providers: [{ id: "builtin", label: "Saved in Lens", scheme: null, configured: true }, VAULT],
references: [
{
section: "mail",
key: "password",
ref: REF,
provider: "vaultwarden",
read: true,
error: null,
read_at: new Date().toISOString(),
missing: false,
},
],
fields: [],
};

const mail = (password: unknown) =>
({
mail: {
values: {
server: "smtp.example.com",
port: 587,
security: "starttls",
username: "lens",
password,
from_address: "lens@example.com",
from_name: "Lens",
},
},
}) as unknown as SettingsView;

const secrets = (values: Record<string, unknown>) =>
({
secrets: {
values: {
cache_minutes: 10,
vaultwarden_url: null,
vaultwarden_email: null,
vaultwarden_client_id: null,
vaultwarden_client_secret: null,
vaultwarden_password: null,
vaultwarden_folder: "Lens",
...values,
},
},
}) as unknown as SettingsView;

function show(id: SectionId, view: SettingsView) {
const qc = new QueryClient({ defaultOptions: { queries: { retry: false }, mutations: { retry: false } } });
return render(
<QueryClientProvider client={qc}>
<TooltipProvider>
<SettingsSection id={id} view={view} onErrors={() => undefined} />
</TooltipProvider>
</QueryClientProvider>,
);
}

beforeEach(() => {
(Admin.updateSettings as jest.Mock).mockReset().mockImplementation(() => ok({ ok: true }));
(Admin.getSecrets as jest.Mock).mockReset().mockImplementation(() => ok(SECRETS));
(Admin.testSecrets as jest.Mock).mockReset();
(Admin.listVaultItems as jest.Mock).mockReset().mockImplementation(() =>
ok({
provider: "vaultwarden",
items: [
{
name: "Mail",
type: "login",
fields: ["username", "password"],
ref: REF,
refs: { username: "vaultwarden://Mail/username", password: REF },
},
],
}),
);
});

describe("Settings › Secrets", () => {
it("sets up Vaultwarden, keeping the key and password write-only", async () => {
show("secrets", secrets({}));
fireEvent.change(screen.getByLabelText("Vaultwarden address"), { target: { value: "https://vault.example.com" } });
fireEvent.change(screen.getByLabelText("Lens’s account"), { target: { value: "lens@example.com" } });
fireEvent.change(screen.getByLabelText("API key client_id"), { target: { value: "user.1234" } });
fireEvent.change(screen.getByLabelText("API key client_secret"), { target: { value: "s3cret" } });
fireEvent.change(screen.getByLabelText("Master password"), { target: { value: "correct horse" } });
// the vault's own sign-in can't be a reference, so there's no picker here
expect(screen.queryByRole("button", { name: /From Vaultwarden/ })).not.toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "Save" }));
await waitFor(() => expect(Admin.updateSettings).toHaveBeenCalledTimes(1));
expect((Admin.updateSettings as jest.Mock).mock.calls[0][0]).toMatchObject({
path: { section: "secrets" },
body: {
vaultwarden_url: "https://vault.example.com",
vaultwarden_email: "lens@example.com",
vaultwarden_client_id: "user.1234",
vaultwarden_client_secret: "s3cret",
vaultwarden_password: "correct horse",
},
});
});

it("refuses an address with a password in it before saving", () => {
show("secrets", secrets({}));
fireEvent.change(screen.getByLabelText("Vaultwarden address"), {
target: { value: "https://me:pw@vault.example.com" },
});
expect(screen.getByText(/no user name, password or query/)).toBeInTheDocument();
});

it("tests the saved sign-in and lists the fields that refer to the vault", async () => {
(Admin.testSecrets as jest.Mock).mockImplementation(() =>
ok({ ok: true, items: 3, folder: "Lens", account: "lens@example.com" }),
);
show(
"secrets",
secrets({
vaultwarden_url: "https://vault.example.com",
vaultwarden_email: "lens@example.com",
vaultwarden_client_id: "user.1234",
vaultwarden_client_secret: { secret: true, set: true },
vaultwarden_password: { secret: true, set: true },
}),
);
expect(await screen.findByText(REF)).toBeInTheDocument();
expect(screen.getByRole("link", { name: "Email › Password" })).toHaveAttribute(
"href",
"/settings/mail?field=mail.password",
);
expect(screen.getByText(/Read from Vaultwarden/)).toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "Test" }));
expect(await screen.findByText(/3 item\(s\) in the folder Lens of lens@example.com/)).toBeInTheDocument();
expect(Admin.testSecrets).toHaveBeenCalledWith(expect.objectContaining({ body: { provider: "vaultwarden" } }));
});
});

describe("a secret field that refers to the vault", () => {
it("shows its reference and whether it was read, never a value", async () => {
show(
"mail",
mail({
secret: true,
set: true,
ref: REF,
provider: "vaultwarden",
read: false,
error: "there's no item called Mail",
}),
);
expect(screen.getByText(REF)).toBeInTheDocument();
expect(screen.getByText(/From Vaultwarden/)).toBeInTheDocument();
expect(screen.getByText(/Not read: there's no item called Mail/)).toBeInTheDocument();
});

it("is picked from the vault's items, and only the reference is saved", async () => {
show("mail", mail({ secret: true, set: true }));
fireEvent.click(screen.getByRole("button", { name: "Replace" }));
fireEvent.click(await screen.findByRole("button", { name: /From Vaultwarden/ }));
fireEvent.click(await screen.findByRole("button", { name: "password" }));
const input = screen.getByLabelText("Password");
expect(input).toHaveValue(REF);
expect(input).toHaveAttribute("type", "text"); // a reference isn't a secret to hide
fireEvent.click(screen.getByRole("button", { name: "Save" }));
await waitFor(() => expect(Admin.updateSettings).toHaveBeenCalledTimes(1));
expect((Admin.updateSettings as jest.Mock).mock.calls[0][0]).toMatchObject({
path: { section: "mail" },
body: { password: REF },
});
expect(Admin.listVaultItems).toHaveBeenCalledWith(
expect.objectContaining({ query: expect.objectContaining({ provider: "vaultwarden" }) }),
);
});

it("Enter in the vault's search picks the first item", async () => {
show("mail", mail(null));
fireEvent.click(await screen.findByRole("button", { name: /From Vaultwarden/ }));
const search = screen.getByRole("searchbox", { name: "Find an item in Vaultwarden" });
await screen.findByText("Mail");
fireEvent.keyDown(search, { key: "Enter" });
expect(screen.getByLabelText("Password")).toHaveValue(REF);
});

it("has no picker when no vault is set up", async () => {
(Admin.getSecrets as jest.Mock).mockImplementation(() =>
ok({ ...SECRETS, providers: [{ ...VAULT, configured: false }], references: [] }),
);
show("mail", mail(null));
await waitFor(() => expect(Admin.getSecrets).toHaveBeenCalled());
expect(screen.queryByRole("button", { name: /From Vaultwarden/ })).not.toBeInTheDocument();
});
});
Loading
Loading