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

Allow supplying ones own subject token for workload identities #184

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions lib/goth/token.ex
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ defmodule Goth.Token do

* `"subject_token_type"`

* `"credential_source"` - information about how to retrieve the subject token, can contain the following keys:
* `"subject_token"`

* `"credential_source"` - information about how to retrieve the subject token if not provided, can contain the following keys:
* `"format"` - including a `"type"` of `"json"` or `"text"` and optionally `"subject_token_field_name"` if `"json"`

* `"file"` - file to read the subject token from
Expand Down Expand Up @@ -380,10 +382,18 @@ defmodule Goth.Token do
%{
"token_url" => token_url,
"audience" => audience,
"subject_token_type" => subject_token_type,
"credential_source" => credential_source
"subject_token_type" => subject_token_type
} = credentials

subject_token =
case credentials do
%{"subject_token" => subject_token} ->
subject_token

%{"credential_source" => credential_source} ->
subject_token_from_credential_source(credential_source, config)
end

headers = [{"Content-Type", "application/x-www-form-urlencoded"}]

body =
Expand All @@ -393,7 +403,7 @@ defmodule Goth.Token do
"requested_token_type" => "urn:ietf:params:oauth:token-type:access_token",
"scope" => List.first(@default_scopes),
"subject_token_type" => subject_token_type,
"subject_token" => subject_token_from_credential_source(credential_source, config)
"subject_token" => subject_token
})

response = request(config.http_client, method: :post, url: token_url, headers: headers, body: body)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"audience": "//iam.googleapis.com/projects/my-project/locations/global/workloadIdentityPools/my-cluster/providers/my-provider",
"subject_token_type": "urn:ietf:params:oauth:token-type:jwt",
"subject_token": "token",
"token_url": "https://sts.googleapis.com/v1/token",
"type": "external_account"
}
24 changes: 24 additions & 0 deletions test/goth/token_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,30 @@ defmodule Goth.TokenTest do
assert token.scope == nil
end

test "fetch/1 from workload identity, subject_token supplied" do
token_bypass = Bypass.open()

Bypass.expect(token_bypass, fn conn ->
assert conn.request_path == "/v1/token"

body = ~s|{"access_token":"dummy","expires_in":599,"token_type":"Bearer"}|
Plug.Conn.resp(conn, 200, body)
end)

credentials =
File.read!("test/data/test-credentials-direct-workload-identity-json.json")
|> Jason.decode!()
|> Map.put("token_url", "http://localhost:#{token_bypass.port}/v1/token")

config = %{
source: {:workload_identity, credentials}
}

{:ok, token} = Goth.Token.fetch(config)
assert token.token == "dummy"
assert token.scope == nil
end

test "fetch/1 from url-based workload identity" do
token_bypass = Bypass.open()
subject_token_bypass = Bypass.open()
Expand Down