Skip to content

Commit 09e72a6

Browse files
authored
Merge pull request #213 from keylimetoolbox/auth_default_account
Allow authorization with Google application default credentials
2 parents 69f583f + e6e10d0 commit 09e72a6

File tree

7 files changed

+60
-2
lines changed

7 files changed

+60
-2
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,29 @@ namespace = File.read('/var/run/secrets/kubernetes.io/serviceaccount/namespace')
167167
```
168168
You can find information about tokens in [this guide](https://kubernetes.io/docs/tasks/access-application-cluster/access-cluster/#accessing-the-api-from-a-pod) and in [this reference](http://kubernetes.io/docs/admin/authentication/).
169169

170+
#### Google's Application Default Credentials
171+
172+
On Google Compute Engine, Google App Engine, or Google Cloud Functions, as well as `gcloud`-configured systems
173+
with [application default credentials](https://developers.google.com/identity/protocols/application-default-credentials),
174+
you can use the token provider to authorize `kubeclient`.
175+
176+
This requires the [`googleauth` gem](https://github.com/google/google-auth-library-ruby) that is _not_ included in
177+
`kubeclient` dependencies so you should add it to your bundle.
178+
179+
```ruby
180+
require 'googleauth'
181+
182+
auth_options = {
183+
bearer_token: Kubeclient::GoogleApplicationDefaultCredentials.token
184+
}
185+
client = Kubeclient::Client.new(
186+
'https://localhost:8443/api/', 'v1', auth_options: auth_options
187+
)
188+
```
189+
190+
Note that this token is good for one hour. If your code runs for longer than that, you should plan to
191+
acquire a new one.
192+
170193
### Non-blocking IO
171194

172195
You can also use kubeclient with non-blocking sockets such as Celluloid::IO, see [here](https://github.com/httprb/http/wiki/Parallel-requests-with-Celluloid%3A%3AIO)

kubeclient.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ Gem::Specification.new do |spec|
2727
spec.add_development_dependency 'webmock', '~> 3.0.1'
2828
spec.add_development_dependency 'vcr'
2929
spec.add_development_dependency 'rubocop', '= 0.49.1'
30+
spec.add_development_dependency 'googleauth', '~> 0.5.1'
31+
3032
spec.add_dependency 'rest-client', '~> 2.0'
3133
spec.add_dependency 'recursive-open-struct', '~> 1.0', '>= 1.0.4'
3234
spec.add_dependency 'http', '~> 2.2.2'

lib/kubeclient.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require 'kubeclient/common'
55
require 'kubeclient/config'
66
require 'kubeclient/entity_list'
7+
require 'kubeclient/google_application_default_credentials'
78
require 'kubeclient/http_error'
89
require 'kubeclient/missing_kind_compatibility'
910
require 'kubeclient/resource'

lib/kubeclient/common.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'json'
22
require 'rest-client'
3+
34
module Kubeclient
45
# Common methods
56
# this is mixed in by other gems
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
module Kubeclient
4+
# Get a bearer token from the Google's application default credentials.
5+
class GoogleApplicationDefaultCredentials
6+
class << self
7+
def token
8+
require 'googleauth'
9+
scopes = ['https://www.googleapis.com/auth/cloud-platform']
10+
authorization = Google::Auth.get_application_default(scopes)
11+
authorization.apply({})
12+
authorization.access_token
13+
end
14+
end
15+
end
16+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require_relative 'test_helper'
2+
require 'googleauth'
3+
4+
# Unit tests for the ApplicationDefaultCredentials token provider
5+
class GoogleApplicationDefaultCredentialsTest < MiniTest::Test
6+
def test_token
7+
auth = Minitest::Mock.new
8+
auth.expect(:apply, nil, [{}])
9+
auth.expect(:access_token, 'valid_token')
10+
11+
Google::Auth.stub(:get_application_default, auth) do
12+
assert_equal('valid_token', Kubeclient::GoogleApplicationDefaultCredentials.token)
13+
end
14+
end
15+
end

test/test_kubeclient.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,13 +651,13 @@ def test_init_username_and_bearer_token
651651
assert_equal(expected_msg, exception.message)
652652
end
653653

654-
def test_init_user_and_bearer_token
654+
def test_init_username_and_bearer_token_file
655655
expected_msg = 'Invalid auth options: specify only one of username/password,' \
656656
' bearer_token or bearer_token_file'
657657
exception = assert_raises(ArgumentError) do
658658
Kubeclient::Client.new(
659659
'http://localhost:8080',
660-
auth_options: { username: 'username', bearer_token: 'token' }
660+
auth_options: { username: 'username', bearer_token_file: 'token-file' }
661661
)
662662
end
663663
assert_equal(expected_msg, exception.message)

0 commit comments

Comments
 (0)