-
Notifications
You must be signed in to change notification settings - Fork 170
feat: iam policy ressource add conditions and expire at #1170
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
Open
pascalinthecloud
wants to merge
3
commits into
ovh:master
Choose a base branch
from
pascalinthecloud:feat/iam-policy-conditions-and-expire-at
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ website/node_modules | |
| *.iml | ||
| *.test | ||
| *.iml | ||
| *.env | ||
|
|
||
| website/vendor | ||
|
|
||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -9,6 +9,70 @@ import ( | |
| ) | ||
|
|
||
| func dataSourceIamPolicy() *schema.Resource { | ||
| // Define the deepest level first (e.g., 3 levels deep) | ||
| conditionLevel3Schema := &schema.Resource{ | ||
| Schema: map[string]*schema.Schema{ | ||
| "operator": { | ||
| Type: schema.TypeString, | ||
| Computed: true, | ||
| Description: "Operator for this condition (MATCH, AND, OR, NOT)", | ||
| }, | ||
| "values": { | ||
| Type: schema.TypeMap, | ||
| Computed: true, | ||
| Description: "Key-value pairs to match (e.g., resource.Tag(name), date(Europe/Paris).WeekDay, request.IP)", | ||
| Elem: &schema.Schema{Type: schema.TypeString}, | ||
| }, | ||
| // No further "condition" Elem here to limit depth | ||
| }, | ||
| } | ||
|
|
||
| // Define the second level of conditions, pointing to the third level | ||
| conditionLevel2Schema := &schema.Resource{ | ||
| Schema: map[string]*schema.Schema{ | ||
| "operator": { | ||
| Type: schema.TypeString, | ||
| Computed: true, | ||
| Description: "Operator for this condition (MATCH, AND, OR, NOT)", | ||
| }, | ||
| "values": { | ||
| Type: schema.TypeMap, | ||
| Computed: true, | ||
| Description: "Key-value pairs to match (e.g., resource.Tag(name), date(Europe/Paris).WeekDay, request.IP)", | ||
| Elem: &schema.Schema{Type: schema.TypeString}, | ||
| }, | ||
| "condition": { | ||
| Type: schema.TypeList, | ||
| Computed: true, | ||
| Description: "A list of nested conditions. This is the recursive part.", | ||
| Elem: conditionLevel3Schema, // Points to the next level | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| // Define the first level of conditions, pointing to the second level | ||
| conditionLevel1Schema := &schema.Resource{ | ||
| Schema: map[string]*schema.Schema{ | ||
| "operator": { | ||
| Type: schema.TypeString, | ||
| Computed: true, | ||
| Description: "Operator for this condition (MATCH, AND, OR, NOT)", | ||
| }, | ||
| "values": { | ||
| Type: schema.TypeMap, | ||
| Computed: true, | ||
| Description: "Key-value pairs to match (e.g., resource.Tag(name), date(Europe/Paris).WeekDay, request.IP)", | ||
| Elem: &schema.Schema{Type: schema.TypeString}, | ||
| }, | ||
| "condition": { | ||
| Type: schema.TypeList, | ||
| Computed: true, | ||
| Description: "A list of nested conditions. This is the recursive part.", | ||
| Elem: conditionLevel2Schema, // Points to the next level | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| return &schema.Resource{ | ||
| Schema: map[string]*schema.Schema{ | ||
| "id": { | ||
|
|
@@ -81,6 +145,17 @@ func dataSourceIamPolicy() *schema.Resource { | |
| Type: schema.TypeBool, | ||
| Computed: true, | ||
| }, | ||
| "expired_at": { | ||
| Type: schema.TypeString, | ||
| Computed: true, | ||
| Description: "Expiration date of the policy, after this date it will no longer be applied", | ||
| }, | ||
| "conditions": { | ||
| Type: schema.TypeList, | ||
| Computed: true, | ||
| Description: "Conditions restrict permissions following resources, date or customer's information", | ||
| Elem: conditionLevel1Schema, // The top-level conditions use the first level schema | ||
| }, | ||
| }, | ||
| ReadContext: datasourceIamPolicyRead, | ||
| } | ||
|
|
@@ -96,12 +171,20 @@ func datasourceIamPolicyRead(ctx context.Context, d *schema.ResourceData, meta a | |
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| for k, v := range pol.ToMap() { | ||
| err := d.Set(k, v) | ||
| if err != nil { | ||
| return diag.Errorf("key: %s; value: %v; err: %v", k, v, err) | ||
| } | ||
| // Debug: Log what we got from the API | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment to remove ? |
||
| polMap := pol.ToMap() | ||
| for k, v := range polMap { | ||
| d.Set(k, v) | ||
| } | ||
|
|
||
| // Explicitly set the new attributes to ensure they're available | ||
| if pol.ExpiredAt != "" { | ||
| d.Set("expired_at", pol.ExpiredAt) | ||
| } | ||
| if pol.Conditions != nil { | ||
| d.Set("conditions", []interface{}{conditionsToMap(pol.Conditions)}) | ||
| } | ||
|
|
||
| d.SetId(id) | ||
| return nil | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -73,6 +73,40 @@ func TestAccIamPolicyDataSource_basic(t *testing.T) { | |
| }) | ||
| } | ||
|
|
||
| func TestAccIamPolicyDataSource_withConditionsAndExpiration(t *testing.T) { | ||
| name := acctest.RandomWithPrefix(test_prefix) | ||
| desc := "IAM policy with conditions and expiration created by Terraform Acc" | ||
| userName := acctest.RandomWithPrefix(test_prefix) | ||
| res := "urn:v1:eu:resource:vps:*" | ||
| expiration := "2025-12-31T23:59:59Z" | ||
| config := fmt.Sprintf(testAccIamPolicyDataSourceConfig, userName, userName, name, desc, res, expiration) | ||
|
|
||
| resource.Test(t, resource.TestCase{ | ||
| PreCheck: func() { testAccPreCheckCredentials(t) }, | ||
| Providers: testAccProviders, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| Config: config, | ||
| Check: resource.ComposeTestCheckFunc( | ||
| resource.TestCheckResourceAttr("data.ovh_iam_policy.policy", "name", name), | ||
| resource.TestCheckResourceAttr("data.ovh_iam_policy.policy", "description", desc), | ||
| resource.TestCheckResourceAttr("data.ovh_iam_policy.policy", "expired_at", expiration), | ||
| resource.TestCheckResourceAttr("data.ovh_iam_policy.policy", "conditions.#", "1"), | ||
| resource.TestCheckResourceAttr("data.ovh_iam_policy.policy", "conditions.0.operator", "OR"), | ||
| resource.TestCheckResourceAttr("data.ovh_iam_policy.policy", "conditions.0.condition.#", "2"), | ||
| resource.TestCheckResourceAttr("data.ovh_iam_policy.policy", "conditions.0.condition.0.operator", "MATCH"), | ||
| resource.TestCheckResourceAttr("data.ovh_iam_policy.policy", "conditions.0.condition.0.values.%", "2"), | ||
| resource.TestCheckResourceAttr("data.ovh_iam_policy.policy", "conditions.0.condition.0.values.resource.Tag(environment)", "production"), | ||
| resource.TestCheckResourceAttr("data.ovh_iam_policy.policy", "conditions.0.condition.0.values.resource.Tag(team)", "platform"), | ||
| resource.TestCheckResourceAttr("data.ovh_iam_policy.policy", "conditions.0.condition.1.operator", "MATCH"), | ||
| resource.TestCheckResourceAttr("data.ovh_iam_policy.policy", "conditions.0.condition.1.values.%", "1"), | ||
| resource.TestCheckResourceAttr("data.ovh_iam_policy.policy", "conditions.0.condition.1.values.date(Europe/Paris).WeekDay", "monday"), | ||
| ), | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| func checkIamPolicyResourceAttr(name, polName, desc, resourceURN, allowAction, exceptAction, denyAction string) []resource.TestCheckFunc { | ||
| // we are not checking identity urn because they are dynamic and depend on the test account NIC | ||
| checks := []resource.TestCheckFunc{ | ||
|
|
@@ -158,3 +192,43 @@ output "keys_present" { | |
| ) | ||
| } | ||
| ` | ||
|
|
||
| const testAccIamPolicyDataSourceConfig = ` | ||
| resource "ovh_me_identity_user" "test_user" { | ||
| login = "%s" | ||
| email = "%[email protected]" | ||
| password = "qwe123!@#" | ||
| } | ||
|
|
||
| resource "ovh_iam_policy" "policy1" { | ||
| name = "%s" | ||
| description = "%s" | ||
| identities = [ovh_me_identity_user.test_user.urn] | ||
| resources = ["%s"] | ||
| allow = ["vps:apiovh:*"] | ||
| expired_at = "%s" | ||
|
|
||
| conditions { | ||
| operator = "OR" | ||
|
|
||
| condition { | ||
| operator = "MATCH" | ||
| values = { | ||
| "resource.Tag(environment)" = "production" | ||
| "resource.Tag(team)" = "platform" | ||
| } | ||
| } | ||
|
|
||
| condition { | ||
| operator = "MATCH" | ||
| values = { | ||
| "date(Europe/Paris).WeekDay" = "monday" | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| data "ovh_iam_policy" "policy" { | ||
| id = ovh_iam_policy.policy1.id | ||
| } | ||
| ` | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it could be useful to emphasize the 3-level depth limitation of
conditionshere