[BETA] OpenID Connect (OIDC)
LiteLLM 支持使用 OpenID Connect (OIDC) 进行上游服务认证。这使您可以避免在配置文件中存储敏感凭据。
此功能处于 Beta 测试阶段
OIDC 身份提供商 (IdP)
LiteLLM 支持以下 OIDC 身份提供商
| 提供商 | 配置名称 | 自定义受众 (Audiences) |
|---|---|---|
| Google Cloud Run | google | 是 |
| CircleCI v1 | circleci | 否 |
| CircleCI v2 | circleci_v2 | 否 |
| GitHub Actions | github | 是 |
| Azure Kubernetes Service | azure | 否 |
| Azure AD | azure | 是 |
| File | file | 否 |
| 环境变量 | env | 否 |
| 环境变量路径 | env_path | 否 |
如果您希望使用其他 OIDC 提供商,请在 GitHub 上提交 issue。
除非您非常清楚自己在做什么,并且确定其他提供商都不适用于您的用例,否则请勿使用 file、env 或 env_path 提供商。提示:它们通常都能满足需求。
OIDC 连接依赖方 (RP)
LiteLLM 支持以下 OIDC 依赖方 / 客户端
- Amazon Bedrock
- Azure OpenAI
- (即将推出) Google Cloud Vertex AI
配置 OIDC
在任何可以使用密钥的地方,都可以直接使用 OIDC。一般格式为:
oidc/config_name_here/audience_here
对于不使用 audience 参数的提供商,您可以(也应该)省略它。
oidc/config_name_here/
非官方提供商(不推荐)
对于非官方的 file 提供商,可以使用以下格式:
oidc/file/home/user/dave/this_is_a_file_with_a_token.txt
对于非官方的 env,使用以下格式,其中 SECRET_TOKEN 是包含令牌的环境变量名称:
oidc/env/SECRET_TOKEN
对于非官方的 env_path,使用以下格式,其中 SECRET_TOKEN 是包含令牌文件路径的环境变量名称:
oidc/env_path/SECRET_TOKEN
如果您倾向于使用 oidc/env_path/AZURE_FEDERATED_TOKEN_FILE,请不要这样做。相反,请使用 oidc/azure/,因为如果 Azure 更改了其 OIDC 配置和/或添加了新功能,这将确保 LiteLLM 的持续支持。
示例
Google Cloud Run -> Amazon Bedrock
model_list:
- model_name: claude-3-haiku-20240307
litellm_params:
model: bedrock/anthropic.claude-3-haiku-20240307-v1:0
aws_region_name: us-west-2
aws_session_name: "litellm"
aws_role_name: "arn:aws:iam::YOUR_THING_HERE:role/litellm-google-demo"
aws_web_identity_token: "oidc/google/https://example.com"
CircleCI v2 -> Amazon Bedrock
model_list:
- model_name: command-r
litellm_params:
model: bedrock/cohere.command-r-v1:0
aws_region_name: us-west-2
aws_session_name: "my-test-session"
aws_role_name: "arn:aws:iam::335785316107:role/litellm-github-unit-tests-circleci"
aws_web_identity_token: "oidc/example-provider/"
针对 CircleCI v2 -> Bedrock 的 Amazon IAM 角色配置
以下配置仅为示例。您应根据具体用例调整权限和信任关系。
权限
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"bedrock:InvokeModel",
"bedrock:InvokeModelWithResponseStream"
],
"Resource": [
"arn:aws:bedrock:*::foundation-model/anthropic.claude-3-haiku-20240307-v1:0",
"arn:aws:bedrock:*::foundation-model/cohere.command-r-v1:0"
]
}
]
}
更多示例请参见 https://docs.aws.amazon.com/bedrock/latest/userguide/security_iam_id-based-policy-examples.html。
信任关系
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::335785316107:oidc-provider/oidc.circleci.com/org/c5a99188-154f-4f69-8da2-b442b1bf78dd"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"oidc.circleci.com/org/c5a99188-154f-4f69-8da2-b442b1bf78dd:aud": "c5a99188-154f-4f69-8da2-b442b1bf78dd"
},
"ForAnyValue:StringLike": {
"oidc.circleci.com/org/c5a99188-154f-4f69-8da2-b442b1bf78dd:sub": [
"org/c5a99188-154f-4f69-8da2-b442b1bf78dd/project/*/user/*/vcs-origin/github.com/BerriAI/litellm/vcs-ref/refs/heads/main",
"org/c5a99188-154f-4f69-8da2-b442b1bf78dd/project/*/user/*/vcs-origin/github.com/BerriAI/litellm/vcs-ref/refs/heads/litellm_*"
]
}
}
}
]
}
此信任关系限制 CircleCI 仅能在主分支和以 litellm_ 开头的所有分支上担任该角色。
对于 CircleCI(v1 和 v2),您还需要在 AWS IAM 设置中添加贵组织的 OIDC 提供商。更多信息请参见 https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create_for-idp_oidc.html。
您永远不需要创建 IAM 用户。如果创建了,说明您没有正确使用 OIDC。您应该只创建一个具有相应权限且与 OIDC 提供商建立了信任关系的角色。
Google Cloud Run -> Azure OpenAI
model_list:
- model_name: gpt-4o-2024-05-13
litellm_params:
model: azure/gpt-4o-2024-05-13
azure_ad_token: "oidc/google/https://example.com"
api_version: "2024-06-01"
api_base: "https://demo-here.openai.azure.com"
model_info:
base_model: azure/gpt-4o-2024-05-13
对于 Azure OpenAI,您需要在环境中定义 AZURE_CLIENT_ID、AZURE_TENANT_ID,以及可选的 AZURE_AUTHORITY_HOST。
export AZURE_CLIENT_ID="91a43c21-cf21-4f34-9085-331015ea4f91" # Azure AD Application (Client) ID
export AZURE_TENANT_ID="f3b1cf79-eba8-40c3-8120-cb26aca169c2" # Will be the same across of all your Azure AD applications
export AZURE_AUTHORITY_HOST="https://login.microsoftonline.com" # 👈 Optional, defaults to "https://login.microsoftonline.com"
您可以通过访问 https://login.microsoftonline.com/YOUR_DOMAIN_HERE/v2.0/.well-known/openid-configuration 并查看 issuer 字段中的 UUID 来找到 AZURE_CLIENT_ID。
除非需要覆盖默认值,否则不要在环境中设置 AZURE_AUTHORITY_HOST。这样,如果默认值将来发生变化,您就不需要更新环境配置。
默认情况下,Azure AD 应用程序使用受众 api://AzureADTokenExchange。我们建议将受众设置为对您的应用程序更具体的名称。
Azure AD 应用程序配置
遗憾的是,Azure 的设置比 AWS 等其他 OIDC 依赖方稍微复杂一些。基本上,您需要:
- 创建一个 Azure 应用程序。
- 为您正在使用的 OIDC IdP(例如 Google Cloud Run)添加联合凭据。
- 将 Azure 应用程序添加到包含 Azure OpenAI 资源的资源组中。
- 为 Azure 应用程序授予访问 Azure OpenAI 资源的必要角色。
以下自定义角色是 Azure 应用程序访问 Azure OpenAI 资源建议的最小权限。您应根据具体用例调整权限。
{
"id": "/subscriptions/24ebb700-ec2f-417f-afad-78fe15dcc91f/providers/Microsoft.Authorization/roleDefinitions/baf42808-99ff-466d-b9da-f95bb0422c5f",
"properties": {
"roleName": "invoke-only",
"description": "",
"assignableScopes": [
"/subscriptions/24ebb700-ec2f-417f-afad-78fe15dcc91f/resourceGroups/your-openai-group-name"
],
"permissions": [
{
"actions": [],
"notActions": [],
"dataActions": [
"Microsoft.CognitiveServices/accounts/OpenAI/deployments/audio/action",
"Microsoft.CognitiveServices/accounts/OpenAI/deployments/search/action",
"Microsoft.CognitiveServices/accounts/OpenAI/deployments/completions/action",
"Microsoft.CognitiveServices/accounts/OpenAI/deployments/chat/completions/action",
"Microsoft.CognitiveServices/accounts/OpenAI/deployments/extensions/chat/completions/action",
"Microsoft.CognitiveServices/accounts/OpenAI/deployments/embeddings/action",
"Microsoft.CognitiveServices/accounts/OpenAI/images/generations/action"
],
"notDataActions": []
}
]
}
}
注意:您的 UUID 将会有所不同。
如果您在设置 Azure AD 应用程序时需要帮助,请联系我们获取付费企业支持。
Azure AD -> Amazon Bedrock
model list:
- model_name: aws/claude-3-5-sonnet
litellm_params:
model: bedrock/us.anthropic.claude-haiku-4-5-20251001-v1:0
aws_region_name: "eu-central-1"
aws_role_name: "arn:aws:iam::12345678:role/bedrock-role"
aws_web_identity_token: "oidc/azure/api://123-456-789-9d04"
aws_session_name: "litellm-session"