diff --git a/DESIGN.md b/DESIGN.md index aa0a62b..3a2331d 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -1974,7 +1974,9 @@ The fields of each event, after `seq`, `time`, `event` and `prev`: The request fields are `request_id`, `client`, `kind` (`secrets`, `ssh_login`, `ssh_grant`, `preapprove`, `standing` or `web_login`), `secrets`, `key`, `hosts`, `host_key` (a SHA256 fingerprint), `remote_user`, and the claimed -`command` and `reason`. A website login (section 21.8) has `logins` (the +`command` and `reason`. A secret of a project is `SECTION/KEY` in +`secrets`, and `project_secrets` lists its `name` and `id` (section 14, +"Projects"). A website login (section 21.8) has `logins` (the `[logins.NAME]` section), `item` (the item ID) and the claimed `origin`, never a username. `login_list` and `login_totp` are its other entries. A `preapprove` request that was approved also has `grant_id`, `uses`, `not_before` and `not_after` (UTC, RFC 3339). A @@ -2426,8 +2428,9 @@ a path, a query, `OPTIONS *` or `PRI *` gets 404. The types are in gets `503` too, also when the audit log says `cancelled`: the client did not get a deny. - More caps, besides those above: at most 32 variables in a request; each - name (of a variable, a secret, a key, a host or a remote user) at most 64 - bytes; a public key at most 4 KiB; the data to sign and each + name (of a variable, a key, a host or a remote user) at most 64 + bytes; a secret name at most 129 bytes, for `SECTION/KEY` of a project + (section 14, "Projects"); a public key at most 4 KiB; the data to sign and each session-bind at most 8 KiB; at most 2 session-binds; at most 20 hosts in an SSH grant (a host listed twice counts twice here). The answer is at most 2 MiB: each value is at most 64 KiB, and the values of one request @@ -3122,6 +3125,108 @@ write to, below directories that pass the same checks. It may be a symbolic link. `broker_pin` is the client's only trust in the broker, so other users must not be able to change it. +### Projects + +*(issue 177; built: `internal/config/project.go`, +`internal/broker/projects.go`, `internal/store/bwsproject.go`)* + +A `[project.NAME]` section allows every secret of one Bitwarden Secrets +Manager project, with one rule. Putting a secret into the project is then +the "allow" step, with no config edit and no reload. + +```toml +[project.ci] +store = "main" # a store of type "bws" +project = "00000000-0000-0000-0000-000000000000" +clients = ["laptop"] +ask = "window" # "always" or "window" +window = "15m" +# preapprove and standing as for [secret.*] +``` + +- `store` must name a store of type `bws`. `project` is the project's + UUID; the broker keeps it in lower case. +- `ask = "never"` is an error. It would make every secret of the project + tap-free, also secrets added later, and the "one `never` per `id`" check + cannot run for secrets the broker has not seen. +- One project may be in one `[project.*]` section only, in one store or + in two. Project UUIDs are compared in lower case. A second section is an + error at the start and at a reload: one secret would get two rules. + +**Names.** A client asks for a project secret as `SECTION/KEY`, where +`KEY` is the secret's `key` in Bitwarden, for example +`nodkey run --secret GH_TOKEN=ci/GITHUB_TOKEN -- ...`. A name without `/` +is always a `[secret.*]` section, so the two kinds cannot clash. A name +with two or more `/` is refused. Each part follows the name rule, so it +has at most 64 bytes, and `SECTION/KEY` at most 129 (`api.MaxSecretName`). +The key is matched exactly, also in upper and lower case. Only a key that +follows the name rule can be asked for; other secrets of the project are +skipped, and their key is never shown. + +**Lookup.** The broker first checks the name, that the section exists, +and that the client is in its `clients` or in the `clients` of a +`[secret.*]` entry of a `bws` store. Then, before the core decides, it +runs `bws secret list PROJECT` once for each project of the request +(section 16) and finds the secret by its key: + +- no secret has the key: denied (`not_allowed`); the audit entry does not + name the key, which the client chose; +- two or more secrets have the key: denied (`not_allowed`). Bitwarden does + not keep keys unique, even in one project, and the broker cannot tell + which one is meant. The denied entry names `SECTION/KEY`, and a line in + the journal says why; never a value; +- one secret: the broker has its ID. + +The request keeps its place for a request body (section 8) while the +lists run. A `SECTION/KEY` goes into the audit subject only once it is +found, so a key that the client made up is never in the audit log. Two +names of one request that are the same secret, such as an entry's name +and `SECTION/KEY` with its ID, deny the request. + +After the tap, the broker gets the secret by its ID with `bws secret +get`, from the store of the project, and checks that the `key` and the +`projectId` of that answer are those of the list. If not, such as for a +rename between the two runs, the request is denied. + +**Which rule wins.** With the ID, the broker looks for a `[secret.*]` +entry with that ID in any store of type `bws`. Two stores can use the +same machine account, so the store is not part of the match. + +- One entry: **the entry wins, completely.** Its `clients`, `ask`, + `window`, `preapprove` and `standing` apply, and the project's rule is + not used. Nothing is merged. A client in the project's `clients` but not + in the entry's is refused. The entry may be looser than the project + (`window` when the project says `always`): the owner wrote it for that + one secret. The entry's own grants are used, as for its own name. +- Two or more entries: denied. There is no clear winner. +- No entry: the project's rule applies. + +The broker applies this on each request, after it has found the secret. +The approval core checks it again with the config that it decides with +(`config.Broker.RuleFor`). + +**Grants are for one secret ID, not for a name.** A grant, a pre-approval +and a standing grant of a project secret are kept per (client, project +section, secret ID). The key becomes an ID when the grant is made, and +each use is checked against that ID. So when someone gives the key of a +granted secret to another secret, a request for `SECTION/KEY` finds the +other ID, and the grant does not cover it: the broker asks again. A +grant covers its secret under a new key too. A secret that leaves the +project is not found by `SECTION/KEY` any more; a `[secret.*]` entry for +it still works while the machine account can read it. + +**Message.** The verified part shows the secret as +`ci/GITHUB_TOKEN (project rule, ID …1a2b3c4d)`: the name, that the +project's rule applies, and the last 8 characters of the ID. When an entry +wins, it shows `ci/GITHUB_TOKEN (rule of secret NAME, ID …1a2b3c4d)`. The +key in it was read from Bitwarden; the client sent the same key, and only +a key that follows the name rule gets here. A list of standing grants +shows a project grant as `ci (project rule, ID …1a2b3c4d)`. + +**Audit.** The entries name the secret as `SECTION/KEY` in `secrets`, +and `project_secrets` holds its ID: `[{"name": "ci/GITHUB_TOKEN", "id": +"…"}]`. + ### Reload A change of the config needs no restart (issue 29). `SIGHUP` asks the @@ -3132,8 +3237,10 @@ the grants that the new config takes away. 1. The broker loads the config file, `known_hosts` and the SSH keys with the same code and the same checks as a start (`config.LoadBroker`, then the keys). A config that fails any check is refused. -2. A reload may change only `[client.*]`, `[secret.*]`, `[key.*]`, the - `known_hosts` path and its hosts. Every other part must be equal, also +2. A reload may change only `[client.*]`, `[secret.*]`, `[key.*]`, + `[project.*]`, the `known_hosts` path and its hosts. It runs the checks + of `[project.*]` again: `ask = "never"` and one project in two + sections refuse the reload. Every other part must be equal, also a part that a later version adds, until it is decided otherwise. A change of `listen`, `tls_key`, `audit_log`, `[telegram]`, `[ntfy]`, `[updates]`, `[preapprove]`, `[standing]` or any `[store.*]` is refused with "needs a restart". @@ -3150,6 +3257,10 @@ the grants that the new config takes away. - its client is still there, with the same pin; - each of its secrets is still there, still lists the client, and has the same store, item and rule (other clients in the list may change); + - a grant of a project secret: the section is still there, with the + same store and project, and the same rule applies to the secret ID, + with the client in its `clients`. A project grant for a secret that + now has its own `[secret.*]` entry ends; - its key is still there, still lists the client, has the same file, the same public key in it and the same rule, and each host of the grant is still in `known_hosts` and still allowed by `hosts`. @@ -4101,6 +4212,8 @@ bws 2.1.0 and bw 2026.9.1 on 2026-10-14 | when | command | what the broker reads | |---|---|---| | a `bws` fetch | `bws secret get ID --output json --color no` | stdout: one JSON object with `id`, `organizationId`, `projectId`, `key`, `value`, `note`, `creationDate` and `revisionDate`. The broker reads only `id` and `value`. | +| a project secret, first (section 14, "Projects") | `bws secret list PROJECT --output json --color no` | stdout: one JSON array of such objects, with the values of all secrets of the project. The broker reads only `id`, `key` and `projectId` of each, and clears the whole answer. `PROJECT` must be a UUID. | +| a project secret, then | `bws secret get ID --output json --color no` | as a `bws` fetch, and also `key` and `projectId`: they must be the key and the project of the list, else the request is denied. | | a `bw` unlock | `bw unlock --passwordfile FILE --raw --nointeraction` | stdout: only the session key, in base64. | | a `bw` fetch, first | `bw sync --force --nointeraction` | only the exit code. | | a `bw` fetch, then | `bw get item ID --nointeraction` | stdout: one JSON object, the item. The broker reads only `id`, `login.password`, `deletedDate` and `archivedDate`. | @@ -4118,6 +4231,12 @@ bws 2.1.0 and bw 2026.9.1 on 2026-10-14 still works. Any other answer is refused, and the request is denied. - `id` must be the asked ID, in upper or lower case. An answer without it, or for another item, is refused. +- The answer of `bws secret list` is one JSON array, not one object, with + the same rules for each element. Each must have a UUID `id`, a `key` + and the `projectId` of the asked project, else the whole answer is + refused. A secret whose `key` breaks the name rule is skipped, and its + key is never kept or shown. The answer is capped at 1 MiB, as every + answer: a project too big for that is refused. - A value that is missing, `null` or empty is refused. So is a value with a NUL byte: the environment of a command cannot hold it. So is an item that is not a login item, an item in the trash (`deletedDate` is not diff --git a/THREAT-MODEL.md b/THREAT-MODEL.md index 596b020..110aad4 100644 --- a/THREAT-MODEL.md +++ b/THREAT-MODEL.md @@ -1649,6 +1649,76 @@ What nodkey does not do: Risk that remains: medium, as for every value that an agent gets (section 5). +**T49. Anyone who can write to a Secrets Manager project changes what +agents can get.** +A `[project.*]` section allows every secret of one project (DESIGN.md +section 14, "Projects"). A person or a machine account that can write to +the project can add a secret, or give the key of a secret to another +secret, with no change of the config. +What nodkey does: +- `ask = "never"` is an error in a `[project.*]` section, so a secret that + is added to the project always needs a tap or a grant from one. +- Grants, pre-approvals and standing grants of a project secret are kept + per secret ID, not per name. When a key moves to another secret, a + grant for `SECTION/KEY` does not hand out the other secret: the broker + asks again. +- The tap message shows the name, that a project rule applies, and the + last 8 characters of the secret's ID, so the person can see that it is + another secret. +- Two secrets with the same key in the project are refused, with an audit + entry: the broker never guesses which one is meant. +- After the tap, the answer of `bws secret get` must have the key and the + project of the list; a rename or a move in between denies the request. +- One project may be in one section only, and a `[secret.*]` entry with + the same ID wins completely, so one secret never has two rules that + could both apply. +- docs/setup.md says: the broker's machine account gets "Can read" on the + project and nothing more, and only the owner may write to the project. +What nodkey does not do: +- It cannot see who wrote to the project. A person with write access to + the project can put any secret there, and a client of the section can + then ask for it with a tap. +Risk that remains: medium while others can write to the project; low when +only the owner can. + +**T50. A key from Bitwarden is shown in the tap message.** +The key of a project secret is typed by whoever can write to the project, +and the message shows it. +What nodkey does: +- Only a key that follows the name rule (letters, digits, `.`, `_` and + `-`, at most 64) can be asked for or shown. The broker skips other + secrets of the project, and never keeps or shows their keys. +- The key is matched exactly; a client cannot reach another secret with + another case. +- Telegram and ntfy check the form of the secret text again before they + send a message. +Risk that remains: low. A key can still be a misleading word, such as +the name of another secret; the end of the ID in the message helps here. + +**T51. `bws secret list` brings every value of the project into the +broker's memory.** +The answer of `bws secret list` holds the values of all secrets of the +project, also secrets that no request asks for. +What nodkey does: +- The broker reads only `id`, `key` and `projectId` of each secret, never + keeps a value or a note from the list, and clears the whole answer + right after reading it. +- The answer is capped at 1 MiB, as every answer of a store. A project + too big for that is refused; the setup guide says to keep a project + small, or to split it. +- The list runs only for a client that the section, or an entry of a + `bws` store, lists, and only once for each project of a request. The + request keeps its place for a request body while the list runs, so the + cap per client of bodies in progress also caps its lists. +What nodkey does not do: +- A client that a section lists can make the broker list the project + without a tap, as often as its caps allow. Each list is denied if the + key is wrong, but it uses the machine account's API limits. +- Go keeps copies that nodkey cannot clear, such as in the buffers of + the pipe from bws (section 5). For a short time, more values are in the + broker's memory than for one `bws secret get`. +Risk that remains: low, for a broker host that is safe (section 7). + ## 7. Assumptions - The broker host is patched, has no agent on it, and only the owner has diff --git a/docs/setup.md b/docs/setup.md index ed053cd..112016f 100644 --- a/docs/setup.md +++ b/docs/setup.md @@ -698,6 +698,66 @@ in the example config, `[store.backup-job]` with the credential another secret through that store. Its token can read only that one project. +### bws: allow a whole project + +A `[project.NAME]` section allows every secret of one Secrets Manager +project, with one rule +([DESIGN.md section 14, "Projects"](../DESIGN.md#projects)). To let agents +use a new secret, you then put it into the project. You do not edit the +config, and you do not reload the broker. + +So anyone who can write to the project decides what agents can get +([THREAT-MODEL.md T49](../THREAT-MODEL.md#6-threats-and-answers)). In the +web app of Secrets Manager: + +1. Make a project only for the secrets that agents may get. Keep it + small: the broker reads the list of the whole project, with all + values, for each request, and it refuses a list of more than 1 MiB. + Split a big project into two. +2. Give the broker's machine account **"Can read"** on this project, and + nothing more. Never "Can read, write". +3. Only you may write to the project. Do not give another person or + machine account write access to it. +4. Give each secret a **key** of letters, digits, `.`, `_` and `-`, at + most 64 characters, such as `GITHUB_TOKEN`. A secret with another key + cannot be asked for. Give each secret its own key: when two secrets of + the project have one key, the broker refuses both. +5. Copy the ID of the project, a UUID. It goes into `project` in the + config. + +In the config: + +```toml +[project.ci] +store = "main" +project = "00000000-0000-0000-0000-000000000000" +clients = ["laptop"] +ask = "window" +window = "15m" +``` + +- `store` must be a store of type `bws`. +- `ask = "never"` is not allowed here. For a job without a tap, use a + `[secret.*]` section for that one secret. +- One project may be in one `[project.*]` section only. + +A client asks for a secret of the project as `SECTION/KEY`: + +```sh +nodkey run --secret GH_TOKEN=ci/GITHUB_TOKEN -- gh pr list +``` + +The key must be written exactly as in Bitwarden, also in upper and lower +case. The tap message shows `ci/GITHUB_TOKEN (project rule, ID …1a2b3c4d)`: +the end of the secret's ID tells you which secret it is. + +**A different rule for one secret.** Give the secret a `[secret.*]` +section with its ID, in any store of type `bws`. That section's rule then +applies to the secret completely, also when a client asks for +`ci/GITHUB_TOKEN`. It may be stricter or looser than the project's rule. +Do not give one secret two `[secret.*]` sections: the broker then refuses +it. + ### 1password: a vault only for nodkey The `1password` store uses the official 1Password SDK inside the broker. @@ -1806,7 +1866,7 @@ To check a config before you use it, run `nodkey broker check` The command prints why, and exits with 1. The journal says why too. Fix the config, and run the command again. -A reload can change `[client.*]`, `[secret.*]`, `[key.*]` and +A reload can change `[client.*]`, `[secret.*]`, `[key.*]`, `[project.*]` and `known_hosts`. A change of `listen`, `tls_key`, `audit_log`, `[telegram]`, `[ntfy]`, `[updates]`, `[preapprove]`, `[standing]` or a `[store.*]` section needs a restart: the reload refuses it, and says so. diff --git a/internal/api/api.go b/internal/api/api.go index 4d2ac69..49367af 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -163,6 +163,10 @@ const ( // MaxName is the most bytes of a name: a variable, a secret, a key, a // host or a remote user. Valid names are shorter. MaxName = 64 + // MaxSecretName is the most bytes of a secret name: a name, or + // SECTION/KEY of a project, two names and a "/" (DESIGN.md section + // 14, "Projects"). Each part is at most MaxName. + MaxSecretName = 2*MaxName + 1 // MaxKey is the most bytes of an SSH public key in SSH wire form. An // RSA key of 16384 bits, the largest that OpenSSH accepts, has about // 2 KiB. diff --git a/internal/api/api_test.go b/internal/api/api_test.go index 6f67c8d..ad85a80 100644 --- a/internal/api/api_test.go +++ b/internal/api/api_test.go @@ -220,8 +220,9 @@ func TestCaps(t *testing.T) { &SecretsRequest{Secrets: make([]SecretRef, MaxSecrets+1)}}, {"variable name", &SecretsRequest{Secrets: []SecretRef{{Var: long[1:]}}}, &SecretsRequest{Secrets: []SecretRef{{Var: long}}}}, - {"secret name", &SecretsRequest{Secrets: []SecretRef{{Name: long[1:]}}}, - &SecretsRequest{Secrets: []SecretRef{{Name: long}}}}, + // A secret name may be SECTION/KEY of a project: 129 bytes. + {"secret name", &SecretsRequest{Secrets: []SecretRef{{Name: strings.Repeat("a", MaxSecretName)}}}, + &SecretsRequest{Secrets: []SecretRef{{Name: strings.Repeat("a", MaxSecretName+1)}}}}, {"arguments", &SecretsRequest{Command: make([]string, MaxArgs)}, &SecretsRequest{Command: make([]string, MaxArgs+1)}}, {"command lines", &SignRequest{Commands: make([][]string, MaxCommands)}, @@ -723,7 +724,8 @@ func TestPreapproveCaps(t *testing.T) { }{ {"secrets", &PreapproveRequest{Secrets: make([]string, MaxSecrets+1)}}, {"hosts", &PreapproveRequest{Hosts: make([]string, MaxGrantHosts+1)}}, - {"secret name", &PreapproveRequest{Secrets: []string{long}}}, + {"secret name", &PreapproveRequest{Secrets: []string{strings.Repeat("a", MaxSecretName+1)}}}, + {"host name", &PreapproveRequest{Hosts: []string{long}}}, {"key", &PreapproveRequest{Key: long}}, {"from", &PreapproveRequest{From: strings.Repeat("1", MaxTime+1)}}, {"until", &PreapproveRequest{Until: strings.Repeat("1", MaxTime+1)}}, diff --git a/internal/api/types.go b/internal/api/types.go index 1cb1d92..37f3e89 100644 --- a/internal/api/types.go +++ b/internal/api/types.go @@ -100,7 +100,7 @@ func (r *SecretsRequest) Check() error { return errTooMany } for _, s := range r.Secrets { - if len(s.Var) > MaxName || len(s.Name) > MaxName { + if len(s.Var) > MaxName || len(s.Name) > MaxSecretName { return errTooLong } } @@ -582,7 +582,12 @@ func (r *PreapproveRequest) Check() error { if len(r.Key) > MaxName || len(r.User) > MaxName || len(r.From) > MaxTime || len(r.Until) > MaxTime { return errTooLong } - for _, s := range append(append([]string(nil), r.Secrets...), r.Hosts...) { + for _, s := range r.Secrets { + if len(s) > MaxSecretName { + return errTooLong + } + } + for _, s := range r.Hosts { if len(s) > MaxName { return errTooLong } diff --git a/internal/approval/check.go b/internal/approval/check.go index 30720be..fbef9ba 100644 --- a/internal/approval/check.go +++ b/internal/approval/check.go @@ -109,17 +109,19 @@ func (c *Core) checkSecrets(r *Request, p *pending) bool { continue // two variables for one secret } seen[name] = true - s, known := p.cfg.Secrets[name] - if !known || p.client == "" || !slices.Contains(s.Clients, p.client) { + it, clients, label, known := secretItem(p, name, r.Projects) + if !known || p.client == "" || !slices.Contains(clients, p.client) { ok = false continue } - if !validRule(s.Rule, s.Clients) { + if !validRule(it.rule, clients) { + ok = false + continue + } + if !p.addSecret(name, label, it, r.Projects) { ok = false continue } - p.items = append(p.items, item{ref: ref{name: name}, rule: s.Rule}) - p.subject.Secrets = append(p.subject.Secrets, name) } if !ok { return false @@ -247,14 +249,16 @@ func (c *Core) checkPreapprove(r *Request, p *pending) bool { continue } seen[name] = true - s, known := p.cfg.Secrets[name] - if !known || p.client == "" || !slices.Contains(s.Clients, p.client) || !allowed(s.Rule, s.Clients) { + it, clients, label, known := secretItem(p, name, r.Projects) + if !known || p.client == "" || !slices.Contains(clients, p.client) || !allowed(it.rule, clients) { + ok = false + continue + } + if !p.addSecret(name, label, it, r.Projects) { ok = false continue } - p.items = append(p.items, item{ref: ref{name: name}, rule: s.Rule}) - p.subject.Secrets = append(p.subject.Secrets, name) - refs = append(refs, ref{name: name}) + refs = append(refs, it.ref) } if len(refs) > MaxPreapproveSecrets { ok = false diff --git a/internal/approval/decide.go b/internal/approval/decide.go index 8f8983c..08ede43 100644 --- a/internal/approval/decide.go +++ b/internal/approval/decide.go @@ -43,6 +43,10 @@ type pending struct { items []item // the secrets, or the one key subject audit.Subject claim Claim + // labels are the texts of the message for the names in + // subject.Secrets, in the same order: the name, and for a project + // secret its rule and the end of its ID. + labels []string hostKey string // KindSSHLogin: the host key in SSH wire form user string // KindSSHLogin, KindSSHGrant: the remote user @@ -108,7 +112,7 @@ func (p *pending) prompt(now moment) Prompt { ShortCode: p.code, Kind: p.kind, Client: p.client, - Secrets: slices.Clone(p.subject.Secrets), + Secrets: slices.Clone(p.labels), Key: p.subject.Key, Hosts: slices.Clone(p.subject.Hosts), HostKey: p.subject.HostKey, @@ -720,6 +724,7 @@ func (c *Core) newResult(p *pending) *Result { s := p.subject s.RequestID = p.id s.Secrets = slices.Clone(s.Secrets) + s.ProjectSecrets = slices.Clone(s.ProjectSecrets) s.Hosts = slices.Clone(s.Hosts) return &Result{ RequestID: p.id, diff --git a/internal/approval/list.go b/internal/approval/list.go index 83a5684..bacb51d 100644 --- a/internal/approval/list.go +++ b/internal/approval/list.go @@ -35,7 +35,7 @@ func (c *Core) ListStanding() []GrantInfo { } gi := GrantInfo{Client: g.client, LastUse: g.lastUse, End: g.end.wall} for _, r := range g.refs { - gi.Secrets = append(gi.Secrets, r.name) + gi.Secrets = append(gi.Secrets, r.text()) } for _, w := range g.windows { gi.Windows = append(gi.Windows, windowText(w)) diff --git a/internal/approval/project.go b/internal/approval/project.go new file mode 100644 index 0000000..7cae709 --- /dev/null +++ b/internal/approval/project.go @@ -0,0 +1,112 @@ +package approval + +import ( + "slices" + "strings" + + "github.com/code6x/nodkey/internal/audit" + "github.com/code6x/nodkey/internal/config" +) + +// ProjectSecret is a secret of a [project.*] section that the broker has +// found for a name SECTION/KEY (DESIGN.md section 14, "Projects"): the +// broker read the key and the ID from "bws secret list". The core keeps +// grants per ID, not per name. +type ProjectSecret struct { + Section string + Key string + ID string // a UUID in lower case +} + +// Name returns SECTION/KEY. +func (s ProjectSecret) Name() string { return s.Section + "/" + s.Key } + +// projectRef returns the ref of the secret id of the project section. +func projectRef(section, id string) ref { + return ref{project: true, name: section + "/" + strings.ToLower(id)} +} + +// splitProjectRef returns the section and the ID of a project ref. +func splitProjectRef(r ref) (section, id string) { + section, id, _ = strings.Cut(r.name, "/") + return section, id +} + +// idEnd returns the last 8 characters of a secret ID, which the message +// shows. +func idEnd(id string) string { return id[len(id)-8:] } + +// text returns how a message shows r in a list of grants: the name of a +// [secret.*] section, or for a project secret the section and the end of +// the ID, such as "ci (project rule, ID …1a2b3c4d)". +func (r ref) text() string { + if !r.project { + return r.name + } + section, id := splitProjectRef(r) + return section + " (project rule, ID …" + idEnd(id) + ")" +} + +// secretItem returns the item, the clients and the text of the message +// for the secret name of a request of p. A name SECTION/KEY must be in +// projects, with the ID that the broker found. Its rule is the one of +// config.Broker.RuleFor: a [secret.*] entry with the same ID wins +// completely, and its grants are the entry's. ok is false when the name +// is not in the config, or not in projects, or has no clear rule. +func secretItem(p *pending, name string, projects map[string]ProjectSecret) (it item, clients []string, label string, ok bool) { + pn, isProject, valid := config.ParseSecretName(name) + if !valid { + return item{}, nil, "", false + } + if !isProject { + s, known := p.cfg.Secrets[name] + if !known { + return item{}, nil, "", false + } + return item{ref: ref{name: name}, rule: s.Rule}, s.Clients, name, true + } + ps, found := projects[name] + if !found || ps.Section != pn.Section || ps.Key != pn.Key || !loginItemPattern.MatchString(ps.ID) { + return item{}, nil, "", false + } + pr, ok := p.cfg.RuleFor(ps.Section, ps.ID) + if !ok { + return item{}, nil, "", false + } + if pr.Entry != "" { + return item{ref: ref{name: pr.Entry}, rule: pr.Rule}, pr.Clients, name + " (rule of secret " + pr.Entry + + ", ID …" + idEnd(ps.ID) + ")", true + } + if pr.Rule.Ask == config.AskNever { + return item{}, nil, "", false // the config never allows it; check again + } + return item{ref: projectRef(ps.Section, ps.ID), rule: pr.Rule}, pr.Clients, + name + " (project rule, ID …" + idEnd(ps.ID) + ")", true +} + +// addSecret adds the secret of name, with its label, to p, and to the +// subject. A project secret adds its ID to the subject too. It reports +// false, and adds nothing, when another name of the request is the same +// secret already, such as an entry and SECTION/KEY with its ID: the +// request is denied. +func (p *pending) addSecret(name, label string, it item, projects map[string]ProjectSecret) bool { + if slices.ContainsFunc(p.items, func(o item) bool { return o.ref == it.ref }) { + return false + } + p.items = append(p.items, it) + p.subject.Secrets = append(p.subject.Secrets, name) + p.labels = append(p.labels, label) + if ps, ok := projects[name]; ok && strings.Contains(name, "/") { + p.subject.ProjectSecrets = append(p.subject.ProjectSecrets, audit.ProjectSecret{Name: name, ID: ps.ID}) + } + return true +} + +// keepsProject reports whether the config next still gives the project +// secret of r to client with the same rule as old. +func keepsProject(old, next *config.Broker, r ref, client string) bool { + section, id := splitProjectRef(r) + pr, ok := next.RuleFor(section, id) + return ok && pr.Entry == "" && old.SameProjectSecret(next, section, id, client) && + slices.Contains(pr.Clients, client) +} diff --git a/internal/approval/reload.go b/internal/approval/reload.go index 9782131..e0aa648 100644 --- a/internal/approval/reload.go +++ b/internal/approval/reload.go @@ -74,6 +74,12 @@ func keeps(old, next *config.Broker, sameKey func(string) bool, g *grant) bool { } continue } + if r.project { + if !keepsProject(old, next, r, g.client) { + return false + } + continue + } if !r.key { if !old.SameSecret(next, r.name, g.client) { return false diff --git a/internal/approval/renew.go b/internal/approval/renew.go index 314c726..4fdebe2 100644 --- a/internal/approval/renew.go +++ b/internal/approval/renew.go @@ -81,7 +81,7 @@ func (c *Core) scanWarnings(now moment) { w := Warning{RequestID: g.reqID, ShortCode: g.code, Client: g.client, Uses: g.perWindow, GrantID: g.id, End: g.end.wall.In(loc)} for _, r := range g.refs { - w.Secrets = append(w.Secrets, r.name) + w.Secrets = append(w.Secrets, r.text()) } for _, x := range g.windows { w.Windows = append(w.Windows, windowText(x)) diff --git a/internal/approval/request.go b/internal/approval/request.go index f974f9d..9cd94d9 100644 --- a/internal/approval/request.go +++ b/internal/approval/request.go @@ -1,6 +1,7 @@ package approval import ( + "slices" "time" "golang.org/x/crypto/ssh" @@ -21,6 +22,10 @@ type Request struct { // Secrets are the secret names of a KindSecrets request. A name may // be there more than once, such as for two variables; it counts once. Secrets []string + // Projects are the secrets of the names SECTION/KEY in Secrets, by + // name, as the broker found them in their project (DESIGN.md section + // 14, "Projects"). A name SECTION/KEY that is not here is denied. + Projects map[string]ProjectSecret // Key is the key name of a KindSSHLogin or KindSSHGrant request. Key string @@ -226,6 +231,12 @@ func (r *Result) mainEvents() []audit.Event { s := r.Subject if s.Kind == audit.KindSecrets { s.Secrets = secrets + s.ProjectSecrets = slices.DeleteFunc(slices.Clone(s.ProjectSecrets), func(ps audit.ProjectSecret) bool { + return !slices.Contains(secrets, ps.Name) + }) + if len(s.ProjectSecrets) == 0 { + s.ProjectSecrets = nil + } } gu := audit.GrantUsed{Subject: s, Via: via} if via == audit.ViaScheduled || via == audit.ViaStanding { diff --git a/internal/approval/standing.go b/internal/approval/standing.go index 43e1a69..d36f44d 100644 --- a/internal/approval/standing.go +++ b/internal/approval/standing.go @@ -75,15 +75,17 @@ func (c *Core) checkStanding(r *Request, p *pending) bool { continue } seen[name] = true - s, known := p.cfg.Secrets[name] - if !known || p.client == "" || !slices.Contains(s.Clients, p.client) || - !s.Rule.Standing || s.Rule.Ask == config.AskNever || !validRule(s.Rule, s.Clients) { + it, clients, label, known := secretItem(p, name, r.Projects) + if !known || p.client == "" || !slices.Contains(clients, p.client) || + !it.rule.Standing || it.rule.Ask == config.AskNever || !validRule(it.rule, clients) { ok = false continue } - p.items = append(p.items, item{ref: ref{name: name}, rule: s.Rule}) - p.subject.Secrets = append(p.subject.Secrets, name) - refs = append(refs, ref{name: name}) + if !p.addSecret(name, label, it, r.Projects) { + ok = false + continue + } + refs = append(refs, it.ref) } if len(refs) > MaxPreapproveSecrets { ok = false diff --git a/internal/approval/standinguse.go b/internal/approval/standinguse.go index bf2a628..4717925 100644 --- a/internal/approval/standinguse.go +++ b/internal/approval/standinguse.go @@ -43,7 +43,9 @@ type Alarmer interface { type StandingRequest struct { Client string Secrets []string - Claim Claim + // Projects are as in Request. + Projects map[string]ProjectSecret + Claim Claim // Token is the hash of the token that the client sent, or nil when // the header was not a valid token. Token *[32]byte @@ -81,7 +83,7 @@ func (c *Core) UseStanding(r StandingRequest) *Result { } defer c.leave(p) p.deadline = p.start.add(c.timeout(audit.KindSecrets)) - if !c.check(&Request{Client: r.Client, Kind: audit.KindSecrets, Secrets: r.Secrets, Claim: r.Claim}, p) { + if !c.check(&Request{Client: r.Client, Kind: audit.KindSecrets, Secrets: r.Secrets, Projects: r.Projects, Claim: r.Claim}, p) { return c.denied(p, audit.CauseNotAllowed) } tok, err := grantkey.New() diff --git a/internal/approval/state.go b/internal/approval/state.go index c11e8c9..55fb612 100644 --- a/internal/approval/state.go +++ b/internal/approval/state.go @@ -20,7 +20,12 @@ type ref struct { // login ref is never equal to a secret or key ref, so a grant for one // never covers the other. login bool - name string + // project says that name is SECTION/ID: a secret of the [project.*] + // section SECTION, by its ID in lower case (DESIGN.md section 14, + // "Projects"). A grant for it covers that one secret, whatever its key + // is now. It is never set together with key or login. + project bool + name string } // grantKey is the scope of a grant (DESIGN.md section 8): a client and a diff --git a/internal/audit/event.go b/internal/audit/event.go index 590d9ff..8cc51d8 100644 --- a/internal/audit/event.go +++ b/internal/audit/event.go @@ -7,6 +7,7 @@ import ( "fmt" "reflect" "regexp" + "slices" "strings" "time" @@ -150,6 +151,13 @@ const ( // holds checked data: the request ID that the broker made, and names from // the broker's own config and known_hosts file. A name that failed the // broker's checks is left out, so a denied request may have no names. +// ProjectSecret is the ID of a secret of a [project.*] section that a +// request named as SECTION/KEY (DESIGN.md section 14, "Projects"). +type ProjectSecret struct { + Name string `json:"name"` // SECTION/KEY + ID string `json:"id"` // the secret's ID, in lower case +} + type Subject struct { // RequestID is the request's random ID: 16 bytes in base64url without // padding (22 characters). It is required. @@ -161,6 +169,9 @@ type Subject struct { Kind Kind `json:"kind,omitempty"` // Secrets are secret names from the config, for KindSecrets. Secrets []string `json:"secrets,omitempty"` + // ProjectSecrets are the IDs of the names in Secrets of the form + // SECTION/KEY: secrets of a [project.*] section. + ProjectSecrets []ProjectSecret `json:"project_secrets,omitempty"` // Key is the key name from the config, for an SSH request. Key string `json:"key,omitempty"` // Hosts are host names from the broker's known_hosts file: the one host @@ -464,6 +475,11 @@ var ( // namePattern is the name rule of DESIGN.md section 14, for clients, // secrets, keys and known hosts. namePattern = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$`) + // projectNamePattern is SECTION/KEY: a secret of a [project.*] + // section, each part by the name rule. + projectNamePattern = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._-]{0,63}/[A-Za-z0-9][A-Za-z0-9._-]{0,63}$`) + // secretIDPattern is the ID of a project secret, in lower case. + secretIDPattern = regexp.MustCompile(`^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`) // windowPattern is the form of a window in a standing request: // "daily 03:00-05:00" or "mon,thu 03:00-05:00". @@ -573,8 +589,13 @@ func (s *Subject) validate(needRequest bool) error { return errors.New("uses must not be negative") } for _, name := range s.Secrets { - if !namePattern.MatchString(name) { - return errors.New("secrets must be names from the config") + if !namePattern.MatchString(name) && !projectNamePattern.MatchString(name) { + return errors.New("secrets must be names from the config, or SECTION/KEY of a project") + } + } + for _, ps := range s.ProjectSecrets { + if !projectNamePattern.MatchString(ps.Name) || !slices.Contains(s.Secrets, ps.Name) || !secretIDPattern.MatchString(ps.ID) { + return errors.New("project_secrets must name a secret SECTION/KEY of secrets, with an ID in lower case") } } if s.Key != "" && !namePattern.MatchString(s.Key) { diff --git a/internal/audit/project_test.go b/internal/audit/project_test.go new file mode 100644 index 0000000..e7cf770 --- /dev/null +++ b/internal/audit/project_test.go @@ -0,0 +1,31 @@ +package audit + +import "testing" + +// TestProjectSecretsValidate: a secret SECTION/KEY of a project, and its +// ID, are valid in an entry; other forms are not. +func TestProjectSecretsValidate(t *testing.T) { + const id = "0a1b2c3d-0000-4000-8000-00000000a001" + good := secretsSubject() + good.Secrets = []string{"github-token", "ci/GITHUB_TOKEN"} + good.ProjectSecrets = []ProjectSecret{{Name: "ci/GITHUB_TOKEN", ID: id}} + if _, _, err := encodeEvent(Request{Subject: good}); err != nil { + t.Fatalf("a good subject: %v", err) + } + for name, change := range map[string]func(s *Subject){ + "two slashes": func(s *Subject) { s.Secrets = []string{"a/b/c"} }, + "a long part": func(s *Subject) { s.Secrets = []string{"c/" + string(make([]byte, 65))} }, + "an ID in upper case": func(s *Subject) { s.ProjectSecrets[0].ID = "0A1B2C3D-0000-4000-8000-00000000A001" }, + "an ID not in names": func(s *Subject) { s.ProjectSecrets[0].Name = "ci/OTHER" }, + "an ID for a name": func(s *Subject) { s.ProjectSecrets[0].Name = "github-token" }, + "no ID": func(s *Subject) { s.ProjectSecrets[0].ID = "" }, + } { + s := good + s.Secrets = append([]string(nil), good.Secrets...) + s.ProjectSecrets = append([]ProjectSecret(nil), good.ProjectSecrets...) + change(&s) + if _, _, err := encodeEvent(Request{Subject: s}); err == nil { + t.Errorf("%s: no error", name) + } + } +} diff --git a/internal/broker/broker.go b/internal/broker/broker.go index b5f2a76..2a85e93 100644 --- a/internal/broker/broker.go +++ b/internal/broker/broker.go @@ -373,6 +373,11 @@ func checkStores(cfg *config.Broker, stores map[string]store.Store) error { return fmt.Errorf("broker: [secret.%s]: its store has no store", name) } } + for _, name := range slices.Sorted(maps.Keys(cfg.Projects)) { + if _, ok := stores[cfg.Projects[name].Store].(store.ProjectStore); !ok { + return fmt.Errorf("broker: [project.%s]: its store cannot list the secrets of a project", name) + } + } return nil } diff --git a/internal/broker/handler.go b/internal/broker/handler.go index 15f668d..50eb920 100644 --- a/internal/broker/handler.go +++ b/internal/broker/handler.go @@ -236,6 +236,11 @@ type request struct { // values are the values fetched so far; they are cleared on every // path. values []store.Value + // projects and listed hold the secrets of the names SECTION/KEY, by + // name, as resolveProjects found them (DESIGN.md section 14, + // "Projects"). They hold no value. + projects map[string]approval.ProjectSecret + listed map[string]store.ProjectSecret // body is set while the request holds a place to read and decode its // body; calling it gives the place back. body func() diff --git a/internal/broker/helpers_test.go b/internal/broker/helpers_test.go index b05bc59..ab611e3 100644 --- a/internal/broker/helpers_test.go +++ b/internal/broker/helpers_test.go @@ -15,6 +15,7 @@ import ( "errors" "fmt" "io" + "maps" "math/big" "net" "net/http" @@ -302,10 +303,17 @@ type fakeStore struct { fetches int given [][]byte started chan struct{} // gets a value when a Fetch starts + // projects are the secrets of each project for ListSecrets, by + // project ID; getAs is the key and project that FetchListed finds for + // an ID, when they differ from the list. lists counts ListSecrets. + projects map[string][]store.ProjectSecret + getAs map[string]store.ProjectSecret + lists int } func newFakeStore() *fakeStore { - return &fakeStore{items: make(map[string]string), started: make(chan struct{}, 64)} + return &fakeStore{items: make(map[string]string), started: make(chan struct{}, 64), + projects: make(map[string][]store.ProjectSecret), getAs: make(map[string]store.ProjectSecret)} } func (f *fakeStore) Start(context.Context) error { return nil } @@ -340,6 +348,29 @@ func (f *fakeStore) Fetch(ctx context.Context, id string) (store.Value, error) { func (f *fakeStore) Close() {} +// ListSecrets acts as store.ProjectStore. +func (f *fakeStore) ListSecrets(_ context.Context, project string) ([]store.ProjectSecret, error) { + f.mu.Lock() + defer f.mu.Unlock() + f.lists++ + if f.err != nil { + return nil, f.err + } + return slices.Clone(f.projects[project]), nil +} + +// FetchListed acts as store.ProjectStore: the value of the ID, when the +// store has the secret with the listed key and project now. +func (f *fakeStore) FetchListed(ctx context.Context, ps store.ProjectSecret) (store.Value, error) { + f.mu.Lock() + now, moved := f.getAs[ps.ID] + f.mu.Unlock() + if moved && (now.Key != ps.Key || now.Project != ps.Project) { + return store.Value{}, store.ErrOtherSecret + } + return f.Fetch(ctx, ps.ID) +} + // set changes the store under its lock, after the broker started. func (f *fakeStore) set(change func(*fakeStore)) { f.mu.Lock() @@ -414,6 +445,9 @@ type envOptions struct { clients []string // vault, if set, is the store "vault" instead of the fake one. vault store.Store + // stores are more stores, by name. The config needs a [store.*] + // section for each. + stores map[string]store.Store } // newEnv starts a broker with this config: @@ -531,10 +565,12 @@ func newEnv(t *testing.T, opts envOptions) *env { if opts.vault != nil { vaultStore = opts.vault } + stores := map[string]store.Store{"main": e.main, "vault": vaultStore} + maps.Copy(stores, opts.stores) svc, err := New(Parts{ Config: e.cfg, Audit: e.audit, - Stores: map[string]store.Store{"main": e.main, "vault": vaultStore}, + Stores: stores, Keys: keys, Logger: NewLogger(e.logs), Version: opts.version, diff --git a/internal/broker/preapprove.go b/internal/broker/preapprove.go index fbee09d..cfcf6e8 100644 --- a/internal/broker/preapprove.go +++ b/internal/broker/preapprove.go @@ -14,6 +14,7 @@ import ( "github.com/code6x/nodkey/internal/api" "github.com/code6x/nodkey/internal/approval" "github.com/code6x/nodkey/internal/audit" + "github.com/code6x/nodkey/internal/config" "github.com/code6x/nodkey/internal/grantkey" "github.com/code6x/nodkey/internal/sshauth" "github.com/code6x/nodkey/internal/transport" @@ -50,13 +51,26 @@ func (rq *request) preapprove(ctx context.Context, r *http.Request) { } sub.Reason = cl.Reason rq.pre = sub - rq.releaseBody() if !ok { + rq.releaseBody() rq.deny(sub, audit.CauseNotAllowed, "", http.StatusForbidden) return } + var names []string // each once, in order; each passed secretAllowed + for _, n := range req.Secrets { + if !slices.Contains(names, n) { + names = append(names, n) + } + } + // The place of the body is held while the stores list the projects. + resolved := rq.resolveProjects(ctx, names) + rq.releaseBody() + if !resolved { + return + } + sub = rq.pre ar := &approval.Request{ - Client: rq.client, Kind: audit.KindPreapprove, Secrets: sub.Secrets, Key: sub.Key, + Client: rq.client, Kind: audit.KindPreapprove, Secrets: names, Projects: rq.projects, Key: sub.Key, RemoteUser: sub.RemoteUser, Hosts: sub.Hosts, Uses: int(req.Uses), NotBefore: from, NotAfter: until, Claim: cl, } var token grantkey.Token @@ -157,12 +171,13 @@ func (s *Service) checkStanding(client string, req *api.PreapproveRequest) (sub continue } seen[name] = true - sec, known := s.view().cfg.Secrets[name] - if !known || !slices.Contains(sec.Clients, client) { + if !secretAllowed(s.view().cfg, client, name) { ok = false continue } - sub.Secrets = append(sub.Secrets, sec.Name) + if _, isProject, _ := config.ParseSecretName(name); !isProject { + sub.Secrets = append(sub.Secrets, name) // a project secret: once it is found + } } for _, w := range req.Windows { spec, valid := windowSpec(w) @@ -234,12 +249,13 @@ func (s *Service) checkPreapprove(client string, req *api.PreapproveRequest) (su continue } seen[name] = true - sec, known := s.view().cfg.Secrets[name] - if !known || !slices.Contains(sec.Clients, client) { + if !secretAllowed(s.view().cfg, client, name) { ok = false continue } - sub.Secrets = append(sub.Secrets, sec.Name) + if _, isProject, _ := config.ParseSecretName(name); !isProject { + sub.Secrets = append(sub.Secrets, name) // a project secret: once it is found + } } return sub, from, until, ok } diff --git a/internal/broker/projects.go b/internal/broker/projects.go new file mode 100644 index 0000000..ff1991c --- /dev/null +++ b/internal/broker/projects.go @@ -0,0 +1,162 @@ +package broker + +import ( + "context" + "errors" + "log/slog" + "net/http" + "slices" + + "github.com/code6x/nodkey/internal/approval" + "github.com/code6x/nodkey/internal/audit" + "github.com/code6x/nodkey/internal/config" + "github.com/code6x/nodkey/internal/store" +) + +// secretAllowed is the broker's first check of a secret name that client +// asks for. A name of a [secret.*] section must list client. A name +// SECTION/KEY needs a [project.*] section SECTION, and client in its +// clients or in the clients of a [secret.*] entry of a bws store, which +// may win over the project's rule. The broker checks the rule of the +// secret itself only after it has found the secret (resolveProjects), and +// the core checks it again. +func secretAllowed(cfg *config.Broker, client, name string) bool { + pn, isProject, valid := config.ParseSecretName(name) + switch { + case !valid: + return false + case !isProject: + sec, known := cfg.Secrets[name] + return known && slices.Contains(sec.Clients, client) + } + p, known := cfg.Projects[pn.Section] + if !known { + return false + } + if slices.Contains(p.Clients, client) { + return true + } + for _, s := range cfg.Secrets { + if cfg.Stores[s.Store].Type == config.StoreBWS && slices.Contains(s.Clients, client) { + return true + } + } + return false +} + +// resolveProjects finds the secret of each name SECTION/KEY of names in +// its project (DESIGN.md section 14, "Projects"): it runs "bws secret +// list" once for each project, and looks for the key exactly. The result +// goes to rq.projects and rq.listed. A key that is not in the project is +// refused; so is a key that two secrets of the project have, with a line +// in the journal that names the section and the key. Each name that it +// finds goes to rq.pre.Secrets; a name that the client made up never +// does. A secret whose rule +// does not list the client is refused. On a refusal or an error it has +// answered the request, and ok is false. +func (rq *request) resolveProjects(ctx context.Context, names []string) bool { + cfg := rq.v.cfg + lists := make(map[string][]store.ProjectSecret) // by section + for _, name := range names { + pn, isProject, _ := config.ParseSecretName(name) + if !isProject { + continue + } + if _, done := rq.listed[name]; done { + continue + } + p := cfg.Projects[pn.Section] + list, done := lists[pn.Section] + if !done { + var ok bool + if list, ok = rq.listProject(ctx, p); !ok { + return false + } + lists[pn.Section] = list + } + var found []store.ProjectSecret + for _, s := range list { + if s.Key == pn.Key { + found = append(found, s) + } + } + switch len(found) { + case 0: + // The key was chosen by the client, and is not in Bitwarden: + // the audit log does not get it. + rq.refuseProject("the key is not in the project, or the machine account cannot read it", "") + return false + case 1: + default: + // Bitwarden does not keep keys unique in a project. The broker + // cannot tell which secret is meant (DESIGN.md section 14). + rq.pre.Secrets = append(rq.pre.Secrets, name) + rq.refuseProject("two or more secrets of the project have this key; give each a key of its own in Bitwarden", name) + return false + } + rq.pre.Secrets = append(rq.pre.Secrets, name) // it is in the project + ps := approval.ProjectSecret{Section: pn.Section, Key: pn.Key, ID: found[0].ID} + rule, ok := cfg.RuleFor(pn.Section, ps.ID) + if !ok || !slices.Contains(rule.Clients, rq.client) { + rq.refuseProject("the secret has no clear rule, or its rule does not list this client", "") + return false + } + if rq.projects == nil { + rq.projects = make(map[string]approval.ProjectSecret) + rq.listed = make(map[string]store.ProjectSecret) + } + rq.projects[name], rq.listed[name] = ps, found[0] + } + return true +} + +// listProject runs "bws secret list" for the project p. On an error it +// has answered the request, and ok is false. +func (rq *request) listProject(ctx context.Context, p config.Project) ([]store.ProjectSecret, bool) { + ps, isPS := rq.s.stores[p.Store].(store.ProjectStore) + if !isPS { + rq.recordDenied(rq.pre, audit.CauseError, audit.CodeStoreFailed, false) + rq.answer(http.StatusServiceUnavailable) + return nil, false + } + if ctx.Err() != nil { + rq.recordDenied(rq.pre, audit.CauseCancelled, "", false) + rq.answer(http.StatusForbidden) + return nil, false + } + lctx, cancel := context.WithTimeout(ctx, fetchTimeout) + list, err := ps.ListSecrets(lctx, p.Project) + cancel() + if err == nil { + return list, true + } + if ctx.Err() != nil { + rq.recordDenied(rq.pre, audit.CauseCancelled, "", false) + rq.answer(http.StatusForbidden) + return nil, false + } + rq.s.log.Warn("broker: a store failed to list the secrets of a project; the request is denied", + slog.String("client", rq.client), slog.String("store", p.Store), slog.String("project", p.Name), + slog.String("error", errorText(err))) + if errors.Is(err, store.ErrRateLimited) { + rq.s.rateLimited(p.Store) + } + rq.recordDenied(rq.pre, audit.CauseError, audit.CodeStoreFailed, false) + rq.answer(http.StatusServiceUnavailable) + return nil, false +} + +// refuseProject denies a request for a project secret that a check +// refused, with a fixed reason in the journal. name is the secret +// SECTION/KEY when the journal may show it: only for a key that two +// secrets have. The key follows the name rule; a value is never shown. +func (rq *request) refuseProject(reason, name string) { + attrs := []any{slog.String("client", rq.client), slog.String("reason", reason)} + if name != "" { + attrs = append(attrs, slog.String("secret", name)) + } + if rq.s.refusedLines.allow() { + rq.s.log.Info("broker: refused a request for a project secret (DESIGN.md section 14, \"Projects\")", attrs...) + } + rq.deny(rq.pre, audit.CauseNotAllowed, "", http.StatusForbidden) +} diff --git a/internal/broker/projects_test.go b/internal/broker/projects_test.go new file mode 100644 index 0000000..f605bc6 --- /dev/null +++ b/internal/broker/projects_test.go @@ -0,0 +1,402 @@ +package broker + +import ( + "maps" + "net/http" + "slices" + "strings" + "testing" + "time" + + "github.com/code6x/nodkey/internal/api" + "github.com/code6x/nodkey/internal/audit" + "github.com/code6x/nodkey/internal/config" + "github.com/code6x/nodkey/internal/store" +) + +const ( + projectCI = "0a1b2c3d-0000-4000-8000-0000000000c1" + idCI1 = "0a1b2c3d-0000-4000-8000-00000000a001" + idCI2 = "0a1b2c3d-0000-4000-8000-00000000a002" +) + +// projectEnv starts a broker with a [project.ci] section of the store +// main: laptop may ask, with a 15 minute window. The project has the +// secret GITHUB_TOKEN (idCI1); idCI2 has a value but no key yet. change, +// if set, changes the config. +func projectEnv(t *testing.T, change func(*config.Broker), opts envOptions) *env { + t.Helper() + opts.config = func(c *config.Broker) { + c.Projects = map[string]config.Project{"ci": {Name: "ci", Store: "main", Project: projectCI, + Clients: []string{"laptop"}, Rule: config.Rule{Ask: config.AskWindow, Window: 15 * time.Minute}}} + if change != nil { + change(c) + } + } + e := newEnv(t, opts) + e.values["ci1"], e.values["ci2"] = madeUp("ci1"), madeUp("ci2") + e.main.set(func(f *fakeStore) { + f.items[idCI1], f.items[idCI2] = e.values["ci1"], e.values["ci2"] + f.projects[projectCI] = []store.ProjectSecret{{ID: idCI1, Key: "GITHUB_TOKEN", Project: projectCI}} + }) + return e +} + +// approveProject asks for name as GH_TOKEN, checks the text of the +// message, taps answer and returns the value. +func approveProject(t *testing.T, e *env, n int, name, label string, answer audit.Answer) string { + t.Helper() + p := e.start(e.httpClient("laptop"), api.PathSecrets, secretsRequest("GH_TOKEN", name)) + asked := e.waitAsked(n) + pr := asked[n-1].Prompt + if !slices.Equal(pr.Secrets, []string{label}) { + t.Fatalf("the message shows %q, want %q", pr.Secrets, label) + } + e.tap(pr.RequestID, answer) + return secretsOf(t, p.wait(t))["GH_TOKEN"] +} + +// TestProjectSecret: a client gets a secret of a project by SECTION/KEY. +// The message shows the name, the project rule and the end of the ID. +// The audit log has the ID. +func TestProjectSecret(t *testing.T) { + e := projectEnv(t, nil, envOptions{}) + if got := approveProject(t, e, 1, "ci/GITHUB_TOKEN", "ci/GITHUB_TOKEN (project rule, ID …0000a001)", + audit.AnswerApproveOnce); got != e.values["ci1"] { + t.Error("the answer does not hold the value of the project secret") + } + if !strings.Contains(e.approver.Asked()[0].Text, "ci/GITHUB_TOKEN (project rule, ID …0000a001)") { + t.Errorf("the message text is %q", e.approver.Asked()[0].Text) + } + req, ok := e.audit.all()[0].(audit.Request) + if !ok || !slices.Equal(req.Secrets, []string{"ci/GITHUB_TOKEN"}) || + !slices.Equal(req.ProjectSecrets, []audit.ProjectSecret{{Name: "ci/GITHUB_TOKEN", ID: idCI1}}) { + t.Errorf("the request entry is %+v", e.audit.all()[0]) + } + e.main.checkCleared(t) +} + +// TestProjectSecretRefused: a key that is not in the project, a name with +// two "/", a section that is not in the config, and a client that the +// project does not list are refused with no message and no value. +func TestProjectSecretRefused(t *testing.T) { + e := projectEnv(t, nil, envOptions{}) + for _, c := range []struct{ client, name string }{ + {"laptop", "ci/NOPE"}, + {"laptop", "ci/github_token"}, // the key is matched exactly + {"laptop", "ci/GITHUB_TOKEN/x"}, + {"laptop", "cd/GITHUB_TOKEN"}, + {"ci", "ci/GITHUB_TOKEN"}, + {"laptop", "ci/"}, + } { + res := e.post(t.Context(), e.httpClient(c.client), api.PathSecrets, secretsRequest("GH_TOKEN", c.name)) + mustStatus(t, res, http.StatusForbidden) + } + if n := len(e.approver.Asked()); n != 0 { + t.Errorf("%d messages, want none", n) + } + if e.main.fetched() != 0 { + t.Error("a value was fetched") + } + for _, ev := range e.audit.all() { + if s := subjectOf(ev); slices.Contains(s.Secrets, "ci/NOPE") || slices.Contains(s.Secrets, "ci/github_token") { + t.Errorf("the audit log has a key that is not in the project: %+v", ev) + } + } +} + +// TestProjectSecretLimits: SECTION/KEY of 129 bytes works; a part of 65 +// bytes is refused, and the client cannot send more than 129 bytes. +func TestProjectSecretLimits(t *testing.T) { + section, key := strings.Repeat("s", 64), strings.Repeat("K", 64) + e := projectEnv(t, func(c *config.Broker) { + p := c.Projects["ci"] + p.Name = section + c.Projects = map[string]config.Project{section: p} + }, envOptions{}) + e.main.set(func(f *fakeStore) { + f.projects[projectCI] = append(f.projects[projectCI], + store.ProjectSecret{ID: idCI2, Key: key, Project: projectCI}) + }) + name := section + "/" + key + if len(name) != api.MaxSecretName { + t.Fatalf("the name has %d bytes", len(name)) + } + if got := approveProject(t, e, 1, name, name+" (project rule, ID …0000a002)", audit.AnswerApproveOnce); got != e.values["ci2"] { + t.Error("the answer does not hold the value") + } + for _, bad := range []string{section + "s/" + key[1:], section[1:] + "/" + key + "K"} { + res := e.post(t.Context(), e.httpClient("laptop"), api.PathSecrets, secretsRequest("GH_TOKEN", bad)) + mustStatus(t, res, http.StatusForbidden) + } + res := e.post(t.Context(), e.httpClient("laptop"), api.PathSecrets, secretsRequest("GH_TOKEN", name+"K")) + if res.status == http.StatusOK { + t.Error("a name of 130 bytes passed") + } + if n := len(e.approver.Asked()); n != 1 { + t.Errorf("%d messages, want 1", n) + } +} + +// TestProjectDuplicateKey: two secrets with the same key in the project: +// the request is refused, with a denied entry that names SECTION/KEY and +// a line in the journal. No value is fetched. +func TestProjectDuplicateKey(t *testing.T) { + e := projectEnv(t, nil, envOptions{}) + e.main.set(func(f *fakeStore) { + f.projects[projectCI] = append(f.projects[projectCI], + store.ProjectSecret{ID: idCI2, Key: "GITHUB_TOKEN", Project: projectCI}) + }) + res := e.post(t.Context(), e.httpClient("laptop"), api.PathSecrets, secretsRequest("GH_TOKEN", "ci/GITHUB_TOKEN")) + mustStatus(t, res, http.StatusForbidden) + events := e.audit.all() + mustEvents(t, events, "request denied:not_allowed") + if s := subjectOf(events[1]); !slices.Equal(s.Secrets, []string{"ci/GITHUB_TOKEN"}) { + t.Errorf("the denied entry names %q", s.Secrets) + } + if !strings.Contains(e.logs.String(), "two or more secrets of the project have this key") || + !strings.Contains(e.logs.String(), "ci/GITHUB_TOKEN") { + t.Errorf("the journal is %q", e.logs.String()) + } + if e.main.fetched() != 0 || len(e.approver.Asked()) != 0 { + t.Error("a value was fetched, or a message sent") + } +} + +// TestProjectGetDiffers: the key or the project in the answer of "bws +// secret get" is not the one of the list: the request is denied after the +// tap, and no value goes out. +func TestProjectGetDiffers(t *testing.T) { + for _, now := range []store.ProjectSecret{ + {ID: idCI1, Key: "GITHUB_TOKEN_OLD", Project: projectCI}, + {ID: idCI1, Key: "GITHUB_TOKEN", Project: "0a1b2c3d-0000-4000-8000-0000000000c2"}, + } { + e := projectEnv(t, nil, envOptions{}) + e.main.set(func(f *fakeStore) { f.getAs[idCI1] = now }) + p := e.start(e.httpClient("laptop"), api.PathSecrets, secretsRequest("GH_TOKEN", "ci/GITHUB_TOKEN")) + e.tap(e.waitAsked(1)[0].Prompt.RequestID, audit.AnswerApproveOnce) + res := p.wait(t) + if res.status == http.StatusOK { + t.Fatalf("%+v: the value went out", now) + } + if strings.Contains(string(res.body), e.values["ci1"]) { + t.Fatal("the answer has the value") + } + if !slices.ContainsFunc(e.audit.all(), func(ev audit.Event) bool { return eventName(ev) == "secret_sent" }) { + continue + } + t.Errorf("%+v: a secret_sent entry", now) + } +} + +// TestProjectGrantRename: a 15 minute grant for ci/GITHUB_TOKEN is for the +// secret idCI1. When another secret gets the key GITHUB_TOKEN, a request +// for ci/GITHUB_TOKEN asks again, and the old secret's grant does not hand +// out the new one. +func TestProjectGrantRename(t *testing.T) { + e := projectEnv(t, nil, envOptions{}) + label := func(id string) string { return "ci/GITHUB_TOKEN (project rule, ID …" + id[len(id)-8:] + ")" } + if got := approveProject(t, e, 1, "ci/GITHUB_TOKEN", label(idCI1), audit.AnswerApproveWindow); got != e.values["ci1"] { + t.Fatal("no value") + } + // The grant covers the same secret. + got := secretsOf(t, e.post(t.Context(), e.httpClient("laptop"), api.PathSecrets, secretsRequest("GH_TOKEN", "ci/GITHUB_TOKEN"))) + if got["GH_TOKEN"] != e.values["ci1"] || len(e.approver.Asked()) != 1 { + t.Fatal("the grant did not cover the same secret") + } + // Someone renames idCI1 and gives its key to idCI2. + e.main.set(func(f *fakeStore) { + f.projects[projectCI] = []store.ProjectSecret{ + {ID: idCI1, Key: "OLD_TOKEN", Project: projectCI}, + {ID: idCI2, Key: "GITHUB_TOKEN", Project: projectCI}, + } + }) + if got := approveProject(t, e, 2, "ci/GITHUB_TOKEN", label(idCI2), audit.AnswerApproveOnce); got != e.values["ci2"] { + t.Error("no value after the second tap") + } + // The grant still covers idCI1 under its new key. + got = secretsOf(t, e.post(t.Context(), e.httpClient("laptop"), api.PathSecrets, secretsRequest("GH_TOKEN", "ci/OLD_TOKEN"))) + if got["GH_TOKEN"] != e.values["ci1"] || len(e.approver.Asked()) != 2 { + t.Error("the grant does not cover its secret under a new key") + } +} + +// TestProjectEntryWins: a [secret.*] entry with the ID of a project +// secret wins completely, also from another bws store. When it is +// stricter, a client of the project is refused; when it is looser, its +// window applies. Two entries with the ID deny. +func TestProjectEntryWins(t *testing.T) { + entry := func(name, st string, clients []string, rule config.Rule) func(*config.Broker) { + return func(c *config.Broker) { + c.Secrets[name] = config.Secret{Name: name, Store: st, ID: idCI1, Clients: clients, Rule: rule} + } + } + window := config.Rule{Ask: config.AskWindow, Window: 30 * time.Minute} + t.Run("stricter", func(t *testing.T) { + e := projectEnv(t, entry("ci-one", "main", []string{"ci"}, window), envOptions{}) + res := e.post(t.Context(), e.httpClient("laptop"), api.PathSecrets, secretsRequest("GH_TOKEN", "ci/GITHUB_TOKEN")) + mustStatus(t, res, http.StatusForbidden) + if len(e.approver.Asked()) != 0 || e.main.fetched() != 0 { + t.Error("a message or a value") + } + }) + t.Run("looser", func(t *testing.T) { + e := projectEnv(t, func(c *config.Broker) { + p := c.Projects["ci"] + p.Rule = config.Rule{Ask: config.AskAlways} + c.Projects["ci"] = p + entry("ci-one", "main", []string{"laptop"}, window)(c) + }, envOptions{}) + approveProject(t, e, 1, "ci/GITHUB_TOKEN", "ci/GITHUB_TOKEN (rule of secret ci-one, ID …0000a001)", + audit.AnswerApproveWindow) + // The entry's grant covers both names. + for _, name := range []string{"ci/GITHUB_TOKEN", "ci-one"} { + got := secretsOf(t, e.post(t.Context(), e.httpClient("laptop"), api.PathSecrets, secretsRequest("GH_TOKEN", name))) + if got["GH_TOKEN"] != e.values["ci1"] { + t.Errorf("%s: no value", name) + } + } + if len(e.approver.Asked()) != 1 { + t.Error("the entry's window did not apply") + } + }) + t.Run("another bws store", func(t *testing.T) { + e := projectEnv(t, func(c *config.Broker) { + c.Stores["main2"] = config.Store{Name: "main2", Type: config.StoreBWS} + entry("ci-one", "main2", []string{"ci"}, window)(c) + }, envOptions{stores: map[string]store.Store{"main2": newFakeStore()}}) + res := e.post(t.Context(), e.httpClient("laptop"), api.PathSecrets, secretsRequest("GH_TOKEN", "ci/GITHUB_TOKEN")) + mustStatus(t, res, http.StatusForbidden) + }) + t.Run("two entries", func(t *testing.T) { + e := projectEnv(t, func(c *config.Broker) { + entry("ci-one", "main", []string{"laptop"}, window)(c) + entry("ci-two", "main", []string{"laptop"}, window)(c) + }, envOptions{}) + res := e.post(t.Context(), e.httpClient("laptop"), api.PathSecrets, secretsRequest("GH_TOKEN", "ci/GITHUB_TOKEN")) + mustStatus(t, res, http.StatusForbidden) + if len(e.approver.Asked()) != 0 { + t.Error("a message for a secret with two entries") + } + }) +} + +// TestProjectReloadAddsEntry: after a reload that adds a [secret.*] entry +// for a project secret, the old project grant no longer works: the next +// request asks again. +func TestProjectReloadAddsEntry(t *testing.T) { + e := projectEnv(t, nil, envOptions{}) + approveProject(t, e, 1, "ci/GITHUB_TOKEN", "ci/GITHUB_TOKEN (project rule, ID …0000a001)", audit.AnswerApproveWindow) + next := *e.svc.view().cfg + next.Secrets = maps.Clone(next.Secrets) + next.Secrets["ci-one"] = config.Secret{Name: "ci-one", Store: "main", ID: idCI1, Clients: []string{"laptop"}, + Rule: config.Rule{Ask: config.AskWindow, Window: 15 * time.Minute}} + ended, err := e.svc.Reload(&next, e.svc.view().keys, strings.Repeat("a", 64)) + if err != nil || ended != 1 { + t.Fatalf("Reload = %d, %v; want 1 grant ended", ended, err) + } + p := e.start(e.httpClient("laptop"), api.PathSecrets, secretsRequest("GH_TOKEN", "ci/GITHUB_TOKEN")) + pr := e.waitAsked(2)[1].Prompt + if !slices.Equal(pr.Secrets, []string{"ci/GITHUB_TOKEN (rule of secret ci-one, ID …0000a001)"}) { + t.Errorf("the message shows %q", pr.Secrets) + } + e.tap(pr.RequestID, audit.AnswerDeny) + mustStatus(t, p.wait(t), http.StatusForbidden) +} + +// TestProjectReloadKeepsGrant: a reload that keeps the project's rule +// keeps its grants. +func TestProjectReloadKeepsGrant(t *testing.T) { + e := projectEnv(t, nil, envOptions{}) + approveProject(t, e, 1, "ci/GITHUB_TOKEN", "ci/GITHUB_TOKEN (project rule, ID …0000a001)", audit.AnswerApproveWindow) + next := *e.svc.view().cfg + if ended, err := e.svc.Reload(&next, e.svc.view().keys, strings.Repeat("a", 64)); err != nil || ended != 0 { + t.Fatalf("Reload = %d, %v", ended, err) + } + got := secretsOf(t, e.post(t.Context(), e.httpClient("laptop"), api.PathSecrets, secretsRequest("GH_TOKEN", "ci/GITHUB_TOKEN"))) + if got["GH_TOKEN"] != e.values["ci1"] || len(e.approver.Asked()) != 1 { + t.Error("the grant did not stay") + } +} + +// TestProjectStoreFails: a failed "bws secret list" denies the request +// with 503 and an error entry; no message is sent. +func TestProjectStoreFails(t *testing.T) { + e := projectEnv(t, nil, envOptions{}) + e.main.set(func(f *fakeStore) { f.err = store.ErrFailed }) + res := e.post(t.Context(), e.httpClient("laptop"), api.PathSecrets, secretsRequest("GH_TOKEN", "ci/GITHUB_TOKEN")) + mustStatus(t, res, http.StatusServiceUnavailable) + if len(e.approver.Asked()) != 0 { + t.Error("a message was sent") + } +} + +// TestProjectMadeUpKeysNotLogged: a key that the client made up never +// reaches the audit log, on any deny path: two unknown keys, a duplicate +// key before an unknown one, a variable on the deny list, and a bad +// preapprove request. +func TestProjectMadeUpKeysNotLogged(t *testing.T) { + e := projectEnv(t, nil, envOptions{}) + e.main.set(func(f *fakeStore) { + f.projects[projectCI] = append(f.projects[projectCI], + store.ProjectSecret{ID: idCI1, Key: "DUP", Project: projectCI}, + store.ProjectSecret{ID: idCI2, Key: "DUP", Project: projectCI}) + }) + c := e.httpClient("laptop") + two := func(a, b string) api.SecretsRequest { + r := secretsRequest("A", a) + r.Secrets = append(r.Secrets, api.SecretRef{Var: "B", Name: b}) + return r + } + mustStatus(t, e.post(t.Context(), c, api.PathSecrets, two("ci/MADEUP1", "ci/MADEUP2")), http.StatusForbidden) + mustStatus(t, e.post(t.Context(), c, api.PathSecrets, two("ci/DUP", "ci/MADEUP3")), http.StatusForbidden) + mustStatus(t, e.post(t.Context(), c, api.PathSecrets, secretsRequest("PATH", "ci/MADEUP4")), http.StatusForbidden) + pre := api.PreapproveRequest{Secrets: []string{"ci/MADEUP5"}, Uses: 1, From: "bad", Until: "bad"} + mustStatus(t, e.post(t.Context(), c, api.PathPreapprove, pre), http.StatusForbidden) + text := encodeEvents(t, e.audit.all()) + e.logs.String() + if strings.Contains(text, "MADEUP") { + t.Errorf("a made-up key is in the audit log or the journal: %s", text) + } + if !strings.Contains(encodeEvents(t, e.audit.all()), "ci/DUP") { + t.Error("the duplicate key is not in the audit log") + } +} + +// TestProjectSameSecretTwice: an entry and SECTION/KEY with its ID name +// the same secret; one request with both is denied. +func TestProjectSameSecretTwice(t *testing.T) { + e := projectEnv(t, func(c *config.Broker) { + c.Secrets["ci-one"] = config.Secret{Name: "ci-one", Store: "main", ID: idCI1, Clients: []string{"laptop"}, + Rule: config.Rule{Ask: config.AskWindow, Window: 15 * time.Minute}} + }, envOptions{}) + r := secretsRequest("A", "ci-one") + r.Secrets = append(r.Secrets, api.SecretRef{Var: "B", Name: "ci/GITHUB_TOKEN"}) + mustStatus(t, e.post(t.Context(), e.httpClient("laptop"), api.PathSecrets, r), http.StatusForbidden) + if len(e.approver.Asked()) != 0 || e.main.fetched() != 0 { + t.Error("a message or a value") + } +} + +// TestProjectPreapprove: a scheduled grant of a project secret is for its +// ID, and a request for SECTION/KEY uses it with no tap. +func TestProjectPreapprove(t *testing.T) { + e := projectEnv(t, func(c *config.Broker) { + p := c.Projects["ci"] + p.Rule.Preapprove = true + c.Projects["ci"] = p + c.Preapprove = config.PreapproveLimits{MaxAhead: 24 * time.Hour, MaxOpen: 24 * time.Hour, MaxUses: 5, MaxGrants: 5} + }, envOptions{}) + c := e.httpClient("laptop") + from, until := noon.Add(-time.Minute).Format(time.RFC3339), noon.Add(time.Hour).Format(time.RFC3339) + p := e.start(c, api.PathPreapprove, api.PreapproveRequest{Secrets: []string{"ci/GITHUB_TOKEN"}, Uses: 1, From: from, Until: until}) + pr := e.waitAsked(1)[0].Prompt + if !slices.Equal(pr.Secrets, []string{"ci/GITHUB_TOKEN (project rule, ID …0000a001)"}) { + t.Fatalf("the message shows %q", pr.Secrets) + } + e.tap(pr.RequestID, audit.AnswerApproveOnce) + mustStatus(t, p.wait(t), http.StatusOK) + got := secretsOf(t, e.post(t.Context(), c, api.PathSecrets, secretsRequest("GH_TOKEN", "ci/GITHUB_TOKEN"))) + if got["GH_TOKEN"] != e.values["ci1"] || len(e.approver.Asked()) != 1 { + t.Error("the scheduled grant was not used") + } +} diff --git a/internal/broker/reload.go b/internal/broker/reload.go index 2312d92..f6eca3f 100644 --- a/internal/broker/reload.go +++ b/internal/broker/reload.go @@ -103,7 +103,11 @@ func (rq *request) stillAllowed(decided *config.Broker, secrets []string, key st } ok = from.SameClient(cur.cfg, rq.client) for _, name := range secrets { - ok = ok && from.SameSecret(cur.cfg, name, rq.client) + if ps, isProject := rq.projects[name]; isProject { + ok = ok && from.SameProjectSecret(cur.cfg, ps.Section, ps.ID, rq.client) + } else { + ok = ok && from.SameSecret(cur.cfg, name, rq.client) + } } if key != "" { ok = ok && from.SameKey(cur.cfg, key, rq.client) && cur.cfg.KeyHost(key, hostKey) diff --git a/internal/broker/secrets.go b/internal/broker/secrets.go index 0dfbe08..c78bbc5 100644 --- a/internal/broker/secrets.go +++ b/internal/broker/secrets.go @@ -5,12 +5,12 @@ import ( "errors" "log/slog" "net/http" - "slices" "github.com/code6x/nodkey/internal/api" "github.com/code6x/nodkey/internal/approval" "github.com/code6x/nodkey/internal/audit" "github.com/code6x/nodkey/internal/claim" + "github.com/code6x/nodkey/internal/config" "github.com/code6x/nodkey/internal/grantkey" "github.com/code6x/nodkey/internal/store" "github.com/code6x/nodkey/internal/transport" @@ -33,8 +33,8 @@ func (rq *request) secrets(ctx context.Context, r *http.Request) { Reason: freeText(req.Reason, maxReasonRunes), } rq.pre.Command, rq.pre.Reason = cl.Command, cl.Reason - rq.releaseBody() if !ok { + rq.releaseBody() // No message for a name the client may not use, or for a // variable on the deny list (DESIGN.md section 5). The audit log // gets only the names that passed. @@ -42,6 +42,13 @@ func (rq *request) secrets(ctx context.Context, r *http.Request) { rq.logDeniedVar(req.Secrets) return } + // The place of the body is held while the stores list the projects, + // so one client cannot keep the stores busy with many requests. + resolved := rq.resolveProjects(ctx, names) + rq.releaseBody() + if !resolved { + return + } var res *approval.Result var rc receipt if tokens := r.Header.Values(api.GrantTokenHeader); len(tokens) > 0 { @@ -49,7 +56,7 @@ func (rq *request) secrets(ctx context.Context, r *http.Request) { res, rc, ok = rq.decideStanding(ctx, names, cl, tokens) } else { res, rc, ok = rq.decide(ctx, &approval.Request{ - Client: rq.client, Kind: audit.KindSecrets, Secrets: names, Claim: cl, + Client: rq.client, Kind: audit.KindSecrets, Secrets: names, Projects: rq.projects, Claim: cl, }) } if !ok { @@ -127,15 +134,17 @@ func (s *Service) checkSecrets(client string, req *api.SecretsRequest) (names, p ok = false } vars[ref.Var] = true - sec, known := s.view().cfg.Secrets[ref.Name] - if !known || !slices.Contains(sec.Clients, client) { + if !secretAllowed(s.view().cfg, client, ref.Name) { ok = false continue } - names = append(names, sec.Name) - if !seen[sec.Name] { - seen[sec.Name] = true - passed = append(passed, sec.Name) + names = append(names, ref.Name) + if _, isProject, _ := config.ParseSecretName(ref.Name); isProject { + continue // the audit log gets it once it is found (resolveProjects) + } + if !seen[ref.Name] { + seen[ref.Name] = true + passed = append(passed, ref.Name) } } return names, passed, ok @@ -190,17 +199,9 @@ func (rq *request) fetch(ctx context.Context, rc receipt, names []string) (map[s rq.cancelled() return nil, false } - sec := rq.v.cfg.Secrets[name] + storeName, fetch := rq.fetcher(name) fctx, cancel := context.WithTimeout(ctx, fetchTimeout) - ref := sec.ID - switch { - case sec.Field != "": - ref += "/" + sec.Field // a 1password store: the item and its field - case sec.SecretName != "": - // An infisical store: the project, environment, path and name. - ref = store.InfisicalRef(sec.Project, sec.Environment, sec.Path, sec.SecretName) - } - v, err := rq.s.stores[sec.Store].Fetch(fctx, ref) + v, err := fetch(fctx) cancel() if err != nil { v.Clear() @@ -211,10 +212,10 @@ func (rq *request) fetch(ctx context.Context, rc receipt, names []string) (map[s // Store errors are fixed texts: the store, the step, a reason // and an exit code, never the program's output. rq.s.log.Warn("broker: a store failed; the request is denied", - slog.String("request_id", rq.sub.RequestID), slog.String("store", sec.Store), + slog.String("request_id", rq.sub.RequestID), slog.String("store", storeName), slog.String("error", errorText(err))) if errors.Is(err, store.ErrRateLimited) { - rq.s.rateLimited(sec.Store) + rq.s.rateLimited(storeName) } rq.fail(audit.CodeStoreFailed) return nil, false @@ -225,6 +226,35 @@ func (rq *request) fetch(ctx context.Context, rc receipt, names []string) (map[s return values, true } +// fetcher returns the store of the secret name and the function that +// gets its value. A project secret is got by its ID from the store of its +// project, and the answer must have the key and the project of the list +// (DESIGN.md section 14, "Projects"). A name that the request did not +// resolve gets a function that fails. +func (rq *request) fetcher(name string) (string, func(context.Context) (store.Value, error)) { + if pn, isProject, _ := config.ParseSecretName(name); isProject { + p := rq.v.cfg.Projects[pn.Section] + ps, isPS := rq.s.stores[p.Store].(store.ProjectStore) + listed, found := rq.listed[name] + return p.Store, func(ctx context.Context) (store.Value, error) { + if !isPS || !found { + return store.Value{}, store.ErrBadID + } + return ps.FetchListed(ctx, listed) + } + } + sec := rq.v.cfg.Secrets[name] + ref := sec.ID + switch { + case sec.Field != "": + ref += "/" + sec.Field // a 1password store: the item and its field + case sec.SecretName != "": + // An infisical store: the project, environment, path and name. + ref = store.InfisicalRef(sec.Project, sec.Environment, sec.Path, sec.SecretName) + } + return sec.Store, func(ctx context.Context) (store.Value, error) { return rq.s.stores[sec.Store].Fetch(ctx, ref) } +} + // decideStanding decides a request that has the header of a standing grant // token (DESIGN.md section 8). The core never asks the person. It writes // the entries, sends the alarm of a denied request, and sends the silent @@ -240,7 +270,9 @@ func (rq *request) decideStanding(ctx context.Context, names []string, cl approv } } rq.failCode = audit.CodeApproverFailed - res := rq.core.UseStanding(approval.StandingRequest{Client: rq.client, Secrets: names, Claim: cl, Token: hash}) + res := rq.core.UseStanding(approval.StandingRequest{ + Client: rq.client, Secrets: names, Projects: rq.projects, Claim: cl, Token: hash, + }) res, rc, ok := rq.settle(res) if !ok { if res.Allowed { diff --git a/internal/client/preapprove.go b/internal/client/preapprove.go index 60bbeab..39f727c 100644 --- a/internal/client/preapprove.go +++ b/internal/client/preapprove.go @@ -16,6 +16,7 @@ import ( "time" "github.com/code6x/nodkey/internal/api" + "github.com/code6x/nodkey/internal/config" "github.com/code6x/nodkey/internal/grantkey" "github.com/code6x/nodkey/internal/loginmatch" "github.com/code6x/nodkey/internal/sshauth" @@ -486,10 +487,18 @@ func checkPreapproveGiven(o *preapproveOptions, given map[string]bool) error { // addSecret adds the value of one --secret to o. seen holds the secrets // that o has already. +// validSecretName reports whether name is a secret name: a name, or +// PROJECT/KEY of a project (DESIGN.md section 14, "Projects"). +func validSecretName(name string) bool { + _, _, ok := config.ParseSecretName(name) + return ok +} + func addSecret(o *preapproveOptions, seen map[string]bool, name string) error { switch { - case !grantNamePattern.MatchString(name): - return fmt.Errorf("--secret: %.64q is not a secret name: it is %s", name, grantNameRule) + case !validSecretName(name): + return fmt.Errorf("--secret: %.140q is not a secret name: it is %s, or PROJECT/KEY with each part so", + name, grantNameRule) case seen[name]: return fmt.Errorf("--secret: the secret %s is given twice", name) case len(o.secrets) == api.MaxSecrets: diff --git a/internal/client/request_test.go b/internal/client/request_test.go index 9647667..7ee56d8 100644 --- a/internal/client/request_test.go +++ b/internal/client/request_test.go @@ -38,9 +38,14 @@ func TestRunUsage(t *testing.T) { {"a dash", []string{"--secret", "gh-token=github-token", "--", "gh"}, `the variable name "gh-token" must match`}, {"digit first", []string{"--secret", "1TOKEN=github-token", "--", "gh"}, `the variable name "1TOKEN" must match`}, {"variable too long", []string{"--secret", strings.Repeat("A", 65) + "=x", "--", "gh"}, "must match"}, - {"no secret name", []string{"--secret", "GH_TOKEN=", "--", "gh"}, "the secret name must have 1 to 64 bytes"}, + {"no secret name", []string{"--secret", "GH_TOKEN=", "--", "gh"}, "the secret name must be NAME or PROJECT/KEY"}, {"secret name too long", []string{"--secret", "GH_TOKEN=" + strings.Repeat("n", 65), "--", "gh"}, - "the secret name must have 1 to 64 bytes"}, + "the secret name must be NAME or PROJECT/KEY"}, + {"project secret name too long", []string{"--secret", "GH_TOKEN=" + strings.Repeat("n", 64) + "/" + strings.Repeat("k", 65), "--", "gh"}, + "the secret name must be NAME or PROJECT/KEY"}, + {"project section too long", []string{"--secret", "GH_TOKEN=" + strings.Repeat("n", 65) + "/" + strings.Repeat("k", 63), "--", "gh"}, + "the secret name must be NAME or PROJECT/KEY"}, + {"two slashes", []string{"--secret", "GH_TOKEN=a/b/c", "--", "gh"}, "the secret name must be NAME or PROJECT/KEY"}, {"a variable twice", []string{"--secret", "GH_TOKEN=a", "--secret", "GH_TOKEN=b", "--", "gh"}, "the variable GH_TOKEN is given twice"}, {"too many", append(thirtyThree, "--", "gh"), "at most 32 --secret options"}, @@ -137,11 +142,11 @@ func TestRunDeniedVars(t *testing.T) { // them. Everything after "--" is the command, also its own options. func TestParseArgs(t *testing.T) { t.Parallel() - o, err := parseArgs([]string{"-secret", "A=a", "--secret=B=b=c", "-reason=why", "--", "gh", "--secret", "--", "x"}) + o, err := parseArgs([]string{"-secret", "A=a", "--secret=B=ci/b", "-reason=why", "--", "gh", "--secret", "--", "x"}) if err != nil { t.Fatal(err) } - want := []api.SecretRef{{Var: "A", Name: "a"}, {Var: "B", Name: "b=c"}} + want := []api.SecretRef{{Var: "A", Name: "a"}, {Var: "B", Name: "ci/b"}} if !slices.Equal(o.secrets, want) || o.reason != "why" || !o.reasonSet || !slices.Equal(o.command, []string{"gh", "--secret", "--", "x"}) { t.Errorf("parseArgs = %+v", o) diff --git a/internal/client/run.go b/internal/client/run.go index f12d8da..b91ba63 100644 --- a/internal/client/run.go +++ b/internal/client/run.go @@ -48,7 +48,8 @@ environment variable VAR. nodkey never prints a value. [A-Za-z_][A-Za-z0-9_]{0,63}, and must not be a name whose value programs show or run, such as PATH, LD_PRELOAD or GIT_SSH_COMMAND, in any case. NAME is - a secret of the broker. + a secret of the broker, or PROJECT/KEY for the + secret KEY of a project of the broker. --grant-token FILE the token file of a standing grant (nodkey preapprove). With it, no tap is needed inside a window of the grant. nodkey replaces the token in FILE with a new one before @@ -517,9 +518,9 @@ func parseSecret(value string) (api.SecretRef, error) { case denied: return api.SecretRef{}, fmt.Errorf("--secret %s=...: nodkey refuses the variable %s, "+ "because programs show or run its value", v, v) - case name == "" || len(name) > api.MaxName: - return api.SecretRef{}, fmt.Errorf("--secret %s=...: the secret name must have 1 to %d bytes", - v, api.MaxName) + case len(name) > api.MaxSecretName || !validSecretName(name): + return api.SecretRef{}, fmt.Errorf("--secret %s=...: the secret name must be NAME or PROJECT/KEY, "+ + "each part %s", v, grantNameRule) } return api.SecretRef{Var: v, Name: name}, nil } diff --git a/internal/config/broker.go b/internal/config/broker.go index d12821a..af4afd5 100644 --- a/internal/config/broker.go +++ b/internal/config/broker.go @@ -69,6 +69,9 @@ type Broker struct { Secrets map[string]Secret // by name Keys map[string]Key // by name Logins map[string]Logins // by name: the [logins.*] sections (DESIGN.md section 21) + // Projects are the [project.*] sections, by name (DESIGN.md section + // 14, "Projects"). + Projects map[string]Project } // Telegram is the [telegram] section. @@ -277,20 +280,21 @@ type Key struct { // in the file. type ( brokerFile struct { - Listen string `toml:"listen"` - TLSKey string `toml:"tls_key"` - AuditLog string `toml:"audit_log"` - KnownHosts string `toml:"known_hosts"` - Telegram *telegramSection `toml:"telegram"` - Ntfy *ntfySection `toml:"ntfy"` - Updates updatesSection `toml:"updates"` - Preapprove preapproveSection `toml:"preapprove"` - Standing *standingSection `toml:"standing"` - Client map[string]clientSection `toml:"client"` - Store map[string]storeSection `toml:"store"` - Secret map[string]secretSection `toml:"secret"` - Key map[string]keySection `toml:"key"` - Logins map[string]loginsSection `toml:"logins"` + Listen string `toml:"listen"` + TLSKey string `toml:"tls_key"` + AuditLog string `toml:"audit_log"` + KnownHosts string `toml:"known_hosts"` + Telegram *telegramSection `toml:"telegram"` + Ntfy *ntfySection `toml:"ntfy"` + Updates updatesSection `toml:"updates"` + Preapprove preapproveSection `toml:"preapprove"` + Standing *standingSection `toml:"standing"` + Client map[string]clientSection `toml:"client"` + Store map[string]storeSection `toml:"store"` + Secret map[string]secretSection `toml:"secret"` + Key map[string]keySection `toml:"key"` + Logins map[string]loginsSection `toml:"logins"` + Project map[string]projectSection `toml:"project"` } telegramSection struct { TokenFile string `toml:"token_file"` @@ -421,7 +425,7 @@ func loadBroker(path, filesDir string) (*Broker, error) { return nil, fmt.Errorf("config %s: %w", path, err) } var f brokerFile - if err := decode(data, &f, "client", "store", "secret", "key", "logins"); err != nil { + if err := decode(data, &f, "client", "store", "secret", "key", "logins", "project"); err != nil { return nil, fmt.Errorf("config %s: %w", path, err) } b, err := newBroker(&f, filesDir) @@ -436,11 +440,12 @@ func loadBroker(path, filesDir string) (*Broker, error) { func newBroker(f *brokerFile, filesDir string) (*Broker, error) { b := &Broker{ - Clients: make(map[string]Client), - Stores: make(map[string]Store), - Secrets: make(map[string]Secret), - Logins: make(map[string]Logins), - Keys: make(map[string]Key), + Clients: make(map[string]Client), + Stores: make(map[string]Store), + Secrets: make(map[string]Secret), + Logins: make(map[string]Logins), + Projects: make(map[string]Project), + Keys: make(map[string]Key), } var err error if b.Listen, err = parseListen(f.Listen); err != nil { @@ -498,6 +503,9 @@ func newBroker(f *brokerFile, filesDir string) (*Broker, error) { if err := b.addLogins(f.Logins); err != nil { return nil, err } + if err := b.addProjects(f.Project); err != nil { + return nil, err + } knownHosts := b.KnownHostsFile if filesDir != "" { knownHosts = filepath.Join(filesDir, filepath.Base(knownHosts)) diff --git a/internal/config/diff.go b/internal/config/diff.go index ca5fe84..e95f416 100644 --- a/internal/config/diff.go +++ b/internal/config/diff.go @@ -43,7 +43,7 @@ func (c Change) String() string { } // groups is the order of the groups in the output. -var groups = []string{"broker", "client", "store", "secret", "logins", "key", "known_hosts"} +var groups = []string{"broker", "client", "store", "secret", "logins", "project", "key", "known_hosts"} // Diff returns the changes from old to next, by group and then by name, // in a fixed order. No change means the two configs mean the same. @@ -54,6 +54,7 @@ func Diff(old, next *Broker) []Change { d.stores(old, next) d.secrets(old, next) d.logins(old, next) + d.projects(old, next) d.keys(old, next) d.knownHosts(old.KnownHosts, next.KnownHosts) d.unknown(old, next) @@ -276,6 +277,39 @@ func (d *differ) secrets(old, next *Broker) { } } +// projects compares the [project.*] sections. A [secret.*] entry with the +// ID of a project secret wins over the project's rule; secrets lists +// those changes. +func (d *differ) projects(old, next *Broker) { + for _, name := range sortedUnion(old.Projects, next.Projects) { + o, inOld := old.Projects[name] + n, inNew := next.Projects[name] + what := "project " + q(name) + switch { + case !inOld: + d.add("project", name, true, "%s: new: every secret of project %s in store %s, %s", + what, q(n.Project), q(n.Store), ruleText(n.Rule)) + for _, c := range n.Clients { + d.add("project", name, true, "%s: client %s may now ask for its secrets", what, q(c)) + } + continue + case !inNew: + d.add("project", name, false, "%s: removed", what) + continue + } + for _, p := range []struct{ key, old, new string }{ + {"store", o.Store, n.Store}, + {"project", o.Project, n.Project}, + } { + if p.old != p.new { + d.add("project", name, true, "%s: now other secrets: %s changed from %s to %s", what, p.key, q(p.old), q(p.new)) + } + } + d.clientList("project", name, what, "ask for its secrets", o.Clients, n.Clients) + d.rule("project", name, what, o.Rule, n.Rule) + } +} + // logins compares the [logins.*] sections. A new or removed rule of an // item is marked as widening: the item's rule replaces the collection's, // and either one can be the looser (DESIGN.md section 21.2). @@ -573,6 +607,7 @@ func (d *differ) unknown(old, next *Broker) { x.Secrets, y.Secrets = common(old.Secrets, next.Secrets) x.Keys, y.Keys = common(old.Keys, next.Keys) x.Logins, y.Logins = common(old.Logins, next.Logins) + x.Projects, y.Projects = common(old.Projects, next.Projects) for name, l := range x.Logins { // Item rules that only one side has are compared in logins. m := y.Logins[name] @@ -608,6 +643,7 @@ var compared = map[reflect.Type][]string{ reflect.TypeFor[Key](): {"Name", "File", "Clients", "Hosts"}, reflect.TypeFor[Logins](): {"Name", "Store", "Collection", "Clients"}, reflect.TypeFor[LoginItemRule](): {"ID", "Clients"}, + reflect.TypeFor[Project](): {"Name", "Store", "Project", "Clients"}, reflect.TypeFor[Rule](): {"Ask", "Window", "Preapprove", "Standing", "Notify", "MaxPerDay", "Hours"}, } diff --git a/internal/config/project.go b/internal/config/project.go new file mode 100644 index 0000000..06fba32 --- /dev/null +++ b/internal/config/project.go @@ -0,0 +1,170 @@ +package config + +import ( + "fmt" + "maps" + "reflect" + "slices" + "strings" +) + +// Project is a [project.NAME] section: every secret of one Bitwarden +// Secrets Manager project, with one rule (DESIGN.md section 14, +// "Projects"). A client asks for a secret of it as NAME/KEY. +type Project struct { + Name string + Store string // a name in Broker.Stores, of type bws + Project string // the project's ID, a UUID in lower case + Clients []string // the names in Broker.Clients that may ask for its secrets + Rule Rule // never "never" +} + +type projectSection struct { + Store string `toml:"store"` + Project string `toml:"project"` + Clients []string `toml:"clients"` + ruleKeys +} + +// ProjectName is a secret name of the form SECTION/KEY, split. +type ProjectName struct { + Section string // the name of the [project.*] section + Key string // the secret's key in Bitwarden +} + +// String returns SECTION/KEY. +func (n ProjectName) String() string { return n.Section + "/" + n.Key } + +// ParseSecretName reads a secret name that a client asks for. A name +// with no "/" is the name of a [secret.*] section: project is false. A +// name with one "/" is SECTION/KEY of a [project.*] section: each part +// must follow the name rule (namePattern), so each is at most 64 bytes. +// A name with two or more "/" is not valid (ok is false), and so is a +// name that breaks the name rule. +func ParseSecretName(name string) (n ProjectName, project, ok bool) { + section, key, found := strings.Cut(name, "/") + if !found { + return ProjectName{}, false, namePattern.MatchString(name) + } + if !namePattern.MatchString(section) || !namePattern.MatchString(key) { + return ProjectName{}, true, false + } + return ProjectName{Section: section, Key: key}, true, true +} + +// MaxNameLen is the most bytes of a name that follows the name rule. +const MaxNameLen = 64 + +// ValidKey reports whether key, the key of a secret in Bitwarden, follows +// the name rule. Only such a key can be asked for, and only such a key is +// shown (DESIGN.md section 14, "Projects"). +func ValidKey(key string) bool { return namePattern.MatchString(key) } + +// addProjects checks the [project.*] sections and adds them to b.Projects. +func (b *Broker) addProjects(m map[string]projectSection) error { + projects := make(map[string]string) // project ID -> section name + for _, name := range slices.Sorted(maps.Keys(m)) { + if err := checkName("project", name); err != nil { + return err + } + sec := sectionName("project", name) + s := m[name] + if s.Store == "" { + return fmt.Errorf(`%s: is missing; name a [store.*] section of type = "bws"`, place(sec, "store")) + } + st, ok := b.Stores[s.Store] + if !ok { + return fmt.Errorf("%s: no [store.*] section has this name", place(sec, "store")) + } + if st.Type != StoreBWS { + return fmt.Errorf(`%s: must name a store of type = "bws"; a project is a Bitwarden Secrets Manager `+ + `project (DESIGN.md section 14, "Projects")`, place(sec, "store")) + } + if !uuidPattern.MatchString(s.Project) { + return fmt.Errorf(`%s: must be the ID of the project, a UUID such as "00000000-0000-0000-0000-000000000000"; `+ + `"bws project list" shows it`, place(sec, "project")) + } + id := strings.ToLower(s.Project) + if other, ok := projects[id]; ok { + return fmt.Errorf("%s: %s has the same project; one project may be in one [project.*] section only, "+ + "else its secrets would have two rules", place(sec, "project"), sectionName("project", other)) + } + projects[id] = name + clients, err := b.clientList(sec, s.Clients) + if err != nil { + return err + } + if s.Ask == string(AskNever) { + return fmt.Errorf(`%s: "never" is not allowed for a project: it would give every secret of the project `+ + `without a tap, also secrets added later; use "always" or "window", or a [secret.*] section for one secret`, + place(sec, "ask")) + } + rule, err := parseRule(sec, s.ruleKeys) + if err != nil { + return err + } + b.Projects[name] = Project{Name: name, Store: s.Store, Project: id, Clients: clients, Rule: rule} + } + return nil +} + +// ProjectRule is the rule that applies to one secret of a project. +type ProjectRule struct { + // Entry is the name of the [secret.*] section with the same secret ID, + // in a store of type bws; "" when the project's rule applies. The + // entry's rule wins completely (DESIGN.md section 14, "Projects"). + Entry string + Clients []string + Rule Rule +} + +// RuleFor returns the rule for the secret id of the project section: +// the rule of the one [secret.*] entry with that ID in any store of type +// bws, or else the project's rule. Nothing is merged. ok is false, and the +// request must be denied, when the section is not in the config, when id +// is not a UUID, or when two or more entries have that ID: there is no +// clear winner. +func (b *Broker) RuleFor(section, id string) (r ProjectRule, ok bool) { + p, found := b.Projects[section] + if !found || !uuidPattern.MatchString(id) { + return ProjectRule{}, false + } + var entries []string + for _, name := range slices.Sorted(maps.Keys(b.Secrets)) { + s := b.Secrets[name] + if b.Stores[s.Store].Type == StoreBWS && strings.EqualFold(s.ID, id) { + entries = append(entries, name) + } + } + switch len(entries) { + case 0: + return ProjectRule{Clients: p.Clients, Rule: p.Rule}, true + case 1: + s := b.Secrets[entries[0]] + return ProjectRule{Entry: s.Name, Clients: s.Clients, Rule: s.Rule}, true + } + return ProjectRule{}, false +} + +// SameProjectSecret reports whether client may still get the secret id +// through the project section in next as it could in b: the client is +// the same, the section has the same store and project, and the same +// rule applies to the secret, with client in its clients. A rule that +// moves from the project to a [secret.*] entry, or back, is not the same. +func (b *Broker) SameProjectSecret(next *Broker, section, id, client string) bool { + op, ok1 := b.Projects[section] + np, ok2 := next.Projects[section] + if !ok1 || !ok2 || !b.SameClient(next, client) || op.Store != np.Store || op.Project != np.Project { + return false + } + or, ok1 := b.RuleFor(section, id) + nr, ok2 := next.RuleFor(section, id) + if !ok1 || !ok2 || or.Entry != nr.Entry || !slices.Contains(or.Clients, client) || + !slices.Contains(nr.Clients, client) { + return false + } + if or.Entry != "" { + return b.SameSecret(next, or.Entry, client) + } + return reflect.DeepEqual(or.Rule, nr.Rule) +} diff --git a/internal/config/project_test.go b/internal/config/project_test.go new file mode 100644 index 0000000..8e6644a --- /dev/null +++ b/internal/config/project_test.go @@ -0,0 +1,217 @@ +package config + +import ( + "slices" + "strings" + "testing" +) + +const ( + testProject = "0A1B2C3D-0000-4000-8000-000000000002" + testProject2 = "1b2c3d4e-0000-4000-8000-000000000003" + // testGitHubID is the id of [secret.github-token] in the test config, + // in a bws store. + testGitHubID = "00000000-0000-0000-0000-000000000001" +) + +// projectTOML is a [project.ci] section of the bws store main. +const projectTOML = `[project.ci] +store = "main" +project = "` + testProject + `" +clients = ["laptop", "backup-box"] +ask = "window" +window = "15m" +preapprove = true + +` + +func addProject(section string) edit { return edit{"[key.deploy]", section + "[key.deploy]"} } + +func TestProjectConfig(t *testing.T) { + e := newBrokerEnv(t) + b, err := loadBroker(e.write(t, addProject(projectTOML)), "") + if err != nil { + t.Fatalf("loadBroker: %v", err) + } + p, ok := b.Projects["ci"] + if !ok || p.Store != "main" || p.Project != strings.ToLower(testProject) || + !slices.Equal(p.Clients, []string{"laptop", "backup-box"}) || p.Rule.Ask != AskWindow || !p.Rule.Preapprove { + t.Fatalf("the project is %+v", p) + } +} + +// TestProjectConfigRefused: each error stops the broker at the start, and +// a reload, which loads the config the same way. +func TestProjectConfigRefused(t *testing.T) { + other := "[project.other]\nstore = \"main\"\nproject = \"" + testProject2 + "\"\nclients = [\"laptop\"]\nask = \"always\"\n\n" + for _, c := range []struct { + name string + cfg string + want string + }{ + {"never", strings.Replace(projectTOML, "ask = \"window\"\nwindow = \"15m\"\npreapprove = true", "ask = \"never\"", 1), + `[project.ci] ask: "never" is not allowed for a project`}, + {"the same project twice", projectTOML + strings.Replace(other, testProject2, strings.ToLower(testProject), 1), + "[project.other] project: [project.ci] has the same project"}, + {"the same project in another store", projectTOML + strings.Replace( + strings.Replace(other, testProject2, strings.ToLower(testProject), 1), `store = "main"`, `store = "main2"`, 1) + + "[store.main2]\ntype = \"bws\"\nprogram = \"{{dir}}/bin/bws\"\ntoken_file = \"${CREDENTIALS_DIRECTORY}/bws-token\"\n\n", + "has the same project"}, + {"no store", strings.Replace(projectTOML, "store = \"main\"\n", "", 1), "store: is missing"}, + {"unknown store", strings.Replace(projectTOML, `store = "main"`, `store = "nope"`, 1), "no [store.*] section"}, + {"not a bws store", strings.Replace(projectTOML, `store = "main"`, `store = "vault"`, 1), `type = "bws"`}, + {"bad project", strings.Replace(projectTOML, testProject, "ci", 1), "project: must be the ID"}, + {"no clients", strings.Replace(projectTOML, "clients = [\"laptop\", \"backup-box\"]\n", "", 1), "clients: is missing"}, + {"unknown client", strings.Replace(projectTOML, `"backup-box"`, `"phone"`, 1), "clients"}, + {"no ask", strings.Replace(projectTOML, "ask = \"window\"\n", "", 1), "ask"}, + {"unknown key", strings.Replace(projectTOML, "preapprove = true", "preapprove = true\nkey = \"X\"", 1), "key"}, + {"a bad name", strings.Replace(projectTOML, "[project.ci]", "[project.\"c/i\"]", 1), "[project.*]: the name of a section"}, + } { + t.Run(c.name, func(t *testing.T) { + e := newBrokerEnv(t) + _, err := loadBroker(e.write(t, addProject(c.cfg)), "") + if err == nil || !strings.Contains(err.Error(), c.want) { + t.Fatalf("error = %v, want one with %q", err, c.want) + } + }) + } +} + +// TestParseSecretName: a plain name, SECTION/KEY with each part by the +// name rule (so the whole is at most 129 bytes), and nothing else. +func TestParseSecretName(t *testing.T) { + n64 := strings.Repeat("n", 64) + for _, c := range []struct { + name string + project, ok bool + section, keyPart string + }{ + {"github-token", false, true, "", ""}, + {"ci/GITHUB_TOKEN", true, true, "ci", "GITHUB_TOKEN"}, + {n64 + "/" + n64, true, true, n64, n64}, // 129 bytes + {n64 + "n/k", true, false, "", ""}, + {"c/" + n64 + "n", true, false, "", ""}, + {n64 + "n", false, false, "", ""}, + {"a/b/c", true, false, "", ""}, + {"/k", true, false, "", ""}, + {"c/", true, false, "", ""}, + {"c/.k", true, false, "", ""}, + {"c/k k", true, false, "", ""}, + {"", false, false, "", ""}, + } { + pn, project, ok := ParseSecretName(c.name) + if project != c.project || ok != c.ok || pn.Section != c.section || pn.Key != c.keyPart { + t.Errorf("ParseSecretName(%q) = %+v, %v, %v", c.name, pn, project, ok) + } + if ok && project && pn.String() != c.name { + t.Errorf("String() = %q", pn.String()) + } + } +} + +// TestProjectRuleFor: a [secret.*] entry with the secret's ID in a bws +// store wins completely, also from another bws store; two entries give +// no rule; an entry in a store of another type does not count. +func TestProjectRuleFor(t *testing.T) { + e := newBrokerEnv(t) + b, err := loadBroker(e.write(t, addProject(projectTOML)), "") + if err != nil { + t.Fatal(err) + } + const free = "00000000-0000-0000-0000-0000000000ff" + if r, ok := b.RuleFor("ci", free); !ok || r.Entry != "" || r.Rule != b.Projects["ci"].Rule { + t.Errorf("a secret with no entry: %+v, %v", r, ok) + } + r, ok := b.RuleFor("ci", strings.ToUpper(testGitHubID)) + if !ok || r.Entry != "github-token" || !slices.Equal(r.Clients, b.Secrets["github-token"].Clients) { + t.Errorf("a secret with an entry: %+v, %v", r, ok) + } + for _, bad := range []struct{ section, id string }{{"nope", free}, {"ci", "x"}, {"ci", ""}} { + if _, ok := b.RuleFor(bad.section, bad.id); ok { + t.Errorf("RuleFor(%q, %q) is ok", bad.section, bad.id) + } + } + // An entry in another bws store wins too. + two := *b + two.Stores = map[string]Store{"main": b.Stores["main"], "vault": b.Stores["vault"], "main2": {Name: "main2", Type: StoreBWS}} + two.Secrets = map[string]Secret{"other": {Name: "other", Store: "main2", ID: free, Clients: []string{"backup-box"}}} + if r, ok := two.RuleFor("ci", free); !ok || r.Entry != "other" { + t.Errorf("an entry in another bws store: %+v, %v", r, ok) + } + // Two entries: no clear winner. + two.Secrets["again"] = Secret{Name: "again", Store: "main", ID: strings.ToUpper(free), Clients: []string{"laptop"}} + if r, ok := two.RuleFor("ci", free); ok { + t.Errorf("two entries: %+v, ok", r) + } + // An entry of a bw store has another kind of ID. + two.Secrets = map[string]Secret{"pw": {Name: "pw", Store: "vault", ID: free, Clients: []string{"backup-box"}}} + if r, ok := two.RuleFor("ci", free); !ok || r.Entry != "" { + t.Errorf("an entry in a bw store: %+v, %v", r, ok) + } +} + +// TestSameProjectSecret: a grant of a project secret stays only while the +// same rule of the project gives it to the client. An entry for the +// secret that a reload adds ends it. +func TestSameProjectSecret(t *testing.T) { + e := newBrokerEnv(t) + old, err := loadBroker(e.write(t, addProject(projectTOML)), "") + if err != nil { + t.Fatal(err) + } + const id = "00000000-0000-0000-0000-0000000000ff" + if !old.SameProjectSecret(old, "ci", id, "laptop") { + t.Error("the same config does not keep the grant") + } + for _, c := range []struct { + name string + cfg string + }{ + {"another window", strings.Replace(projectTOML, `window = "15m"`, `window = "10m"`, 1)}, + {"no client", strings.Replace(projectTOML, `"laptop", `, "", 1)}, + {"another project", strings.Replace(projectTOML, testProject, testProject2, 1)}, + {"no section", ""}, + {"an entry for the secret", projectTOML + "[secret.ci-one]\nstore = \"main\"\nid = \"" + id + + "\"\nclients = [\"laptop\"]\nask = \"window\"\nwindow = \"15m\"\n\n"}, + } { + next, err := loadBroker(e.write(t, addProject(c.cfg)), "") + if err != nil { + t.Fatalf("%s: %v", c.name, err) + } + if old.SameProjectSecret(next, "ci", id, "laptop") { + t.Errorf("%s: the grant stays", c.name) + } + if err := old.ReloadFixed(next); err != nil { + t.Errorf("%s: a reload is refused: %v", c.name, err) + } + } +} + +func TestDiffProjects(t *testing.T) { + added := addProject(projectTOML) + for _, c := range []struct { + diffCase + widens bool + }{ + {diffCase{name: "new project", new: []edit{added}, want: `project "ci": client "laptop" may now ask for its secrets`}, true}, + {diffCase{name: "another project", old: []edit{added}, new: []edit{{testProject, testProject2}}, + want: `project "ci": now other secrets: project changed`}, true}, + {diffCase{name: "removed", old: []edit{added}, new: []edit{{projectTOML, ""}}, want: `project "ci": removed`}, false}, + {diffCase{name: "a window", old: []edit{added}, new: []edit{{"\"backup-box\"]\nask = \"window\"\nwindow = \"15m\"", "\"backup-box\"]\nask = \"window\"\nwindow = \"1h\""}}, + want: `project "ci": window changed from 15m to 1h`}, true}, + } { + t.Run(c.name, func(t *testing.T) { + changes := c.run(t) + ch, ok := findChange(changes, c.want) + if !ok { + t.Fatalf("no change with %q in %v", c.want, changes) + } + if ch.Widens != c.widens { + t.Errorf("%q: widens = %v, want %v", ch.Text, ch.Widens, c.widens) + } + if _, ok := findChange(changes, "does not know"); ok { + t.Errorf("a project change is not known to config-diff: %v", changes) + } + }) + } +} diff --git a/internal/config/reload.go b/internal/config/reload.go index dc95e30..9b646b5 100644 --- a/internal/config/reload.go +++ b/internal/config/reload.go @@ -23,7 +23,7 @@ func (b *Broker) ReloadFixed(next *Broker) error { x, y := *b, *next for _, c := range []*Broker{&x, &y} { c.Path, c.Sum, c.KnownHostsFile, c.KnownHosts = "", "", "", nil - c.Clients, c.Secrets, c.Keys = nil, nil, nil + c.Clients, c.Secrets, c.Keys, c.Projects = nil, nil, nil, nil } type part struct { name string diff --git a/internal/ntfy/project_test.go b/internal/ntfy/project_test.go new file mode 100644 index 0000000..6968267 --- /dev/null +++ b/internal/ntfy/project_test.go @@ -0,0 +1,18 @@ +package ntfy + +import "testing" + +// TestProjectSecretsInMessages: as in package telegram. +func TestProjectSecretsInMessages(t *testing.T) { + p := secretsPrompt(t) + p.Secrets = []string{"ci/GITHUB_TOKEN (project rule, ID …0000a001)", "ci/K (rule of secret ci-one, ID …0000a001)"} + if err := checkPrompt(p, false); err != nil { + t.Errorf("project secrets: %v", err) + } + for _, s := range []string{"ci/K/x", "ci/K (project rule, ID …0000A001)", "ci/K\nx"} { + p.Secrets = []string{s} + if err := checkPrompt(p, false); err == nil { + t.Errorf("%q: no error", s) + } + } +} diff --git a/internal/ntfy/text.go b/internal/ntfy/text.go index e66911c..c22592a 100644 --- a/internal/ntfy/text.go +++ b/internal/ntfy/text.go @@ -45,7 +45,13 @@ const ( ) var ( - namePattern = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$`) + namePattern = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$`) + // secretPattern is a secret in a message: a name of a [secret.*] + // section; or SECTION/KEY of a [project.*] section, perhaps with its + // rule and the end of its ID; or a project section with its rule and + // the end of an ID, in a list of grants (DESIGN.md section 14, + // "Projects"). + secretPattern = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._-]{0,63}(/[A-Za-z0-9][A-Za-z0-9._-]{0,63})?( \((project rule|rule of secret [A-Za-z0-9][A-Za-z0-9._-]{0,63}), ID …[0-9a-f]{8}\))?$`) remoteUserPattern = regexp.MustCompile(`^[A-Za-z0-9._][A-Za-z0-9._-]{0,31}$`) hostKeyPattern = regexp.MustCompile(`^SHA256:[A-Za-z0-9+/]{43}$`) shortCodePattern = regexp.MustCompile(`^[2-9a-hjkmnp-z]{4}$`) @@ -83,7 +89,7 @@ func checkPrompt(p approval.Prompt, notice bool) error { return bad("the short code") case !namePattern.MatchString(p.Client): return bad("the client name") - case !allNames(p.Secrets, namePattern), !allNames(p.Hosts, namePattern), !allNames(p.Windows, windowPattern): + case !allNames(p.Secrets, secretPattern), !allNames(p.Hosts, namePattern), !allNames(p.Windows, windowPattern): return bad("a name") case p.Key != "" && !namePattern.MatchString(p.Key): return bad("the key") diff --git a/internal/store/bws.go b/internal/store/bws.go index 1bbc0cd..f04ce00 100644 --- a/internal/store/bws.go +++ b/internal/store/bws.go @@ -54,19 +54,11 @@ func (s *bwsStore) Start(context.Context) error { // in BWS_ACCESS_TOKEN, and returns the field "value" of the answer. bws // gets no PATH: it starts no other program. func (s *bwsStore) Fetch(ctx context.Context, id string) (Value, error) { - const step = "bws secret get" + const step = stepGetSecret if !isUUID(id) { return Value{}, stepError(s.run.store, step, ErrBadID) } - if err := acquire(ctx, s.slots); err != nil { - return Value{}, stepError(s.run.store, step, err) - } - defer func() { <-s.slots }() - env, err := s.tokenEnv() - if err != nil { - return Value{}, stepError(s.run.store, step, err) - } - out, err := s.run.run(ctx, step, []string{"secret", "get", id, "--output", "json", "--color", "no"}, env) + out, err := s.bws(ctx, step, "secret", "get", id, "--output", "json", "--color", "no") if err != nil { return Value{}, err } diff --git a/internal/store/bwsproject.go b/internal/store/bwsproject.go new file mode 100644 index 0000000..d6a23d2 --- /dev/null +++ b/internal/store/bwsproject.go @@ -0,0 +1,211 @@ +package store + +import ( + "context" + "errors" + + "github.com/code6x/nodkey/internal/config" +) + +// The steps of a project secret (DESIGN.md section 16). +const ( + stepListSecrets = "bws secret list" //nolint:gosec // G101: the name of a step, not a credential. + stepGetSecret = "bws secret get" +) + +// ErrOtherSecret is the error of a "bws secret get" answer whose key or +// project is not the one that "bws secret list" gave for the ID: the +// secret was renamed or moved between the two runs. +var ErrOtherSecret = errors.New("the secret's key or project changed since it was listed") + +// ProjectSecret is what nodkey reads from one secret of a list: its ID in +// lower case, its key, and its project's ID in lower case. Never a value. +type ProjectSecret struct { + ID string + Key string + Project string +} + +// ProjectStore is a store that hands out the secrets of a Bitwarden +// Secrets Manager project (DESIGN.md section 14, "Projects"). Only the +// bws store is one. +type ProjectStore interface { + // ListSecrets runs "bws secret list PROJECT" and returns the ID, the + // key and the project of each secret whose key follows the name rule + // (config.ValidKey). Other secrets are left out, and their key is not + // kept. The answer holds the values of all the secrets; it is cleared. + ListSecrets(ctx context.Context, project string) ([]ProjectSecret, error) + // FetchListed runs "bws secret get" for s.ID and returns the value. It + // is ErrOtherSecret when the answer's key or projectId is not s.Key + // and s.Project. + FetchListed(ctx context.Context, s ProjectSecret) (Value, error) +} + +// ListSecrets implements ProjectStore. +func (s *bwsStore) ListSecrets(ctx context.Context, project string) ([]ProjectSecret, error) { + if !isUUID(project) { + return nil, stepError(s.run.store, stepListSecrets, ErrBadID) + } + out, err := s.bws(ctx, stepListSecrets, "secret", "list", project, "--output", "json", "--color", "no") + if err != nil { + return nil, err + } + list, err := readList(out, project) + if err != nil { + return nil, stepError(s.run.store, stepListSecrets, err) + } + return list, nil +} + +// readList reads the answer of "bws secret list" with parseBWSList, and +// clears it: it holds the value of every secret of the project. +func readList(out []byte, project string) ([]ProjectSecret, error) { + defer clear(out) + return parseBWSList(out, project) +} + +// FetchListed implements ProjectStore. +func (s *bwsStore) FetchListed(ctx context.Context, ps ProjectSecret) (Value, error) { + if !isUUID(ps.ID) || !isUUID(ps.Project) || !config.ValidKey(ps.Key) { + return Value{}, stepError(s.run.store, stepGetSecret, ErrBadID) + } + out, err := s.bws(ctx, stepGetSecret, "secret", "get", ps.ID, "--output", "json", "--color", "no") + if err != nil { + return Value{}, err + } + defer clear(out) + v, err := parseBWSListed(out, ps) + if err != nil { + return Value{}, stepError(s.run.store, stepGetSecret, err) + } + return v, nil +} + +// bws takes a slot and runs bws with args and the token, as Fetch does. +// The caller must clear the answer. +func (s *bwsStore) bws(ctx context.Context, step string, args ...string) ([]byte, error) { + if err := acquire(ctx, s.slots); err != nil { + return nil, stepError(s.run.store, step, err) + } + defer func() { <-s.slots }() + env, err := s.tokenEnv() + if err != nil { + return nil, stepError(s.run.store, step, err) + } + return s.run.run(ctx, step, args, env) +} + +// listedFields are the fields that nodkey reads from one secret. +type listedFields struct { + id, project string + key string + keyOK bool // the key follows the name rule + idSet, projectSet bool + keySet bool +} + +// readListedField reads the field key of a secret into f. It reads "id", +// "key" and "projectId", and skips the others, the value and the note +// among them. A key that breaks the name rule is read, checked and +// cleared, never kept. +func readListedField(r *jsonReader, key string, f *listedFields) error { + switch key { + case "id": + s, err := r.text(maxIDLen) + f.id, f.idSet = s, true + return err + case "projectId": + if r.peek() == 'n' { + return r.null() // a secret in no project + } + s, err := r.text(maxIDLen) + f.project, f.projectSet = s, true + return err + case "key": + b, err := r.secret() + defer clear(b) + if err != nil { + return err + } + f.keySet = true + if len(b) <= config.MaxNameLen && config.ValidKey(string(b)) { + f.key, f.keyOK = string(b), true + } + return nil + } + return r.skip() +} + +// parseBWSList reads the answer of "bws secret list PROJECT --output +// json": one JSON array of secrets. Each must have a UUID "id", a "key", +// and "projectId" equal to project; else the whole answer is +// ErrBadAnswer. Secrets with a key that breaks the name rule are left out. +func parseBWSList(data []byte, project string) ([]ProjectSecret, error) { + r := &jsonReader{data: data} + r.space() + var list []ProjectSecret + err := r.arrayEach(func(r *jsonReader) error { + var f listedFields + if err := r.object(func(r *jsonReader, key string) error { return readListedField(r, key, &f) }); err != nil { + return err + } + if !f.idSet || !f.keySet || !f.projectSet || !isUUID(f.id) || !sameUUID(f.project, project) { + return ErrBadAnswer + } + if f.keyOK { + list = append(list, ProjectSecret{ID: lowerASCII(f.id), Key: f.key, Project: lowerASCII(f.project)}) + } + return nil + }) + if err == nil { + r.space() + if r.pos != len(r.data) { + err = ErrBadAnswer + } + } + if err != nil { + return nil, err + } + return list, nil +} + +// parseBWSListed reads the answer of "bws secret get ID --output json" for +// the listed secret ps: one JSON object. It reads "id", "key", +// "projectId" and "value". The id must be ps.ID, else it is an answer for +// another secret. The key must be ps.Key exactly, and the projectId +// ps.Project, else it is ErrOtherSecret. +func parseBWSListed(data []byte, ps ProjectSecret) (Value, error) { + var f listedFields + var value []byte + err := readAnswer(data, func(r *jsonReader, key string) error { + if key != "value" { + return readListedField(r, key, &f) + } + switch r.peek() { + case 'n': + return r.null() + case '"': + b, err := r.secret() + value = b + return err + } + return ErrBadAnswer + }) + switch { + case err != nil: + case !f.idSet || !sameUUID(f.id, ps.ID): + err = errOtherItem + case !f.keyOK || f.key != ps.Key || !f.projectSet || !sameUUID(f.project, ps.Project): + err = ErrOtherSecret + } + return keepValue(value, err) +} + +// lowerASCII returns s with the ASCII letters in lower case. +func lowerASCII(s string) string { + b := []byte(s) + for i, c := range b { + b[i] = lower(c) + } + return string(b) +} diff --git a/internal/store/bwsproject_test.go b/internal/store/bwsproject_test.go new file mode 100644 index 0000000..4746f51 --- /dev/null +++ b/internal/store/bwsproject_test.go @@ -0,0 +1,172 @@ +package store + +import ( + "encoding/json" + "errors" + "slices" + "strings" + "testing" + + "github.com/code6x/nodkey/internal/config" +) + +const ( + testProjectID = "0a1b2c3d-0000-4000-8000-000000000002" // as in bwsSecretJSON + otherSecretID = "7a1e2d3c-4b5a-4987-8a6b-5c4d3e2f1a0c" //nolint:gosec // G101: a made-up secret ID, not a credential. +) + +// bwsListedJSON is one secret of a project as bws shows it. +func bwsListedJSON(id, key, project, value string) map[string]any { + return map[string]any{ + "id": id, "organizationId": "0a1b2c3d-0000-4000-8000-000000000001", "projectId": project, + "key": key, "value": value, "note": madeUp("note"), "creationDate": "2026-10-14T12:00:00.000000Z", + "revisionDate": "2026-10-14T12:00:00.000000Z", + } +} + +func jsonText(t *testing.T, v any) string { + t.Helper() + b, err := json.Marshal(v) + if err != nil { + t.Fatal(err) + } + return string(b) +} + +// TestBWSListSecrets: "bws secret list PROJECT" runs with the token in the +// environment, and the store returns only the ID, the key and the +// project of each secret. A key that breaks the name rule is left out. +// No value and no note of the answer is in the result. +func TestBWSListSecrets(t *testing.T) { + t.Parallel() + token, v1, v2, v3 := madeUp("token"), madeUp("v1"), madeUp("v2"), madeUp("v3") + list := []any{ + bwsListedJSON(strings.ToUpper(testItemID), "GITHUB_TOKEN", testProjectID, v1), + bwsListedJSON(otherSecretID, "bad key with spaces", testProjectID, v2), + bwsListedJSON("8a1e2d3c-4b5a-4987-8a6b-5c4d3e2f1a0d", strings.Repeat("k", 65), testProjectID, v3), + } + sc := fakeScenario{Token: token, Lists: map[string]string{testProjectID: jsonText(t, list)}} + f := newTestFake(t, config.StoreBWS, sc, testDeps()) + got, err := f.store.(ProjectStore).ListSecrets(t.Context(), testProjectID) + if err != nil { + t.Fatalf("ListSecrets: %v", err) + } + want := []ProjectSecret{{ID: testItemID, Key: "GITHUB_TOKEN", Project: testProjectID}} + if !slices.Equal(got, want) { + t.Errorf("ListSecrets = %+v, want %+v", got, want) + } + calls := f.calls() + if len(calls) != 1 { + t.Fatalf("bws ran %d times, want 1", len(calls)) + } + if want := []string{"secret", "list", testProjectID, "--output", "json", "--color", "no"}; !slices.Equal(calls[0].Args, want) { + t.Errorf("arguments = %q, want %q", calls[0].Args, want) + } + if envValue(calls[0].Env, "BWS_ACCESS_TOKEN") != token { + t.Error("BWS_ACCESS_TOKEN is not the token") + } + // Not a UUID: bws does not run. + if _, err := f.store.(ProjectStore).ListSecrets(t.Context(), "--help"); !errors.Is(err, ErrBadID) { + t.Errorf("a project that is not a UUID: %v, want ErrBadID", err) + } + if n := len(f.calls()); n != 1 { + t.Errorf("bws ran for a project that is not a UUID") + } +} + +// TestReadListClears: the answer of "bws secret list" holds every value of +// the project; readList clears all of it, also when the answer is bad. +func TestReadListClears(t *testing.T) { + t.Parallel() + value := madeUp("value") + good := jsonText(t, []any{bwsListedJSON(testItemID, "GITHUB_TOKEN", testProjectID, value)}) + for _, text := range []string{good, good[:len(good)-1], "[" + good + "]"} { + out := []byte(text) + list, _ := readList(out, testProjectID) + for i, c := range out { + if c != 0 { + t.Fatalf("readList left byte %d of the answer", i) + } + } + for _, s := range list { + noCanary(t, "the list", s.ID+s.Key+s.Project, value) + } + } +} + +// TestParseBWSListBad: an answer that is not an array of secrets of the +// project, with an id, a key and a projectId each, is refused whole. +func TestParseBWSListBad(t *testing.T) { + t.Parallel() + one := func(change func(map[string]any)) string { + s := bwsListedJSON(testItemID, "GITHUB_TOKEN", testProjectID, madeUp("v")) + change(s) + return jsonText(t, []any{s}) + } + for name, text := range map[string]string{ + "an object": jsonText(t, bwsListedJSON(testItemID, "K", testProjectID, "v")), + "another project": one(func(s map[string]any) { s["projectId"] = "0a1b2c3d-0000-4000-8000-000000000009" }), + "no project": one(func(s map[string]any) { delete(s, "projectId") }), + "a null project": one(func(s map[string]any) { s["projectId"] = nil }), + "no id": one(func(s map[string]any) { delete(s, "id") }), + "an id not a UUID": one(func(s map[string]any) { s["id"] = "x" }), + "no key": one(func(s map[string]any) { delete(s, "key") }), + "a key not a text": one(func(s map[string]any) { s["key"] = 7 }), + "text after": jsonText(t, []any{}) + "x", + "two keys in one": `[{"id":"` + testItemID + `","key":"A","key":"B","projectId":"` + testProjectID + `"}]`, + "invalid UTF-8 key": "[{\"id\":\"" + testItemID + "\",\"key\":\"\xff\",\"projectId\":\"" + testProjectID + "\"}]", + } { + if list, err := parseBWSList([]byte(text), testProjectID); err == nil { + t.Errorf("%s: no error, list %+v", name, list) + } + } + if list, err := parseBWSList([]byte("[]"), testProjectID); err != nil || len(list) != 0 { + t.Errorf("an empty project: %v, %v", list, err) + } +} + +// TestBWSFetchListed: the value comes only when the answer of "bws secret +// get" has the key and the project of the list. A rename or a move +// between the two runs is ErrOtherSecret, and no value comes. +func TestBWSFetchListed(t *testing.T) { + t.Parallel() + token, value := madeUp("token"), madeUp("value") + listed := ProjectSecret{ID: testItemID, Key: "GITHUB_TOKEN", Project: testProjectID} + for _, c := range []struct { + name string + key, project string + want error + }{ + {"the same", "GITHUB_TOKEN", testProjectID, nil}, + {"the project in upper case", "GITHUB_TOKEN", strings.ToUpper(testProjectID), nil}, + {"renamed", "GITHUB_TOKEN_OLD", testProjectID, ErrOtherSecret}, + {"another case", "github_token", testProjectID, ErrOtherSecret}, + {"moved", "GITHUB_TOKEN", "0a1b2c3d-0000-4000-8000-000000000009", ErrOtherSecret}, + } { + sc := fakeScenario{Token: token, Items: map[string]string{ + testItemID: jsonText(t, bwsListedJSON(testItemID, c.key, c.project, value)), + }} + f := newTestFake(t, config.StoreBWS, sc, testDeps()) + v, err := f.store.(ProjectStore).FetchListed(t.Context(), listed) + if !errors.Is(err, c.want) { + t.Errorf("%s: %v, want %v", c.name, err, c.want) + } + if c.want == nil { + checkValue(t, v, value) + } else if v.Bytes() != nil { + t.Errorf("%s: a value came", c.name) + } + if err != nil { + noCanary(t, c.name, err.Error(), value) + } + v.Clear() + } + // The answer for another ID is refused. + sc := fakeScenario{Token: token, Items: map[string]string{ + testItemID: jsonText(t, bwsListedJSON(otherSecretID, "GITHUB_TOKEN", testProjectID, value)), + }} + f := newTestFake(t, config.StoreBWS, sc, testDeps()) + if _, err := f.store.(ProjectStore).FetchListed(t.Context(), listed); !errors.Is(err, ErrBadAnswer) { + t.Errorf("another ID: %v, want ErrBadAnswer", err) + } +} diff --git a/internal/store/fake_test.go b/internal/store/fake_test.go index 87a2e96..e0c6606 100644 --- a/internal/store/fake_test.go +++ b/internal/store/fake_test.go @@ -76,7 +76,7 @@ type fakeScenario struct { Password string `json:"password"` // bw: the master password, the first line of the password file Session string `json:"session"` // bw: the session key that unlock makes; random if empty Items map[string]string `json:"items"` // the answer for each item ID, in lower case - Lists map[string]string `json:"lists"` // bw: the answer of "list items" for each collection ID, in lower case + Lists map[string]string `json:"lists"` // bw: the answer of "list items" for each collection ID; bws: of "secret list" for each project ID; in lower case Steps map[string]fakeStep `json:"steps"` // what a step does instead of its usual work Pause time.Duration `json:"pause"` // each run waits this long first } @@ -164,16 +164,20 @@ func runFake(program string) int { // fakeBWS acts as bws. func fakeBWS(home string, sc *fakeScenario) int { args := os.Args[1:] - if len(args) < 3 || args[0] != "secret" || args[1] != "get" { + if len(args) < 3 || args[0] != "secret" || (args[1] != "get" && args[1] != "list") { return fakeFail("error: unexpected arguments", 2) } - if step, ok := sc.Steps["secret get"]; ok { + if step, ok := sc.Steps["secret "+args[1]]; ok { return fakeOverride(home, step) } if os.Getenv("BWS_ACCESS_TOKEN") != sc.Token || sc.Token == "" { return fakeFail("Error: \n 0: Access token is not in a valid format", 1) } - item, ok := sc.Items[strings.ToLower(args[2])] + answers := sc.Items + if args[1] == "list" { + answers = sc.Lists + } + item, ok := answers[strings.ToLower(args[2])] if !ok { return fakeFail("Error: \n 0: Received error message from server: [404 Not Found]", 1) } diff --git a/internal/telegram/grants.go b/internal/telegram/grants.go index 98ed3f2..0d79352 100644 --- a/internal/telegram/grants.go +++ b/internal/telegram/grants.go @@ -101,7 +101,7 @@ func grantsText(list []approval.GrantInfo, weekly bool) (*text, error) { return t, nil } for _, g := range list { - if !namePattern.MatchString(g.Client) || !allNames(g.Secrets) || len(g.Secrets) == 0 || + if !namePattern.MatchString(g.Client) || !allSecrets(g.Secrets) || len(g.Secrets) == 0 || g.End.IsZero() { return nil, errors.New("telegram: a standing grant in the list is not valid") } diff --git a/internal/telegram/project_test.go b/internal/telegram/project_test.go new file mode 100644 index 0000000..d75e324 --- /dev/null +++ b/internal/telegram/project_test.go @@ -0,0 +1,35 @@ +package telegram + +import "testing" + +// TestProjectSecretsInMessages: a message shows a secret of a project as +// SECTION/KEY with its rule and the end of its ID; other text in the +// place of a secret is refused. +func TestProjectSecretsInMessages(t *testing.T) { + for _, s := range []string{ + "ci/GITHUB_TOKEN", + "ci/GITHUB_TOKEN (project rule, ID …0000a001)", + "ci/GITHUB_TOKEN (rule of secret ci-one, ID …0000a001)", + "ci (project rule, ID …0000a001)", + } { + p := secretsPrompt() + p.Secrets = []string{"github-token", s} + if err := checkPrompt(p, false); err != nil { + t.Errorf("%q: %v", s, err) + } + } + for _, s := range []string{ + "ci/GITHUB_TOKEN/x", + "ci/GITHUB TOKEN", + "ci/GITHUB_TOKEN (project rule, ID …0000A001)", + "ci/GITHUB_TOKEN (project rule)", + "ci/GITHUB_TOKEN (rule of secret a b, ID …0000a001)", + "ci/GITHUB_TOKEN\n✅ client: x", + } { + p := secretsPrompt() + p.Secrets = []string{s} + if err := checkPrompt(p, false); err == nil { + t.Errorf("%q: no error", s) + } + } +} diff --git a/internal/telegram/renew.go b/internal/telegram/renew.go index a7371d9..f94ea72 100644 --- a/internal/telegram/renew.go +++ b/internal/telegram/renew.go @@ -27,7 +27,7 @@ func warningText(w approval.Warning) (*text, error) { return nil, bad("the short code") case !namePattern.MatchString(w.Client): return nil, bad("the client name") - case len(w.Secrets) == 0 || !allNames(w.Secrets): + case len(w.Secrets) == 0 || !allSecrets(w.Secrets): return nil, bad("the secrets") case len(w.Windows) == 0 || len(w.Windows) > 7: return nil, bad("the windows") diff --git a/internal/telegram/text.go b/internal/telegram/text.go index 5b844b6..0295174 100644 --- a/internal/telegram/text.go +++ b/internal/telegram/text.go @@ -69,6 +69,12 @@ const maxWindowMinutes = int(config.MaxWindow / time.Minute) // Patterns for the checked fields of a prompt, as in package audit. var ( namePattern = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$`) + // secretPattern is a secret in a message: a name of a [secret.*] + // section; or SECTION/KEY of a [project.*] section, perhaps with its + // rule and the end of its ID; or a project section with its rule and + // the end of an ID, in a list of grants (DESIGN.md section 14, + // "Projects"). + secretPattern = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._-]{0,63}(/[A-Za-z0-9][A-Za-z0-9._-]{0,63})?( \((project rule|rule of secret [A-Za-z0-9][A-Za-z0-9._-]{0,63}), ID …[0-9a-f]{8}\))?$`) // windowPattern is the form of a window of a standing grant, as // the core shows it: "daily 03:00-05:00" or "mon,thu 03:00-05:00". windowPattern = regexp.MustCompile(`^(daily|(mon|tue|wed|thu|fri|sat|sun)(,(mon|tue|wed|thu|fri|sat|sun)){0,6}) ([01][0-9]|2[0-3]):[0-5][0-9]-([01][0-9]|2[0-3]):[0-5][0-9]$`) @@ -276,7 +282,7 @@ func checkPrompt(p approval.Prompt, notice bool) error { return bad("the fields of a website login") } case audit.KindSecrets: - if len(p.Secrets) == 0 || !allNames(p.Secrets) || p.Key != "" || len(p.Hosts) != 0 || p.HostKey != "" || + if len(p.Secrets) == 0 || !allSecrets(p.Secrets) || p.Key != "" || len(p.Hosts) != 0 || p.HostKey != "" || p.RemoteUser != "" || !p.GrantEnds.IsZero() { return bad("the fields of a secrets request") } @@ -304,7 +310,7 @@ func checkPrompt(p approval.Prompt, notice bool) error { } break } - named := len(p.Secrets) > 0 && allNames(p.Secrets) && p.Key == "" && len(p.Hosts) == 0 && p.RemoteUser == "" + named := len(p.Secrets) > 0 && allSecrets(p.Secrets) && p.Key == "" && len(p.Hosts) == 0 && p.RemoteUser == "" keyed := len(p.Secrets) == 0 && namePattern.MatchString(p.Key) && len(p.Hosts) > 0 && len(p.Hosts) <= approval.MaxGrantHosts && allNames(p.Hosts) && remoteUserPattern.MatchString(p.RemoteUser) if (!named && !keyed) || p.HostKey != "" || !p.GrantEnds.IsZero() || p.WindowMinutes != 0 || @@ -318,7 +324,7 @@ func checkPrompt(p approval.Prompt, notice bool) error { return bad("the fields of a scheduled grant") } case audit.KindStanding: - if notice || len(p.Secrets) == 0 || !allNames(p.Secrets) || p.Key != "" || len(p.Hosts) != 0 || + if notice || len(p.Secrets) == 0 || !allSecrets(p.Secrets) || p.Key != "" || len(p.Hosts) != 0 || p.RemoteUser != "" || p.HostKey != "" || !p.GrantEnds.IsZero() || p.WindowMinutes != 0 || p.Uses < 1 || p.From.IsZero() || !p.From.Before(p.Until) || p.Via != "" || p.GrantID != "" || p.UsesLeft != 0 || !p.Once || len(p.Windows) < 1 || @@ -380,6 +386,18 @@ func checkLoginPrompt(p approval.Prompt, notice bool) error { return nil } +// allSecrets reports whether each of names is a secret as a message shows +// it (secretPattern). +func allSecrets(names []string) bool { + for _, n := range names { + if !secretPattern.MatchString(n) { + return false + } + } + return true +} + +// allNames reports whether each of names follows the name rule. func allNames(names []string) bool { for _, n := range names { if !namePattern.MatchString(n) { @@ -759,7 +777,7 @@ func alarmText(a approval.Alarm) (*text, error) { return nil, bad("the short code") case !namePattern.MatchString(a.Client): return nil, bad("the client name") - case len(a.Secrets) > 0 && !allNames(a.Secrets): + case len(a.Secrets) > 0 && !allSecrets(a.Secrets): return nil, bad("the secrets") case (a.GrantID == "") != (a.Cause == audit.AlarmUnknownToken) || (a.GrantID != "" && !validRequestID(a.GrantID)): return nil, bad("the grant ID")