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

Imdsv2 support #134

Merged
merged 5 commits into from
Jun 1, 2020
Merged

Imdsv2 support #134

merged 5 commits into from
Jun 1, 2020

Conversation

srprash
Copy link
Collaborator

@srprash srprash commented May 29, 2020

Issue #, if available:

Description of changes:
Adding IMDSv2 support for fetching the EC2 instance metadata via the EC2Plugin. The metadata is fetched from instance identity document with a fallback to V1 if V2 fails.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

_logger.Error(e, "Exception occurred while getting EC2 metadata from IMDSv2");
_logger.DebugFormat("Unable to get metadata from IMDSv2 endpoint. Falling back to IMDSv1 endpoint");

// try IMDSv1 endpoint for metadata
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a similar comment in the node PR - since there's no difference in the document endpoint for v2 and v1 I think we should just have error handling around the token request. The can continue with the same logic, including the token if it's available or otherwise try without it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's my version in Java which takes that approach, the flow is just

  • get token
  • get document (include token if we got one)
  • parse document

Don't need to treat v1 as a different endpoint

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes. Good call out. The node PR discussion slipped out of my mind while writing this.

@srprash srprash requested review from anuraaga and willarmiros May 29, 2020 15:47
Copy link
Contributor

@anuraaga anuraaga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a bit LGTM. Thanks!

sdk/src/Core/Plugins/EC2Plugin.cs Outdated Show resolved Hide resolved
catch (Exception)
{
_logger.DebugFormat("Error occurred while getting EC2 metadata");
return null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not much experience with c# but is it possible to return an empty dictionary here instead of null? No points to do a null check at the caller if it's same semantics as empty

https://stackoverflow.com/a/54902869

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't use ImmutableDictionary since it's only supported for .NetCore and .Net 5. Bummer :(

returning a new empty Dictionary<>() instead to avoid the null check.

Copy link
Contributor

@willarmiros willarmiros left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to @anuraaga minor comments but otherwise LGTM

@normj
Copy link
Member

normj commented May 31, 2020

Is there a reason you don't just use the Amazon.Util.EC2InstanceMetadata class to access the instance metadata? https://github.com/aws/aws-sdk-net/blob/master/sdk/src/Core/Amazon.Util/_bcl%2Bnetstandard/EC2InstanceMetadata.cs

@anuraaga
Copy link
Contributor

Hi @normj thanks for the question! The AWS SDK is fairly heavyweight, with most of its logic in order to support SDK calls. As this is an instrumentation library for user apps, and many of those apps may not even use the AWS SDK in their code, we want to reduce the dependency footprint so that adding tracing has as little impact on the user's app as possible. If a small library containing only the metadata lookup was extracted out of Core, we would probably use that, but given the small amount of logic for the HTTP fetch and a simple parse, inlining seems ok for now. What do you think?

@normj
Copy link
Member

normj commented May 31, 2020

@anuraaga Totally agree you should not take a reference on AWSSDK.Core just for the metadata but it looks like the project already has a dependency on AWSSDK.Core

https://github.com/aws/aws-xray-sdk-dotnet/blob/master/sdk/src/Core/AWSXRayRecorder.Core.csproj#L40-L43

@anuraaga
Copy link
Contributor

anuraaga commented May 31, 2020

@normj Good eyes :) This and some of our other language SDKs have used it too for fetching sampling configuration, but because we proxy requests through the X-Ray domain, which signs the requests itself, it's actually overkill to pull in the machinery of the AWS SDK especially since we don't need to expose these models to users. So we've just begun to move away from using the SDK dependency hoping a future major version can be much more lightweight and improve user experience. Here's an example from the Java SDK where in 3.0.0 we intend to remove the dependency.

@srprash Do you think we can add this point to #102? And maybe even add a comment in the .csproj as a pointer to people that find it.

@normj
Copy link
Member

normj commented May 31, 2020

@anuraaga that makes sense to move away from using the SDK for these small use cases and avoid bring in a large dependency that could possibly cause conflict with the versions the application is using.

@srprash srprash merged commit 1b90091 into master Jun 1, 2020
@srprash
Copy link
Collaborator Author

srprash commented Jun 1, 2020

@srprash Do you think we can add this point to #102? And maybe even add a comment in the .csproj as a pointer to people that find it.

@anuraaga Sure. That's a good point for visibility.

@srprash srprash deleted the imdsv2_support branch June 3, 2020 20:27
enif-lee pushed a commit to enif-lee/aws-xray-sdk-dotnet that referenced this pull request Sep 11, 2020
* Refactored the EC2Plugin to use IMDSv2 endpoint via HTTP and fallback to v1 if v2 fails

* Reafctored code to non-static members. Added unit tests for EC2Plugin

* Refactored the logic to fetch metadata in a single flow irrespective of v1 and v2

* Refactored to return empty dict instead of null to drop reduntant null check

* Removed some unnecessary variables and assignments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants