-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]> Signed-off-by: Modular Magician <[email protected]>
- Loading branch information
1 parent
5bc5b3b
commit 89affbe
Showing
8 changed files
with
144 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:enhancement | ||
iam: Added plan-time validation for IAM members | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package google | |
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
|
@@ -23,6 +24,7 @@ func TestAccProjectIamBinding_basic(t *testing.T) { | |
org := getTestOrgFromEnv(t) | ||
pid := fmt.Sprintf("tf-test-%d", randInt(t)) | ||
role := "roles/compute.instanceAdmin" | ||
member := "user:[email protected]" | ||
vcrTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
|
@@ -36,7 +38,7 @@ func TestAccProjectIamBinding_basic(t *testing.T) { | |
}, | ||
// Apply an IAM binding | ||
{ | ||
Config: testAccProjectAssociateBindingBasic(pid, pname, org, role), | ||
Config: testAccProjectAssociateBindingBasic(pid, pname, org, role, member), | ||
}, | ||
projectIamBindingImportStep("google_project_iam_binding.acceptance", pid, role), | ||
}, | ||
|
@@ -51,6 +53,7 @@ func TestAccProjectIamBinding_multiple(t *testing.T) { | |
pid := fmt.Sprintf("tf-test-%d", randInt(t)) | ||
role := "roles/compute.instanceAdmin" | ||
role2 := "roles/viewer" | ||
member := "user:[email protected]" | ||
|
||
vcrTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
|
@@ -65,7 +68,7 @@ func TestAccProjectIamBinding_multiple(t *testing.T) { | |
}, | ||
// Apply an IAM binding | ||
{ | ||
Config: testAccProjectAssociateBindingBasic(pid, pname, org, role), | ||
Config: testAccProjectAssociateBindingBasic(pid, pname, org, role, member), | ||
}, | ||
// Apply another IAM binding | ||
{ | ||
|
@@ -116,6 +119,7 @@ func TestAccProjectIamBinding_update(t *testing.T) { | |
org := getTestOrgFromEnv(t) | ||
pid := fmt.Sprintf("tf-test-%d", randInt(t)) | ||
role := "roles/compute.instanceAdmin" | ||
member := "user:[email protected]" | ||
|
||
vcrTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
|
@@ -130,7 +134,7 @@ func TestAccProjectIamBinding_update(t *testing.T) { | |
}, | ||
// Apply an IAM binding | ||
{ | ||
Config: testAccProjectAssociateBindingBasic(pid, pname, org, role), | ||
Config: testAccProjectAssociateBindingBasic(pid, pname, org, role, member), | ||
}, | ||
projectIamBindingImportStep("google_project_iam_binding.acceptance", pid, role), | ||
|
||
|
@@ -248,7 +252,29 @@ func TestAccProjectIamBinding_withCondition(t *testing.T) { | |
}) | ||
} | ||
|
||
func testAccProjectAssociateBindingBasic(pid, name, org, role string) string { | ||
// Test that an IAM binding with invalid members returns an error. | ||
func TestAccProjectIamBinding_invalidMembers(t *testing.T) { | ||
t.Parallel() | ||
|
||
org := getTestOrgFromEnv(t) | ||
pid := fmt.Sprintf("tf-test-%d", randInt(t)) | ||
role := "roles/compute.instanceAdmin" | ||
vcrTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccProjectAssociateBindingBasic(pid, pname, org, role, "[email protected]"), | ||
ExpectError: regexp.MustCompile("invalid value for members\\.0 \\(IAM members must have one of the values outlined here: https://cloud.google.com/billing/docs/reference/rest/v1/Policy#Binding\\)"), | ||
}, | ||
{ | ||
Config: testAccProjectAssociateBindingBasic(pid, pname, org, role, "user:[email protected]"), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccProjectAssociateBindingBasic(pid, name, org, role, member string) string { | ||
return fmt.Sprintf(` | ||
resource "google_project" "acceptance" { | ||
project_id = "%s" | ||
|
@@ -258,10 +284,10 @@ resource "google_project" "acceptance" { | |
resource "google_project_iam_binding" "acceptance" { | ||
project = google_project.acceptance.project_id | ||
members = ["user:[email protected]"] | ||
members = ["%s"] | ||
role = "%s" | ||
} | ||
`, pid, name, org, role) | ||
`, pid, name, org, member, role) | ||
} | ||
|
||
func testAccProjectAssociateBindingMultiple(pid, name, org, role, role2 string) string { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package google | |
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
|
@@ -167,6 +168,28 @@ func TestAccProjectIamMember_withCondition(t *testing.T) { | |
}) | ||
} | ||
|
||
func TestAccProjectIamMember_invalidMembers(t *testing.T) { | ||
t.Parallel() | ||
|
||
org := getTestOrgFromEnv(t) | ||
pid := fmt.Sprintf("tf-test-%d", randInt(t)) | ||
role := "roles/compute.instanceAdmin" | ||
|
||
vcrTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccProjectAssociateMemberBasic(pid, pname, org, role, "[email protected]"), | ||
ExpectError: regexp.MustCompile("invalid value for member \\(IAM members must have one of the values outlined here: https://cloud.google.com/billing/docs/reference/rest/v1/Policy#Binding\\)"), | ||
}, | ||
{ | ||
Config: testAccProjectAssociateMemberBasic(pid, pname, org, role, "user:[email protected]"), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccProjectAssociateMemberBasic(pid, name, org, role, member string) string { | ||
return fmt.Sprintf(` | ||
resource "google_project" "acceptance" { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package google | |
import ( | ||
"encoding/json" | ||
"fmt" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
|
@@ -16,6 +17,7 @@ func TestAccProjectIamPolicy_basic(t *testing.T) { | |
|
||
org := getTestOrgFromEnv(t) | ||
pid := fmt.Sprintf("tf-test-%d", randInt(t)) | ||
member := "user:[email protected]" | ||
vcrTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
|
@@ -30,7 +32,7 @@ func TestAccProjectIamPolicy_basic(t *testing.T) { | |
// Apply an IAM policy from a data source. The application | ||
// merges policies, so we validate the expected state. | ||
{ | ||
Config: testAccProjectAssociatePolicyBasic(pid, pname, org), | ||
Config: testAccProjectAssociatePolicyBasic(pid, pname, org, member), | ||
}, | ||
{ | ||
ResourceName: "google_project_iam_policy.acceptance", | ||
|
@@ -156,6 +158,28 @@ func TestAccProjectIamPolicy_withCondition(t *testing.T) { | |
}) | ||
} | ||
|
||
// Test that an IAM policy with invalid members returns errors. | ||
func TestAccProjectIamPolicy_invalidMembers(t *testing.T) { | ||
t.Parallel() | ||
|
||
org := getTestOrgFromEnv(t) | ||
pid := fmt.Sprintf("tf-test-%d", randInt(t)) | ||
|
||
vcrTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccProjectAssociatePolicyBasic(pid, pname, org, "[email protected]"), | ||
ExpectError: regexp.MustCompile("invalid value for bindings\\.1\\.members\\.0 \\(IAM members must have one of the values outlined here: https://cloud.google.com/billing/docs/reference/rest/v1/Policy#Binding\\)"), | ||
}, | ||
{ | ||
Config: testAccProjectAssociatePolicyBasic(pid, pname, org, "user:[email protected]"), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func getStatePrimaryResource(s *terraform.State, res, expectedID string) (*terraform.InstanceState, error) { | ||
// Get the project resource | ||
resource, ok := s.RootModule().Resources[res] | ||
|
@@ -228,7 +252,7 @@ func testAccProjectExistingPolicy(t *testing.T, pid string) resource.TestCheckFu | |
} | ||
} | ||
|
||
func testAccProjectAssociatePolicyBasic(pid, name, org string) string { | ||
func testAccProjectAssociatePolicyBasic(pid, name, org, member string) string { | ||
return fmt.Sprintf(` | ||
resource "google_project" "acceptance" { | ||
project_id = "%s" | ||
|
@@ -245,7 +269,7 @@ data "google_iam_policy" "admin" { | |
binding { | ||
role = "roles/storage.objectViewer" | ||
members = [ | ||
"user:[email protected]", | ||
"%s", | ||
] | ||
} | ||
binding { | ||
|
@@ -256,7 +280,7 @@ data "google_iam_policy" "admin" { | |
] | ||
} | ||
} | ||
`, pid, name, org) | ||
`, pid, name, org, member) | ||
} | ||
|
||
func testAccProjectAssociatePolicyAuditConfigBasic(pid, name, org string) string { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters