Skip to content

Commit 0c93fa7

Browse files
authored
feat: Support short and long usernames in rego (#5)
1 parent 009331c commit 0c93fa7

File tree

2 files changed

+52
-25
lines changed

2 files changed

+52
-25
lines changed

rego/hdfs.rego

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ default allow = false
77
# HDFS authorizer
88
allow if {
99
some acl in acls
10-
matches_identity(input.callerUgi.shortUserName, acl.identity)
10+
matches_identity(acl.identity)
1111
matches_resource(input.path, acl.resource)
1212
action_sufficient_for_operation(acl.action, input.operationName)
1313
}
@@ -19,14 +19,19 @@ groups := {group |
1919
group := trim_prefix(raw, "/")
2020
}
2121

22-
# Identity mentions the user explicitly
23-
matches_identity(user, identity) if {
24-
identity == concat("", ["user:", user])
22+
# Identity mentions the (long) userName explicitly
23+
matches_identity(identity) if {
24+
identity == concat("", ["user:", input.callerUgi.userName])
2525
}
2626

27-
# Identity mentions group the user is part of
28-
matches_identity(user, identity) if {
29-
some group in groups_for_user[user]
27+
# Identity mentions the shortUserName explicitly
28+
matches_identity(identity) if {
29+
identity == concat("", ["shortUser:", input.callerUgi.shortUserName])
30+
}
31+
32+
# Identity mentions group the user is part of (by looking up using the (long) userName)
33+
matches_identity(identity) if {
34+
some group in groups_for_user[input.callerUgi.userName]
3035
identity == concat("", ["group:", group])
3136
}
3237

@@ -171,7 +176,11 @@ admin_actions := {
171176
"transitionToStandby": "full",
172177
}
173178

174-
groups_for_user := {"admin": ["admins"], "alice": ["developers"], "bob": []}
179+
groups_for_user := {
180+
"admin/[email protected]": ["admins"],
181+
"alice/[email protected]": ["developers"],
182+
183+
}
175184

176185
acls := [
177186
{
@@ -190,22 +199,27 @@ acls := [
190199
"resource": "hdfs:dir:/developers-ro/",
191200
},
192201
{
193-
"identity": "user:alice",
202+
"identity": "user:alice/[email protected]",
194203
"action": "rw",
195204
"resource": "hdfs:dir:/alice/",
196205
},
197206
{
198-
"identity": "user:bob",
207+
"identity": "user:bob/[email protected]",
199208
"action": "rw",
200209
"resource": "hdfs:dir:/bob/",
201210
},
202211
{
203-
"identity": "user:bob",
212+
"identity": "user:bob/[email protected]",
204213
"action": "ro",
205214
"resource": "hdfs:dir:/developers/",
206215
},
207216
{
208-
"identity": "user:bob",
217+
"identity": "user:bob/[email protected]",
218+
"action": "rw",
219+
"resource": "hdfs:file:/developers/file-from-bob",
220+
},
221+
{
222+
"identity": "shortUser:bob",
209223
"action": "rw",
210224
"resource": "hdfs:file:/developers/file-from-bob",
211225
},

rego/hdfs_test.rego

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import rego.v1
55
test_admin_access_to_slash if {
66
allow with input as {
77
"callerUgi": {
8-
"shortUserName": "admin"
8+
"shortUserName": "admin",
9+
"userName": "admin/[email protected]",
910
},
1011
"path": "/top-level",
1112
"operationName": "setErasureCodingPolicy",
@@ -15,7 +16,8 @@ test_admin_access_to_slash if {
1516
test_admin_access_to_alice if {
1617
allow with input as {
1718
"callerUgi": {
18-
"shortUserName": "admin"
19+
"shortUserName": "admin",
20+
"userName": "admin/[email protected]",
1921
},
2022
"path": "/alice/file",
2123
"operationName": "create",
@@ -26,7 +28,8 @@ test_admin_access_to_alice if {
2628
test_admin_access_to_alice_nested_file if {
2729
allow with input as {
2830
"callerUgi": {
29-
"shortUserName": "admin"
31+
"shortUserName": "admin",
32+
"userName": "admin/[email protected]",
3033
},
3134
"path": "/alice/nested/file",
3235
"operationName": "create",
@@ -36,7 +39,8 @@ test_admin_access_to_alice_nested_file if {
3639
test_admin_access_to_developers if {
3740
allow with input as {
3841
"callerUgi": {
39-
"shortUserName": "admin"
42+
"shortUserName": "admin",
43+
"userName": "admin/[email protected]",
4044
},
4145
"path": "/developers/file",
4246
"operationName": "create",
@@ -48,7 +52,8 @@ test_admin_access_to_developers if {
4852
test_alice_access_to_alice_folder if {
4953
allow with input as {
5054
"callerUgi": {
51-
"shortUserName": "alice"
55+
"shortUserName": "alice",
56+
"userName": "alice/[email protected]",
5257
},
5358
"path": "/alice",
5459
"operationName": "create",
@@ -58,7 +63,8 @@ test_alice_access_to_alice_folder if {
5863
test_alice_access_to_alice if {
5964
allow with input as {
6065
"callerUgi": {
61-
"shortUserName": "alice"
66+
"shortUserName": "alice",
67+
"userName": "alice/[email protected]",
6268
},
6369
"path": "/alice/file",
6470
"operationName": "create",
@@ -68,7 +74,8 @@ test_alice_access_to_alice if {
6874
test_alice_no_access_to_bob if {
6975
not allow with input as {
7076
"callerUgi": {
71-
"shortUserName": "alice"
77+
"shortUserName": "alice",
78+
"userName": "alice/[email protected]",
7279
},
7380
"path": "/bob/file",
7481
"operationName": "open",
@@ -78,7 +85,8 @@ test_alice_no_access_to_bob if {
7885
test_alice_access_to_developers if {
7986
allow with input as {
8087
"callerUgi": {
81-
"shortUserName": "alice"
88+
"shortUserName": "alice",
89+
"userName": "alice/[email protected]",
8290
},
8391
"path": "/developers/file",
8492
"operationName": "create",
@@ -92,7 +100,8 @@ test_alice_access_to_developers if {
92100
test_bob_no_access_to_alice if {
93101
not allow with input as {
94102
"callerUgi": {
95-
"shortUserName": "bob"
103+
"shortUserName": "bob",
104+
"userName": "bob/[email protected]",
96105
},
97106
"path": "/alice/file",
98107
"operationName": "open",
@@ -102,7 +111,8 @@ test_bob_no_access_to_alice if {
102111
test_bob_access_to_bob if {
103112
allow with input as {
104113
"callerUgi": {
105-
"shortUserName": "bob"
114+
"shortUserName": "bob",
115+
"userName": "bob/[email protected]",
106116
},
107117
"path": "/bob/file",
108118
"operationName": "create",
@@ -112,7 +122,8 @@ test_bob_access_to_bob if {
112122
test_bob_ro_access_to_developers if {
113123
allow with input as {
114124
"callerUgi": {
115-
"shortUserName": "bob"
125+
"shortUserName": "bob",
126+
"userName": "bob/[email protected]",
116127
},
117128
"path": "/developers/file",
118129
"operationName": "open",
@@ -122,7 +133,8 @@ test_bob_ro_access_to_developers if {
122133
test_bob_no_rw_access_to_developers if {
123134
not allow with input as {
124135
"callerUgi": {
125-
"shortUserName": "bob"
136+
"shortUserName": "bob",
137+
"userName": "bob/[email protected]",
126138
},
127139
"path": "/developers/file",
128140
"operationName": "create",
@@ -132,7 +144,8 @@ test_bob_no_rw_access_to_developers if {
132144
test_bob_rw_access_to_developers_special_file if {
133145
allow with input as {
134146
"callerUgi": {
135-
"shortUserName": "bob"
147+
"shortUserName": "bob",
148+
"userName": "bob/[email protected]",
136149
},
137150
"path": "/developers/file-from-bob",
138151
"operationName": "create",

0 commit comments

Comments
 (0)