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

aws-s3: Unable to use Bucket.fromBucketName on legacy s3 bucket with underscore in the name #22640

Closed
NoahCarnahan opened this issue Oct 25, 2022 · 6 comments · Fixed by #30679 or codu-code/codu#969 · May be fixed by NOUIY/aws-solutions-constructs#112, codu-code/codu#987 or gitafolabi/kreuzlaker#2
Labels
@aws-cdk/aws-s3 Related to Amazon S3 bug This issue is a bug. effort/small Small work item – less than a day of effort p1

Comments

@NoahCarnahan
Copy link

NoahCarnahan commented Oct 25, 2022

Describe the bug

I'm attempting to reference an s3 bucket in my cdk code that was created years ago outside of cdk. The bucket has an underscore in its name, which was legal for buckets before March 1, 2018 (new buckets can't have underscores in their names). I've tried referencing the bucket with both Bucket.fromBucketName and Bucket.fromBucketAttributes, but both throw this error:

jsii.errors.JSIIError: Invalid S3 bucket name (value: xxxx_xxxxxx)
Bucket name must only contain lowercase characters and the symbols, period (.) and dash (-) (offset: 4)

(note that I've censored the actual bucket name for privacy)

Also note that I'm using the python version of the cdk library. Is there any way I can reference this bucket from CDK?

Expected Behavior

I expect to be able to import the bucket.

Current Behavior

Error message is:

jsii.errors.JSIIError: Invalid S3 bucket name (value: xxxx_xxxxxx)
Bucket name must only contain lowercase characters and the symbols, period (.) and dash (-) (offset: 4)

Stack trace (paths modified for privacy)

  File "<project root>/.venv/lib/python3.9/site-packages/aws_cdk/aws_s3/__init__.py", line 16101, in from_bucket_attributes
    return typing.cast(IBucket, jsii.sinvoke(cls, "fromBucketAttributes", [scope, id, attrs]))
  File "<project root>/.venv/lib/python3.9/site-packages/jsii/_kernel/__init__.py", line 148, in wrapped
    return _recursize_dereference(kernel, fn(kernel, *args, **kwargs))
  File "<project root>/.venv/lib/python3.9/site-packages/jsii/_kernel/__init__.py", line 405, in sinvoke
    response = self.provider.sinvoke(
  File "<project root>/.venv/lib/python3.9/site-packages/jsii/_kernel/providers/process.py", line 365, in sinvoke
    return self._process.send(request, InvokeResponse)
  File "<project root>/.venv/lib/python3.9/site-packages/jsii/_kernel/providers/process.py", line 329, in send
    raise JSIIError(resp.error) from JavaScriptError(resp.stack)
jsii.errors.JSIIError: Invalid S3 bucket name (value: xxxx_xxxxxx)
Bucket name must only contain lowercase characters and the symbols, period (.) and dash (-) (offset: 4)

Reproduction Steps

from aws_cdk import aws_s3 as s3

bucket = s3.Bucket.from_bucket_attributes(
    self,
    "foo-bar",
    account="myAccount",
    bucket_arn="arn:aws:s3:::foo_bar"
)

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.46.0 (build 5a0595e)

Framework Version

No response

Node.js Version

v18.0.0

OS

macOS 12.6

Language

Python

Language Version

Python 3.9.12

Other information

No response

@NoahCarnahan NoahCarnahan added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Oct 25, 2022
@github-actions github-actions bot added the @aws-cdk/aws-s3 Related to Amazon S3 label Oct 25, 2022
@peterwoodworth
Copy link
Contributor

We do not currently support this, thanks for calling this out @NoahCarnahan,

This code is checking for only letters, numbers, periods, and hyphens, and throws an error if any other character is found:

const charsetMatch = bucketName.match(/[^a-z0-9.-]/);
if (charsetMatch) {
errors.push('Bucket name must only contain lowercase characters and the symbols, period (.) and dash (-) '

We shouldn't be throwing an error for underscores for cases like yours, so we should be checking for underscores as well here.

Until we fix this, there isn't a great workaround that I can think of. Off the top of my head, you could supply an arn without the underscore, and then use the bucket normally. But make sure to use escape hatches to override the arn/name wherever your bucket arn or name is passed in (you can view the template to see where your imported bucket is being used exactly by seeing where its arn is in the template)

@peterwoodworth peterwoodworth added good first issue Related to contributions. See CONTRIBUTING.md p2 effort/small Small work item – less than a day of effort and removed needs-triage This issue or PR still needs to be triaged. labels Oct 28, 2022
@chicken-teriyaki-cup-rice
Copy link

chicken-teriyaki-cup-rice commented Nov 6, 2022

I'd like to fix this. @peterwoodworth I see why this would have been a bad idea.

@chicken-teriyaki-cup-rice
Copy link

chicken-teriyaki-cup-rice commented Nov 6, 2022

I see why this would have been a bad idea.

@NoahCarnahan
Copy link
Author

Thanks both for looking into this

Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

1 similar comment
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment