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

Support fetching tokens from compute instance metadata #85

Merged
merged 3 commits into from
Feb 25, 2021

Conversation

mcrumm
Copy link
Contributor

@mcrumm mcrumm commented Feb 23, 2021

Hi @peburrows and @wojtekmach - I'm loving the redesign for v1.3!

This PR attempts to get the ball rolling for instance metadata support. WDYT?

Ref: #82

Copy link
Collaborator

@wojtekmach wojtekmach left a comment

Choose a reason for hiding this comment

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

Looks great!

README.md Outdated Show resolved Hide resolved
lib/goth.ex Outdated Show resolved Hide resolved
lib/goth/token.ex Outdated Show resolved Hide resolved

def start(_type, _args) do
children = [
{Goth, name: MyApp.Goth, url: "http://metadata.google.internal", credentials: {:instance, "default"}}
Copy link
Collaborator

Choose a reason for hiding this comment

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

double-checking "http://metadata.google.internal" or "https://..."?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can confirm it's HTTP -- probably because it's internal
Here's a reference in the docs: https://cloud.google.com/appengine/docs/standard/java/accessing-instance-metadata#identifying_which_metadata_endpoint_to_use

To query the metadata server for an access token, you must configure
the following:

* `url` must be set to the base url for the metadata server.
Copy link
Collaborator

@wojtekmach wojtekmach Feb 24, 2021

Choose a reason for hiding this comment

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

I wonder: if {:instance, account} is given, should we automatically default the url or that'd be trying to be too clever?

The advantage is there's less copy-pasting for the users as they'd most likely always use the same exact metadata url (I never used it so I wouldn't know, though.) We could do the same thing for the lower level Goth.Token.fetch/1, then it's really super simple to use:

iex> Goth.Token.fetch(credentials: {:instance, "default"})

(This reminds me that currently that function needs to explicitly be passed the url & scope, but I think it's fine to use the same defaults as in Goth.start_link, I'll make this change later.)

(Edit: done: #86)

Here's how we could document this then, I think it doesn't sound too bad:

## Google Compute Metadata

Every compute instance stores its metadata on a metadata server.
Goth can query this metadata server to fetch authentication credentials
for a service account within the instance.

To query the metadata server for an access token, you must set `:credentials`
to tuple `{:instance, account}`. If such credentials are set, the `:url`
will automatically default to `https://metadata.google.internal`.

WDYT?

Copy link
Contributor Author

@mcrumm mcrumm Feb 25, 2021

Choose a reason for hiding this comment

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

Yeah, I did not add the "magic" because I was not sure we wanted it and also I was not sure how we would want to test it. So I am open to suggestions there :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

yeah, testing it would be a little bit tricky, we'd still assert on the /computeMetadata... bit but we wouldn't test the the hostname was set to metadata.google.internal because then we couldn't use Bypass which then I guess we could introduce a mock but I'd rather avoid that. I wouldn't be super concerned if we didn't test that particular bit, that we use the correct hostname, though.

Coming back to wether we want to introduce this magic in the first place, it's your call :D If not today, we'd always be able to add it later if someone wanted it because it would be backwards compatible, if someone explicitly sets the :url we would never touch it.

test/goth/token_test.exs Show resolved Hide resolved
@wojtekmach
Copy link
Collaborator

I've added the http client contract on master which caused conflicts, please rebase and sorry about that!

@wojtekmach wojtekmach merged commit 66af30d into peburrows:master Feb 25, 2021
@wojtekmach
Copy link
Collaborator

Thank you!

@mcrumm mcrumm deleted the mc-instance-credentials branch February 25, 2021 21:29
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.

2 participants