Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azuread_application: unknown scope/role IDs and values are flagged as duplicates #528

Merged
merged 3 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions docs/resources/application.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ resource "azuread_application" "example" {
allowed_member_types = ["User", "Application"]
description = "Admins can manage roles and perform all task actions"
display_name = "Admin"
is_enabled = true
enabled = true
id = "1b19509b-32b1-4e9f-b71d-4992aa991967"
value = "admin"
}

Expand All @@ -70,7 +71,7 @@ resource "azuread_application" "example" {
description = "ReadOnly roles have limited query access"
display_name = "ReadOnly"
enabled = true
id = "%[6]s"
id = "497406e4-012a-4267-bf18-45a1cb148a01"
value = "User"
}

Expand Down
5 changes: 3 additions & 2 deletions docs/resources/group.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ The following arguments are supported:
* `mail_enabled` - (Optional) Whether the group is a mail enabled, with a shared group mailbox. At least one of `mail_enabled` or `security_enabled` must be specified. Only Microsoft 365 groups can be mail enabled (see the `types` property).
* `mail_nickname` - (Optional) The mail alias for the group, unique in the organisation. Required for mail-enabled groups. Changing this forces a new resource to be created.
* `members` - (Optional) A set of members who should be present in this group. Supported object types are Users, Groups or Service Principals.

!> **Warning** Do not use the `azuread_group_member` resource at the same time as the `members` argument.
manicminer marked this conversation as resolved.
Show resolved Hide resolved

* `owners` - (Optional) A set of object IDs of principals that will be granted ownership of the group. Supported object types are users or service principals. By default, the principal being used to execute Terraform is assigned as the sole owner. Groups cannot be created with no owners or have all their owners removed.

-> **Group Ownership** It's recommended to always specify one or more group owners, including the principal being used to execute Terraform, such as in the example above. When removing group owners, if a user principal has been assigned ownership, the last user cannot be removed as an owner. Microsoft 365 groups are required to always have at least one owner which _must be a user_ (i.e. not a service principal).
Expand All @@ -105,8 +108,6 @@ The following arguments are supported:

-> **Group Name Uniqueness** Group names are not unique within Azure Active Directory. Use the `prevent_duplicate_names` argument to check for existing groups if you want to avoid name collisions.

!> **Warning** Do not use the `azuread_group_member` resource at the same time as the `members` argument.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:
Expand Down
6 changes: 6 additions & 0 deletions internal/acceptance/testcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ func (td TestData) ResourceTestIgnoreDangling(t *testing.T, _ types.TestResource
}

func (td TestData) runAcceptanceTest(t *testing.T, testCase resource.TestCase) {
testCase.ExternalProviders = map[string]resource.ExternalProvider{
"random": {
Source: "hashicorp/random",
VersionConstraint: ">= 3.0.0",
},
}
testCase.ProviderFactories = map[string]func() (*schema.Provider, error){
"azuread": func() (*schema.Provider, error) {
return AzureADProvider, nil
Expand Down
52 changes: 52 additions & 0 deletions internal/services/applications/application_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,23 @@ func TestAccApplication_appRoles(t *testing.T) {
})
}

func TestAccApplication_duplicateAppRolesOauth2PermissionsIdsUnknown(t *testing.T) {
data := acceptance.BuildTestData(t, "azuread_application", "test")
r := ApplicationResource{}

data.ResourceTest(t, r, []resource.TestStep{
{
Config: r.duplicateAppRolesOauth2PermissionsIdsUnknown(data),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("app_role.#").HasValue("1"),
check.That(data.ResourceName).Key("app_role_ids.%").HasValue("1"),
check.That(data.ResourceName).Key("api.0.oauth2_permission_scope.#").HasValue("1"),
check.That(data.ResourceName).Key("oauth2_permission_scope_ids.%").HasValue("1"),
),
},
})
}

func TestAccApplication_duplicateAppRolesOauth2PermissionsValues(t *testing.T) {
data := acceptance.BuildTestData(t, "azuread_application", "test")
r := ApplicationResource{}
Expand Down Expand Up @@ -877,6 +894,41 @@ resource "azuread_application" "test" {
`, data.RandomInteger, uuids[0], uuids[1], uuids[2], uuids[3])
}

func (ApplicationResource) duplicateAppRolesOauth2PermissionsIdsUnknown(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azuread" {}
provider "random" {}

resource "random_uuid" "test" {
count = 2
}

resource "azuread_application" "test" {
display_name = "acctest-APP-%[1]d"

api {
oauth2_permission_scope {
admin_consent_description = "Administer the application"
admin_consent_display_name = "Administer"
enabled = true
id = random_uuid.test[0].id
type = "Admin"
value = "administer"
}
}

app_role {
allowed_member_types = ["User"]
description = "Admins can manage roles and perform all task actions"
display_name = "Admin"
enabled = true
id = random_uuid.test[1].id
value = "administrate"
}
}
`, data.RandomInteger, data.UUID(), data.UUID())
}

func (ApplicationResource) duplicateAppRolesOauth2PermissionsValues(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azuread" {}
Expand Down
8 changes: 4 additions & 4 deletions internal/services/applications/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,20 +311,20 @@ func applicationValidateRolesScopes(appRoles, oauth2Permissions []interface{}) e

for _, roleRaw := range appRoles {
role := roleRaw.(map[string]interface{})
if id := role["id"].(string); id != "" {
if id := role["id"].(string); id != "" && id != tf.PluginSdkUnknownValue {
ids = append(ids, id)
}
if val := role["value"].(string); val != "" {
if val := role["value"].(string); val != "" && val != tf.PluginSdkUnknownValue {
values = append(values, val)
}
}

for _, scopeRaw := range oauth2Permissions {
scope := scopeRaw.(map[string]interface{})
if id := scope["id"].(string); id != "" {
if id := scope["id"].(string); id != "" && id != tf.PluginSdkUnknownValue {
ids = append(ids, id)
}
if val := scope["value"].(string); val != "" {
if val := scope["value"].(string); val != "" && val != tf.PluginSdkUnknownValue {
values = append(values, val)
}
}
Expand Down
6 changes: 6 additions & 0 deletions internal/tf/pluginsdk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package tf

// PluginSdkUnknownValue is a dummy value used/sent by the plugin SDK when a real value is not known at plan time,
// e.g. during a CustomizeDiff function
// See https://github.com/hashicorp/terraform-plugin-sdk/blob/main/internal/configs/hcl2shim/values.go#L16
const PluginSdkUnknownValue = "74D93920-ED26-11E3-AC10-0800200C9A66"