summaryrefslogtreecommitdiffstats
path: root/test/check.c
blob: 4db4b9ef90c098a815a5cc84ae40bb1c499852e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// The decision, and the sentence that goes with it: a refusal names the level
// held, the ref, and the level required.
#include "test.h"

static void test_check(void)
{
    policy_t p;
    int rc;

    rc = load_text(&p,
        "alice * admin\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"
        "erin  secrets admin\n");
    CHECK_INT(rc, 0);

    // the ladder
    expect_allow(&p, "bob", "team", LV_WRITE, "refs/heads/feature/x");
    expect_deny(&p, "bob", "team", LV_FORCE, "refs/heads/feature/x", "needs force");
    expect_deny(&p, "bob", "team", LV_ADMIN, "refs/heads/feature/x", "needs admin");
    expect_allow(&p, "carol", "team", LV_FORCE, "refs/heads/feature/x");
    expect_allow(&p, "dave", "team", LV_ADMIN, "refs/heads/main");

    // strongest match wins, additively
    expect_allow(&p, "dave", "team", LV_WRITE, "refs/heads/feature/x");
    expect_allow(&p, "alice", "anything", LV_ADMIN, "refs/heads/main");

    // '@all' reaches the named and nobody else.  The '@all *' rule reaches every
    // repository, so a named user is not confined to their own rule's repo.

    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");

    // main is not special: refused because no rule of theirs names it, 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 '@all' like anyone named, 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");
    expect_allow(&p, "dave", "team", LV_ADMIN, "refs/pull/1/head");
    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", "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");
    expect_allow(&p, "erin", "secrets.git", LV_ADMIN, "refs/heads/main");

    // the question without a ref
    expect_allow(&p, "dave", "team", LV_FORCE, NULL);
    expect_allow(&p, "alice", "team", LV_ADMIN, NULL);
    expect_allow(&p, "bob", "team", LV_WRITE, NULL);
    expect_deny(&p, "bob", "team", LV_FORCE, NULL, "no force");
    expect_allow(&p, "erin", "team", LV_WRITE, NULL);      // via '@all *'
    expect_deny(&p, "nobody", "team", LV_WRITE, NULL, "no write");

    // what a denial names: the level held, and the level needed
    {
        decision_t d;

        authz_check(&p, "bob", "team", LV_ADMIN, "refs/heads/feature/x", &d);
        CHECK(!d.allowed);
        CHECK_INT(d.need, LV_ADMIN);
        CHECK(strstr(d.why, "bob has write") != NULL);
        CHECK(strstr(d.why, "needs admin") != NULL);

        authz_check(&p, "nobody", "team", LV_WRITE, "refs/heads/dev", &d);
        CHECK(!d.allowed);
        CHECK(strstr(d.why, "nobody has no rule for team") != NULL);
    }
    policy_free(&p);
}

static void test_scope_edges(void)
{
    policy_t p;
    int rc;

    // 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_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
    rc = load_text(&p, "bob web write refs/heads/*\n");
    CHECK_INT(rc, 0);
    expect_allow(&p, "bob", "web", LV_WRITE, "v1");                 // refs/heads/v1
    expect_deny(&p, "bob", "web", LV_WRITE, "refs/tags/v1", "needs write");
    expect_deny(&p, "bob", "web", LV_WRITE, "tags/v1", "needs write");
    policy_free(&p);

    // a user may hold several rules for one repository; all of them count
    rc = load_text(&p,
        "bob team write refs/heads/dev\n"
        "bob team force refs/heads/feature/*\n");
    CHECK_INT(rc, 0);
    expect_allow(&p, "bob", "team", LV_WRITE, "refs/heads/dev");
    expect_allow(&p, "bob", "team", LV_FORCE, "refs/heads/feature/x");
    // A refusal names the level held on the ref in question, not the strongest
    // level held somewhere in the repository: "you have force here" next to a
    // refusal is a sentence nobody can act on.
    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",
                "but not on refs/heads/other");
    policy_free(&p);

    // 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/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
    rc = load_text(&p, "bob team write\n");
    CHECK_INT(rc, 0);
    CHECK(authz_repo_reachable(&p, "team"));
    CHECK(authz_repo_reachable(&p, "team.git"));
    CHECK(!authz_repo_reachable(&p, "other"));
    policy_free(&p);

    rc = load_text(&p, "bob * write\n");
    CHECK_INT(rc, 0);
    CHECK(authz_repo_reachable(&p, "anything"));
    policy_free(&p);
}

int main(void)
{
    test_init("");

    test_check();
    test_scope_edges();

    return test_report("check");
}