-
Notifications
You must be signed in to change notification settings - Fork 167
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
Generate GCP tokens through cmd-path
config
#410
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'kubeclient/google_application_default_credentials' | ||
require 'kubeclient/gcp_command_credentials' | ||
|
||
module Kubeclient | ||
# Handle different ways to get a bearer token for Google Cloud Platform. | ||
class GCPAuthProvider | ||
class << self | ||
def token(config) | ||
if config.key?('cmd-path') | ||
Kubeclient::GCPCommandCredentials.token(config) | ||
else | ||
Kubeclient::GoogleApplicationDefaultCredentials.token | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
module Kubeclient | ||
# Generates a bearer token for Google Cloud Platform. | ||
class GCPCommandCredentials | ||
class << self | ||
def token(config) | ||
require 'open3' | ||
require 'shellwords' | ||
require 'json' | ||
require 'jsonpath' | ||
|
||
cmd = config['cmd-path'] | ||
args = config['cmd-args'] | ||
token_key = config['token-key'] | ||
|
||
out, err, st = Open3.capture3(cmd, *args.split(' ')) | ||
|
||
raise "exec command failed: #{err}" unless st.success? | ||
|
||
extract_token(out, token_key) | ||
end | ||
|
||
private | ||
|
||
def extract_token(output, token_key) | ||
JsonPath.on(output, token_key.gsub(/^{|}$/, '')).first | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
apiVersion: v1 | ||
clusters: | ||
- cluster: | ||
server: https://localhost:8443 | ||
insecure-skip-tls-verify: true | ||
name: localhost:8443 | ||
contexts: | ||
- context: | ||
cluster: localhost:8443 | ||
namespace: default | ||
user: application-default-credentials | ||
name: localhost/application-default-credentials | ||
kind: Config | ||
preferences: {} | ||
users: | ||
- name: application-default-credentials | ||
user: | ||
auth-provider: | ||
config: | ||
access-token: <fake_token> | ||
cmd-args: config config-helper --format=json | ||
cmd-path: /path/to/gcloud | ||
expiry: 2019-04-09T19:26:18Z | ||
expiry-key: '{.credential.token_expiry}' | ||
token-key: '{.credential.access_token}' | ||
name: gcp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require_relative 'test_helper' | ||
require 'open3' | ||
|
||
# Unit tests for the GCPCommandCredentials token provider | ||
class GCPCommandCredentialsTest < MiniTest::Test | ||
def test_token | ||
opts = { 'cmd-args' => 'config config-helper --format=json', | ||
'cmd-path' => '/path/to/gcloud', | ||
'expiry-key' => '{.credential.token_expiry}', | ||
'token-key' => '{.credential.access_token}' } | ||
|
||
creds = JSON.dump( | ||
'credential' => { | ||
'access_token' => '9A3A941836F2458175BE18AA1971EBBF47949B07', | ||
'token_expiry' => '2019-04-12T15:02:51Z' | ||
} | ||
) | ||
|
||
st = Minitest::Mock.new | ||
st.expect(:success?, true) | ||
|
||
Open3.stub(:capture3, [creds, nil, st]) do | ||
assert_equal('9A3A941836F2458175BE18AA1971EBBF47949B07', | ||
Kubeclient::GCPCommandCredentials.token(opts)) | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the interests of hardening, would you be opposed to adding retries here? We have begun observing intermittent
Signet::RemoteServerError
exceptions that, according to Google, we can/should retry on.I don't think pushing up retryable errors to the caller is the right thing to do; we should handle them here and only throw once we've exhausted our retry limit.
Full error we observe:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@timothysmith0609 sounds like something that the
googleauth
gem should be dealing with, notkubeclient
itself.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're absolutely right. It turns out
googleauth
does retry, but only when using the GCE node service account (no idea why that's the case). It looks like it should be possible to wrap the other case in their retry routine so I'll try to solve this upstream.