Skip to content
Open
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
58 changes: 58 additions & 0 deletions iap/generate_self_signed_jwt.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be .rb not .py.

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright 2024 Google LLC

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# https://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require "googleauth"
require "google/cloud/iam_credentials/v1"
require "jwt"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JWT and IAM Credentials client gems probably need to be added to the Gemfile.


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be region tags in this file?

def generate_jwt_payload(service_account_email, resource_url)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the parentheses where possible.

iat = Time.now.utc
exp = iat + 3600

{
iss: service_account_email,
sub: service_account_email,
aud: resource_url,
iat: iat.to_i, # Use integer timestamp for JWT compatibility
exp: exp.to_i,
}.to_json
end

def sign_jwt(target_sa, resource_url)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove parentheses

scope = "https://www.googleapis.com/auth/iam"
credentials = Google::Auth.get_application_default([scope])
iam_client = Google::Cloud::IamCredentials::V1::IAMCredentials::Client.new(credentials: credentials)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove parentheses


response = iam_client.sign_jwt(
name: iam_client.service_account_path('-', target_sa),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use double quotes for strings.

payload: generate_jwt_payload(target_sa, resource_url),
)
response.signed_jwt
end

def sign_jwt_with_key_file(credential_key_file_path, resource_url)
key_data = JSON.parse(File.read(credential_key_file_path))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove parentheses on this line and the line above it

private_key = key_data["private_key"]
private_key_id = key_data["private_key_id"]
service_account_email = key_data["client_email"]

payload = generate_jwt_payload(service_account_email, resource_url)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove parentheses


JWT.encode(
payload,
OpenSSL::PKey::RSA.new(private_key),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to require "openssl" for this to work?

"RS256",
kid: private_key_id # Include key ID in header
)
end