# legato secret

## Executable

`secret` is a command of the app itself, and the [`legato` command](https://legatobar.com/docs/cli.md) passes it on. These two do the same thing:

```bash
legato secret list
/Applications/Legato.app/Contents/MacOS/Legato secret list
```

`legato secret` hands everything after `secret` to the app unchanged. It reads the same stdin and prints the same output, with the same exit status. The examples on this page use `legato`, which is at `/Applications/Legato.app/Contents/Helpers/legato`.

## Commands

| Command | Behavior |
| --- | --- |
| `legato secret set NAME [--host HOST] [--replace]` | Saves the value read from stdin in the Keychain. |
| `legato secret set NAME --1password REFERENCE [--1password REFERENCE] [--host HOST] [--replace]` | Links a 1Password reference (`op://Vault/Item/field`) in place of a value, after checking that it can be read. Give it twice for a [secret in two fields](https://legatobar.com/docs/1password.md#a-secret-in-two-fields), first field first: Legato sends them as `first:second`. Reads nothing from stdin. |
| `legato secret expires NAME DATE\|none [--host HOST]` | Changes or clears only a saved secret's expiry date. The value isn't read or changed. |
| `legato secret list` | Prints the saved secrets with their hosts, expiry dates and the pages that use them, then the secrets your pages name that aren't saved yet. Also says whether a service account token is saved. Values are never printed. |
| `legato secret delete NAME [--host HOST]` | Removes a saved secret, or a [connection](https://legatobar.com/docs/source-requests.md#connections) from this Mac without signing it out of the service. `set` and `expires` refuse a connection: change it in Settings → Secrets. |
| `legato secret token set [--replace]` | Saves the 1Password service account token read from stdin. |
| `legato secret token delete` | Removes the service account token. |
| `legato secret help` | Prints the usage. |

Both forms of `set` also take `--expires DATE`, the day the token stops working: `2027-02-09`, or `90d` for 90 days from today on this Mac. A secret saved without it has no expiry date, even if the one it replaces had one. See [Expiry dates](https://legatobar.com/docs/secrets.md#expiry-dates).

`NAME` is the name a page's source gives its secret: 1–48 lowercase letters, digits and hyphens, starting with a letter or digit. `HOST` is a hostname, with no URL or port. Leave it out when the name goes with one host, counting the pages that name it and the secrets already saved under it. For a new secret, that is the host of the page that names it, so write the page first. If nothing uses the name yet, or it goes with more than one host, give `--host`. A secret or token that is already saved is replaced only with `--replace`.

## Stdin and 1Password

At a terminal, you type the value and it isn't echoed. A piped value is read to the end, and a trailing newline from the pipe is dropped. An empty value is refused. A service account token starts with `ops_`.

For `--1password`, the `OP_SERVICE_ACCOUNT_TOKEN` in your shell must be the same token that is saved in Legato. Legato checks the reference by reading it before it saves anything (both of them, for a secret in two fields). A different token, an app sign-in or no token is refused. Use Settings → Secrets for those setups. See [Secrets from 1Password](https://legatobar.com/docs/1password.md#from-the-command-line-or-an-agent).

After a change, cards that were waiting for the secret try again at once.

## Output and exit status

Success and help print plain text on stdout and exit with status 0. A failure prints plain text on stderr. The exit status is 1 when the command couldn't do what you asked: the name isn't valid, the host can't be worked out, the secret isn't saved, or the Keychain refused. It is 2 when the command line itself is wrong. If `legato` can't find the app it came with, it says so on stderr and exits with status 1. `legato secret` with nothing after it prints the usage to stderr and exits with status 2.

## Examples and success output

Save a secret in the Keychain by typing its value at the prompt:

```bash
legato secret set pihole --host example.com
legato secret list
```

Saving prints `Saved pihole for example.com.`, followed by ` It expires on 2027-02-09.` when `--expires` was given. The list starts with `1Password service account: saved` or `1Password service account: not saved`. Each saved secret then has a line with its name, its host and `saved`, or `from 1Password` for a reference. If pages name the secret, `used by` and their file names follow. With a page file `pihole.json` that names this secret, the list is:

```text
1Password service account: not saved
pihole  example.com  saved  used by pihole.json
```

A secret with an expiry date shows it after where it's kept: `tailscale  api.tailscale.com  saved  expires 2027-02-09  used by tailscale.json`. To change only the date, or clear it:

```bash
legato secret expires pihole 2027-02-09 --host example.com
legato secret expires pihole none --host example.com
```

These print `pihole for example.com expires on 2027-02-09.` and `Cleared the expiry date of pihole for example.com.`

A secret that a page names and that isn't saved yet has a line of the form `NAME  HOST  needed by FILES`. With nothing to list, the command prints `No secrets saved, and no page names one.`

A script can pipe the value in. This one reads it from 1Password with the `op` tool. `--replace` is there because the secret was saved above:

```bash
op read 'op://Example/Pihole/password' | legato secret set pihole --host example.com --replace
```

To link a 1Password reference, save the service account token first. Then link the reference from a shell that has the same token in `OP_SERVICE_ACCOUNT_TOKEN`:

```bash
printenv OP_SERVICE_ACCOUNT_TOKEN | legato secret token set
legato secret set pihole --host example.com --1password op://Example/Pihole/password --replace
```

The first command prints `Saved the 1Password service account.`. If a token is already saved, it is refused unless you add `--replace`. The second prints `Saved pihole for example.com, read from 1Password (op://Example/Pihole/password).`.

To remove the example secret and the service account token:

```bash
legato secret delete pihole --host example.com
legato secret token delete
```

These print `Removed pihole for example.com.` and `Removed the 1Password service account. Secrets from 1Password stop until one is saved again.` Run `token delete` only if you saved the token for this example. Every secret that Legato reads from 1Password needs it.
