ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

使用 Terraform 模块在 Teleport 中实现 Azure 虚拟机自动发现(Azure Discovery Module)实战指南

使用 Terraform 模块在 Teleport 中实现 Azure 虚拟机自动发现(Azure Discovery Module)实战指南 网络安全认证鉴权运维后端【免费下载链接】teleportThe easiest, and most secure way to access and protect all of your infrastructure.项目地址https://gitcode.com/gh_mirrors/tel/teleport点击查看免费下载本指南围绕 integrations/terraform-modules/teleport/discovery/azure/README.md 展开系统讲解 Teleport 官方 Terraform 模块teleport/discovery/azure的用途、工作原理、参数与实战用法。该模块用于在 Azure 与 Teleport 集群之间一键创建虚拟机自动发现所需的全套资源Azure 托管身份、联邦身份凭据、自定义角色与角色分配以及 Teleport 侧的discovery_config、integration与token资源。读完本文你将能够独立完成单订阅与管理组通配符订阅两种场景下的 Azure VM 自动发现配置并理解其底层 OIDC 联邦与 join 认证链路。模块概览一个模块解决 Azure VM 自动发现的全链路资源Teleport 的 Azure Discovery Service发现服务可以周期性扫描指定 Azure 订阅、资源组与标签范围内的虚拟机并对符合匹配条件的 VM 自动安装 Teleport agent 并注册进集群。本模块一次性声明了这条链路所需的全部基础设施Azure 用户分配托管身份user-assigned managed identity供 Teleport Discovery Service 调用 Azure API扫描并管理匹配资源组中的虚拟机Azure 联邦身份凭据federated identity credential在 Azure 与 Teleport 集群之间建立信任使托管身份能够使用 Teleport proxy 签发的 OIDC token 完成身份认证即 workload identity federationAzure 自定义角色定义与角色分配授予托管身份发现虚拟机并对其执行安装命令所需的最小权限Teleportdiscovery_config集群资源配置发现参数订阅、资源组、标签决定哪些 Azure VM 会被发现并纳入集群Teleportintegration集群资源在 Teleport 集群中保存 Azure OIDC 集成配置关联 Azure tenant ID 与 client ID 以支持认证Teleporttoken集群资源提供 join token供被发现的 Azure VM 在安装后认证并加入 Teleport 集群。从源码结构看模块由 7 个.tf文件组成职责划分清晰目录文件职责main.tf模块入口定义公共 locals、随机后缀、Azure 客户端配置与 Teleport proxy ping 探测azure_managed_identity.tf托管身份与联邦身份凭据azure_role_definition.tf自定义角色定义与多 scope 角色分配teleport_discovery_config.tfTeleport discovery_config 资源teleport_integration.tfTeleport Azure OIDC integration 资源teleport_provision_token.tfTeleport provision tokenjoin 规则outputs.tf对外输出工作原理OIDC 联邦 Azure join 认证链路理解本模块关键在于两条认证链路的配合链路一Discovery Service 以托管身份访问 Azure。模块在azure_managed_identity.tf中创建azurerm_user_assigned_identity并在其上创建azurerm_federated_identity_credentialresource azurerm_federated_identity_credential teleport_discovery_service { count local.use_oidc_integration local.create_azure_managed_identity ? 1 : 0 audience [api://AzureADTokenExchange] # Extract the host from proxy_addr (format: host:port) to construct the OIDC issuer URL issuer replace(local.teleport_proxy_public_url, /:[0-9].*/, ) name var.azure_federated_identity_credential_name user_assigned_identity_id one(azurerm_user_assigned_identity.teleport_discovery_service[*].id) subject teleport-azure }这里issuer由teleport_proxy_public_addr推导去掉端口构造 OIDC issuer URLsubject固定为teleport-azureaudience 使用 Azure 标准api://AzureADTokenExchange。这意味着Azure 信任 Teleport proxy 签发的 OIDC tokenDiscovery Service 可凭该 token 换取 Azure 访问令牌从而以托管身份调用 Azure 管理 API。链路二被发现的 VM 使用 Azure 凭据加入 Teleport。模块在teleport_provision_token.tf中创建teleport_provision_tokenjoin_method为azure、roles为[Node]并通过spec.azure.allow规则限定哪些订阅/资源组/租户的 VM 允许加入。VM 安装 agent 后以自身托管身份至少具备Microsoft.Compute/virtualMachines/read权限向 Teleport 证明身份并完成 join。模块还对创建顺序做了显式约束teleport_discovery_config通过depends_on等待 integration 就绪integration 又等待联邦凭据与角色分配完成注释说明这样可避免Discovery Service 在权限就绪前运行导致的约 5 分钟延迟见 teleport_discovery_config.tf 与 teleport_integration.tf。前置条件使用本模块前需满足安装 Teleport Terraform Provider模块依赖官方teleportprovidersource 为terraform.releases.teleport.dev/gravitational/teleport请按 integrations/terraform 的说明完成 provider 配置与认证为待发现的每台 Azure VM 分配托管身份每台要纳入发现的 Azure VM 必须被分配至少一个托管身份且该身份具备Microsoft.Compute/virtualMachines/read权限。这是 VM 后续执行 join 与安装流程的基础准备 Teleport 集群信息需要集群 proxy 的公网地址形如example.teleport.sh:443不含 URL scheme以及 discovery group 名称。版本要求与 Provider 依赖模块声明的版本约束见 versions.tf项要求terraform 1.5.7azurerm 4.0http 3.0random 3.0teleport 18.7.6其中azurerm用于创建 Azure 资源http用于在 apply 前探测 Teleport proxy 的/webapi/find端点以获取集群名random用于生成资源名后缀teleport用于管理 Teleport 集群内资源。注意 Teleport provider 的版本下限为18.7.6使用旧版 Teleport 集群时需要先升级。实战一在单个 Azure 订阅中发现虚拟机完整可运行示例位于 examples/single-subscription/main.tf核心代码如下locals { apply_azure_tags { origin example } apply_teleport_resource_labels { origin example } } data azurerm_client_config current {} # Resource group for the Azure resources created by the module. resource azurerm_resource_group example { name teleport-discovery location eastus tags local.apply_azure_tags } module azure_discovery { source ../.. teleport_discovery_group_name cloud-discovery-group teleport_proxy_public_addr example.teleport.sh:443 # Name of an existing Azure Resource Group where Azure resources will be created. azure_resource_group_name azurerm_resource_group.example.name # Region where Azure managed identity will be created (eastus) azure_managed_identity_location azurerm_resource_group.example.location # Discover Azure VMs across all resource groups in the current subscription # and matching the specified tags. azure_matchers [ { types [vm] subscriptions [data.azurerm_client_config.current.subscription_id] resource_groups [*] regions [westus, eastus] tags { env [example] } } ] # Apply the additional Azure tag originexample to all Azure resources created by this module apply_azure_tags local.apply_azure_tags # Apply the additional Teleport label originexample to all Teleport resources created by this module apply_teleport_resource_labels local.apply_teleport_resource_labels # Using a custom installer script on discovered VMs instead of the default installer script. teleport_installer_script_name teleport_installer.example.metadata.name }该示例的关键点subscriptions直接引用当前登录的 Azure subscription ID发现范围被限定在单个订阅内resource_groups [*]表示扫描该订阅下所有资源组regions限定区域tags限定带envexample标签的 VM示例同时展示了teleport_installer自定义安装脚本的用法脚本本质是下载 proxy 托管的通用 install.sh 并调用teleport install autodiscover-node可通过tctl get installer/default-installer查看默认脚本后按需修改再由teleport_installer_script_name指定。实战二在 Azure 管理组跨订阅中发现虚拟机当需要跨多个订阅发现 VM 时使用管理组或租户范围的通配符订阅匹配。完整示例位于 examples/management-group/main.tfmodule azure_discovery { source ../.. teleport_discovery_group_name cloud-discovery-group teleport_proxy_public_addr example.teleport.sh:443 azure_resource_group_name azurerm_resource_group.example.name azure_managed_identity_location azurerm_resource_group.example.location # Management group ID or tenant ID. Using a Tenant ID will use the Root # management group scope for the managed identitys role. # # Operations on the root scope will require elevated access. azure_management_group_id data.azurerm_client_config.current.tenant_id # Discover Azure VMs across all subscriptions matching the specified tags. azure_matchers [ { types [vm] subscriptions [*] tags { TeleportEnroll [true] } } ] apply_azure_tags local.apply_azure_tags apply_teleport_resource_labels local.apply_teleport_resource_labels teleport_installer_script_name teleport_installer.example.metadata.name }要点azure_management_group_id可传管理组 ID 或租户 ID传租户 ID 时会使用 Root 管理组作为角色分配 scope在根 scope 上的操作需要提升权限Azure 全局管理员提升访问权限subscriptions [*]是通配符匹配发现角色分配 scope 内可见的所有订阅使用通配符订阅时必须同时设置azure_management_group_id或azure_role_assignment_scopes否则模块会在 teleport_discovery_config.tf 的 precondition 中报错示例注释中还给出了可选的azure_role_assignment_scopes写法用于把角色分配限定到子管理组或具体订阅# azure_role_assignment_scopes [ # /providers/Microsoft.Management/managementGroups/child-mg, # /subscriptions/00000000-0000-0000-0000-000000000000, # ]参数详解Inputs模块全部输入参数如下来源 README.md 与 variables.tf必填参数名称说明teleport_discovery_group_nameTeleport discovery group 名称。要生效该名称必须与至少一个 Teleport Discovery Service 实例配置的discovery_group匹配Teleport Cloud 集群请使用cloud-discovery-group。teleport_proxy_public_addrTeleport 集群 proxy 公网地址格式host:port不含 URL scheme。模块通过 validation 强制要求不得包含://且必须包含:。azure_matchersAzure 资源发现匹配器。类型为list(object({ types list(string), subscriptions list(string), resource_groups optional(list(string), [*]), regions optional(list(string), [*]), tags optional(map(list(string)), { * : [*] }) }))。当前types仅支持vm。通配符*订阅匹配仅在设置azure_management_group_id或azure_role_assignment_scopes时允许且*必须是subscriptions列表中的唯一项。可选参数名称默认值说明createtrue是否创建全部资源的总开关。create_azure_managed_identitytrue是否创建 Azure 托管身份与角色资源为false时不创建任何 Azure 资源用于复用已有身份的扩展场景。当use_oidc_integration为true时必须为true。use_oidc_integrationtrue是否创建 Azure OIDC integration 与联邦身份凭据并在 discovery config 中引用。azure_managed_identity_namediscovery-identityAzure 用户分配托管身份名称。azure_managed_identity_use_name_prefixtrue为true时该名称作为前缀会拼接随机后缀为false时使用精确名称。azure_managed_identity_locationnull托管身份所在 Azure 区域如eastus。create_azure_managed_identitytrue时必填模块通过 precondition 校验。azure_resource_group_namenull存放 Azure 资源的已有资源组名称。create_azure_managed_identitytrue时必填。azure_federated_identity_credential_nameteleport-federation为 workload identity federation 创建的联邦身份凭据名称。azure_role_definition_nameteleport-discovery为 Teleport Discovery 创建的自定义角色定义名称。azure_role_definition_use_name_prefixtrue角色定义名称是否作为前缀使用。azure_management_group_idnull用于推导角色分配 scope 的管理组或租户 ID。设置azure_role_assignment_scopes时无效两者均未设置时从azure_matchers推导订阅 scope。azure_role_assignment_scopes[]显式指定 Azure 发现角色的分配 scope。默认从azure_management_group_id或azure_matchers.subscriptions推导。当其中包含多于一个管理组 scope 时必须设置azure_management_group_id。teleport_discovery_config_namediscoveryteleport_discovery_config资源名称。teleport_discovery_config_use_name_prefixtrue该名称是否作为前缀使用。teleport_integration_namediscoveryteleport_integration资源名称。teleport_integration_use_name_prefixtrue该名称是否作为前缀使用。teleport_provision_token_namediscoveryteleport_provision_token资源名称。teleport_provision_token_use_name_prefixtrue该名称是否作为前缀使用。teleport_installer_script_namedefault-installer使用的既有 Teleport 安装脚本名称。teleport_provision_token_allow_rulesnullprovision token 的自定义 allow 规则类型list(object({ subscription optional(string), resource_groups optional(list(string)), tenant optional(string) }))。使用通配符*订阅匹配且create_azure_managed_identitytrue时默认生成基于租户 ID 的 allow 规则create_azure_managed_identityfalse且使用通配符订阅时必填。apply_azure_tags{}附加到所有 Azure 资源的额外 Azure 标签。apply_teleport_resource_labels{}附加到所有 Teleport 资源的额外标签。关于通配符订阅与 allow 规则的联动可参考 teleport_provision_token.tf模块自动把通配符 matcher 展开为仅租户限制规则tenant azure_tenant_id把普通 matcher 展开为按订阅 资源组限制的规则列表这些规则被写入spec.azure.allow。源码级实现剖析公共 locals 与 proxy 探测main.tf模块在 main.tf 中通过data http teleport_ping请求https://proxy_addr/webapi/find获取集群名用于给所有 Azure 资源打上TeleportCluster、TeleportIntegration、TeleportIACToolterraform标签以及teleport.dev/iac-toolterraform等 Teleport 标签。random_id.suffix4 字节为所有带前缀命名的资源生成唯一后缀避免多环境部署冲突。最小权限角色定义azure_role_definition.tf自定义角色teleport_discovery的权限在 azure_role_definition.tf 中定义遵循最小权限原则vm_actions [ Microsoft.Compute/virtualMachines/read, Microsoft.Compute/virtualMachines/runCommands/read, Microsoft.Compute/virtualMachines/runCommands/write, Microsoft.Compute/virtualMachineScaleSets/read, Microsoft.Compute/virtualMachineScaleSets/virtualMachines/read, Microsoft.Compute/virtualMachineScaleSets/virtualMachines/runCommands/read, Microsoft.Compute/virtualMachineScaleSets/virtualMachines/runCommands/write, ] role_actions distinct(concat( [Microsoft.Resources/subscriptions/read], local.uses_vm ? local.vm_actions : [], ))Microsoft.Compute/virtualMachines/runCommands/write正是在 VM 上运行安装命令所需的权限角色分配通过for_each对每个 scope 创建azurerm_role_assignment。注意角色定义要求assignable_scopes中的管理组 scope 最多一个除非显式设置azure_management_group_id。discovery_config 与 install_paramsteleport_discovery_config.tfteleport_discovery_config.tf 为每个 matcher 自动附加字段当use_oidc_integrationtrue时附加integration integration 名称当 matcher 包含vm类型时附加install_paramsvm_install_params { join_method azure join_token local.teleport_provision_token_name script_name var.teleport_installer_script_name }它把join_methodazure、join token 名称与安装脚本名称绑定进 discovery 配置VM 被发现后即按此参数完成 agent 安装与 join。输出与验证Outputs模块对外暴露 3 个输出见 outputs.tf输出说明azure_discovery_role_definition为 Teleport Discovery Service 创建的 Azure 角色定义对象。azure_teleport_discovery_managed_identity为 Teleport Discovery Service 创建的托管身份对象。teleport_discovery_config_nameTeleport 动态discovery_config名称可用tctl get discovery_config/name查看详情。teleport_integration_nameTeleportintegration资源名称可用tctl get integrations/name查看或在 Teleport Web UI 的Zero Trust Access Integrations中查看。teleport_provision_token_name允许 Teleport 节点使用 Azure 凭据加入集群的 provision token 名称可用tctl get token/name查看。使用建议与常见问题资源组与区域必须预先确定模块不会创建托管身份所在的资源组azure_resource_group_name必须指向已存在的资源组azure_managed_identity_location同样必须显式提供示例中均复用示例资源组的名称与位置。使用通配符订阅的三个硬性约束均由模块 precondition 强制校验① 必须设置azure_management_group_id或azure_role_assignment_scopes② 若create_azure_managed_identityfalse复用外部身份必须显式设置teleport_provision_token_allow_rules③*必须是subscriptions列表的唯一元素。OIDC integration 与托管身份强绑定use_oidc_integrationtrue要求create_azure_managed_identitytrue因为 integration 需要引用模块创建身份的client_id与租户 ID见 teleport_integration.tf。依赖顺序已内建integration 等待联邦凭据与角色分配完成discovery_config 等待 integration 完成这避免了 Discovery Service 提前运行导致的权限竞态与约 5 分钟延迟。命名冲突规避所有关键资源托管身份、角色定义、discovery config、integration、token都支持*_use_name_prefix开关多环境部署时建议保持默认true以附加随机后缀。相关资源模块源码与示例integrations/terraform-modules/teleport/discovery/azure单订阅示例examples/single-subscription/main.tf管理组示例examples/management-group/main.tf模块测试tests/main.tftest.hcl 与 tests/examples_single_subscription.tftest.hclTerraform 模块总目录integrations/terraform-modules赞分享网络安全认证鉴权运维后端【免费下载链接】teleportThe easiest, and most secure way to access and protect all of your infrastructure.项目地址https://gitcode.com/gh_mirrors/tel/teleport点击查看免费下载相关推荐Teleport 通过 Terraform 在 Azure 管理组中发现虚拟机management-group 示例完全指南Teleport 通过 Terraform 在 Azure 管理组中发现虚拟机management group 示例完全指南 导读 在拥有大量 Azure 订网络安全认证鉴权运维后端Teleport AWS Discovery Terraform 模块实战自动发现 EC2 / EKS / RDS 资源的完整指南Teleport AWS Discovery Terraform 模块实战自动发现 EC2 / EKS / RDS 资源的完整指南 本指南围绕 Telepor网络安全认证鉴权运维后端Teleport AWS Organization Discovery 实战用 Terraform 模块实现跨多账号 EC2 资源发现Teleport AWS Organization Discovery 实战用 Terraform 模块实现跨多账号 EC2 资源发现 导读 本文基于 Tel网络安全认证鉴权运维后端创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表