Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs 菜单
Docs 主页
/ /
Atlas 架构中心
/ /

授权和身份验证示例

以下示例展示了如何实现我们针对不同访问权限类型的身份验证和授权的建议。这些示例展示了如何使用Atlas CLI和 Terraform实现这些建议。

要学习;了解Terraform,请参阅 Terraform 和MongoDB Atlas提供程序入门和MongoDB Atlas提供程序 Terraform 文档。

要访问权限Atlas用户界面:

  • 实施IP访问权限列表。示例,

    atlas organizations apiKeys accessLists create --apiKey <API_KEY_ID> --ip <IP_ADDRESS>
  • 实施 联合身份验证或Atlas凭证和多重身份验证 (MFA)。

    要设立联合身份验证,请使用 atlas-federatedAuthentication 和相关命令。然后,您可以使用 atlas-users-invite 命令邀请用户加入您的组织和项目。

    要开始使用,请使用以下命令:

    atlas federatedAuthentication --help

    注意

    对于使用 SSO 进行身份验证的用户,您还需要配置 SSO 身份提供者。

    有关详细信息,请参阅配置联合身份验证。

对于劳动力数据库访问权限:

  • 使用 Workforce Identity Federation。

  • 使用 atlas-dbusers-certs-create 命令创建 Atlas 托管的 MFA(多因素身份验证)X.509 客户端证书。

    注意

    要使用自管理证书,必须首先配置 X.509 :

    atlas dbusers certs create --username <USERNAME> --projectId <PROJECT_ID> [--monthsUntilExpiration <MONTHS>] [--output json]

对于工作负载(计算机)访问权限,请使用 Workload Identity Federation。

使用 serviceAccounts 和相关命令创建和管理服务帐户。示例,

atlas api serviceAccounts createServiceAccount [options]

对于开发和测试环境,您还可以使用API密钥。示例,

atlas organizations apiKeys create [options]

以下示例演示如何配置身份验证和授权。在使用 Terraform 创建资源之前,您必须:

  • 创建您的付款组织并为该付款组织创建一个 API 密钥。请在终端中运行以下命令,将您的 API 密钥存储为环境变量:

    export MONGODB_ATLAS_PUBLIC_KEY="<insert your public key here>"
    export MONGODB_ATLAS_PRIVATE_KEY="<insert your private key here>"
  • 安装 Terraform

您必须为每个示例创建以下文件。将每个示例的文件放在各自的目录中。更改 ID 和名称以使用您的值。然后运行命令以初始化 Terraform、查看 Terraform 计划并应用更改。

locals {
tags = {
CreatedBy = "Terraform"
Owner = var.owner
Module = "tf-example-oidc-azure"
Name = var.project_name
}
}
resource "azurerm_resource_group" "this" {
name = var.project_name
location = var.location
tags = local.tags
}
resource "azurerm_virtual_network" "this" {
name = var.project_name
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.this.location
resource_group_name = azurerm_resource_group.this.name
tags = local.tags
}
resource "azurerm_subnet" "internal" {
name = "internal"
resource_group_name = azurerm_resource_group.this.name
virtual_network_name = azurerm_virtual_network.this.name
address_prefixes = ["10.0.2.0/24"]
}
resource "azurerm_public_ip" "vm-public-ip" {
name = "public-ip-${var.project_name}"
location = var.location
resource_group_name = azurerm_resource_group.this.name
allocation_method = "Dynamic"
domain_name_label = var.project_name
tags = local.tags
}
resource "azurerm_network_interface" "this" {
name = "ip-${var.project_name}"
location = var.location
resource_group_name = azurerm_resource_group.this.name
tags = local.tags
ip_configuration {
subnet_id = azurerm_subnet.internal.id
name = "public"
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.vm-public-ip.id
}
}
resource "azurerm_user_assigned_identity" "this" {
location = var.location
name = var.project_name
resource_group_name = azurerm_resource_group.this.name
tags = local.tags
}
resource "azurerm_linux_virtual_machine" "this" {
name = var.project_name
resource_group_name = azurerm_resource_group.this.name
location = var.location
size = "Standard_F2"
admin_username = var.vm_admin_username
custom_data = data.cloudinit_config.this.rendered
network_interface_ids = [azurerm_network_interface.this.id]
tags = local.tags
admin_ssh_key {
username = var.vm_admin_username
public_key = var.ssh_public_key
}
source_image_reference {
publisher = "Canonical"
offer = "0001-com-ubuntu-server-jammy"
sku = "22_04-lts"
version = "latest"
}
os_disk {
storage_account_type = "Standard_LRS"
caching = "ReadWrite"
disk_size_gb = 30
}
identity {
type = "UserAssigned"
identity_ids = [azurerm_user_assigned_identity.this.id]
}
}
# Azure Variables
variable "token_audience" {
type = string
default = "https://management.azure.com/"
description = "Used as resource when getting the access token. See more in the [Azure documentation](https://learn.microsoft.com/en-us/entra/identity/managed-identities-azure-resources/how-to-use-vm-token#get-a-token-using-http)"
}
# MongoDB Atlas variables
variable "org_id" {
type = string
description = "MongoDB Atlas Organization ID"
}
variable "project_id" {
type = string
description = "MongoDB Atlas Project ID"
}
variable "project_name" {
type = string
description = "MongoDB Atlas Project Name"
}
variable "connection_strings" {
type = list(string)
description = "MongoDB Atlas Cluster Standard Connection Strings"
}
org_id = "32b6e34b3d91647abb20e7b8"
project_id = "67212db237c5766221eb6ad9"
project_name = "My Project"
connection_strings =
token_audience = "https://management.azure.com/"
output "vm_fqdn" {
value = azurerm_public_ip.vm-public-ip.fqdn
description = "Fully Qualified Domain Name (FQDN) of the Virtual Machine (VM)"
}
output "ssh_connection_string" {
value = "ssh ${var.vm_admin_username}@${azurerm_public_ip.vm-public-ip.fqdn}"
description = "Useful for connecting to the instance"
}
output "user_test_conn_string" {
value = "mongodb+srv://${local.test_user_username}:${local.test_user_password}@${replace(mongodbatlas_advanced_cluster.this.connection_strings[0].standard_srv, "mongodb+srv://", "")}/?retryWrites=true"
sensitive = true
description = "Useful for connecting to the database from Compass or other tool to validate data"
}
output "user_oidc_conn_string" {
value = local.mongodb_oidc_uri
sensitive = true
description = "Useful to see the format of the OIDC connection string"
}

使用以下命令在Atlas中设立 OIDC联合身份提供商,以便与Azure一起使用。它允许使用Azure Active Directory 颁发的 OIDC 令牌访问权限。

# Connection string to use in this configuration
locals {
mongodb_uri = var.connection_strings[0]
}
# Atlas organization details to use in the configuration
data "mongodbatlas_federated_settings" "this" {
org_id = var.org_id
name = var.project_name
project_id = var.project_id
}
# Configure an identity provider for federated authentication
resource "mongodbatlas_federated_settings_identity_provider" "oidc" {
federation_settings_id = data.mongodbatlas_federated_settings.this.id
audience = var.token_audience
authorization_type = "USER"
description = "oidc-for-azure"
# e.g. "https://sts.windows.net/91405384-d71e-47f5-92dd-759e272cdc1c/"
issuer_uri = "https://sts.windows.net/${azurerm_user_assigned_identity.this.tenant_id}/"
idp_type = "WORKLOAD"
name = "OIDC-for-azure"
protocol = "OIDC"
# groups_claim = null
user_claim = "sub"
}
resource "mongodbatlas_federated_settings_org_config" "this" {
federation_settings_id = data.mongodbatlas_federated_settings.this.id
org_id = var.org_id
domain_restriction_enabled = false
domain_allow_list = []
data_access_identity_provider_ids = [mongodbatlas_federated_settings_identity_provider.oidc.idp_id]
}

使用以下命令创建 OIDC 联合身份验证用户。

resource "mongodbatlas_database_user" "oidc" {
project_id = var.project_id
username = "${mongodbatlas_federated_settings_identity_provider.oidc.idp_id}/${azurerm_user_assigned_identity.this.principal_id}"
oidc_auth_type = "USER"
auth_database_name = "$external" # required when using OIDC USER authentication
roles {
role_name = "atlasAdmin"
database_name = "admin"
}
}

使用以下命令创建名为 my_custom_role 的自定义角色,该角色允许对名为 myDb 的数据库中的任何集合更新、添加和删除操作。

resource "mongodbatlas_custom_db_role" "create_role" {
project_id = var.project_id
role_name = "my_custom_role"
actions {
action = "UPDATE"
resources {
database_name = "myDb"
}
}
actions {
action = "INSERT"
resources {
database_name = "myDb"
}
}
actions {
action = "REMOVE"
resources {
database_name = "myDb"
}
}
}

提示

有关在所有支柱上实施我们建议的 Terraform 示例,请参阅 GitHub 中的以下示例之一:

有关将Atlas角色分配给特定群组的Atlas项目的示例,请参阅示例。

后退

授权