github.com/pulumi/pulumi-aws/sdk/v6@v6.32.0/go/aws/iam/getPrincipalPolicySimulation.go (about) 1 // Code generated by the Pulumi Terraform Bridge (tfgen) Tool DO NOT EDIT. 2 // *** WARNING: Do not edit by hand unless you're certain you know what you are doing! *** 3 4 package iam 5 6 import ( 7 "context" 8 "reflect" 9 10 "github.com/pulumi/pulumi-aws/sdk/v6/go/aws/internal" 11 "github.com/pulumi/pulumi/sdk/v3/go/pulumi" 12 ) 13 14 // Runs a simulation of the IAM policies of a particular principal against a given hypothetical request. 15 // 16 // You can use this data source in conjunction with 17 // Preconditions and Postconditions so that your configuration can test either whether it should have sufficient access to do its own work, or whether policies your configuration declares itself are sufficient for their intended use elsewhere. 18 // 19 // > **Note:** Correctly using this data source requires familiarity with various details of AWS Identity and Access Management, and how various AWS services integrate with it. For general information on the AWS IAM policy simulator, see [Testing IAM policies with the IAM policy simulator](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_testing-policies.html). This data source wraps the `iam:SimulatePrincipalPolicy` API action described on that page. 20 // 21 // ## Example Usage 22 // 23 // ### Self Access-checking Example 24 // 25 // The following example raises an error if the credentials passed to the AWS provider do not have access to perform the three actions `s3:GetObject`, `s3:PutObject`, and `s3:DeleteObject` on the S3 bucket with the given ARN. 26 // 27 // <!--Start PulumiCodeChooser --> 28 // ```go 29 // package main 30 // 31 // import ( 32 // 33 // "github.com/pulumi/pulumi-aws/sdk/v6/go/aws" 34 // "github.com/pulumi/pulumi-aws/sdk/v6/go/aws/iam" 35 // "github.com/pulumi/pulumi/sdk/v3/go/pulumi" 36 // 37 // ) 38 // 39 // func main() { 40 // pulumi.Run(func(ctx *pulumi.Context) error { 41 // current, err := aws.GetCallerIdentity(ctx, nil, nil) 42 // if err != nil { 43 // return err 44 // } 45 // _, err = iam.LookupPrincipalPolicySimulation(ctx, &iam.LookupPrincipalPolicySimulationArgs{ 46 // ActionNames: []string{ 47 // "s3:GetObject", 48 // "s3:PutObject", 49 // "s3:DeleteObject", 50 // }, 51 // PolicySourceArn: current.Arn, 52 // ResourceArns: []string{ 53 // "arn:aws:s3:::my-test-bucket", 54 // }, 55 // }, nil) 56 // if err != nil { 57 // return err 58 // } 59 // return nil 60 // }) 61 // } 62 // 63 // ``` 64 // <!--End PulumiCodeChooser --> 65 // 66 // If you intend to use this data source to quickly raise an error when the given credentials are insufficient then you must use `dependsOn` inside any resource which would require those credentials, to ensure that the policy check will run first: 67 // 68 // <!--Start PulumiCodeChooser --> 69 // ```go 70 // package main 71 // 72 // import ( 73 // 74 // "github.com/pulumi/pulumi-aws/sdk/v6/go/aws/s3" 75 // "github.com/pulumi/pulumi/sdk/v3/go/pulumi" 76 // 77 // ) 78 // 79 // func main() { 80 // pulumi.Run(func(ctx *pulumi.Context) error { 81 // _, err := s3.NewBucketObject(ctx, "example", &s3.BucketObjectArgs{ 82 // Bucket: pulumi.Any("my-test-bucket"), 83 // }, pulumi.DependsOn([]pulumi.Resource{ 84 // s3ObjectAccess, 85 // })) 86 // if err != nil { 87 // return err 88 // } 89 // return nil 90 // }) 91 // } 92 // 93 // ``` 94 // <!--End PulumiCodeChooser --> 95 // 96 // ### Testing the Effect of a Declared Policy 97 // 98 // The following example declares an S3 bucket and a user that should have access to the bucket, and then uses `iam.getPrincipalPolicySimulation` to verify that the user does indeed have access to perform needed operations against the bucket. 99 // 100 // <!--Start PulumiCodeChooser --> 101 // ```go 102 // package main 103 // 104 // import ( 105 // 106 // "encoding/json" 107 // "fmt" 108 // 109 // "github.com/pulumi/pulumi-aws/sdk/v6/go/aws" 110 // "github.com/pulumi/pulumi-aws/sdk/v6/go/aws/iam" 111 // "github.com/pulumi/pulumi-aws/sdk/v6/go/aws/s3" 112 // "github.com/pulumi/pulumi/sdk/v3/go/pulumi" 113 // 114 // ) 115 // 116 // func main() { 117 // pulumi.Run(func(ctx *pulumi.Context) error { 118 // current, err := aws.GetCallerIdentity(ctx, nil, nil) 119 // if err != nil { 120 // return err 121 // } 122 // example, err := iam.NewUser(ctx, "example", &iam.UserArgs{ 123 // Name: pulumi.String("example"), 124 // }) 125 // if err != nil { 126 // return err 127 // } 128 // exampleBucketV2, err := s3.NewBucketV2(ctx, "example", &s3.BucketV2Args{ 129 // Bucket: pulumi.String("my-test-bucket"), 130 // }) 131 // if err != nil { 132 // return err 133 // } 134 // _, err = iam.NewUserPolicy(ctx, "s3_access", &iam.UserPolicyArgs{ 135 // Name: pulumi.String("example_s3_access"), 136 // User: example.Name, 137 // Policy: exampleBucketV2.Arn.ApplyT(func(arn string) (pulumi.String, error) { 138 // var _zero pulumi.String 139 // tmpJSON0, err := json.Marshal(map[string]interface{}{ 140 // "Version": "2012-10-17", 141 // "Statement": []map[string]interface{}{ 142 // map[string]interface{}{ 143 // "Action": "s3:GetObject", 144 // "Effect": "Allow", 145 // "Resource": arn, 146 // }, 147 // }, 148 // }) 149 // if err != nil { 150 // return _zero, err 151 // } 152 // json0 := string(tmpJSON0) 153 // return pulumi.String(json0), nil 154 // }).(pulumi.StringOutput), 155 // }) 156 // if err != nil { 157 // return err 158 // } 159 // accountAccess, err := s3.NewBucketPolicy(ctx, "account_access", &s3.BucketPolicyArgs{ 160 // Bucket: exampleBucketV2.Bucket, 161 // Policy: pulumi.All(exampleBucketV2.Arn, exampleBucketV2.Arn).ApplyT(func(_args []interface{}) (string, error) { 162 // exampleBucketV2Arn := _args[0].(string) 163 // exampleBucketV2Arn1 := _args[1].(string) 164 // var _zero string 165 // tmpJSON1, err := json.Marshal(map[string]interface{}{ 166 // "Version": "2012-10-17", 167 // "Statement": []map[string]interface{}{ 168 // map[string]interface{}{ 169 // "Action": "s3:*", 170 // "Effect": "Allow", 171 // "Principal": map[string]interface{}{ 172 // "AWS": current.AccountId, 173 // }, 174 // "Resource": []string{ 175 // exampleBucketV2Arn, 176 // fmt.Sprintf("%v/*", exampleBucketV2Arn1), 177 // }, 178 // }, 179 // }, 180 // }) 181 // if err != nil { 182 // return _zero, err 183 // } 184 // json1 := string(tmpJSON1) 185 // return json1, nil 186 // }).(pulumi.StringOutput), 187 // }) 188 // if err != nil { 189 // return err 190 // } 191 // _ = iam.LookupPrincipalPolicySimulationOutput(ctx, iam.GetPrincipalPolicySimulationOutputArgs{ 192 // ActionNames: pulumi.StringArray{ 193 // pulumi.String("s3:GetObject"), 194 // }, 195 // PolicySourceArn: example.Arn, 196 // ResourceArns: pulumi.StringArray{ 197 // exampleBucketV2.Arn, 198 // }, 199 // ResourcePolicyJson: accountAccess.Policy, 200 // }, nil) 201 // return nil 202 // }) 203 // } 204 // 205 // ``` 206 // <!--End PulumiCodeChooser --> 207 // 208 // When using `iam.getPrincipalPolicySimulation` to test the effect of a policy declared elsewhere in the same configuration, it's important to use `dependsOn` to make sure that the needed policy has been fully created or updated before running the simulation. 209 func LookupPrincipalPolicySimulation(ctx *pulumi.Context, args *LookupPrincipalPolicySimulationArgs, opts ...pulumi.InvokeOption) (*LookupPrincipalPolicySimulationResult, error) { 210 opts = internal.PkgInvokeDefaultOpts(opts) 211 var rv LookupPrincipalPolicySimulationResult 212 err := ctx.Invoke("aws:iam/getPrincipalPolicySimulation:getPrincipalPolicySimulation", args, &rv, opts...) 213 if err != nil { 214 return nil, err 215 } 216 return &rv, nil 217 } 218 219 // A collection of arguments for invoking getPrincipalPolicySimulation. 220 type LookupPrincipalPolicySimulationArgs struct { 221 // A set of IAM action names to run simulations for. Each entry in this set adds an additional hypothetical request to the simulation. 222 // 223 // Action names consist of a service prefix and an action verb separated by a colon, such as `s3:GetObject`. Refer to [Actions, resources, and condition keys for AWS services](https://docs.aws.amazon.com/service-authorization/latest/reference/reference_policies_actions-resources-contextkeys.html) to see the full set of possible IAM action names across all AWS services. 224 ActionNames []string `pulumi:"actionNames"` 225 // A set of additional principal policy documents to include in the simulation. The simulator will behave as if each of these policies were associated with the object specified in `policySourceArn`, allowing you to test the effect of hypothetical policies not yet created. 226 AdditionalPoliciesJsons []string `pulumi:"additionalPoliciesJsons"` 227 // The ARN of an user that will appear as the "caller" of the simulated requests. If you do not specify `callerArn` then the simulation will use the `policySourceArn` instead, if it contains a user ARN. 228 CallerArn *string `pulumi:"callerArn"` 229 // Each `context` block defines an entry in the table of additional context keys in the simulated request. 230 // 231 // IAM uses context keys for both custom conditions and for interpolating dynamic request-specific values into policy values. If you use policies that include those features then you will need to provide suitable example values for those keys to achieve a realistic simulation. 232 Contexts []GetPrincipalPolicySimulationContext `pulumi:"contexts"` 233 // A set of [permissions boundary policy documents](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_boundaries.html) to include in the simulation. 234 PermissionsBoundaryPoliciesJsons []string `pulumi:"permissionsBoundaryPoliciesJsons"` 235 // The [ARN](https://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html) of the IAM user, group, or role whose policies will be included in the simulation. 236 // 237 // You must closely match the form of the real service request you are simulating in order to achieve a realistic result. You can use the following additional arguments to specify other characteristics of the simulated requests: 238 PolicySourceArn string `pulumi:"policySourceArn"` 239 // A set of ARNs of resources to include in the simulation. 240 // 241 // This argument is important for actions that have either required or optional resource types listed in [Actions, resources, and condition keys for AWS services](https://docs.aws.amazon.com/service-authorization/latest/reference/reference_policies_actions-resources-contextkeys.html), and you must provide ARNs that identify AWS objects of the appropriate types for the chosen actions. 242 // 243 // The policy simulator only automatically loads policies associated with the `policySourceArn`, so if your given resources have their own resource-level policy then you'll also need to provide that explicitly using the `resourcePolicyJson` argument to achieve a realistic simulation. 244 ResourceArns []string `pulumi:"resourceArns"` 245 // Specifies a special simulation type to run. Some EC2 actions require special simulation behaviors and a particular set of resource ARNs to achieve a realistic result. 246 // 247 // For more details, see the `ResourceHandlingOption` request parameter for [the underlying `iam:SimulatePrincipalPolicy` action](https://docs.aws.amazon.com/IAM/latest/APIReference/API_SimulatePrincipalPolicy.html). 248 ResourceHandlingOption *string `pulumi:"resourceHandlingOption"` 249 // An AWS account ID to use for any resource ARN in `resourceArns` that doesn't include its own AWS account ID. If unspecified, the simulator will use the account ID from the `callerArn` argument as a placeholder. 250 ResourceOwnerAccountId *string `pulumi:"resourceOwnerAccountId"` 251 // An IAM policy document representing the resource-level policy of all of the resources specified in `resourceArns`. 252 // 253 // The policy simulator cannot automatically load policies that are associated with individual resources, as described in the documentation for `resourceArns` above. 254 ResourcePolicyJson *string `pulumi:"resourcePolicyJson"` 255 } 256 257 // A collection of values returned by getPrincipalPolicySimulation. 258 type LookupPrincipalPolicySimulationResult struct { 259 ActionNames []string `pulumi:"actionNames"` 260 AdditionalPoliciesJsons []string `pulumi:"additionalPoliciesJsons"` 261 // `true` if all of the simulation results have decision "allowed", or `false` otherwise. 262 AllAllowed bool `pulumi:"allAllowed"` 263 CallerArn *string `pulumi:"callerArn"` 264 Contexts []GetPrincipalPolicySimulationContext `pulumi:"contexts"` 265 Id string `pulumi:"id"` 266 PermissionsBoundaryPoliciesJsons []string `pulumi:"permissionsBoundaryPoliciesJsons"` 267 PolicySourceArn string `pulumi:"policySourceArn"` 268 ResourceArns []string `pulumi:"resourceArns"` 269 ResourceHandlingOption *string `pulumi:"resourceHandlingOption"` 270 ResourceOwnerAccountId *string `pulumi:"resourceOwnerAccountId"` 271 ResourcePolicyJson *string `pulumi:"resourcePolicyJson"` 272 // A set of result objects, one for each of the simulated requests, with the following nested attributes: 273 Results []GetPrincipalPolicySimulationResult `pulumi:"results"` 274 } 275 276 func LookupPrincipalPolicySimulationOutput(ctx *pulumi.Context, args LookupPrincipalPolicySimulationOutputArgs, opts ...pulumi.InvokeOption) LookupPrincipalPolicySimulationResultOutput { 277 return pulumi.ToOutputWithContext(context.Background(), args). 278 ApplyT(func(v interface{}) (LookupPrincipalPolicySimulationResult, error) { 279 args := v.(LookupPrincipalPolicySimulationArgs) 280 r, err := LookupPrincipalPolicySimulation(ctx, &args, opts...) 281 var s LookupPrincipalPolicySimulationResult 282 if r != nil { 283 s = *r 284 } 285 return s, err 286 }).(LookupPrincipalPolicySimulationResultOutput) 287 } 288 289 // A collection of arguments for invoking getPrincipalPolicySimulation. 290 type LookupPrincipalPolicySimulationOutputArgs struct { 291 // A set of IAM action names to run simulations for. Each entry in this set adds an additional hypothetical request to the simulation. 292 // 293 // Action names consist of a service prefix and an action verb separated by a colon, such as `s3:GetObject`. Refer to [Actions, resources, and condition keys for AWS services](https://docs.aws.amazon.com/service-authorization/latest/reference/reference_policies_actions-resources-contextkeys.html) to see the full set of possible IAM action names across all AWS services. 294 ActionNames pulumi.StringArrayInput `pulumi:"actionNames"` 295 // A set of additional principal policy documents to include in the simulation. The simulator will behave as if each of these policies were associated with the object specified in `policySourceArn`, allowing you to test the effect of hypothetical policies not yet created. 296 AdditionalPoliciesJsons pulumi.StringArrayInput `pulumi:"additionalPoliciesJsons"` 297 // The ARN of an user that will appear as the "caller" of the simulated requests. If you do not specify `callerArn` then the simulation will use the `policySourceArn` instead, if it contains a user ARN. 298 CallerArn pulumi.StringPtrInput `pulumi:"callerArn"` 299 // Each `context` block defines an entry in the table of additional context keys in the simulated request. 300 // 301 // IAM uses context keys for both custom conditions and for interpolating dynamic request-specific values into policy values. If you use policies that include those features then you will need to provide suitable example values for those keys to achieve a realistic simulation. 302 Contexts GetPrincipalPolicySimulationContextArrayInput `pulumi:"contexts"` 303 // A set of [permissions boundary policy documents](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_boundaries.html) to include in the simulation. 304 PermissionsBoundaryPoliciesJsons pulumi.StringArrayInput `pulumi:"permissionsBoundaryPoliciesJsons"` 305 // The [ARN](https://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html) of the IAM user, group, or role whose policies will be included in the simulation. 306 // 307 // You must closely match the form of the real service request you are simulating in order to achieve a realistic result. You can use the following additional arguments to specify other characteristics of the simulated requests: 308 PolicySourceArn pulumi.StringInput `pulumi:"policySourceArn"` 309 // A set of ARNs of resources to include in the simulation. 310 // 311 // This argument is important for actions that have either required or optional resource types listed in [Actions, resources, and condition keys for AWS services](https://docs.aws.amazon.com/service-authorization/latest/reference/reference_policies_actions-resources-contextkeys.html), and you must provide ARNs that identify AWS objects of the appropriate types for the chosen actions. 312 // 313 // The policy simulator only automatically loads policies associated with the `policySourceArn`, so if your given resources have their own resource-level policy then you'll also need to provide that explicitly using the `resourcePolicyJson` argument to achieve a realistic simulation. 314 ResourceArns pulumi.StringArrayInput `pulumi:"resourceArns"` 315 // Specifies a special simulation type to run. Some EC2 actions require special simulation behaviors and a particular set of resource ARNs to achieve a realistic result. 316 // 317 // For more details, see the `ResourceHandlingOption` request parameter for [the underlying `iam:SimulatePrincipalPolicy` action](https://docs.aws.amazon.com/IAM/latest/APIReference/API_SimulatePrincipalPolicy.html). 318 ResourceHandlingOption pulumi.StringPtrInput `pulumi:"resourceHandlingOption"` 319 // An AWS account ID to use for any resource ARN in `resourceArns` that doesn't include its own AWS account ID. If unspecified, the simulator will use the account ID from the `callerArn` argument as a placeholder. 320 ResourceOwnerAccountId pulumi.StringPtrInput `pulumi:"resourceOwnerAccountId"` 321 // An IAM policy document representing the resource-level policy of all of the resources specified in `resourceArns`. 322 // 323 // The policy simulator cannot automatically load policies that are associated with individual resources, as described in the documentation for `resourceArns` above. 324 ResourcePolicyJson pulumi.StringPtrInput `pulumi:"resourcePolicyJson"` 325 } 326 327 func (LookupPrincipalPolicySimulationOutputArgs) ElementType() reflect.Type { 328 return reflect.TypeOf((*LookupPrincipalPolicySimulationArgs)(nil)).Elem() 329 } 330 331 // A collection of values returned by getPrincipalPolicySimulation. 332 type LookupPrincipalPolicySimulationResultOutput struct{ *pulumi.OutputState } 333 334 func (LookupPrincipalPolicySimulationResultOutput) ElementType() reflect.Type { 335 return reflect.TypeOf((*LookupPrincipalPolicySimulationResult)(nil)).Elem() 336 } 337 338 func (o LookupPrincipalPolicySimulationResultOutput) ToLookupPrincipalPolicySimulationResultOutput() LookupPrincipalPolicySimulationResultOutput { 339 return o 340 } 341 342 func (o LookupPrincipalPolicySimulationResultOutput) ToLookupPrincipalPolicySimulationResultOutputWithContext(ctx context.Context) LookupPrincipalPolicySimulationResultOutput { 343 return o 344 } 345 346 func (o LookupPrincipalPolicySimulationResultOutput) ActionNames() pulumi.StringArrayOutput { 347 return o.ApplyT(func(v LookupPrincipalPolicySimulationResult) []string { return v.ActionNames }).(pulumi.StringArrayOutput) 348 } 349 350 func (o LookupPrincipalPolicySimulationResultOutput) AdditionalPoliciesJsons() pulumi.StringArrayOutput { 351 return o.ApplyT(func(v LookupPrincipalPolicySimulationResult) []string { return v.AdditionalPoliciesJsons }).(pulumi.StringArrayOutput) 352 } 353 354 // `true` if all of the simulation results have decision "allowed", or `false` otherwise. 355 func (o LookupPrincipalPolicySimulationResultOutput) AllAllowed() pulumi.BoolOutput { 356 return o.ApplyT(func(v LookupPrincipalPolicySimulationResult) bool { return v.AllAllowed }).(pulumi.BoolOutput) 357 } 358 359 func (o LookupPrincipalPolicySimulationResultOutput) CallerArn() pulumi.StringPtrOutput { 360 return o.ApplyT(func(v LookupPrincipalPolicySimulationResult) *string { return v.CallerArn }).(pulumi.StringPtrOutput) 361 } 362 363 func (o LookupPrincipalPolicySimulationResultOutput) Contexts() GetPrincipalPolicySimulationContextArrayOutput { 364 return o.ApplyT(func(v LookupPrincipalPolicySimulationResult) []GetPrincipalPolicySimulationContext { return v.Contexts }).(GetPrincipalPolicySimulationContextArrayOutput) 365 } 366 367 func (o LookupPrincipalPolicySimulationResultOutput) Id() pulumi.StringOutput { 368 return o.ApplyT(func(v LookupPrincipalPolicySimulationResult) string { return v.Id }).(pulumi.StringOutput) 369 } 370 371 func (o LookupPrincipalPolicySimulationResultOutput) PermissionsBoundaryPoliciesJsons() pulumi.StringArrayOutput { 372 return o.ApplyT(func(v LookupPrincipalPolicySimulationResult) []string { return v.PermissionsBoundaryPoliciesJsons }).(pulumi.StringArrayOutput) 373 } 374 375 func (o LookupPrincipalPolicySimulationResultOutput) PolicySourceArn() pulumi.StringOutput { 376 return o.ApplyT(func(v LookupPrincipalPolicySimulationResult) string { return v.PolicySourceArn }).(pulumi.StringOutput) 377 } 378 379 func (o LookupPrincipalPolicySimulationResultOutput) ResourceArns() pulumi.StringArrayOutput { 380 return o.ApplyT(func(v LookupPrincipalPolicySimulationResult) []string { return v.ResourceArns }).(pulumi.StringArrayOutput) 381 } 382 383 func (o LookupPrincipalPolicySimulationResultOutput) ResourceHandlingOption() pulumi.StringPtrOutput { 384 return o.ApplyT(func(v LookupPrincipalPolicySimulationResult) *string { return v.ResourceHandlingOption }).(pulumi.StringPtrOutput) 385 } 386 387 func (o LookupPrincipalPolicySimulationResultOutput) ResourceOwnerAccountId() pulumi.StringPtrOutput { 388 return o.ApplyT(func(v LookupPrincipalPolicySimulationResult) *string { return v.ResourceOwnerAccountId }).(pulumi.StringPtrOutput) 389 } 390 391 func (o LookupPrincipalPolicySimulationResultOutput) ResourcePolicyJson() pulumi.StringPtrOutput { 392 return o.ApplyT(func(v LookupPrincipalPolicySimulationResult) *string { return v.ResourcePolicyJson }).(pulumi.StringPtrOutput) 393 } 394 395 // A set of result objects, one for each of the simulated requests, with the following nested attributes: 396 func (o LookupPrincipalPolicySimulationResultOutput) Results() GetPrincipalPolicySimulationResultArrayOutput { 397 return o.ApplyT(func(v LookupPrincipalPolicySimulationResult) []GetPrincipalPolicySimulationResult { return v.Results }).(GetPrincipalPolicySimulationResultArrayOutput) 398 } 399 400 func init() { 401 pulumi.RegisterOutputType(LookupPrincipalPolicySimulationResultOutput{}) 402 }