身份验证和授权
身份验证和授权是密切相关的概念,有时简写为 AuthN 和 AuthZ。身份验证 (AuthN) 是验证用户身份的过程,而授权 (AuthZ) 是确定用户具有哪些权限的过程。
身份验证
Weaviate 通过 API 密钥或 OpenID Connect (OIDC) 进行用户身份验证来控制访问,并提供匿名访问选项。然后可以将不同的授权级别分配给用户,如下面的图表所示。
例如,使用 API 密钥 jane-secret 登录的用户可以被授予管理员权限,而使用 API 密钥 ian-secret 登录的用户可以被授予只读权限。
总而言之,Weaviate 允许以下身份验证方法
- API 密钥
- OpenID Connect (OIDC)
- 匿名访问(无需身份验证,强烈不建议,除非用于开发或评估)
请注意,API 密钥和 OIDC 身份验证可以同时启用。
配置身份验证的方式取决于您的部署方法,具体取决于您是在 Docker 还是 Kubernetes 中运行 Weaviate。以下,我们为两者提供了示例。
对于 Weaviate Cloud (WCD) 实例,身份验证已预配置为使用 OIDC 和 API 密钥访问。您可以使用 OIDC 通过您的 WCD 凭据验证 Weaviate,或使用 API 密钥。
API 密钥
有关如何在 Weaviate 中使用 API 密钥的更多详细信息,请查看身份验证指南。
我们建议使用客户端库来验证 Weaviate。有关更多信息,请参阅操作指南:连接页面。
OIDC
有关如何在 Weaviate 中使用 OIDC 身份验证的更多详细信息,请查看身份验证指南。
OIDC 标准允许许多不同的方法(流程)来获取令牌。适当的方法可能因您的具体情况而异,包括令牌颁发者的配置以及您的要求。
OIDC 身份验证流程超出本文档的范围,但以下是一些需要考虑的选项
- 对于机器到机器授权,使用
client credentials flow(客户端凭据流程)。(请注意,这授权的是一个应用程序,而不是用户。)- 已通过 Okta 和 Azure 作为身份提供商进行验证;GCP 不支持客户端凭据授权流程(截至 2022 年 12 月)。
- Weaviate 的 Python 客户端直接支持此方法。
- 客户端凭据流程通常不带刷新令牌,并且凭据保存在各自的客户端中,以便在旧令牌到期时获取新的访问令牌。
- 对于像 Weaviate Cloud 这样的受信任应用程序,使用
resource owner password flow(资源所有者密码流程)。 - 如果 Azure 是您的令牌颁发者,或者您希望防止暴露密码,请使用
hybrid flow(混合流程)。
对 Weaviate 客户端的支持
如果 Weaviate 数据库配置为使用 client credentials grant 流程或 resource owner password flow 流程,则 Weaviate 客户端可以实例化一个包含身份验证流程的连接到 Weaviate 数据库。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
import os
import weaviate
from weaviate.classes.init import Auth
# Best practice: store your credentials in environment variables
weaviate_url = os.environ["WEAVIATE_URL"]
weaviate_username = os.environ["WCD_USERNAME"]
weaviate_password = os.environ["WCD_PASSWORD"]
client = weaviate.connect_to_weaviate_cloud(
cluster_url=weaviate_url, # Replace with your Weaviate Cloud URL
auth_credentials=Auth.client_password(
username=weaviate_username, # Your Weaviate Cloud username
password=weaviate_password # Your Weaviate Cloud password
)
)
手动获取和传递令牌
手动获取和传递令牌
对于您可能希望手动获取令牌的情况或工作流程,我们下面概述了执行此操作的步骤,用于资源所有者密码流程和混合流程。
资源所有者密码流程
- 发送 GET 请求到
WEAVIATE_INSTANCE_URL/v1/.well-known/openid-configuration以获取 Weaviate 的 OIDC 配置 (wv_oidc_config)。将 WEAVIATE_INSTANCE_URL 替换为您的实例 URL。 - 从
wv_oidc_config解析clientId和href。 - 发送 GET 请求到
href以获取令牌颁发者的 OIDC 配置 (token_oidc_config)。 - 如果
token_oidc_config包含可选的grant_types_supported键,请检查password是否在值列表中。- 如果
password不在列表中,则令牌颁发者可能未配置为resource owner password flow。您可能需要重新配置令牌颁发者或使用其他方法。 - 如果未提供
grant_types_supported键,您可能需要联系令牌颁发者以查看是否支持resource owner password flow。
- 如果
- 向
token_oidc_config的token_endpoint发送 POST 请求,请求体为{"grant_type": "password", "client_id": client_id, "username": USERNAME, "password": PASSWORD。将USERNAME和PASSWORD替换为实际值。
- 解析响应 (
token_resp),并在token_resp中查找access_token。这是您的 Bearer 令牌。
混合流程
- 发送 GET 请求到
WEAVIATE_INSTANCE_URL/v1/.well-known/openid-configuration以获取 Weaviate 的 OIDC 配置 (wv_oidc_config)。将 WEAVIATE_INSTANCE_URL 替换为您的实例 URL。 - 从
wv_oidc_config解析clientId和href - 发送 GET 请求到
href以获取令牌颁发者的 OIDC 配置 (token_oidc_config) - 根据
token_oidc_config中的authorization_endpoint构造一个 URL (auth_url)。它将如下所示{authorization_endpoint}?client_id={clientId}&response_type=code%20id_token&response_mode=fragment&redirect_url={redirect_url}&scope=openid&nonce=abcdredirect_url必须已在您的令牌颁发者处预注册。
- 在浏览器中转到
auth_url,如果提示,请登录。如果成功,令牌颁发者会将浏览器重定向到redirect_url,其中包含其他参数,其中包括一个id_token参数。 - 解析
id_token参数值。这是您的 Bearer 令牌。
代码示例
此示例演示如何获取 OIDC 令牌。
import requests
import re
url = "https://:8080" # <-- Replace with your actual Weaviate URL
# Get Weaviate's OIDC configuration
weaviate_open_id_config = requests.get(url + "/v1/.well-known/openid-configuration")
if weaviate_open_id_config.status_code == "404":
print("Your Weaviate instance is not configured with openid")
response_json = weaviate_open_id_config.json()
client_id = response_json["clientId"]
href = response_json["href"]
# Get the token issuer's OIDC configuration
response_auth = requests.get(href)
if "grant_types_supported" in response_auth.json():
# For resource owner password flow
assert "password" in response_auth.json()["grant_types_supported"]
username = "username" # <-- Replace with the actual username
password = "password" # <-- Replace with the actual password
# Construct the POST request to send to 'token_endpoint'
auth_body = {
"grant_type": "password",
"client_id": client_id,
"username": username,
"password": password,
}
response_post = requests.post(response_auth.json()["token_endpoint"], auth_body)
print("Your access_token is:")
print(response_post.json()["access_token"])
else:
# For hybrid flow
authorization_url = response_auth.json()["authorization_endpoint"]
parameters = {
"client_id": client_id,
"response_type": "code%20id_token",
"response_mode": "fragment",
"redirect_url": url,
"scope": "openid",
"nonce": "abcd",
}
# Construct 'auth_url'
parameter_string = "&".join([key + "=" + item for key, item in parameters.items()])
response_auth = requests.get(authorization_url + "?" + parameter_string)
print("To login, open the following url with your browser:")
print(authorization_url + "?" + parameter_string)
print(
"After the login you will be redirected, the token is the 'id_token' parameter of the redirection url."
)
# You could use this regular expression to parse the token
resp_txt = "Redirection URL"
token = re.search("(?<=id_token=).+(?=&)", resp_txt)[0]
print("Set as bearer token in the clients to access Weaviate.")
令牌生命周期
令牌具有可配置的到期时间,由令牌颁发者设置。我们建议建立一个工作流程,以便在到期前定期获取新的令牌。
将 Bearer 添加到请求
当您使用 API 密钥验证 Weaviate 时,请在请求标头中添加 API 密钥。
格式为:Authorization: Bearer WEAVIATE_API_KEY。将 WEAVIATE_API_KEY 替换为您的 Weaviate 实例的 API 密钥。
例如,cURL 命令如下所示
curl https://:8080/v1/objects -H "Authorization: Bearer ${WEAVIATE_API_KEY}" | jq
授权
Weaviate 通过基于用户的身份验证状态的授权级别提供差异化访问。可以授予用户管理员权限、只读权限或根本没有权限。从 v1.29.0 开始,Weaviate 还支持基于角色的访问控制 (RBAC),以便对用户权限进行更细粒度的控制。
下图说明了用户请求通过身份验证和授权流程的过程
Weaviate 中可用的授权方案如下
在管理员列表授权方案中,可以授予匿名用户权限。
配置授权的方式取决于您的部署方法,具体取决于您是在 Docker 还是 Kubernetes 中运行 Weaviate。以下,我们为两者提供了示例。
更多资源
问题和反馈
如果您有任何问题或反馈,请在 用户论坛 中告诉我们。
