-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #410 from lucasmazza/gcp-cmd-token
Generate GCP tokens through `cmd-path` config
- Loading branch information
Showing
8 changed files
with
138 additions
and
10 deletions.
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 |