diff options
| -rw-r--r-- | README | 38 | ||||
| -rw-r--r-- | authz.conf | 47 | ||||
| -rw-r--r-- | src/authz.h | 34 | ||||
| -rw-r--r-- | src/edit.c | 122 | ||||
| -rw-r--r-- | src/main.c | 13 | ||||
| -rw-r--r-- | src/policy.c | 164 | ||||
| -rw-r--r-- | src/validate.c | 3 | ||||
| -rw-r--r-- | tests/run.sh | 89 | ||||
| -rw-r--r-- | tests/unit.c | 134 |
9 files changed, 217 insertions, 427 deletions
@@ -23,11 +23,10 @@ Commands git-authz [--policy PATH] validate [options] git-authz [--policy PATH] grant <user> <repo> <level> [ref ...] git-authz [--policy PATH] revoke <user> <repo> - git-authz [--policy PATH] protect [--repo R] <ref pattern> ... `check` answers one question and is for humans and scripts. `authorize` is what git invokes. `validate` asks whether the host is wired to run the policy at all. -The last three edit the file. +The last two edit the file. Exit status is the interface, and it does not change: @@ -87,15 +86,24 @@ and works out what the update *is* before it asks who may do it: delete the branch HEAD points at admin a ref outside refs/heads, refs/tags admin -`set protected-refs <patterns>` raises the price of the listed refs by one level: -write needs force, force needs admin. `set protected-refs.<repo>` replaces that -list for one repository, so a repo with different customs does not inherit -somebody else's. +That is the whole of it. A rule says what it says: there is no second list +somewhere else in the file that can make one ref cost more than another, so the +answer to "who can move main" is the lines that name main, and nothing else. + +Which means an unscoped rule really does cover main: + + dave team admin # every ref in team, main included + +and a rule that means to leave main alone has to say so by naming what it covers +instead: + + bob team write refs/heads/feature/* # main is not named, so not granted Anything that is not a branch or a tag - `refs/notes`, `refs/merge-requests`, -whatever a tool invents next - is admin-only. It is the part of the namespace no -policy was written with in mind, and the safe reading of "not mentioned" is "not -granted". +whatever a tool invents next - is admin-only whatever a rule appears to grant +about it. That is the one price the program raises on its own, and it is the one +the policy cannot name in advance: it is the part of the namespace no policy was +written with in mind, and the safe reading of "not mentioned" is "not granted". Identity @@ -169,7 +177,6 @@ Editing the file git-authz grant bob team write refs/heads/feature/* git-authz revoke bob team - git-authz protect --repo team refs/heads/release Each writes the file atomically - a temporary in the same directory, then a rename, keeping the mode and owner - and preserves everything else in the file, @@ -200,8 +207,13 @@ Reading this as an operator Because the message ends with what it would have taken: - deny: bob has write on team refs/heads/main; it needs force - (raised from write: refs/heads/main is protected) + deny: bob holds write on team, but not on refs/heads/main; + that ref needs write + + or, when the ref is covered by a rule but costs more than it looks: + + deny: bob has write on team refs/notes/x; it needs admin + (neither a branch nor a tag) which is the whole design in one line: a refusal that names the level held, the ref, the level required and why, so the fix is obvious to the person who @@ -214,7 +226,7 @@ Tests make test `tests/unit.c` tests the policy engine directly - matching, normalisation, the -ladder, `@all`, protection, the sentences - and is the place where the semantics +ladder, `@all`, scope, the sentences - and is the place where the semantics are pinned. `tests/run.sh` drives the binary the way git does: an identity in the environment, `old new ref` lines on stdin, the repository as the working directory, the hook reached through its own name. It uses no git at all, so it @@ -2,36 +2,40 @@ # # One file, read by the receive hook on every push and by nothing that serves # reads. The deployed copy is mounted read-only - a ConfigMap - so changes are -# made to the copy in git and rolled out; `git-authz grant`, `revoke` and -# `protect` edit a source copy and refuse, with an explanation, when pointed at -# the read-only one. +# made to the copy in git and rolled out; `git-authz grant` and `revoke` edit a +# source copy and refuse, with an explanation, when pointed at the read-only one. # # <user> <repo> <write|force|admin> [ <ref pattern> ... ] # # Levels stack: admin implies force implies write, and every rule for a user -# counts, so the strongest match wins. There is no 'deny': access is taken -# away by deleting the rule that gave it. +# counts, so the strongest match wins. There is no 'deny': access is taken away +# by deleting the rule that gave it, which means the file always says the whole +# of what it grants. +# +# A rule with no ref patterns covers the whole repository, refs included. With +# patterns, it covers exactly those refs and nothing else - so the answer to +# "who can move main" is the lines that name main, and there is no second list +# somewhere else in the file that can raise the price of a ref behind your back. # # A ref pattern is a glob in which '*' crosses '/'. "main", "heads/main" and # "refs/heads/main" name the same branch; a bare name is always a branch, so a -# tag has to be written "refs/tags/v1" or "tags/v1". With no patterns at all -# the rule covers the whole repository, refs included. +# tag has to be written "refs/tags/v1" or "tags/v1". # -# '@all' means everyone this policy already names somewhere. It is not a -# wildcard for anonymous users: nobody reaches it who has no rule of their own. - -# What costs an extra level to move. A write on these needs force, a force -# needs admin, and refs outside refs/heads and refs/tags need admin regardless. -set protected-refs refs/heads/main refs/tags/* +# '@all' means everyone this policy already names somewhere. It is not a wildcard +# for anonymous users: a name that appears nowhere in the file reaches nothing, +# so adding a rule for a person is the whole of onboarding. +# +# Whatever a rule says about them, refs outside refs/heads and refs/tags need +# admin: refs/notes, refs/merge-requests, whatever a tool invents next are not +# part of the namespace a policy was written with in mind. -# The account for break-glass and for the refs nobody else should touch: it is -# unscoped, so it covers the whole repository including refs/notes and friends. +# The account for break-glass and for the refs nobody else should touch. alice * admin -# Default for everyone the policy knows: their own branches, anywhere. Reads -# are open, so this is about writing, and there is no rule here to be the -# fall-through for someone the policy has never mentioned. -@all * write refs/heads/* +# Default for everyone the policy knows: their own working branches, anywhere. +# Reads are open, so this is only about writing - and it deliberately does not +# reach main, which is named below, person by person. +@all * write refs/heads/feature/* refs/heads/bugfix/* # Contributor on team: feature branches only. bob team write refs/heads/feature/* @@ -39,8 +43,11 @@ bob team write refs/heads/feature/* # Senior on team: may rewrite his feature branches and delete them. carol team force refs/heads/feature/* -# Maintainer of team: everything, including main, tags, and deletions. +# Maintainer of team: everything, including main, tags and deletions. dave team admin +# Frank does releases: main and the release tags, and nothing else. +frank team write refs/heads/main refs/tags/release/* + # Team A's repository, with an unrelated ACL. erin secrets admin diff --git a/src/authz.h b/src/authz.h index 53f251d..794c12c 100644 --- a/src/authz.h +++ b/src/authz.h @@ -49,20 +49,14 @@ typedef struct { rule_t *rules; size_t nrules, rcap; - char *protected_global; /* space separated normalised patterns, or NULL */ - char **prot_repo; /* repositories with their own list */ - char **prot_refs; /* and the list that goes with each */ - size_t nprot, pcap; - int errors; - int warnings; int unreadable; /* the file itself could not be opened */ char *path; /* what messages call the file */ } policy_t; typedef struct { int allowed; - level_t need; /* what it cost in the end, after any raise */ + level_t need; /* what it cost in the end */ char why[512]; /* the sentence the person pushing reads */ } decision_t; @@ -83,8 +77,8 @@ void policy_init(policy_t *p, const char *path); void policy_free(policy_t *p); /* Read and parse. Returns 0 when the file is usable, -1 when it produced - * errors, -2 when it could not be read at all. Errors and warnings are - * reported as they are found, on stderr, with the line number. */ + * errors, -2 when it could not be read at all. Errors are reported as they are + * found, on stderr, with the line number. */ int policy_load(policy_t *p); /* Refs and repositories have one spelling each internally. A bare name is a @@ -115,13 +109,14 @@ int authz_level_of(const char *name); * "everybody this policy already knows about", and never invents a name. */ int authz_user_known(const policy_t *p, const char *user); -/* What a ref really costs. Anything that is not a branch or a tag is for - * maintainers; on a protected ref 'write' is not enough and 'force' is not - * enough either. When the answer is higher than 'have', the reason - * ("protected", "neither a branch nor a tag") is written to raised, which must - * hold at least 64 bytes. */ -level_t authz_need_for(const policy_t *p, const char *repo, const char *ref, - level_t have, char *raised, size_t raisedsz); +/* What a ref really costs beyond what the rule said. Anything that is not a + * branch or a tag is for maintainers, whatever a rule about it appears to + * grant: refs/notes, refs/merge-requests, whatever a tool invents next, are not + * part of the namespace a policy was written with in mind, and the safe reading + * of "not mentioned" is "not granted". When the answer is higher than 'have', + * the reason is written to raised, which must hold at least 64 bytes. */ +level_t authz_need_for(const char *ref, level_t have, char *raised, + size_t raisedsz); /* The decision. 'ref' may be NULL, which asks "may this user do this * somewhere in the repository" rather than about one ref. */ @@ -133,10 +128,6 @@ void authz_check(const policy_t *p, const char *user, const char *repo, * matching rather than a decision. */ int authz_repo_reachable(const policy_t *p, const char *repo); -/* The protected list in force for one repository, as stored (normalised - * patterns, space separated). Never NULL. */ -const char *authz_protected_for(const policy_t *p, const char *repo); - /* --------------------------------------------------------------- hook.c */ /* authorize mode: identity from the environment, ref lines from stdin, the @@ -179,7 +170,7 @@ typedef struct { const char *out; /* NULL edits in place; "-" is stdout */ } edit_opts; -/* Shared by the three editing commands: pulls --out and --stdout out of the +/* Shared by the two editing commands: pulls --out and --stdout out of the * argument list wherever they sit (an option at the end reads better in a * shell history) and compacts what is left into argv, returning the count in * *consumed. Returns -1 on a missing value. */ @@ -191,7 +182,6 @@ int authz_reject_options(int argc, char **argv); int cmd_grant(const char *path, edit_opts o, int argc, char **argv); int cmd_revoke(const char *path, edit_opts o, int argc, char **argv); -int cmd_protect(const char *path, edit_opts o, int argc, char **argv); /* ---------------------------------------------------------------- main.c * Shared little helpers, so that no file invents its own way to fail. */ @@ -205,29 +205,6 @@ static int is_rule_for(const char *line, const char *user, const char *repo) return same; } -static int is_set_for(const char *line, const char *key) -{ - char *copy = xstrdup(line); - char *text = authz_trim(copy); - char *hash = strchr(text, '#'); - size_t klen = strlen(key); - int hit = 0; - - if (hash != NULL) - *hash = '\0'; - if (strncmp(text, "set", 3) == 0 && isspace((unsigned char)text[3])) { - text = authz_trim(text + 3); - hit = strncmp(text, key, klen) == 0 && - (text[klen] == '\0' || isspace((unsigned char)text[klen])); - } - free(copy); - return hit; -} - -/* --------------------------------------------------------------- emitting */ - -/* "user repo level ref ref" - one line, no alignment tricks, because the next - * tool to read this is diff. */ static char *rule_text(const char *user, const char *repo, const char *level, int nrefs, char **refs) { @@ -392,7 +369,8 @@ int cmd_grant(const char *path, edit_opts o, int argc, char **argv) lines_push(&lines, xstrdup("# <user> <repo> <write|force|admin> " "[ref pattern ...] levels stack: " "admin > force > write\n")); - lines_push(&lines, xstrdup("set protected-refs refs/heads/main\n")); + lines_push(&lines, xstrdup("# Name the refs a rule covers: with no " + "patterns it covers the whole repository.\n")); } else if (read_lines(path, &lines) != 0) return 3; @@ -493,99 +471,3 @@ int cmd_revoke(const char *path, edit_opts o, int argc, char **argv) return finish(&lines, path, o, what); } } - -int cmd_protect(const char *path, edit_opts o, int argc, char **argv) -{ - lines_t lines; - const char *repo = NULL; - char key[256]; - char *line; - strbuf_t text; - int i; - int replaced = -1; - int created = 0; - for (i = 0; i < argc; i++) { /* --repo anywhere, like --out */ - if (strcmp(argv[i], "--repo") != 0) - continue; - if (i + 1 >= argc) { - fprintf(stderr, "%s: --repo wants a repository name\n", progname); - return 64; - } - repo = argv[i + 1]; - for (; i + 2 < argc; i++) - argv[i] = argv[i + 2]; - argc -= 2; - break; - } - if (argc < 1) { - fprintf(stderr, "%s: protect wants [--repo <name>] <ref pattern> ...\n", - progname); - return 64; - } - if (repo != NULL && !authz_repo_pattern_ok(repo)) { - fprintf(stderr, "%s: invalid repo pattern \"%s\"\n", progname, repo); - return 64; - } - for (i = 0; i < argc; i++) { - if (argv[i][0] == '-' && argv[i][1] == '-') { - fprintf(stderr, "%s: unknown option \"%s\" (the only one here is " - "--repo, which comes first)\n", progname, argv[i]); - return 64; - } - if (argv[i][0] == '!' || argv[i][0] == '-') { - fprintf(stderr, "%s: ref patterns are additive, there is no deny: %s\n", - progname, argv[i]); - return 64; - } - } - if (refuse_broken(path, &created) != 0) - return 3; - if (created) { - lines_init(&lines); - lines_push(&lines, xstrdup("# git-authz policy\n")); - } else if (read_lines(path, &lines) != 0) - return 3; - - if (repo != NULL) - snprintf(key, sizeof(key), "protected-refs.%s", repo); - else - snprintf(key, sizeof(key), "protected-refs"); - - sb_init(&text); - sb_addf(&text, "set %s", key); - for (i = 0; i < argc; i++) { - char *one = authz_norm_ref(argv[i]); - - sb_addf(&text, " %s", one); - free(one); - } - sb_add(&text, "\n"); - - line = xstrdup(text.s); /* the array owns its own copy; text is - * released by sb_free below */ - for (i = 0; i < (int)lines.n; i++) { - if (!is_set_for(lines.v[i], key)) - continue; - free(lines.v[i]); - lines.v[i] = line; - replaced = i + 1; - break; - } - if (replaced < 0) { - lines_push(&lines, line); - replaced = (int)lines.n; - } - sb_free(&text); - - { - char what[512]; - - if (created) - snprintf(what, sizeof(what), "protected refs for %s (%s, new policy)", - repo != NULL ? repo : "every repository", key); - else - snprintf(what, sizeof(what), "protected refs for %s (%s, line %d)", - repo != NULL ? repo : "every repository", key, replaced); - return finish(&lines, path, o, what); - } -} @@ -3,7 +3,7 @@ * check an operator asking, or a script * authorize git asking, once per push (also reached as 'pre-receive') * validate the entrypoint and CI asking before anything is served - * edit changing the file (grant, revoke, protect) + * edit changing the file (grant, revoke) * * They are one program because they must agree, exactly, about what a rule * means: a check that answers differently from the hook is worse than two @@ -58,14 +58,14 @@ static void usage(FILE *f) "\n" " grant <user> <repo> <level> [ref ...] add one rule, or replace that pair's\n" " revoke <user> <repo> take it away again\n" -" protect [--repo R] <ref pattern> ... set which refs need an extra level\n" " --out FILE | --stdout write elsewhere instead of in place\n" " The deployed policy is a read-only mount on purpose; these are for the\n" " source copy, and they say so when they are pointed at a read-only file.\n" "\n" "Levels are monotone: admin implies force implies write. '@all' means everyone\n" "the policy already names. \"main\", \"heads/main\" and \"refs/heads/main\" are the\n" -"same branch; a bare name is always a branch and never a tag.\n" +"same branch; a bare name is always a branch and never a tag. A rule says what\n" +"it says: nothing else in the file can raise the price of a ref.\n" "\n" "Exit status: 0 allowed, 1 denied, 3 the policy could not be read or does not\n" "parse - nothing is allowed on a policy that cannot be read - and 64 for usage.\n", @@ -274,8 +274,7 @@ int main(int argc, char **argv) /* The three commands that change the file. They read it themselves, and * they must not disagree with the checker about what a rule means, which is * why they are in the same binary. */ - if (strcmp(cmd, "grant") == 0 || strcmp(cmd, "revoke") == 0 || - strcmp(cmd, "protect") == 0) { + if (strcmp(cmd, "grant") == 0 || strcmp(cmd, "revoke") == 0) { int consumed = 0; if (authz_parse_out(argc, argv, &eo, &consumed) != 0) @@ -283,9 +282,7 @@ int main(int argc, char **argv) argc -= consumed; /* what is left was compacted to the front */ if (strcmp(cmd, "grant") == 0) return cmd_grant(path, eo, argc, argv); - if (strcmp(cmd, "revoke") == 0) - return cmd_revoke(path, eo, argc, argv); - return cmd_protect(path, eo, argc, argv); + return cmd_revoke(path, eo, argc, argv); } policy_init(&p, path); diff --git a/src/policy.c b/src/policy.c index 1866bb2..ef429e0 100644 --- a/src/policy.c +++ b/src/policy.c @@ -2,8 +2,6 @@ * * Grammar (see authz.conf for the prose version): * - * set protected-refs <pattern> ... globally, default refs/heads/main - * set protected-refs.<repo> <pattern> ... for one repository, replacing it * <user> <repo> <write|force|admin> [<ref pattern> ...] * * '#' starts a comment wherever it appears. Rules are additive and the @@ -92,16 +90,6 @@ static void perr(policy_t *p, int line, const char *fmt, ...) va_end(ap); } -static void pwarn(policy_t *p, int line, const char *fmt, ...) -{ - va_list ap; - - p->warnings++; - va_start(ap, fmt); - report(p, line, "warning", fmt, ap); - va_end(ap); -} - /* ------------------------------------------------------------ name shapes */ int authz_user_pattern_ok(const char *s) @@ -203,29 +191,6 @@ char *authz_norm_ref(const char *in) } /* A whole list of them, space separated, the way 'set' lines store them. */ -static char *norm_ref_list(const char *value) -{ - char *copy = xstrdup(value); - char *fields[MAX_FIELDS]; - strbuf_t out; - int i, n; - - sb_init(&out); - n = authz_split_ws(copy, fields, MAX_FIELDS); - for (i = 0; i < n; i++) { - char *one = authz_norm_ref(fields[i]); - - if (out.len != 0) - sb_add(&out, " "); - sb_add(&out, one); - free(one); - } - free(copy); - return out.s; -} - -/* ------------------------------------------------------------------ tables */ - static rule_t *rule_slot(policy_t *p) { rule_t *r; @@ -239,35 +204,11 @@ static rule_t *rule_slot(policy_t *p) return r; } -static void set_protected_for(policy_t *p, const char *repo, const char *refs, int line) -{ - size_t i; - - for (i = 0; i < p->nprot; i++) { - if (strcmp(p->prot_repo[i], repo) == 0) { - pwarn(p, line, "protected-refs for %s is set twice; the last one wins", repo); - free(p->prot_refs[i]); - p->prot_refs[i] = xstrdup(refs); - return; - } - } - if (p->nprot == p->pcap) { - p->pcap = p->pcap != 0 ? p->pcap * 2 : 8; - p->prot_repo = xrealloc(p->prot_repo, p->pcap * sizeof(*p->prot_repo)); - p->prot_refs = xrealloc(p->prot_refs, p->pcap * sizeof(*p->prot_refs)); - } - p->prot_repo[p->nprot] = xstrdup(repo); - p->prot_refs[p->nprot] = xstrdup(refs); - p->nprot++; -} - -/* ---------------------------------------------------------------- the file */ - static void parse_set(policy_t *p, char *line, int lineno) { char *body = authz_trim(line + 3); char *sp = body; - char *key, *val, *repo; + char *key; while (*sp != '\0' && !isspace((unsigned char)*sp)) sp++; @@ -276,24 +217,19 @@ static void parse_set(policy_t *p, char *line, int lineno) return; } key = xsubstr(body, (size_t)(sp - body)); - val = authz_trim(sp); - - if (strcmp(key, "protected-refs") == 0) { - if (p->protected_global != NULL) - pwarn(p, lineno, "set protected-refs appears twice; the last one wins"); - free(p->protected_global); - p->protected_global = norm_ref_list(val); - } else if (strncmp(key, "protected-refs.", 15) == 0) { - repo = authz_norm_repo(key + 15); - if (repo[0] == '\0' || strcmp(repo, "*") == 0) { - perr(p, lineno, "protected-refs. needs one repository, not \"%s\"", key + 15); - } else { - set_protected_for(p, repo, val, lineno); - } - free(repo); - } else { + + /* protected-refs was removed, and it is named rather than left to "unknown + * setting" on purpose. A policy that still carries one of these lines has + * to be refused rather than read as if the line were a comment: ignoring it + * would leave every branch one fast-forward away from whoever holds 'write' + * on them, which is the opposite of what the line was there to prevent, and + * the operator would learn that from a push instead of from the parser. */ + if (strncmp(key, "protected-refs", 14) == 0) + perr(p, lineno, "protected-refs no longer exists: name the refs a rule " + "covers instead, for example \"dave team admin refs/heads/main\""); + else perr(p, lineno, "unknown setting \"%s\"", key); - } + free(key); } @@ -364,13 +300,6 @@ void policy_free(policy_t *p) free(p->rules[i].refs); } free(p->rules); - free(p->protected_global); - for (i = 0; i < p->nprot; i++) { - free(p->prot_repo[i]); - free(p->prot_refs[i]); - } - free(p->prot_repo); - free(p->prot_refs); free(p->path); memset(p, 0, sizeof(*p)); } @@ -464,67 +393,26 @@ int authz_repo_reachable(const policy_t *p, const char *repo) return 0; } -const char *authz_protected_for(const policy_t *p, const char *repo) -{ - char *want; - size_t i; - - want = authz_norm_repo(repo); - for (i = 0; i < p->nprot; i++) { - if (strcmp(p->prot_repo[i], want) == 0) { - free(want); - return p->prot_refs[i]; - } - } - free(want); - if (p->protected_global != NULL && p->protected_global[0] != '\0') - return p->protected_global; - return DEFAULT_PROTECTED; -} - -static int is_protected(const policy_t *p, const char *repo, const char *ref) -{ - const char *list = authz_protected_for(p, repo); - char *copy = xstrdup(list); - char *fields[MAX_FIELDS]; - int n, i, hit = 0; - - n = authz_split_ws(copy, fields, MAX_FIELDS); - for (i = 0; i < n; i++) { - if (glob_match(fields[i], ref)) { - hit = 1; - break; - } - } - free(copy); - return hit; -} - static int ordinary_ref(const char *ref) { return strncmp(ref, "refs/heads/", 11) == 0 || strncmp(ref, "refs/tags/", 10) == 0; } -level_t authz_need_for(const policy_t *p, const char *repo, const char *ref, - level_t have, char *raised, size_t raisedsz) +level_t authz_need_for(const char *ref, level_t have, char *raised, + size_t raisedsz) { if (raised != NULL && raisedsz > 0) raised[0] = '\0'; - /* Anything that is not a branch or a tag is for maintainers: refs/notes, - * refs/merge-requests, whatever a tool invents next. */ + /* Anything that is not a branch or a tag is for maintainers, whatever a rule + * appears to grant about it. Nothing else raises a price here: a rule says + * what it says, and there is no second, invisible list that can make a ref + * cost more behind the operator's back. */ if (!ordinary_ref(ref) && have != LV_ADMIN) { if (raised != NULL) snprintf(raised, raisedsz, "neither a branch nor a tag"); return LV_ADMIN; } - if (!is_protected(p, repo, ref)) - return have; - if (have == LV_WRITE || have == LV_FORCE) { - if (raised != NULL) - snprintf(raised, raisedsz, "protected"); - return have == LV_WRITE ? LV_FORCE : LV_ADMIN; - } return have; } @@ -637,7 +525,7 @@ void authz_check(const policy_t *p, const char *user, const char *repo, } nref = authz_norm_ref(ref); - want = authz_need_for(p, repo, nref, need, raised, sizeof(raised)); + want = authz_need_for(nref, need, raised, sizeof(raised)); out->need = want; scope(p, user, repo, want, nref, &s); @@ -647,9 +535,9 @@ void authz_check(const policy_t *p, const char *user, const char *repo, if (raised[0] != '\0') snprintf(out->why, sizeof(out->why), - "%s has %s on %s %s (raised from %s: %s is %s)", + "%s has %s on %s %s (this ref costs %s, not %s: %s)", user, authz_level_name(want), r, nref, - authz_level_name(need), nref, raised); + authz_level_name(want), authz_level_name(need), raised); else snprintf(out->why, sizeof(out->why), "%s has %s on %s %s", user, authz_level_name(want), r, nref); @@ -667,17 +555,17 @@ void authz_check(const policy_t *p, const char *user, const char *repo, user, r, nref, authz_level_name(want)); else if (s.here == LV_NONE) snprintf(out->why, sizeof(out->why), - "%s has %s on %s, and nothing at all on %s; it needs %s%s%s%s", + "%s holds %s on %s, but not on %s; that ref needs %s%s%s%s", user, authz_level_name(s.strongest), r, nref, authz_level_name(want), - raised[0] != '\0' ? " (raised: " : "", + raised[0] != '\0' ? " (" : "", raised[0] != '\0' ? raised : "", raised[0] != '\0' ? ")" : ""); else if (raised[0] != '\0') snprintf(out->why, sizeof(out->why), - "%s has %s on %s %s; it needs %s (raised from %s: %s)", + "%s has %s on %s %s; it needs %s (%s)", user, authz_level_name(s.here), r, nref, - authz_level_name(want), authz_level_name(need), raised); + authz_level_name(want), raised); else snprintf(out->why, sizeof(out->why), "%s has %s on %s %s; it needs %s", user, authz_level_name(s.here), r, nref, authz_level_name(want)); diff --git a/src/validate.c b/src/validate.c index ab8348c..81865d5 100644 --- a/src/validate.c +++ b/src/validate.c @@ -456,8 +456,7 @@ int cmd_validate(const policy_t *p, validate_opts o) p->path, p->errors); return 3; } - vok("%s: %zu rule(s), protected refs: %s", p->path, p->nrules, - authz_protected_for(p, "*")); + vok("%s: %zu rule(s)", p->path, p->nrules); if (stat(p->path, &st) == 0 && (st.st_mode & 0004) == 0) vwarn("%s is not readable by anyone outside its owner and group: the hook " diff --git a/tests/run.sh b/tests/run.sh index a1c687b..1d8aa7d 100644 --- a/tests/run.sh +++ b/tests/run.sh @@ -134,10 +134,9 @@ section "fixture" cat >"$TMP/authz.conf" <<'EOF' # who may move which refs -set protected-refs refs/heads/main refs/tags/* - alice * admin # the one account that can do anything -@all * write refs/heads/* # anyone the policy names: their own branches +@all * write refs/heads/feature/* # anyone the policy names: working branches +frank team write refs/heads/main refs/tags/* # releases, and nothing else bob team write refs/heads/feature/* carol team force refs/heads/feature/* dave team admin @@ -203,7 +202,7 @@ out=$(do_check dave team write refs/heads/anything); rc=$? expect "admin answers a write too" 0 "allow" "$rc" "$out" out=$(POLICY="$TMP/plain.conf" do_check erin other write refs/heads/dev); rc=$? expect "a stranger to the repository" 1 "no rule for other" "$rc" "$out" -out=$(do_check erin other write refs/heads/dev); rc=$? +out=$(do_check erin other write refs/heads/feature/x); rc=$? expect "unless '@all' reaches that repository too" 0 "allow" "$rc" "$out" section "check: names mean what they say" @@ -211,13 +210,13 @@ section "check: names mean what they say" out=$(do_check bob team.git write refs/heads/feature/x); rc=$? expect "team.git is team" 0 "allow" "$rc" "$out" out=$(do_check bob team write main); rc=$? -expect "a bare name is a branch, and a protected one" 1 "protected" "$rc" "$out" +expect "a bare name is a branch" 1 "refs/heads/main" "$rc" "$out" out=$(do_check bob team write heads/main); rc=$? -expect "heads/main is the same branch" 1 "protected" "$rc" "$out" +expect "heads/main is the same branch" 1 "refs/heads/main" "$rc" "$out" out=$(do_check bob team write refs/heads/main); rc=$? -expect "and so is refs/heads/main" 1 "protected" "$rc" "$out" +expect "and so is refs/heads/main" 1 "refs/heads/main" "$rc" "$out" out=$(do_check bob team write v1); rc=$? -expect "a bare v1 is a branch, never a tag" 0 "refs/heads/v1" "$rc" "$out" +expect "a bare v1 is a branch, never a tag" 1 "refs/heads/v1" "$rc" "$out" out=$(do_check bob team write tags/v1); rc=$? expect "and it takes tags/v1 to mean a tag" 1 "refs/tags/v1" "$rc" "$out" out=$(do_check dave team write refs/tags/v1); rc=$? @@ -225,18 +224,24 @@ expect "an admin can tag" 0 "allow" "$rc" "$out" out=$(do_check carol team write refs/heads/feature/deep/nested); rc=$? expect "* crosses a slash" 0 "allow" "$rc" "$out" -section "check: protected refs cost more" +section "check: a rule covers what it names, and nothing else" out=$(do_check bob team write refs/heads/main); rc=$? -expect "write on main costs force" 1 "raised from write" "$rc" "$out" -out=$(do_check carol team write refs/heads/main); rc=$? -expect "carol's force is on feature branches, not on main" 1 "carol has write on team refs/heads/main" "$rc" "$out" +expect "bob's rules never name main" 1 "but not on refs/heads/main" "$rc" "$out" out=$(do_check carol team force refs/heads/main); rc=$? -expect "force on a protected ref costs admin, wherever it is held" 1 "needs admin (raised" "$rc" "$out" +expect "and neither do carol's, at any level" 1 "but not on refs/heads/main" "$rc" "$out" +out=$(do_check frank team write refs/heads/main); rc=$? +expect "frank's rule does name it, so he may move it" 0 "allow" "$rc" "$out" +out=$(do_check frank team write refs/heads/feature/x); rc=$? +expect "and he reaches the '@all' rule like anyone else named" 0 "allow" "$rc" "$out" +out=$(do_check frank team force refs/heads/main); rc=$? +expect "naming a ref is not the same as holding every level on it" 1 "needs force" "$rc" "$out" out=$(do_check dave team force refs/heads/main); rc=$? -expect "admin pays nothing extra" 0 "allow" "$rc" "$out" +expect "an unscoped admin covers main too" 0 "allow" "$rc" "$out" out=$(do_check bob team write refs/tags/v1); rc=$? -expect "tags are protected too" 1 "protected" "$rc" "$out" +expect "tags have to be named as well" 1 "refs/tags/v1" "$rc" "$out" +out=$(do_check frank team write refs/tags/v1); rc=$? +expect "and frank named his" 0 "allow" "$rc" "$out" section "check: refs that are neither branch nor tag" @@ -249,7 +254,7 @@ expect "notes are for admins" 0 "allow" "$rc" "$out" section "check: '@all' is everyone the policy names" -out=$(do_check carol web write refs/heads/any); rc=$? +out=$(do_check carol web write refs/heads/feature/any); rc=$? expect "a named user reaches the @all rule" 0 "allow" "$rc" "$out" out=$(do_check nobody team write refs/heads/dev); rc=$? expect "a name the policy does not have gets nothing" 1 "no rule" "$rc" "$out" @@ -311,7 +316,8 @@ out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/feature/x | expect "deleting a branch she holds force on is allowed" 0 "-" "$rc" "$out" out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/dev | push carol team --head=refs/heads/other); rc=$? -expect "and only force on that ref will do, not force somewhere else" 1 "carol has write on team refs/heads/dev" "$rc" "$out" +expect "and only force on that ref will do, not force somewhere else" 1 \ + "carol holds force on team, but not on refs/heads/dev" "$rc" "$out" out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/dev | push dave team --head=refs/heads/dev); rc=$? @@ -365,14 +371,15 @@ out=$(printf '%s %s %s\n%s %s %s\n' \ "$ZERO" "$A" refs/heads/feature/one \ "$ZERO" "$B" refs/heads/main | push bob team); rc=$? -expect "one protected ref stops the whole push" 1 "refs/heads/main; it needs force" "$rc" "$out" +expect "one ref nobody may stop the whole push" 1 "refs/heads/main" "$rc" "$out" expect_not "and the refs before it are not reported either" 1 "feature/one" "$rc" "$out" out=$(printf '%s %s %s\n%s %s %s\n' \ "$ZERO" "$A" refs/heads/main \ "$ZERO" "$B" refs/heads/feature/one | push bob team); rc=$? -expect "the refusal says who, what and what it would take" 1 "bob has write" "$rc" "$out" +expect "the refusal says who, what and what it would take" 1 \ + "bob holds write on team, but not on refs/heads/main" "$rc" "$out" expect_not "and stops at the first refusal" 1 "feature/one" "$rc" "$out" section "authorize: input that is not a push" @@ -398,7 +405,8 @@ expect "the hook name selects authorize, and git's argument is ignored" 0 "-" "$ out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/release | POLICY="$TMP/plain.conf" hook bob "$TMP/root/team.git"); rc=$? -expect "and denies what authorize denies, on the repository it found" 1 "bob has write on team" "$rc" "$out" +expect "and denies what authorize denies, on the repository it found" 1 \ + "bob holds write on team, but not on refs/heads/release" "$rc" "$out" out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x | POLICY="$TMP/plain.conf" hook none "$TMP/root/team.git"); rc=$? @@ -580,12 +588,10 @@ expect "warnings alone still let the host serve" 0 "is in force" "$rc" "$out" # -------------------------------------------------------------- edit (grant) -section "grant, revoke, protect" +section "grant and revoke" cat >"$TMP/edit.conf" <<'EOF' # policy, kept in git and rolled out as a ConfigMap -set protected-refs refs/heads/main - alice * admin # break glass bob team write refs/heads/feature/* EOF @@ -602,7 +608,7 @@ else fi out=$("$AUTHZ" --policy "$TMP/edit.conf" grant bob team admin 2>&1); rc=$? -expect "grant replaces the pair's rule" 0 "replaced line 5" "$rc" "$out" +expect "grant replaces the pair's rule" 0 "replaced line 3" "$rc" "$out" n=$(grep -c '^bob' "$TMP/edit.conf") [ "$n" = 1 ] && ok || bad "bob's rule is not collapsed into one line ($n lines)" out=$("$AUTHZ" --policy "$TMP/edit.conf" check bob team admin refs/heads/main 2>&1); rc=$? @@ -615,16 +621,16 @@ expect "and the access is gone" 1 "no rule" "$rc" "$out" out=$("$AUTHZ" --policy "$TMP/edit.conf" revoke bob team 2>&1); rc=$? expect "revoking nothing reports nothing" 1 "no rule for bob" "$rc" "$out" -out=$("$AUTHZ" --policy "$TMP/edit.conf" protect refs/heads/main refs/heads/stable 2>&1); rc=$? -expect "protect rewrites the setting" 0 "line 2" "$rc" "$out" -out=$("$AUTHZ" --policy "$TMP/edit.conf" check alice team force refs/heads/stable 2>&1); rc=$? -expect "the new protected ref is in force" 0 "allow" "$rc" "$out" -out=$("$AUTHZ" --policy "$TMP/edit.conf" protect --repo team refs/heads/release 2>&1); rc=$? -expect "protect can name one repository" 0 "protected-refs.team" "$rc" "$out" -if grep -q '^set protected-refs.team refs/heads/release$' "$TMP/edit.conf"; then +out=$("$AUTHZ" --policy "$TMP/edit.conf" grant frank team write refs/heads/main refs/tags/release/* 2>&1); rc=$? +expect "grant can name several refs" 0 "granted frank" "$rc" "$out" +out=$("$AUTHZ" --policy "$TMP/edit.conf" check frank team write refs/heads/main 2>&1); rc=$? +expect "and the list is in force" 0 "allow" "$rc" "$out" +out=$("$AUTHZ" --policy "$TMP/edit.conf" check frank team write refs/heads/dev 2>&1); rc=$? +expect "and covers nothing else of his own" 1 "but not on refs/heads/dev" "$rc" "$out" +if grep -q '^frank team write refs/heads/main refs/tags/release/\*$' "$TMP/edit.conf"; then ok else - bad "the per-repository setting is not the line it should be" + bad "the refs did not land on one line as written" fi out=$("$AUTHZ" --policy "$TMP/edit.conf" grant bob team nope 2>&1); rc=$? @@ -633,8 +639,17 @@ out=$("$AUTHZ" --policy "$TMP/edit.conf" grant bob group/team write 2>&1); rc=$? expect "grant rejects a repository name with a slash in it" 64 "invalid repo pattern" "$rc" "$out" out=$("$AUTHZ" --policy "$TMP/edit.conf" grant bob team write '!refs/heads/main' 2>&1); rc=$? expect "grant has no deny" 64 "no deny" "$rc" "$out" -out=$("$AUTHZ" --policy "$TMP/edit.conf" protect 2>&1); rc=$? -expect "protect wants a pattern" 64 "wants" "$rc" "$out" +out=$("$AUTHZ" --policy "$TMP/edit.conf" protect refs/heads/main 2>&1); rc=$? +expect "protect is gone, and says it is unknown" 64 "unknown command" "$rc" "$out" +printf 'set protected-refs refs/heads/main\n' >>"$TMP/edit.conf" +out=$("$AUTHZ" --policy "$TMP/edit.conf" check alice team admin refs/heads/main 2>&1); rc=$? +expect "a policy that still carries protected-refs is refused, not ignored" 3 "no longer exists" "$rc" "$out" +out=$("$AUTHZ" --policy "$TMP/edit.conf" validate --no-host 2>&1); rc=$? +expect "and validate says every push is denied" 3 "not used" "$rc" "$out" +grep -v 'protected-refs' "$TMP/edit.conf" > "$TMP/edit.conf.clean" && + cp "$TMP/edit.conf.clean" "$TMP/edit.conf" +out=$("$AUTHZ" --policy "$TMP/edit.conf" validate --no-host 2>&1); rc=$? +expect "cleaned, it validates again" 0 "is in force" "$rc" "$out" cp "$TMP/edit.conf" "$TMP/edit.conf.orig" out=$("$AUTHZ" --policy "$TMP/broken.conf" grant bob team write 2>&1); rc=$? @@ -654,9 +669,9 @@ out=$("$AUTHZ" --policy "$TMP/edit.conf" grant erin secrets admin --stdout 2>&1 expect "--stdout reports on stderr" 0 "untouched" "$rc" "$out" cp "$TMP/edit.conf" "$TMP/edit.conf.bak" out=$("$AUTHZ" --policy "$TMP/edit.conf" revoke carol team --stdout 2>/dev/null); rc=$? -expect "--stdout prints the new file" 0 "set protected-refs" "$rc" "$out" +expect "--stdout prints the new file" 0 "alice * admin" "$rc" "$out" cmp -s "$TMP/edit.conf" "$TMP/edit.conf.bak" && ok || bad "--stdout modified the policy" -if contains "$out" "set protected-refs" && contains "$out" "break glass"; then +if contains "$out" "alice * admin" && contains "$out" "break glass"; then ok else bad "--stdout lost the comments or the settings" @@ -774,7 +789,7 @@ if [ -f authz.conf ]; then out=$("$AUTHZ" --policy authz.conf check bob team write refs/heads/feature/x 2>&1); rc=$? expect "bob works on features" 0 "allow" "$rc" "$out" out=$("$AUTHZ" --policy authz.conf check bob team write refs/heads/main 2>&1); rc=$? - expect "bob cannot touch main" 1 "protected" "$rc" "$out" + expect "bob cannot touch main" 1 "but not on refs/heads/main" "$rc" "$out" out=$("$AUTHZ" --policy authz.conf check nobody team write refs/heads/x 2>&1); rc=$? expect "a stranger cannot" 1 "no rule" "$rc" "$out" fi diff --git a/tests/unit.c b/tests/unit.c index 0d21ee3..cf55282 100644 --- a/tests/unit.c +++ b/tests/unit.c @@ -237,8 +237,7 @@ static void test_parse(void) " \n" "alice web admin # trailing comment\n" "bob web write refs/heads/feature/* refs/heads/dev\n" - "@all team write main\n" - "set protected-refs main refs/tags/*\n"); + "@all team write main\n"); CHECK_INT(rc, 0); CHECK_INT(p.errors, 0); CHECK_INT(p.nrules, 3); @@ -250,7 +249,6 @@ static void test_parse(void) CHECK_STR(p.rules[1].refs[0], "refs/heads/feature/*"); CHECK_INT(p.rules[1].nrefs, 2); CHECK_STR(p.rules[2].refs[0], "refs/heads/main"); /* normalised at parse time */ - CHECK_STR(p.protected_global, "refs/heads/main refs/tags/*"); policy_free(&p); /* Errors: each of these must stop the file being used at all. The complaints @@ -288,7 +286,14 @@ static void test_parse(void) CHECK_INT(rc, -1); policy_free(&p); - rc = load_text(&p, "set protected-refs\n"); + /* Removed settings are refused by name, not ignored: a policy that + * still carries one would otherwise mean "everyone with write can fast + * -forward main" while looking like it said the opposite. */ + rc = load_text(&p, "set protected-refs refs/heads/main\n"); + CHECK_INT(rc, -1); + policy_free(&p); + + rc = load_text(&p, "set protected-refs.web refs/heads/main\n"); CHECK_INT(rc, -1); policy_free(&p); @@ -301,19 +306,10 @@ static void test_parse(void) close(keep); } - /* The default protected list, and the two ways to replace it. */ + /* There is no settings list any more: a rule says what it says. */ rc = load_text(&p, "alice * admin\n"); CHECK_INT(rc, 0); - CHECK_STR(authz_protected_for(&p, "web"), "refs/heads/main"); - policy_free(&p); - - rc = load_text(&p, - "set protected-refs refs/heads/main refs/heads/stable\n" - "set protected-refs.web refs/heads/release\n" - "alice * admin\n"); - CHECK_INT(rc, 0); - CHECK_STR(authz_protected_for(&p, "web"), "refs/heads/release"); - CHECK_STR(authz_protected_for(&p, "other"), "refs/heads/main refs/heads/stable"); + CHECK_INT(p.nrules, 1); policy_free(&p); } @@ -347,35 +343,32 @@ static void test_known_users(void) static void test_need_for(void) { - policy_t p; char raised[64]; - int rc; - rc = load_text(&p, - "set protected-refs refs/heads/main refs/tags/*\n" - "alice * admin\n"); - CHECK_INT(rc, 0); - CHECK_INT(authz_need_for(&p, "web", "refs/heads/dev", LV_WRITE, raised, - sizeof(raised)), LV_WRITE); + /* A branch or a tag costs exactly what the rule said: there is no list + * elsewhere in the file that can raise the price of one. */ + CHECK_INT(authz_need_for("refs/heads/dev", LV_WRITE, raised, sizeof(raised)), + LV_WRITE); CHECK_STR(raised, ""); - CHECK_INT(authz_need_for(&p, "web", "refs/heads/main", LV_WRITE, raised, - sizeof(raised)), LV_FORCE); - CHECK_STR(raised, "protected"); - CHECK_INT(authz_need_for(&p, "web", "refs/heads/main", LV_FORCE, raised, - sizeof(raised)), LV_ADMIN); - CHECK_INT(authz_need_for(&p, "web", "refs/heads/main", LV_ADMIN, raised, - sizeof(raised)), LV_ADMIN); - CHECK_STR(raised, ""); /* admin is already the top */ - CHECK_INT(authz_need_for(&p, "web", "refs/tags/v1", LV_WRITE, raised, - sizeof(raised)), LV_FORCE); - CHECK_INT(authz_need_for(&p, "web", "refs/pull/1/head", LV_WRITE, raised, - sizeof(raised)), LV_ADMIN); + CHECK_INT(authz_need_for("refs/heads/main", LV_WRITE, raised, sizeof(raised)), + LV_WRITE); + CHECK_INT(authz_need_for("refs/tags/v1", LV_WRITE, raised, sizeof(raised)), + LV_WRITE); + CHECK_INT(authz_need_for("refs/heads/feature/x", LV_FORCE, raised, sizeof(raised)), + LV_FORCE); + + /* Anything outside refs/heads and refs/tags is for maintainers, whatever a + * rule appears to grant about it. */ + CHECK_INT(authz_need_for("refs/pull/1/head", LV_WRITE, raised, sizeof(raised)), + LV_ADMIN); CHECK_STR(raised, "neither a branch nor a tag"); - CHECK_INT(authz_need_for(&p, "web", "refs/pull/1/head", LV_FORCE, raised, - sizeof(raised)), LV_ADMIN); - CHECK_INT(authz_need_for(&p, "web", "refs/notes/x", LV_ADMIN, raised, - sizeof(raised)), LV_ADMIN); - policy_free(&p); + CHECK_INT(authz_need_for("refs/pull/1/head", LV_FORCE, raised, sizeof(raised)), + LV_ADMIN); + CHECK_INT(authz_need_for("refs/notes/x", LV_WRITE, raised, sizeof(raised)), + LV_ADMIN); + CHECK_INT(authz_need_for("refs/notes/x", LV_ADMIN, raised, sizeof(raised)), + LV_ADMIN); + CHECK_STR(raised, ""); /* admin is already the top */ } static void test_check(void) @@ -384,9 +377,9 @@ static void test_check(void) int rc; rc = load_text(&p, - "set protected-refs refs/heads/main refs/tags/*\n" "alice * admin\n" - "@all * write refs/heads/*\n" + "@all * write refs/heads/feature/*\n" + "frank team write refs/heads/main refs/tags/*\n" "bob team write refs/heads/feature/*\n" "carol team force refs/heads/feature/*\n" "dave team admin\n" @@ -407,19 +400,27 @@ static void test_check(void) /* '@all' reaches the named, and nobody else. Note that the '@all *' rule * below reaches every repository, so a named user is not confined to the * repository their own rule names - that is what the rule says. */ - expect_allow(&p, "carol", "web", LV_WRITE, "refs/heads/any"); - expect_allow(&p, "erin", "web", LV_WRITE, "refs/heads/any"); + expect_allow(&p, "carol", "web", LV_WRITE, "refs/heads/feature/any"); + expect_allow(&p, "erin", "web", LV_WRITE, "refs/heads/feature/any"); expect_deny(&p, "nobody", "web", LV_WRITE, "refs/heads/any", "no rule"); expect_deny(&p, "", "web", LV_WRITE, "refs/heads/any", "no identity"); expect_deny(&p, NULL, "web", LV_WRITE, "refs/heads/any", "no identity"); - /* protected refs raise the cost, and say so */ - expect_deny(&p, "bob", "team", LV_WRITE, "refs/heads/main", "raised from write"); - expect_deny(&p, "carol", "team", LV_FORCE, "refs/heads/main", "raised from force"); - /* the raise applies to the level the action asked for, not to the best - * level the user happens to hold somewhere else */ - expect_deny(&p, "carol", "team", LV_WRITE, "refs/heads/main", "needs force"); + /* main is not special: it is refused here because no rule of bob's or + * carol's names it, and allowed for dave because his names everything. */ + expect_deny(&p, "bob", "team", LV_WRITE, "refs/heads/main", + "but not on refs/heads/main"); + expect_deny(&p, "carol", "team", LV_FORCE, "refs/heads/main", + "but not on refs/heads/main"); expect_allow(&p, "dave", "team", LV_WRITE, "refs/heads/main"); + /* and the person whose rule does name main, and nothing else, gets main */ + expect_allow(&p, "frank", "team", LV_WRITE, "refs/heads/main"); + /* frank reaches the '@all' rule like anyone else the policy names, so what + * he lacks is a branch nobody's rule names, and a rewrite of his own */ + expect_allow(&p, "frank", "team", LV_WRITE, "refs/heads/feature/x"); + expect_deny(&p, "frank", "team", LV_WRITE, "refs/heads/dev", + "but not on refs/heads/dev"); + expect_deny(&p, "frank", "team", LV_FORCE, "refs/heads/main", "needs force"); /* refs that are not branches or tags: only an admin, at any request level */ expect_allow(&p, "dave", "team", LV_WRITE, "refs/pull/1/head"); @@ -427,9 +428,9 @@ static void test_check(void) expect_deny(&p, "bob", "team", LV_ADMIN, "refs/notes/x", "needs admin"); /* the same branch, three spellings */ - expect_deny(&p, "bob", "team", LV_WRITE, "main", "protected"); - expect_deny(&p, "bob", "team", LV_WRITE, "heads/main", "protected"); - expect_deny(&p, "bob", "team", LV_WRITE, "refs/heads/main", "protected"); + expect_deny(&p, "bob", "team", LV_WRITE, "main", "but not on refs/heads/main"); + expect_deny(&p, "bob", "team", LV_WRITE, "heads/main", + "but not on refs/heads/main"); /* repository matching: .git is the same repository */ expect_allow(&p, "bob", "team.git", LV_WRITE, "refs/heads/feature/x"); @@ -465,14 +466,14 @@ static void test_scope_edges(void) policy_t p; int rc; - /* a rule with no refs is the whole repository, protected refs included */ + /* a rule with no refs is the whole repository: every branch, main included, + * which is exactly why naming the refs is the honest way to write one */ rc = load_text(&p, "bob team write\n"); CHECK_INT(rc, 0); expect_allow(&p, "bob", "team", LV_WRITE, "refs/heads/anything"); - expect_deny(&p, "bob", "team", LV_WRITE, "refs/heads/main", "protected"); - /* write on everything still is not force: the raise asks for the next step - * up, and bob never got it */ - expect_deny(&p, "bob", "team", LV_FORCE, "refs/heads/main", "needs admin"); + expect_allow(&p, "bob", "team", LV_WRITE, "refs/heads/main"); + /* write on everything still is not force */ + expect_deny(&p, "bob", "team", LV_FORCE, "refs/heads/main", "needs force"); policy_free(&p); /* a bare name is a branch, and only a branch */ @@ -496,18 +497,17 @@ static void test_scope_edges(void) expect_deny(&p, "bob", "team", LV_FORCE, "refs/heads/dev", "bob has write on team refs/heads/dev; it needs force"); expect_deny(&p, "bob", "team", LV_WRITE, "refs/heads/other", - "nothing at all on refs/heads/other"); + "but not on refs/heads/other"); policy_free(&p); - /* per-repository protection replaces the global list, it does not add to it */ - rc = load_text(&p, - "set protected-refs refs/heads/main\n" - "set protected-refs.web refs/heads/release\n" - "bob web write refs/heads/*\n"); + /* naming refs in a rule is the whole of the mechanism: what is not named is + * not granted, and the refusal says so rather than blaming a list */ + rc = load_text(&p, "bob web write refs/heads/feature/* refs/heads/dev\n"); CHECK_INT(rc, 0); - expect_allow(&p, "bob", "web", LV_WRITE, "refs/heads/main"); - expect_deny(&p, "bob", "web", LV_WRITE, "refs/heads/release", "protected"); - expect_deny(&p, "bob", "api", LV_WRITE, "refs/heads/release", "no rule"); + expect_allow(&p, "bob", "web", LV_WRITE, "refs/heads/dev"); + expect_allow(&p, "bob", "web", LV_WRITE, "refs/heads/feature/x"); + expect_deny(&p, "bob", "web", LV_WRITE, "refs/heads/main", "but not on"); + expect_deny(&p, "bob", "api", LV_WRITE, "refs/heads/dev", "no rule"); policy_free(&p); /* reachable-by-nobody detection, which validate reports */ |
