Apache 鳄梨酱使用 postgres 创建用户
Apache guacamole create user using postgres
我使用来自 docker hub 的最新 apache guacamole 和 postgres 12 作为数据库,
我想使用 postgres 创建一个登录用户,但它不起作用。
这是从文档创建用户的方法:
-- Generate salt
SET @salt = UNHEX(SHA2(UUID(), 256));
-- Create user and hash password with salt
INSERT INTO guacamole_user (username, password_salt, password_hash)
VALUES ('myuser', @salt, UNHEX(SHA2(CONCAT('mypassword', HEX(@salt)), 256)));
但是第一个命令给我一个错误:
guacamole_db=# SET @salt = UNHEX(SHA2(UUID(), 256));
ERROR: syntax error at or near "@"
LINE 1: SET @salt = UNHEX(SHA2(UUID(), 256));
^
根据文档,这是他们在 postgres 中创建默认用户的方式:"guacadmin"
INSERT INTO guacamole_entity (name, type) VALUES ('guacadmin', 'USER');
INSERT INTO guacamole_user (entity_id, password_hash, password_salt, password_date)
SELECT
entity_id,
decode('CA458A7D494E3BE824F5E1E175A1556C0F8EEF2C2D7DF3633BEC4A29C4411960', 'hex'), -- 'guacadmin'
decode('FE24ADC5E11E2B25288D1704ABE67A79E342ECC26064CE69C5B3177795A82264', 'hex'),
CURRENT_TIMESTAMP
FROM guacamole_entity WHERE name = 'guacadmin' AND guacamole_entity.type = 'USER';
-- Grant admin permission to read/update/administer self
INSERT INTO guacamole_user_permission (entity_id, affected_user_id, permission)
SELECT guacamole_entity.entity_id, guacamole_user.user_id, permission::guacamole_object_permission_type
FROM (
VALUES
('guacadmin', 'guacadmin', 'READ'),
('guacadmin', 'guacadmin', 'UPDATE'),
('guacadmin', 'guacadmin', 'ADMINISTER')
) permissions (username, affected_username, permission)
JOIN guacamole_entity ON permissions.username = guacamole_entity.name AND guacamole_entity.type = 'USER'
JOIN guacamole_entity affected ON permissions.affected_username = affected.name AND guacamole_entity.type = 'USER'
JOIN guacamole_user ON guacamole_user.entity_id = affected.entity_id;
我如何翻译它以创建新用户,"test" 密码为“123456”且仅读取权限?
这是相关表格的样子:
guacamole_entity :
entity_id | name | type
-----------+-----------+------
1 | guacadmin | USER
guacamole_user:
user_id | entity_id | password_hash | password_salt | pass
word_date | disabled | expired | access_window_start | access_window_end | valid_from | valid_until | timezone | full_name | email_address | organization | organiza
tional_role
---------+-----------+--------------------------------------------------------------------+--------------------------------------------------------------------+------------
------------------+----------+---------+---------------------+-------------------+------------+-------------+----------+-----------+---------------+--------------+---------
------------
1 | 1 | \xca458a7d494e3be824f5e1e175a1556c0f8eef2c2d7df3633bec4a29c4411960 | \xfe24adc5e11e2b25288d1704abe67a79e342ecc26064ce69c5b3177795a82264 | 2020-01-13
09:10:56.73947+00 | f | f | | | | | | | | |
guacamole_user_permission :
entity_id | affected_user_id | permission
-----------+------------------+------------
1 | 1 | READ
1 | 1 | UPDATE
1 | 1 | ADMINISTER
您提到的示例是为 MySql 制作的。生成密码的函数使用MySql特定函数,所以你必须在PostgreSQL中找到合适的替代函数。
快速搜索发现以下可能的替代品:
非十六进制 - MySQL's HEX() and UNHEX() equivalent in Postgres?
SHA2 - 可能是 pgcrypto 模块的一些函数,https://www.postgresql.org/docs/8.3/pgcrypto.html
UUID - 可能是 uuid_generate_v1() 或 uuid_generate_v4()
我最终在 python 中使用 POST 请求创建了一个用户:
第一部分是我们需要为所有要发送到鳄梨酱的请求获取令牌 api:
导入请求
url = "http://guacamoleipornginx/api"
headers ={
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded'}
res = requests.post(url + "/tokens", headers=headers, data={'username': 'guacadmin', 'password': 'guacadmin'})
authToken = res.json()['authToken']
使用该令牌,我们可以发送创建用户的请求:用您的值填写 USERNAME nad USERPASSWORD。
headers ={
'Accept': 'application/json',
'Content-Type': 'application/json;charset=UTF-8'}
uri = "session/data/postgresql/users?token=" + authToken
data = {
"username": $USERNAME,
"password": $USERPASSWORD,
"attributes":{"disabled":"","expired":"","access-window-start":"","access-window-end":"","valid-from":"","valid-until":"","timezone":None}}
res = requests.post(url + "/" + uri, headers=headers, json=data)
这里的最后一部分是授予您在上面创建的用户查看连接的权限,
如果不需要,则不必使用它。
对于此请求,我们将需要您创建的连接的连接 ID。
我的鳄梨酱是 docker 我到这里来的鳄梨酱:
https://github.com/boschkundendienst/guacamole-docker-compose
并且使用了nginx + gouacamole和postgreSql,所以在postgrs中可以看到guacamole_connection
下的connection id。然后您可以将连接权限添加到创建的用户:
uri = "session/data/postgresql/users/" + $USERNAME+ "/permissions?token=" + authToken
ID = 52 # here is the connection id of some connection created in guoacamole psql db.
data = [
{"op":"add",
"path": '/connectionPermissions/' + str(ID),
"value":"READ"}]
res = requests.patch(url + "/" + uri, headers=headers, json=data)
您也可以使用此方法将连接添加到鳄梨酱。您只需要打开 F12 并查看它在创建连接时发送的值,并使用上面相同的方法使用它。
我将添加另一件很酷的事情 - 这是您查看活动连接的方式:
url = "http://guacamoleipornginx/api"
uri = "/session/data/postgresql/activeConnections?token=" + authToken
res = requests.get(url + "/" + uri)
我希望这对我在获取所有这些信息时遇到了很多麻烦。
正如 minokolic 提到的,您所指的示例是为 MySql 制作的,实际上没有必要像 MySql 那样做。这是我为了在 PostgreSQL 中创建一个新用户所做的,来自 Python:
CREATE_USER = (
"INSERT INTO guacamole_user "
"(entity_id, password_hash, password_date) "
"VALUES (%(entity_id)s, decode(%(pwd)s, 'hex'), CURRENT_TIMESTAMP)"
)
使用那个 TSQL 查询,注意我们使用了 decode
函数,我们也删除了盐,如果你想使用盐,你也应该在该列中使用 decode
。
然后,散列您的用户密码:
hashed_pwd = hashlib.sha256(
user_password.encode('utf-8')
).hexdigest()
旁注:如果你想使用盐,这应该在哈希之前连接到密码。
然后,执行游标,创建用户:
cursor.execute(
CREATE_USER, {'entity_id': entity_id, 'pwd': hashed_pwd},
)
这就足够了。
我使用来自 docker hub 的最新 apache guacamole 和 postgres 12 作为数据库, 我想使用 postgres 创建一个登录用户,但它不起作用。
这是从文档创建用户的方法:
-- Generate salt
SET @salt = UNHEX(SHA2(UUID(), 256));
-- Create user and hash password with salt
INSERT INTO guacamole_user (username, password_salt, password_hash)
VALUES ('myuser', @salt, UNHEX(SHA2(CONCAT('mypassword', HEX(@salt)), 256)));
但是第一个命令给我一个错误:
guacamole_db=# SET @salt = UNHEX(SHA2(UUID(), 256));
ERROR: syntax error at or near "@"
LINE 1: SET @salt = UNHEX(SHA2(UUID(), 256));
^
根据文档,这是他们在 postgres 中创建默认用户的方式:"guacadmin"
INSERT INTO guacamole_entity (name, type) VALUES ('guacadmin', 'USER');
INSERT INTO guacamole_user (entity_id, password_hash, password_salt, password_date)
SELECT
entity_id,
decode('CA458A7D494E3BE824F5E1E175A1556C0F8EEF2C2D7DF3633BEC4A29C4411960', 'hex'), -- 'guacadmin'
decode('FE24ADC5E11E2B25288D1704ABE67A79E342ECC26064CE69C5B3177795A82264', 'hex'),
CURRENT_TIMESTAMP
FROM guacamole_entity WHERE name = 'guacadmin' AND guacamole_entity.type = 'USER';
-- Grant admin permission to read/update/administer self
INSERT INTO guacamole_user_permission (entity_id, affected_user_id, permission)
SELECT guacamole_entity.entity_id, guacamole_user.user_id, permission::guacamole_object_permission_type
FROM (
VALUES
('guacadmin', 'guacadmin', 'READ'),
('guacadmin', 'guacadmin', 'UPDATE'),
('guacadmin', 'guacadmin', 'ADMINISTER')
) permissions (username, affected_username, permission)
JOIN guacamole_entity ON permissions.username = guacamole_entity.name AND guacamole_entity.type = 'USER'
JOIN guacamole_entity affected ON permissions.affected_username = affected.name AND guacamole_entity.type = 'USER'
JOIN guacamole_user ON guacamole_user.entity_id = affected.entity_id;
我如何翻译它以创建新用户,"test" 密码为“123456”且仅读取权限?
这是相关表格的样子:
guacamole_entity :
entity_id | name | type
-----------+-----------+------
1 | guacadmin | USER
guacamole_user:
user_id | entity_id | password_hash | password_salt | pass
word_date | disabled | expired | access_window_start | access_window_end | valid_from | valid_until | timezone | full_name | email_address | organization | organiza
tional_role
---------+-----------+--------------------------------------------------------------------+--------------------------------------------------------------------+------------
------------------+----------+---------+---------------------+-------------------+------------+-------------+----------+-----------+---------------+--------------+---------
------------
1 | 1 | \xca458a7d494e3be824f5e1e175a1556c0f8eef2c2d7df3633bec4a29c4411960 | \xfe24adc5e11e2b25288d1704abe67a79e342ecc26064ce69c5b3177795a82264 | 2020-01-13
09:10:56.73947+00 | f | f | | | | | | | | |
guacamole_user_permission :
entity_id | affected_user_id | permission
-----------+------------------+------------
1 | 1 | READ
1 | 1 | UPDATE
1 | 1 | ADMINISTER
您提到的示例是为 MySql 制作的。生成密码的函数使用MySql特定函数,所以你必须在PostgreSQL中找到合适的替代函数。
快速搜索发现以下可能的替代品:
非十六进制 - MySQL's HEX() and UNHEX() equivalent in Postgres?
SHA2 - 可能是 pgcrypto 模块的一些函数,https://www.postgresql.org/docs/8.3/pgcrypto.html
UUID - 可能是 uuid_generate_v1() 或 uuid_generate_v4()
我最终在 python 中使用 POST 请求创建了一个用户: 第一部分是我们需要为所有要发送到鳄梨酱的请求获取令牌 api:
导入请求
url = "http://guacamoleipornginx/api"
headers ={
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded'}
res = requests.post(url + "/tokens", headers=headers, data={'username': 'guacadmin', 'password': 'guacadmin'})
authToken = res.json()['authToken']
使用该令牌,我们可以发送创建用户的请求:用您的值填写 USERNAME nad USERPASSWORD。
headers ={
'Accept': 'application/json',
'Content-Type': 'application/json;charset=UTF-8'}
uri = "session/data/postgresql/users?token=" + authToken
data = {
"username": $USERNAME,
"password": $USERPASSWORD,
"attributes":{"disabled":"","expired":"","access-window-start":"","access-window-end":"","valid-from":"","valid-until":"","timezone":None}}
res = requests.post(url + "/" + uri, headers=headers, json=data)
这里的最后一部分是授予您在上面创建的用户查看连接的权限, 如果不需要,则不必使用它。 对于此请求,我们将需要您创建的连接的连接 ID。 我的鳄梨酱是 docker 我到这里来的鳄梨酱: https://github.com/boschkundendienst/guacamole-docker-compose
并且使用了nginx + gouacamole和postgreSql,所以在postgrs中可以看到guacamole_connection
下的connection id。然后您可以将连接权限添加到创建的用户:
uri = "session/data/postgresql/users/" + $USERNAME+ "/permissions?token=" + authToken
ID = 52 # here is the connection id of some connection created in guoacamole psql db.
data = [
{"op":"add",
"path": '/connectionPermissions/' + str(ID),
"value":"READ"}]
res = requests.patch(url + "/" + uri, headers=headers, json=data)
您也可以使用此方法将连接添加到鳄梨酱。您只需要打开 F12 并查看它在创建连接时发送的值,并使用上面相同的方法使用它。
我将添加另一件很酷的事情 - 这是您查看活动连接的方式:
url = "http://guacamoleipornginx/api"
uri = "/session/data/postgresql/activeConnections?token=" + authToken
res = requests.get(url + "/" + uri)
我希望这对我在获取所有这些信息时遇到了很多麻烦。
正如 minokolic 提到的,您所指的示例是为 MySql 制作的,实际上没有必要像 MySql 那样做。这是我为了在 PostgreSQL 中创建一个新用户所做的,来自 Python:
CREATE_USER = (
"INSERT INTO guacamole_user "
"(entity_id, password_hash, password_date) "
"VALUES (%(entity_id)s, decode(%(pwd)s, 'hex'), CURRENT_TIMESTAMP)"
)
使用那个 TSQL 查询,注意我们使用了 decode
函数,我们也删除了盐,如果你想使用盐,你也应该在该列中使用 decode
。
然后,散列您的用户密码:
hashed_pwd = hashlib.sha256(
user_password.encode('utf-8')
).hexdigest()
旁注:如果你想使用盐,这应该在哈希之前连接到密码。
然后,执行游标,创建用户:
cursor.execute(
CREATE_USER, {'entity_id': entity_id, 'pwd': hashed_pwd},
)
这就足够了。