-
Notifications
You must be signed in to change notification settings - Fork 210
Create generate_self_signed_jwt.py #1414
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove parentheses |
||
|
|
||
| JWT.encode( | ||
| payload, | ||
| OpenSSL::PKey::RSA.new(private_key), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you need to |
||
| "RS256", | ||
| kid: private_key_id # Include key ID in header | ||
| ) | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be
.rbnot.py.