Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Terraform module for Azure AD Service Principal and its Azure role assignment

License

Notifications You must be signed in to change notification settings

claranet/terraform-azurerm-service-principal

Repository files navigation

Azure Service Principal

Changelog Notice Apache V2 License TF Registry

Azure terraform module to create an Azure AD Service Principal and assign specified role(s) to choosen Azure scope(s).

Global versioning rule for Claranet Azure modules

Module version Terraform version AzureRM version
>= 7.x.x 1.3.x >= 3.0
>= 6.x.x 1.x >= 3.0
>= 5.x.x 0.15.x >= 2.0
>= 4.x.x 0.13.x / 0.14.x >= 2.0
>= 3.x.x 0.12.x >= 2.0
>= 2.x.x 0.12.x < 2.0
< 2.x.x 0.11.x < 2.0

Contributing

If you want to contribute to this repository, feel free to use our pre-commit git hook configuration which will help you automatically update and format some files for you by enforcing our Terraform code module best-practices.

More details are available in the CONTRIBUTING.md file.

Usage

This module is optimized to work with the Claranet terraform-wrapper tool which set some terraform variables in the environment needed by this module. More details about variables set by the terraform-wrapper available in the documentation.

data "azurerm_subscription" "primary" {
}

data "azurerm_resource_group" "rg" {
  name = "dsrg_test"
}

data "azuread_group" "readers" {
  display_name = "Claranet Readers"
}

data "azuread_users" "owner_users" {
  user_principal_names = ["jean.dupont@xxxx.clara.net", "owner.yyyy@contoso.com"]
}

resource "azurerm_role_definition" "example" {
  name  = "my-custom-role-definition"
  scope = data.azurerm_subscription.primary.id

  permissions {
    actions     = ["Microsoft.Resources/subscriptions/resourceGroups/read"]
    not_actions = []
  }

  assignable_scopes = [
    data.azurerm_subscription.primary.id,
  ]
}

module "sp" {
  source  = "claranet/service-principal/azurerm"
  version = "x.x.x"

  sp_display_name = "claranet-tools"
  sp_owners       = data.azuread_users.owner_users.object_ids

  sp_scope_assignment = [
    {
      scope     = data.azurerm_subscription.primary.id
      role_name = null
      role_id   = azurerm_role_definition.example.role_definition_resource_id
    },
    {
      scope     = data.azurerm_resource_group.rg.id
      role_name = "Contributor"
    }
  ]

  sp_groups_member = {
    (data.azuread_group.readers.display_name) = data.azuread_group.readers.object_id
  }

  sp_aad_app_tags = ["foo", "bar"]

  # az ad sp list --display-name "Microsoft Graph" --query '[].{appDisplayName:appDisplayName, appId:appId}'
  sp_required_resource_access = {
    # Azure Healthcare APIs
    "4f6778d8-5aef-43dc-a1ff-b073724b9495" = [{
      resource_access_id   = "4f6778d8-5aef-43dc-a1ff-b073724b9495" # user_impersonation - Application
      resource_access_type = "Role"
    }]
    # Microsoft.Graph
    "00000003-0000-0000-c000-000000000000" = [{
      resource_access_id   = "e1fe6dd8-ba31-4d61-89e7-88639da4683d" # User.Read - Delegated
      resource_access_type = "Scope"
      },
      {
        resource_access_id   = "19dbc75e-c2e2-444c-a770-ec69d8559fc7" # Directory.ReadWrite.All - Delegated
        resource_access_type = "Scope"
      }
    ]
  }
}

Providers

Name Version
azuread ~> 2.44
azurerm ~> 3.0
random ~> 3.5

Modules

No modules.

Resources

Name Type
azuread_application.aad_app resource
azuread_group_member.sp_group_member resource
azuread_service_principal.sp resource
azuread_service_principal_password.sp_pwd resource
azurerm_role_assignment.sp_role resource
random_uuid.api_settings resource

Inputs

Name Description Type Default Required
api_settings Settings for the APIs you need to define using this Service Principal.
object({
known_client_applications = optional(list(string), [])
mapped_claims_enabled = optional(bool, false)
requested_access_token_version = optional(number, 1)
oauth2_permission_scopes = optional(list(object({
admin_consent_description = string
admin_consent_display_name = string
enabled = optional(bool, true)
id = optional(string)
type = optional(string, "User")
user_consent_description = optional(string)
user_consent_display_name = optional(string)
value = optional(string)
})), [])
})
{} no
identifier_uris A set of user-defined URI(s) that uniquely identify an application within its Azure AD tenant, or within a verified custom domain if the application is multi-tenant. list(string) [] no
sp_aad_app_tags A set of tags to apply to the application. Tag values also propagate to any linked service principals. list(string) [] no
sp_display_name Azure Service Principal (and AAD application) display name. string n/a yes
sp_groups_member Map of AAD Groups (group name => object ID) to add this Service Principal. map(string) {} no
sp_owners A set of object IDs of principals that will be granted ownership of both the AAD Application and associated Service Principal. Supported object types are users or service principals. list(string) [] no
sp_required_resource_access List of Service Principal Application OAuth permission scopes configuration. https://registry.terraform.io/providers/hashicorp/azuread/latest/docs/resources/application#resource_access
map(list(object({
resource_access_id = string
resource_access_type = string
})))
{} no
sp_scope_assignment List of object representing the scopes and roles to assign the Service Principal with.
list(object({
scope = string
role_name = optional(string)
role_id = optional(string)

delegated_managed_identity_resource_id = optional(string)
skip_service_principal_aad_check = optional(bool, false)
}))
[] no
sp_token_validity_duration Azure Service Principal token/password duration before it expires. Defaults to 2 years. Notation documentation: https://pkg.go.dev/time#ParseDuration string "17520h" no
web_settings Configuration for web related settings for this Service Principal.
object({
homepage_url = optional(string, null)
logout_url = optional(string, null)
redirect_uris = optional(list(string), [])
access_token_issuance_enabled = optional(bool)
id_token_issuance_enabled = optional(bool)
})
{} no

Outputs

Name Description
sp_aad_groups Azure Service Principal AAD groups membership.
sp_app_id Azure Service Principal App ID.
sp_name Azure Service Principal name.
sp_object_id Azure Service Principal Object ID.
sp_required_resource_access Azure Service Principal required resource access.
sp_role_scope_assignment Azure Service Principal assigned roles and scopes.
sp_secret_key Azure Service Principal secret key/password.
sp_validity_end_date Azure Service Principal validity date.

About

Terraform module for Azure AD Service Principal and its Azure role assignment

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages