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

(elasticsearch): Domain.fromDomainAttributes gives "Invalid URL" when endpoint is a token #15188

Closed
HelioStrike opened this issue Jun 18, 2021 · 2 comments · Fixed by #15219
Closed
Labels
@aws-cdk/aws-elasticsearch Related to Amazon Elasticsearch Service bug This issue is a bug. effort/small Small work item – less than a day of effort p1

Comments

@HelioStrike
Copy link
Contributor

I'm getting a Invalid URL when trying to use Domain.fromDomainAttributes with a CfnDomain object. The CfnDomain object was retrieved from a CfnInclude object. Here's the snippet of code that's failing for me.

private getDomainFromTemplate(name: string, cfnTemplate: CfnInclude): IDomain {
    const cfnDomain = cfnTemplate.getResource(name) as CfnDomain;
    return Domain.fromDomainAttributes(this, name, {domainArn: cfnDomain.attrArn, domainEndpoint: cfnDomain.attrDomainEndpoint});
}

Reproduction Steps

Here's some minimal code that causes the bug.

import * as elasticsearch from '@aws-cdk/aws-elasticsearch';

const domain = new elasticsearch.CfnDomain(this, "domain", {
    domainName: "domainName"
});
elasticsearch.Domain.fromDomainAttributes(this, name, {domainArn: domain.attrArn, domainEndpoint: domain.attrDomainEndpoint});

What did you expect to happen?

The error shouldn't happen and the Domain object should be successfully created.

What actually happened?

internal/url.js:257
  throw new ERR_INVALID_URL(input);
  ^

TypeError [ERR_INVALID_URL]: Invalid URL: ${Token[TOKEN.1766]}
    at onParseError (internal/url.js:257:9)
    at new URL (internal/url.js:333:5)
    at extractNameFromEndpoint (/Users/saimuty/workplace/dr_infra/src/JuryDisputeResolutionNAWSCDK/node_modules/monocdk/lib/aws-elasticsearch/lib/domain.js:946:26)
    at Function.fromDomainAttributes (/Users/saimuty/workplace/dr_infra/src/JuryDisputeResolutionNAWSCDK/node_modules/monocdk/lib/aws-elasticsearch/lib/domain.js:903:28)

Environment

  • CDK CLI Version : 1.107.0 (build 52c4434)
  • Framework Version: 1.107.0 (build 52c4434)
  • Node.js Version: v12.21.0
  • OS : MacOS Big Sur 11.4
  • Language (Version): Typescript (4.0.5)

This is 🐛 Bug Report

@HelioStrike HelioStrike added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jun 18, 2021
@github-actions github-actions bot added the @aws-cdk/aws-elasticsearch Related to Amazon Elasticsearch Service label Jun 18, 2021
@BenChaimberg BenChaimberg added effort/small Small work item – less than a day of effort p1 and removed needs-triage This issue or PR still needs to be triaged. labels Jun 18, 2021
@BenChaimberg
Copy link
Contributor

We should extract the domain name from the ARN instead of the endpoint, if it's available. Change required here:

const domainName = extractNameFromEndpoint(domainEndpoint);
to use Stack.parseArn.

@HelioStrike if you create a PR for this fix, please feel free to request my review

@BenChaimberg BenChaimberg removed their assignment Jun 18, 2021
@BenChaimberg BenChaimberg changed the title (@aws-cdk/aws-elasticsearch): Domain.fromDomainAttributes gives "Invalid URL" (elasticsearch): Domain.fromDomainAttributes gives "Invalid URL" Jun 18, 2021
@BenChaimberg BenChaimberg changed the title (elasticsearch): Domain.fromDomainAttributes gives "Invalid URL" (elasticsearch): Domain.fromDomainAttributes gives "Invalid URL" when endpoint is a token Jun 18, 2021
@BenChaimberg BenChaimberg removed their assignment Jun 18, 2021
@mergify mergify bot closed this as completed in #15219 Jun 22, 2021
mergify bot pushed a commit that referenced this issue Jun 22, 2021
…hen endpoint is a token (#15219)

`Domain.fromDomainAttributes` throws the error "Invalid URL" when a token endpoint is provided. In this PR, the domain name is retrieved from the scope using the arn present in `DomainAttributes`, and if no name is found, we try to get it from the endpoint (as it has been done so far). Also added a test to verify that the error disappears with this change, even when it is given token attributes.

closes #15188

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@github-actions
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.

Seiya6329 pushed a commit to Seiya6329/aws-cdk that referenced this issue Jun 22, 2021
…hen endpoint is a token (aws#15219)

`Domain.fromDomainAttributes` throws the error "Invalid URL" when a token endpoint is provided. In this PR, the domain name is retrieved from the scope using the arn present in `DomainAttributes`, and if no name is found, we try to get it from the endpoint (as it has been done so far). Also added a test to verify that the error disappears with this change, even when it is given token attributes.

closes aws#15188

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
matthewsvu pushed a commit to matthewsvu/aws-cdk that referenced this issue Jun 22, 2021
…hen endpoint is a token (aws#15219)

`Domain.fromDomainAttributes` throws the error "Invalid URL" when a token endpoint is provided. In this PR, the domain name is retrieved from the scope using the arn present in `DomainAttributes`, and if no name is found, we try to get it from the endpoint (as it has been done so far). Also added a test to verify that the error disappears with this change, even when it is given token attributes.

closes aws#15188

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
hollanddd pushed a commit to hollanddd/aws-cdk that referenced this issue Aug 26, 2021
…hen endpoint is a token (aws#15219)

`Domain.fromDomainAttributes` throws the error "Invalid URL" when a token endpoint is provided. In this PR, the domain name is retrieved from the scope using the arn present in `DomainAttributes`, and if no name is found, we try to get it from the endpoint (as it has been done so far). Also added a test to verify that the error disappears with this change, even when it is given token attributes.

closes aws#15188

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
mergify bot pushed a commit that referenced this issue Jul 5, 2022
Documentation for changes made in PR #15219 - issue #15188

Normally one would not put the domain endpoint as a static value in a CF stack.
A more realistic use case is to manage an opensearch domain from CF stack A and use it via CF stack B, C, D ...
Stack A would export the domainEndpoint and also domainArn - the other stacks can then import those values and import the domain.

This PR will add an example for the realistic scenario so people do not end up with errors like "Invalid URL" when a token endpoint is provided.



----

### All Submissions:

* [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
	* [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
daschaa pushed a commit to daschaa/aws-cdk that referenced this issue Jul 9, 2022
Documentation for changes made in PR aws#15219 - issue aws#15188

Normally one would not put the domain endpoint as a static value in a CF stack.
A more realistic use case is to manage an opensearch domain from CF stack A and use it via CF stack B, C, D ...
Stack A would export the domainEndpoint and also domainArn - the other stacks can then import those values and import the domain.

This PR will add an example for the realistic scenario so people do not end up with errors like "Invalid URL" when a token endpoint is provided.



----

### All Submissions:

* [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
	* [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-elasticsearch Related to Amazon Elasticsearch Service bug This issue is a bug. effort/small Small work item – less than a day of effort p1
Projects
None yet
2 participants