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

d/aws_iam_session_context: New data source #19957

Merged
merged 29 commits into from
Jun 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
d86a246
caller_identity: Add source role
YakDriver Jun 23, 2021
b81de6a
d/caller_identity: Add source_arn
YakDriver Jun 24, 2021
11d04ce
tests/d/caller_identity: Unit test source_arn
YakDriver Jun 24, 2021
c659b64
docs/d/caller_identity: Docs for source_arn
YakDriver Jun 24, 2021
62d7c93
d/caller_identity: Add changelog
YakDriver Jun 24, 2021
984d0cd
docs/d/caller_identity: Add example
YakDriver Jun 24, 2021
4ecde63
tests/d/caller_identity: Appease dear linter
YakDriver Jun 24, 2021
f887951
ds/caller_identity: Revert to previous
YakDriver Jun 25, 2021
ad1f9a3
ds/iam_assumed_role_source: Update changelog
YakDriver Jun 25, 2021
ae63556
i/iam_role: Add role finder
YakDriver Jun 25, 2021
1125604
provider: Add new data source
YakDriver Jun 25, 2021
3353636
d/iam_assumed_role_source: New data source
YakDriver Jun 25, 2021
1cdcdf2
tests/d/iam_assumed_role_source: New data source
YakDriver Jun 25, 2021
8394cf0
docs/d/iam_assumed_role_source: New data source
YakDriver Jun 25, 2021
914cd83
tests/d/iam_assumed_role_source: Remove comments
YakDriver Jun 25, 2021
39f176a
docs/d/caller_identity: Remove old attribute
YakDriver Jun 25, 2021
a4704cc
docs/d/iam_assumed_role_source: Clarify docs
YakDriver Jun 25, 2021
1a02f96
iam: Rename role finder for general use
YakDriver Jun 28, 2021
8abcda5
d/iam_assumed_role: Rework ARN helper function
YakDriver Jun 28, 2021
074ec4e
tests/d/iam_assumed_role: Rework ARN helper function
YakDriver Jun 28, 2021
c096125
d/iam_assumed_role: Add extra check for nil
YakDriver Jun 28, 2021
aa4183c
d/iam_session_context: Refactor
YakDriver Jun 28, 2021
3f2f55b
d/iam_session_context: Refactor attribute
YakDriver Jun 28, 2021
4947200
d/iam_session_context: Update changelog
YakDriver Jun 28, 2021
8c76c24
d/iam_session_context: Refactor arguments, attributes
YakDriver Jun 28, 2021
1232685
docs/d/iam_session_context: Fix docs
YakDriver Jun 28, 2021
db6575b
d/iam_session_context: Rework helper
YakDriver Jun 28, 2021
a824e7c
Update website/docs/d/iam_session_context.markdown
YakDriver Jun 28, 2021
4252de0
Fix docs
YakDriver Jun 28, 2021
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
3 changes: 3 additions & 0 deletions .changelog/19957.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_iam_session_context
```
2 changes: 1 addition & 1 deletion aws/data_source_aws_caller_identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func dataSourceAwsCallerIdentityRead(d *schema.ResourceData, meta interface{}) e
res, err := client.GetCallerIdentity(&sts.GetCallerIdentityInput{})

if err != nil {
return fmt.Errorf("Error getting Caller Identity: %w", err)
return fmt.Errorf("getting Caller Identity: %w", err)
}

log.Printf("[DEBUG] Received Caller Identity: %s", res)
Expand Down
128 changes: 128 additions & 0 deletions aws/data_source_aws_iam_session_context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package aws

import (
"fmt"
"regexp"
"strings"

"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/iam/finder"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/iam/waiter"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource"
)

func dataSourceAwsIAMSessionContext() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsIAMSessionContextRead,

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateArn,
},
"issuer_arn": {
Type: schema.TypeString,
Computed: true,
},
"issuer_id": {
Type: schema.TypeString,
Computed: true,
},
"issuer_name": {
Type: schema.TypeString,
Computed: true,
},
"session_name": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceAwsIAMSessionContextRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).iamconn

arn := d.Get("arn").(string)

d.SetId(arn)

roleName := ""
sessionName := ""
var err error

if roleName, sessionName = roleNameSessionFromARN(arn); roleName == "" {
d.Set("issuer_arn", arn)
d.Set("issuer_id", "")
d.Set("issuer_name", "")
d.Set("session_name", "")

return nil
}

var role *iam.Role

err = resource.Retry(waiter.PropagationTimeout, func() *resource.RetryError {
var err error

role, err = finder.Role(conn, roleName)

if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, iam.ErrCodeNoSuchEntityException) {
return resource.RetryableError(err)
}

if err != nil {
return resource.NonRetryableError(err)
}

return nil
})

if tfresource.TimedOut(err) {
role, err = finder.Role(conn, roleName)
}

if err != nil {
return fmt.Errorf("unable to get role (%s): %w", roleName, err)
}

if role == nil || role.Arn == nil {
return fmt.Errorf("empty role returned (%s)", roleName)
}

d.Set("issuer_arn", role.Arn)
d.Set("issuer_id", role.RoleId)
d.Set("issuer_name", roleName)
d.Set("session_name", sessionName)

return nil
}

// roleNameSessionFromARN returns the role and session names in an ARN if any.
// Otherwise, it returns empty strings.
func roleNameSessionFromARN(rawARN string) (string, string) {
parsedARN, err := arn.Parse(rawARN)

if err != nil {
return "", ""
}

reAssume := regexp.MustCompile(`^assumed-role/.{1,}/.{2,}`)

if !reAssume.MatchString(parsedARN.Resource) || parsedARN.Service != "sts" {
return "", ""
}

parts := strings.Split(parsedARN.Resource, "/")

if len(parts) < 3 {
return "", ""
}

return parts[len(parts)-2], parts[len(parts)-1]
}
Loading