Skip to content

Clarify the box.session.su() description #5062

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

Open
xuniq opened this issue Apr 4, 2025 · 0 comments · May be fixed by #5065
Open

Clarify the box.session.su() description #5062

xuniq opened this issue Apr 4, 2025 · 0 comments · May be fixed by #5065
Assignees
Labels
reference [location] Tarantool manual, Reference part

Comments

@xuniq
Copy link
Contributor

xuniq commented Apr 4, 2025

Product: Tarantool
Root document: https://www.tarantool.io/en/doc/latest/reference/reference_lua/box_session/su/
SME: @ Totktonada @ sergepetrenko @ a1div0
Jira ticket: https://jira.vk.team/browse/TNTP-2284

Details

@ a1div0 :

Тип аргумента №2
Согласно описанию вторым аргументом может быть название функции или сама функция:
"... function-to-execute – имя функции или определение функции ..."

Однако по факту метод всегда требует функцию:

g.test_bug_2284_need_func = function()
    rawset(_G, "get_current_user", function()
        return box.session.user()
    end)

    t.assert_error_msg_contains("expected function as 2 argument",
        box.session.su, "unittestuser", "get_current_user")
end

Временное переключение на пользователя не работает
Вот этот тест не сработает:

g.test_bug_2284_temporary_user_switch = function()
    rawset(_G, "get_current_user", function()
        return box.session.user()
    end)

    local reference_user = box.session.user()
    t.assert_not_equals(reference_user, "unittestuser")

    local func = rawget(_G, "get_current_user")
    local res = box.session.su("unittestuser", func)
    t.assert_equals(res, "unittestuser") -- << -- Упадёт тут, в res будет исходный пользователь
    t.assert_equals(box.session.user(), reference_user)
end

При этом просто переключение на пользователя работает исправно. Если заменить вызов "box.session.su("unittestuser", func)" на вот это, то всё работает:

function wrapped_su(run_as_user, func_name, ...)
    local func = rawget(_G, func_name)
    local current_user = box.session.user()
    box.session.su(run_as_user)
    local result_list = { pcall(func, ...) }
    box.session.su(current_user)
    local ok = table.remove(result_list, 1)
    if not ok then
        error(result_list[1])
    end

    return unpack(result_list)
end

@ sergepetrenko :

Согласно описанию вторым аргументом может быть название функции или сама функция:
"... function-to-execute – имя функции или определение функции ..."

Не считаю багом. Давайте поправим описание в документации. box.session.su() принимает вторым аргументом объект-функцию. Т.е. либо определение функции (function() ... end), либо, и правда, имя функции, как переменной, только без кавычек:

tarantool> _ = rawset(_G, "get_current_user", function() return box.session.user() end)
---
...

tarantool> box.session.su('unittestuser', get_current_user)
---
- admin
...

tarantool> box.session.su('unittestuser', function() return box.session.user() end)
---
- admin
...

По поводу временного переключения пользователя тоже недостаток документации.
Есть box.session.user() - показывает всегда исходного пользователя, не смотря на box.session.su(), а есть box.session.effective_user() - показывает текущего пользователя, уважает box.session.su(). Упоминания box.session.effective_user() в кокументации я не нашёл:

tarantool> box.session.su('unittestuser', function() return box.session.user() end)
---
- admin
...

tarantool> box.session.su('unittestuser', function() return box.session.effective_user() end)
---
- unittestuser
...

@ Totktonada :

Хочу тоже немного в это вникнуть. Есть ощущение, что некоторые вещи так и должны работать.

Например, su с функцией меняет пользователя в рамках работы функции. После работы функции возвращается старый пользователь. Это, кажется, ок.

@xuniq xuniq added the reference [location] Tarantool manual, Reference part label Apr 4, 2025
@xuniq xuniq self-assigned this Apr 4, 2025
xuniq added a commit that referenced this issue Apr 4, 2025
- Update description in `box.session.su()`, `box.session.user()`, and `box.session.euid()`
- Add `box.session.effective_user()` page

Fixes #5062
@xuniq xuniq linked a pull request Apr 4, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
reference [location] Tarantool manual, Reference part
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant