psql:连接数据库后如何切换到不同的角色?

问题描述 投票:0回答:3

是否可以更改用户在初始连接后与 postgres 交互时使用的 postgresql 角色?

数据库将在 Web 应用程序中使用,我想在具有连接池的表和模式上采用数据库级别规则。通过阅读 postgresql 文档,如果我最初以具有超级用户角色的用户身份进行连接,我似乎可以切换角色,但我更愿意最初以具有最小权限的用户身份进行连接,并根据需要进行切换。切换时必须指定用户密码就可以了(事实上我更喜欢它)。

我错过了什么?

更新:我已经按照@Milen的建议尝试了

SET ROLE
SET SESSION AUTHORIZATION
,但是如果用户不是超级用户,这两个命令似乎都不起作用:

$ psql -U test
psql (8.4.4)
Type "help" for help.

test=> \du test
          List of roles
 Role name | Attributes |   Member of    
-----------+------------+----------------
 test      |            | {connect_only}

test=> \du test2
          List of roles
 Role name | Attributes |   Member of    
-----------+------------+----------------
 test2     |            | {connect_only}

test=> set role test2;
ERROR:  permission denied to set role "test2"
test=> \q
postgresql psql
3个回答
71
投票
--create a user that you want to use the database as:

create role neil;

--create the user for the web server to connect as:

create role webgui noinherit login password 's3cr3t';

--let webgui set role to neil:

grant neil to webgui; --this looks backwards but is correct.

webgui
现在位于
neil
组中,因此
webgui
可以呼叫
set role neil
。但是,
webgui
并没有继承
neil
的权限。

稍后,以 webgui 身份登录:

psql -d some_database -U webgui
(enter s3cr3t as password)

set role neil;

webgui
不需要
superuser
许可。

您希望在数据库会话开始时

set role
并在会话结束时重置它。在 Web 应用程序中,这分别对应于从数据库连接池获取连接并释放它。这是一个使用 Tomcat 的连接池和 Spring Security 的示例:

public class SetRoleJdbcInterceptor extends JdbcInterceptor {

    @Override
    public void reset(ConnectionPool connectionPool, PooledConnection pooledConnection) {

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        if(authentication != null) {
            try {

                /* 
                  use OWASP's ESAPI to encode the username to avoid SQL Injection. Can't use parameters with SET ROLE. Need to write PG codec.

                  Or use a whitelist-map approach
                */
                String username = ESAPI.encoder().encodeForSQL(MY_CODEC, authentication.getName());

                Statement statement = pooledConnection.getConnection().createStatement();
                statement.execute("set role \"" + username + "\"");
                statement.close();
            } catch(SQLException exp){
                throw new RuntimeException(exp);
            }
        }
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        if("close".equals(method.getName())){
            Statement statement = ((Connection)proxy).createStatement();
            statement.execute("reset role");
            statement.close();
        }

        return super.invoke(proxy, method, args);
    }
}


6
投票

如果有人仍然需要它(就像我一样)。

指定的role_name必须是当前会话用户所属的角色。 https://www.postgresql.org/docs/10/sql-set-role.html

我们需要使当前会话用户成为该角色的成员:

create role myrole;
set role myrole;
grant myrole to myuser;
set role myrole;

产生:

Role ROLE created.


Error starting at line : 4 in command -
set role myrole
Error report -
ERROR: permission denied to set role "myrole"

Grant succeeded.


Role SET succeeded.
© www.soinside.com 2019 - 2024. All rights reserved.