Skip to content
This repository has been archived by the owner on Jul 1, 2024. It is now read-only.

Commit

Permalink
Fix failing program test (#4157)
Browse files Browse the repository at this point in the history
* Fix failing program test

* Make the Java example 'work'

* Add necessary dependencies
  • Loading branch information
cnunciato committed Apr 10, 2024
1 parent 9165eca commit 4a539d4
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,66 +7,67 @@
{
var bucket = new Aws.S3.Bucket("my-bucket");

var ownershipControls = new Aws.S3.BucketOwnershipControls("ownership-controls", new()
{
Bucket = bucket.Id,
Rule = new Aws.S3.Inputs.BucketOwnershipControlsRuleArgs
var ownershipControls = new Aws.S3.BucketOwnershipControls(
"ownership-controls",
new()
{
ObjectOwnership = "ObjectWriter"
Bucket = bucket.Id,
Rule = new Aws.S3.Inputs.BucketOwnershipControlsRuleArgs
{
ObjectOwnership = "ObjectWriter"
}
}
});
);

var publicAccessBlock = new Aws.S3.BucketPublicAccessBlock("public-access-block", new()
{
Bucket = bucket.Id,
BlockPublicAcls = false
});
var publicAccessBlock = new Aws.S3.BucketPublicAccessBlock(
"public-access-block",
new() { Bucket = bucket.Id, BlockPublicAcls = false }
);

var bucketMetric = new Aws.S3.BucketMetric("my-bucket-metric", new()
{
Bucket = bucket.Id
});
var bucketMetric = new Aws.S3.BucketMetric("my-bucket-metric", new() { Bucket = bucket.Id });

var bucketNotification = new Aws.S3.BucketNotification("my-bucket-notification", new()
{
Bucket = bucket.Id
});
var bucketNotification = new Aws.S3.BucketNotification(
"my-bucket-notification",
new() { Bucket = bucket.Id }
);

var bucketObject = new Aws.S3.BucketObject("my-bucket-object", new Aws.S3.BucketObjectArgs
{
Bucket = bucket.Id,
Content = "hello world"
}, new CustomResourceOptions
{
DependsOn = new List<Resource> { publicAccessBlock, ownershipControls }
});
var bucketObject = new Aws.S3.BucketObject(
"my-bucket-object",
new Aws.S3.BucketObjectArgs { Bucket = bucket.Id, Content = "hello world" },
new CustomResourceOptions
{
DependsOn = new List<Resource> { publicAccessBlock, ownershipControls }
}
);

var bucketPolicy = new Aws.S3.BucketPolicy("my-bucket-policy", new()
{
Bucket = bucket.Id,
Policy = bucket.Id.Apply(id => PublicReadPolicyForBucket(id))
});
var bucketPolicy = new Aws.S3.BucketPolicy(
"my-bucket-policy",
new() { Bucket = bucket.Id, Policy = bucket.Id.Apply(id => PublicReadPolicyForBucket(id)) },
new CustomResourceOptions
{
DependsOn = new List<Resource> { publicAccessBlock, ownershipControls }
}
);

return new Dictionary<string, object?>
{
{ "bucketName", bucket.Id }
};
return new Dictionary<string, object?> { { "bucketName", bucket.Id } };
});

static string PublicReadPolicyForBucket(string bucketName)
{
return JsonSerializer.Serialize(new
{
Version = "2012-10-17",
Statement = new[]
return JsonSerializer.Serialize(
new
{
new
Version = "2012-10-17",
Statement = new[]
{
Effect = "Allow",
Principal = "*",
Action = "s3:GetObject",
Resource = $"arn:aws:s3:::{bucketName}/*"
new
{
Effect = "Allow",
Principal = "*",
Action = "s3:GetObject",
Resource = $"arn:aws:s3:::{bucketName}/*"
}
}
}
});
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/json"

"github.com/pulumi/pulumi-aws/sdk/v6/go/aws/s3"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
Expand Down Expand Up @@ -73,7 +74,10 @@ func main() {
policyJSON, err := json.Marshal(policy)
return string(policyJSON), err
}).(pulumi.StringOutput),
})
}, pulumi.DependsOn([]pulumi.Resource{
publicAccessBlock,
ownershipControls,
}))
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

<properties>
<encoding>UTF-8</encoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.release>17</maven.compiler.release>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.release>11</maven.compiler.release>
<mainClass>myproject.App</mainClass>
<mainArgs/>
</properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
import com.pulumi.aws.s3.BucketPublicAccessBlock;
import com.pulumi.aws.s3.BucketPublicAccessBlockArgs;
import com.pulumi.aws.s3.inputs.BucketOwnershipControlsRuleArgs;
import com.pulumi.core.Output;
import com.pulumi.resources.CustomResourceOptions;
import java.util.Map;
import static com.pulumi.codegen.internal.Serialization.*;

public class App {
public static void main(String[] args) {
Expand Down Expand Up @@ -55,21 +54,22 @@ public static void main(String[] args) {
var bucketPolicy = new BucketPolicy("my-bucket-policy", BucketPolicyArgs.builder()
.bucket(bucket.id())
.policy(bucket.id().applyValue(App::publicReadPolicyForBucket))
.build(), CustomResourceOptions.builder()
.dependsOn(publicAccessBlock, ownershipControls)
.build());
});
}

private static String publicReadPolicyForBucket(String bucketName) {
return String.format("""
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::%s/*"
}]
}
""", bucketName);
return String.format(serializeJson(
jsonObject(
jsonProperty("Version", "2012-10-17"),
jsonProperty("Statement", jsonArray(jsonObject(
jsonProperty("Effect", "Allow"),
jsonProperty("Action", "s3:GetObject"),
jsonProperty("Principal", "*"),
jsonProperty("Resource", "arn:aws:s3:::%s/*")
)))
)), bucketName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ const bucketObject = new aws.s3.BucketObject(
{ dependsOn: [publicAccessBlock, ownershipControls] },
);

const bucketPolicy = new aws.s3.BucketPolicy("my-bucket-policy", {
bucket: bucket.bucket,
policy: bucket.bucket.apply(publicReadPolicyForBucket),
});
const bucketPolicy = new aws.s3.BucketPolicy(
"my-bucket-policy",
{
bucket: bucket.bucket,
policy: bucket.bucket.apply(publicReadPolicyForBucket),
},
{ dependsOn: [publicAccessBlock, ownershipControls] },
);

function publicReadPolicyForBucket(bucketName) {
return JSON.stringify({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,59 @@

bucket = aws.s3.Bucket("my-bucket")

ownership_controls = aws.s3.BucketOwnershipControls("ownership-controls",
ownership_controls = aws.s3.BucketOwnershipControls(
"ownership-controls",
bucket=bucket.id,
rule=aws.s3.BucketOwnershipControlsRuleArgs(
object_ownership="ObjectWriter",
)
),
)

public_access_block = aws.s3.BucketPublicAccessBlock("public-access-block",
public_access_block = aws.s3.BucketPublicAccessBlock(
"public-access-block",
bucket=bucket.id,
block_public_acls=False,
)

bucket_metric = aws.s3.BucketMetric("my-bucket-metric",
bucket_metric = aws.s3.BucketMetric(
"my-bucket-metric",
bucket=bucket.id,
)

bucket_notification = aws.s3.BucketNotification("my-bucket-notification",
bucket_notification = aws.s3.BucketNotification(
"my-bucket-notification",
bucket=bucket.id,
)

bucket_object = aws.s3.BucketObject("my-bucket-object",
bucket_object = aws.s3.BucketObject(
"my-bucket-object",
bucket=bucket.id,
content="hello world",
opts=pulumi.ResourceOptions(depends_on=[public_access_block, ownership_controls]),
)


def public_read_policy_for_bucket(bucket_name):
return pulumi.Output.all(bucket_name).apply(lambda args: json.dumps({
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": f"arn:aws:s3:::{args[0]}/*"
}]
}))

bucket_policy = aws.s3.BucketPolicy("my-bucket-policy",
return pulumi.Output.all(bucket_name).apply(
lambda args: json.dumps(
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": f"arn:aws:s3:::{args[0]}/*",
}
],
}
)
)


bucket_policy = aws.s3.BucketPolicy(
"my-bucket-policy",
bucket=bucket.id,
policy=bucket.id.apply(public_read_policy_for_bucket),
opts=pulumi.ResourceOptions(depends_on=[public_access_block, ownership_controls]),
)
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ const bucketObject = new aws.s3.BucketObject(
{ dependsOn: [publicAccessBlock, ownershipControls] },
);

const bucketPolicy = new aws.s3.BucketPolicy("my-bucket-policy", {
bucket: bucket.id,
policy: bucket.id.apply(publicReadPolicyForBucket),
});
const bucketPolicy = new aws.s3.BucketPolicy(
"my-bucket-policy",
{
bucket: bucket.id,
policy: bucket.id.apply(publicReadPolicyForBucket),
},
{ dependsOn: [publicAccessBlock, ownershipControls] },
);

function publicReadPolicyForBucket(bucketName: string): string {
return JSON.stringify({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ resources:
type: aws:s3:BucketPolicy
properties:
bucket: ${my-bucket.id}
policy: ${publicReadPolicyForBucket.json}

variables:
publicReadPolicyForBucket:
fn::invoke:
function: aws:iam:getPolicyDocument
arguments:
statements:
- effect: Allow
principal: "*"
action: s3:GetObject
resource: ${my-bucket.id}/*
policy:
fn::toJSON:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal: "*"
Action: "s3:GetObject"
Resource: "arn:aws:s3:::${my-bucket.id}/*"
options:
dependsOn:
- ${public-access-block}
- ${ownership-controls}
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
package myproject;

import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.aws.s3.Bucket;
import com.pulumi.aws.s3.BucketPolicy;
import com.pulumi.aws.s3.inputs.BucketPolicyPolicyArgs;
import java.util.Map;
import com.pulumi.aws.s3.BucketPolicyArgs;
import static com.pulumi.codegen.internal.Serialization.*;

public class App {
public static void main(String[] args) {
Pulumi.run(ctx -> {
var bucket = new Bucket("myBucket");

var bucketArn = bucket.arn();
var policyDocument = bucketArn.apply(arn -> String.format("""
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": {"Service": "lambda.amazonaws.com"},
"Action": ["s3:PutObject", "s3:PutObjectAcl"],
"Resource": "%s/*"
}]
}""", arn));
var policyDocument = bucket.arn().applyValue(arn -> serializeJson(
jsonObject(
jsonProperty("Version", "2012-10-17"),
jsonProperty("Statement", jsonArray(jsonObject(
jsonProperty("Effect", "Allow"),
jsonProperty("Action", jsonArray("s3:PutObject", "s3:PutObjectAcl")),
jsonProperty("Principal", jsonObject(
jsonProperty("Service", "lambda.amazonaws.com")
)),
jsonProperty("Resource", arn + "/*")
)))
)
));

var bucketPolicy = new BucketPolicy("myBucketPolicy", BucketPolicyArgs.builder()
.bucket(bucket.id())
Expand Down

0 comments on commit 4a539d4

Please sign in to comment.