Skip to content

Commit ca2ef36

Browse files
authored
Validate credential account (#345)
Added validation to check whether it's an `external_account` or `service_account` to fix error `missing client_email` caused by https://github.com/googleapis/google-auth-library-ruby/blob/main/lib/googleauth/json_key_reader.rb#L24 when using Workload Identity Federation. - `service_account` –> `Google::Auth::ServiceAccountCredentials` - `external_account` –> `Google::Auth::ExternalAccount::Credentials`
1 parent 55aaa28 commit ca2ef36

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

lib/fastlane/plugin/firebase_app_distribution/helper/firebase_app_distribution_auth_client.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ def firebase_token(refresh_token, debug)
9999
end
100100

101101
def service_account(google_service_path, debug)
102-
service_account_credentials = Google::Auth::ServiceAccountCredentials.make_creds(
102+
# check if it's an external account or service account
103+
json_file = JSON.parse(File.read(google_service_path))
104+
auth = json_file["type"] == "external_account" ? Google::Auth::ExternalAccount::Credentials : Google::Auth::ServiceAccountCredentials
105+
service_account_credentials = auth.make_creds(
103106
json_key_io: File.open(google_service_path),
104107
scope: SCOPE
105108
)

spec/firebase_app_distribution_auth_client_spec.rb

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
let(:fake_binary) { double("Binary") }
44
let(:fake_binary_contents) { double("Contents") }
55
let(:firebase_auth) { Signet::OAuth2::Client }
6-
let(:service_auth) { Google::Auth::ServiceAccountCredentials }
6+
let(:service_account_auth) { Google::Auth::ServiceAccountCredentials }
7+
let(:fake_service_account_contents_json) { "{\"type\": \"service_account\"}" }
8+
let(:external_account_auth) { Google::Auth::ExternalAccount::Credentials }
9+
let(:fake_external_account_contents_json) { "{\"type\": \"external_account\"}" }
710
let(:fake_firebase_tools_contents) { "{\"tokens\": {\"refresh_token\": \"refresh_token\"} }" }
811
let(:fake_firebase_tools_contents_no_tokens_field) { "{}" }
912
let(:fake_firebase_tools_contents_no_refresh_field) { "{\"tokens\": \"empty\"}" }
@@ -20,14 +23,18 @@
2023
allow(fake_oauth_client).to receive(:access_token)
2124
.and_return("fake_auth_token")
2225

23-
allow(service_auth).to receive(:make_creds)
26+
allow(service_account_auth).to receive(:make_creds)
27+
.and_return(fake_service_creds)
28+
allow(external_account_auth).to receive(:make_creds)
2429
.and_return(fake_service_creds)
2530
allow(fake_service_creds).to receive(:fetch_access_token!)
2631
.and_return(payload)
2732

2833
allow(File).to receive(:open).and_call_original
2934
allow(File).to receive(:open)
3035
.and_return(fake_binary)
36+
allow(File).to receive(:read)
37+
.and_return(fake_service_account_contents_json)
3138
allow(fake_binary).to receive(:read)
3239
.and_return(fake_binary_contents)
3340
allow(fake_binary_contents).to receive(:key)
@@ -56,10 +63,20 @@
5663
.to eq(fake_service_creds)
5764
end
5865

59-
it 'auths with service credentials environment variable' do
66+
it 'auths with service account credentials environment variable' do
67+
allow(ENV).to receive(:[])
68+
.with("GOOGLE_APPLICATION_CREDENTIALS")
69+
.and_return("google_service_path")
70+
expect(auth_client.get_authorization(empty_val, empty_val))
71+
.to eq(fake_service_creds)
72+
end
73+
74+
it 'auths with external account credentials environment variable' do
6075
allow(ENV).to receive(:[])
6176
.with("GOOGLE_APPLICATION_CREDENTIALS")
6277
.and_return("google_service_path")
78+
allow(File).to receive(:read)
79+
.and_return(fake_external_account_contents_json)
6380
expect(auth_client.get_authorization(empty_val, empty_val))
6481
.to eq(fake_service_creds)
6582
end

0 commit comments

Comments
 (0)