Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(AclFamily): add acl whoami command #1774

Merged
merged 3 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/server/acl/acl_family.cc
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ void AclFamily::DelUser(CmdArgList args, ConnectionContext* cntx) {
cntx->SendOk();
}

void AclFamily::WhoAmI(CmdArgList args, ConnectionContext* cntx) {
cntx->SendSimpleString(absl::StrCat("User is ", cntx->authed_username));
}

using CI = dfly::CommandId;

using MemberFunc = void (AclFamily::*)(CmdArgList args, ConnectionContext* cntx);
Expand All @@ -229,6 +233,7 @@ constexpr uint32_t kAcl = acl::CONNECTION;
constexpr uint32_t kList = acl::ADMIN | acl::SLOW | acl::DANGEROUS;
constexpr uint32_t kSetUser = acl::ADMIN | acl::SLOW | acl::DANGEROUS;
constexpr uint32_t kDelUser = acl::ADMIN | acl::SLOW | acl::DANGEROUS;
constexpr uint32_t kWhoAmI = acl::SLOW;

// We can't implement the ACL commands and its respective subcommands LIST, CAT, etc
// the usual way, (that is, one command called ACL which then dispatches to the subcommand
Expand All @@ -245,6 +250,8 @@ void AclFamily::Register(dfly::CommandRegistry* registry) {
.HFUNC(SetUser);
*registry << CI{"ACL DELUSER", CO::ADMIN | CO::NOSCRIPT | CO::LOADING, 2, 0, 0, 0, acl::kDelUser}
.HFUNC(DelUser);
*registry << CI{"ACL WHOAMI", CO::ADMIN | CO::NOSCRIPT | CO::LOADING, 1, 0, 0, 0, acl::kWhoAmI}
.HFUNC(WhoAmI);
}

#undef HFUNC
Expand Down
1 change: 1 addition & 0 deletions src/server/acl/acl_family.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class AclFamily final {
void List(CmdArgList args, ConnectionContext* cntx);
void SetUser(CmdArgList args, ConnectionContext* cntx);
void DelUser(CmdArgList args, ConnectionContext* cntx);
void WhoAmI(CmdArgList args, ConnectionContext* cntx);

// Helper function that updates all open connections and their
// respective ACL fields on all the available proactor threads
Expand Down
36 changes: 25 additions & 11 deletions tests/dragonfly/acl_family_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,22 +196,20 @@ async def test_acl_categories_multi_exec_squash(df_local_factory):
async def test_acl_deluser(df_server):
client = aioredis.Redis(port=df_server.port)

try:
res = await client.execute_command("ACL DELUSER adi")
except redis.exceptions.ResponseError as e:
assert e.args[0] == "User adi does not exist"
with pytest.raises(redis.exceptions.ResponseError):
await client.execute_command("ACL DELUSER random")

res = await client.execute_command("ACL SETUSER adi ON >pass +@transaction +@string")
res = await client.execute_command("ACL SETUSER george ON >pass +@transaction +@string")
assert res == b"OK"

res = await client.execute_command("AUTH adi pass")
res = await client.execute_command("AUTH george pass")
assert res == b"OK"

await client.execute_command("MULTI")
await client.execute_command("SET key 44")

admin_client = aioredis.Redis(port=df_server.port)
await admin_client.execute_command("ACL DELUSER adi")
await admin_client.execute_command("ACL DELUSER george")

with pytest.raises(redis.exceptions.ConnectionError):
await client.execute_command("EXEC")
Expand All @@ -220,7 +218,7 @@ async def test_acl_deluser(df_server):


script = """
for i = 1, 10000 do
for i = 1, 100000 do
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I slightly increased this to avoid flakes but what I run locally won't necessary reflect the gh runners since the machines we are running on the regressions tests are far more powerful than mine

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They're less powerful. The flakes I was worried about is that it setuser migth run to quickly

redis.call('SET', 'key', i)
redis.call('SET', 'key1', i)
redis.call('SET', 'key2', i)
Expand All @@ -244,7 +242,7 @@ async def test_acl_del_user_while_running_lua_script(df_server):

for i in range(1, 4):
res = await admin_client.get(f"key{i}")
assert res == b"10000"
assert res == b"100000"

await admin_client.close()

Expand All @@ -258,12 +256,28 @@ async def test_acl_with_long_running_script(df_server):

await asyncio.gather(
client.eval(script, 4, "key", "key1", "key2", "key3"),
admin_client.execute_command("ACL SETUSER -@string -@scripting"),
admin_client.execute_command("ACL SETUSER roman -@string -@scripting"),
)

for i in range(1, 4):
res = await admin_client.get(f"key{i}")
assert res == b"10000"
assert res == b"100000"

await client.close()
await admin_client.close()


@pytest.mark.asyncio
async def test_acl_whoami(async_client):
await async_client.execute_command("ACL SETUSER kostas >kk +@ALL ON")

with pytest.raises(redis.exceptions.ResponseError):
await async_client.execute_command("ACL WHOAMI WHO")

result = await async_client.execute_command("ACL WHOAMI")
assert result == "User is default"

result = await async_client.execute_command("AUTH kostas kk")

result = await async_client.execute_command("ACL WHOAMI")
assert result == "User is kostas"
Comment on lines +270 to +283
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please stop using pytests 😆

Copy link
Collaborator

@romange romange Aug 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. This type of logic we test with unit tests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I promise I will move these to units next week after the release ❤️

Loading