Skip to content

Commit 403f1a3

Browse files
Frasslemjeffryesrshadelukehoban
authored
Output-ify JSON use in CreateRoleStack (#1325)
Co-authored-by: Matthew (Matt) Jeffryes <[email protected]> Co-authored-by: Richard Shade <[email protected]> Co-authored-by: Luke Hoban <[email protected]> Co-authored-by: Richard Shade <[email protected]>
1 parent 62a5448 commit 403f1a3

File tree

1 file changed

+40
-16
lines changed

1 file changed

+40
-16
lines changed

aws-cs-assume-role/create-role/CreateRoleStack.cs

+40-16
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,33 @@ public CreateRoleStack()
2525
// https://www.pulumi.com/docs/intro/concepts/resources/#additionalsecretoutputs
2626
new CustomResourceOptions { AdditionalSecretOutputs = { "secret" } });
2727

28-
var tempPolicy = unprivilegedUser.Arn.Apply((string arn) =>
29-
{
30-
AssumeRolePolicyArgs policyArgs = new AssumeRolePolicyArgs(arn);
31-
return JsonSerializer.Serialize<AssumeRolePolicyArgs>(policyArgs);
32-
});
28+
AssumeRolePolicyArgs policyArgs = new AssumeRolePolicyArgs(unprivilegedUser.Arn);
29+
var tempPolicy = Output.Create(policyArgs).Apply(args => JsonSerializer.Serialize(args,
30+
new JsonSerializerOptions
31+
{
32+
WriteIndented = false,
33+
PropertyNamingPolicy = null // Remove camelCase policy
34+
}));
35+
36+
// Alternative approach using a direct string-based policy document
37+
var directPolicy = unprivilegedUser.Arn.Apply(arn => @$"{{
38+
""Version"": ""2012-10-17"",
39+
""Statement"": [
40+
{{
41+
""Sid"": ""AllowAssumeRole"",
42+
""Effect"": ""Allow"",
43+
""Principal"": {{
44+
""AWS"": ""{arn}""
45+
}},
46+
""Action"": ""sts:AssumeRole""
47+
}}
48+
]
49+
}}");
3350

3451
var allowS3ManagementRole = new Iam.Role("allow-s3-management", new Iam.RoleArgs
3552
{
3653
Description = "Allow management of S3 buckets",
37-
AssumeRolePolicy = tempPolicy
54+
AssumeRolePolicy = directPolicy // Use the direct string approach instead
3855
});
3956

4057
var rolePolicy = new Iam.RolePolicy("allow-s3-management-policy", new Iam.RolePolicyArgs
@@ -60,42 +77,49 @@ public CreateRoleStack()
6077

6178
public class AssumeRolePolicyArgs
6279
{
80+
[JsonPropertyName("Version")]
6381
public string Version => "2012-10-17";
64-
public StatementArgs Statement { get; private set; }
6582

66-
public AssumeRolePolicyArgs(string arn)
83+
[JsonPropertyName("Statement")]
84+
public StatementArgs[] Statement { get; private set; }
85+
86+
public AssumeRolePolicyArgs(Input<string> arn)
6787
{
68-
Statement = new StatementArgs(arn);
88+
Statement = new StatementArgs[] { new StatementArgs(arn) };
6989
}
70-
7190
}
7291

7392
public class StatementArgs
7493
{
94+
[JsonPropertyName("Sid")]
7595
public string Sid => "AllowAssumeRole";
96+
97+
[JsonPropertyName("Effect")]
7698
public string Effect => "Allow";
99+
100+
[JsonPropertyName("Principal")]
77101
public PrincipalArgs Principal { get; private set; }
102+
103+
[JsonPropertyName("Action")]
78104
public string Action => "sts:AssumeRole";
79105

80-
public StatementArgs(string arn)
106+
public StatementArgs(Input<string> arn)
81107
{
82108
Principal = new PrincipalArgs(arn);
83109
}
84110
}
85111

86112
public class PrincipalArgs
87113
{
88-
public string AWS { get; private set; }
114+
[JsonPropertyName("AWS")]
115+
public Input<string> AWS { get; private set; }
89116

90-
public PrincipalArgs(string arn)
117+
public PrincipalArgs(Input<string> arn)
91118
{
92119
AWS = arn;
93120
}
94121
}
95122

96-
97-
98-
99123
[Output]
100124
public Output<string> roleArn { get; set; }
101125
[Output]

0 commit comments

Comments
 (0)