-
Notifications
You must be signed in to change notification settings - Fork 115
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
Add google cloud configuration object for kubeclient #88
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
44 changes: 44 additions & 0 deletions
44
lib/kubernetes-deploy/kubeclient_builder/google_friendly_config.rb
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,44 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'googleauth' | ||
module KubernetesDeploy | ||
module KubeclientBuilder | ||
class GoogleFriendlyConfig < Kubeclient::Config | ||
def fetch_user_auth_options(user) | ||
if user['auth-provider'] && (user['auth-provider']['name'] == 'gcp') | ||
{ bearer_token: new_token } | ||
else | ||
super | ||
end | ||
end | ||
|
||
def self.read(filename) | ||
new(YAML.load_file(filename), File.dirname(filename)) | ||
end | ||
|
||
def new_token | ||
scopes = ['https://www.googleapis.com/auth/cloud-platform'] | ||
authorization = Google::Auth.get_application_default(scopes) | ||
|
||
authorization.apply({}) | ||
|
||
authorization.access_token | ||
|
||
rescue Signet::AuthorizationError => e | ||
err_message = json_error_message(e.response.body) || e.message | ||
raise KubeException.new(e.response.status, err_message, e.response.body) | ||
end | ||
|
||
private | ||
|
||
def json_error_message(body) | ||
json_error_msg = begin | ||
JSON.parse(body || '') || {} | ||
rescue JSON::ParserError | ||
{} | ||
end | ||
json_error_msg['message'] | ||
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
107 changes: 107 additions & 0 deletions
107
test/unit/kubernetes-deploy/google_friendly_config_test.rb
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,107 @@ | ||
# frozen_string_literal: true | ||
require 'test_helper' | ||
|
||
class GoogleFriendlyConfigTest < KubernetesDeploy::TestCase | ||
def setup | ||
WebMock.disable_net_connect! | ||
set_google_env_vars | ||
end | ||
|
||
def teardown | ||
WebMock.allow_net_connect! | ||
end | ||
|
||
def test_auth_use_default_gcp_success | ||
config = KubernetesDeploy::KubeclientBuilder::GoogleFriendlyConfig.new(kubeconfig, "") | ||
|
||
stub_request(:post, 'https://www.googleapis.com/oauth2/v3/token') | ||
.to_return( | ||
headers: { 'Content-Type' => 'application/json' }, | ||
body: { | ||
"access_token": "bearer_token", | ||
"token_type": "Bearer", | ||
"expires_in": 3600, | ||
"id_token": "identity_token" | ||
}.to_json, | ||
status: 200 | ||
) | ||
|
||
context = config.context("google") | ||
assert_equal 'bearer_token', context.auth_options[:bearer_token] | ||
end | ||
|
||
def test_auth_use_default_gcp_failure | ||
config = KubernetesDeploy::KubeclientBuilder::GoogleFriendlyConfig.new(kubeconfig, "") | ||
|
||
stub_request(:post, 'https://www.googleapis.com/oauth2/v3/token') | ||
.to_return( | ||
headers: { 'Content-Type' => 'application/json' }, | ||
body: '', | ||
status: 401 | ||
) | ||
|
||
assert_raises(KubeException) do | ||
config.context("google") | ||
end | ||
end | ||
|
||
def test_non_google_auth_works | ||
config = KubernetesDeploy::KubeclientBuilder::GoogleFriendlyConfig.new(kubeconfig, "") | ||
|
||
context = config.context("minikube") | ||
|
||
assert_equal 'test', context.auth_options[:password] | ||
assert_equal 'admin', context.auth_options[:username] | ||
end | ||
|
||
def kubeconfig | ||
{ | ||
'apiVersion' => 'v1', | ||
'clusters' => [ | ||
{ 'cluster' => { 'server' => 'https://192.168.64.3:8443' }, 'name' => 'test' } | ||
], | ||
'contexts' => [ | ||
{ | ||
'context' => { | ||
'cluster' => 'test', | ||
'user' => 'google' | ||
}, | ||
'name' => 'google' | ||
}, | ||
{ | ||
'context' => { | ||
'cluster' => 'test', 'user' => 'minikube' | ||
}, | ||
'name' => 'minikube' | ||
}, | ||
], | ||
'users' => [ | ||
{ | ||
'name' => 'google', | ||
'user' => { | ||
'auth-provider' => { | ||
'name' => 'gcp', | ||
'config' => { 'access_token' => 'test' } | ||
} | ||
} | ||
}, | ||
{ | ||
'name' => 'minikube', | ||
'user' => { | ||
'password' => 'test', | ||
'username' => 'admin' | ||
} | ||
} | ||
] | ||
}.stringify_keys | ||
end | ||
|
||
def set_google_env_vars | ||
ENV["GOOGLE_PRIVATE_KEY"] ||= "FAKE" | ||
ENV["GOOGLE_CLIENT_EMAIL"] ||= "fake@email.com" | ||
ENV["GOOGLE_ACCOUNT_TYPE"] ||= 'authorized_user' | ||
ENV["GOOGLE_CLIENT_ID"] ||= 'fake' | ||
ENV["GOOGLE_CLIENT_SECRET"] ||= 'fake' | ||
ENV["REFRESH_TOKEN_VAR"] ||= 'fake' | ||
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.
There's probably a reason why it's necessary, but I'd prefer them to be reset on teardown
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.
Its because the
googleauth
gem does a bunch of magic inspection to determine how to load configuration for a host (ie checking if creds are locally stored on disk, whether this is a GCE host, etc...).