Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs Menu
Docs Home
/ /
Atlas Architecture Center
/ /

認可と認証の例

次の例は、さまざまなアクセス タイプに対する認証と認可に関する推奨事項を実装する方法を示しています。例では、Atlas CLI と Terraform の両方を使用して推奨事項を実装する方法を示します。

Terraform の詳細については、 「Terraform を使い始める」および「MongoDB Atlas Provider 」および「 MongoDB Atlas Provider Terraform のドキュメント 」を参照してください。

Atlas UIにアクセスするには、以下の手順を行います。

  • 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 ID プロバイダー も構成する必要があります。

    詳細については、「 フェデレーション認証の構成 」を参照してください。

ワークフォースデータベースにアクセスするには、以下の手順を行います。

  • Workforce IdP を使用する。

  • 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"
}

以下を使用して、 Azureで使用するために Atlas で OIDC フェデレーティッドIdPを設定します。 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"
}
}
}

Tip

すべてのピリオドに対して推奨事項を強制する Terraform の例については、 GitHub の次のいずれかの例を参照してください。

Atlas ロールが特定のグループに割り当てられた Atlasプロジェクトの例については、「例」を参照してください。

戻る

承認