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

Create generate_self_signed_jwt.py #1414

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

riathakkar
Copy link

Description

Upload sample script for IAP public documentation update.

This will be documented the IAP public documentation

Note: It's a good idea to open an issue first for discussion.

Checklist

  • Tests pass
  • Lint pass: bundle exec rubocop
  • Please merge this PR for me once it is approved.

@riathakkar riathakkar requested a review from a team as a code owner May 3, 2024 22:08
@product-auto-label product-auto-label bot added the samples Issues that are directly related to samples. label May 3, 2024
Copy link
Member

@dazuma dazuma left a comment

Choose a reason for hiding this comment

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

Do you have tests for this file?

Copy link
Member

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.

require "googleauth"
require "google/cloud/iam_credentials/v1"
require "jwt"

Copy link
Member

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?

require "google/cloud/iam_credentials/v1"
require "jwt"

def generate_jwt_payload(service_account_email, resource_url)
Copy link
Member

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.

}.to_json
end

def sign_jwt(target_sa, resource_url)
Copy link
Member

Choose a reason for hiding this comment

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

Remove parentheses

def sign_jwt(target_sa, resource_url)
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
Member

Choose a reason for hiding this comment

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

Remove parentheses

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
Member

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_id = key_data["private_key_id"]
service_account_email = key_data["client_email"]

payload = generate_jwt_payload(service_account_email, resource_url)
Copy link
Member

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
Member

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?

iam_client = Google::Cloud::IamCredentials::V1::IAMCredentials::Client.new(credentials: credentials)

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

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.


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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
samples Issues that are directly related to samples.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants