Skip to content

Commit

Permalink
enforce clean bucket names
Browse files Browse the repository at this point in the history
clean up scenarios
  • Loading branch information
cwensel committed Oct 16, 2023
1 parent 4c6ee0d commit d71a2de
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 8 deletions.
2 changes: 1 addition & 1 deletion clusterless-scenario/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ $ ../gradlew clean installDist copyScenarios
```

```shell
$ build/install/cls-scenario/bin/cls-scenario --dry-run -f build/scenarios
$ build/install/cls-scenario/bin/cls-scenario --dry-run --verify-on-dry-run --stop-on-failure -f build/scenarios
```
1 change: 1 addition & 0 deletions clusterless-scenario/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ val run = tasks.named<JavaExec>("run") {
// "--server",
// "localhost:8080",
// "--disable-destroy",
"--stop-on-failure",
"--cls-app",
"${mainInstall.destinationDir.absolutePath}/bin/cls",
"-f",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ local unit = 'Twelfths';
main: {
name: 'copy-a-chain',
version: '20230101',
pathURI: bucketPrefix + '/copy-a/',
},
},
sinks: {
Expand All @@ -83,7 +82,6 @@ local unit = 'Twelfths';
main: {
name: 'copy-b-chain',
version: '20230101',
pathURI: bucketPrefix + '/copy-b/',
},
},
sinks: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local stage = std.extVar('scenario.stage');
local account = std.extVar('scenario.aws.account');
local region = std.extVar('scenario.aws.region');
local bucketName = 'clusterless-frequent-filter-boundary-test-' + account + '-' + region;
local bucketName = 'clusterless-freq-filter-bndry-test-' + account + '-' + region;
local bucketPrefix = 's3://' + bucketName;
local unit = 'Twelfths';

Expand All @@ -26,7 +26,7 @@ local unit = 'Twelfths';
boundaries: [
{
type: 'aws:core:s3PutListenerBoundary',
name: 'FreqPutListener',
name: 'FreqPutLstnr',
eventArrival: 'frequent',
dataset: {
name: 'ingress-frequent-boundary',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local stage = std.extVar('scenario.stage');
local account = std.extVar('scenario.aws.account');
local region = std.extVar('scenario.aws.region');
local bucketName = 'clusterless-frequent-filter-copy-test-' + account + '-' + region;
local bucketName = 'clusterless-freq-filter-copy-test-' + account + '-' + region;
local bucketPrefix = 's3://' + bucketName;
local unit = 'Twelfths';

Expand All @@ -26,7 +26,7 @@ local unit = 'Twelfths';
boundaries: [
{
type: 'aws:core:s3PutListenerBoundary',
name: 'FreqPutListener',
name: 'FreqPutLstnr',
eventArrival: 'frequent',
dataset: {
name: 'ingress-frequent-copy',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2023 Chris K Wensel <[email protected]>. All Rights Reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

package clusterless.cls.substrate.aws.resources;

public class Buckets {
/**
* <a href="https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html">Naming Rules</a>
*
* @param bucketName
* @return
*/
public static String verifyBucketName(String bucketName) {
// try to be helpful, then be strict
if (bucketName.length() > 63) {
throw new IllegalStateException("bucket name too long, must be < 64 characters, got: " + bucketName);
}

if (!bucketName.equals(bucketName.toLowerCase())) {
throw new IllegalStateException("bucket name may not contain uppercase characters, got: " + bucketName);
}

if (bucketName.contains("..")) {
throw new IllegalStateException("bucket name may not contain two adjacent periods, got: " + bucketName);
}

if (!bucketName.matches("^[0-9a-z].*")) {
throw new IllegalStateException("bucket name must start with a letter or number, got: " + bucketName);
}

if (!bucketName.matches(".*[0-9a-z]$")) {
throw new IllegalStateException("bucket name must end with a letter or number, got: " + bucketName);
}

if (!bucketName.matches("(?!(^((2(5[0-5]|[0-4][0-9])|[01]?[0-9]{1,2})\\.){3}(2(5[0-5]|[0-4][0-9])|[01]?[0-9]{1,2})$|^xn--|.+-s3alias$))^[a-z0-9][a-z0-9.-]{1,61}[a-z0-9]$")) {
throw new IllegalStateException("bucket name is not valid, got: " + bucketName);
}

return bucketName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import clusterless.cls.config.CommonConfig;
import clusterless.cls.substrate.aws.construct.ResourceConstruct;
import clusterless.cls.substrate.aws.managed.ManagedComponentContext;
import clusterless.cls.substrate.aws.resources.Buckets;
import clusterless.cls.substrate.aws.util.TagsUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -45,7 +46,7 @@ public S3BucketResourceConstruct(@NotNull ManagedComponentContext context, @NotN
.encryption(BucketEncryption.S3_MANAGED)
.enforceSsl(true) // adds a bucket policy on aws:SecureTransport
.versioned(model().versioned())
.bucketName(model().bucketName())
.bucketName(Buckets.verifyBucketName(model().bucketName()))
.removalPolicy(removeOnDestroy ? RemovalPolicy.DESTROY : RemovalPolicy.RETAIN)
.autoDeleteObjects(removeOnDestroy) // cdk adds a lambda if true
// as of 2.64.0 a lambda is installed -> https://github.com/aws/aws-cdk/issues/24086
Expand Down

0 comments on commit d71a2de

Please sign in to comment.