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

feat: Added Temporary token based authentication similar to cli --access-token-file #1467

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

AmitChotaliya
Copy link

Fixes #1165

This features allows using access token in the sdk without having any of the GCP related credentials.

Example Usage:

  • Generate the token using CLI gcloud auth print-access-token --impersonate-service-account=abc@xyz.iam.gserviceaccount.com
  • Generate the token using python.
from google.auth import compute_engine
from google.auth import default, credentials
from google.auth.transport.requests import Request
import google.auth
from google.cloud import compute_v1

def generate_access_token():    
    creds, project = google.auth.default()    
    
    creds.refresh(Request())
    return creds.token, creds.expiry
  • Copy this access token to a different/local machine or container having no other google credentials.
  • Set the required environment variable.
export GOOGLE_TEMPORARY_ACCESS_TOKEN=token copied from above
  • Try running the following python code to verify if the credentials are working.
def list_virtual_machines(project_id, creds):
    # Create a client for Compute Engine with the provided access token
    compute_client = compute_v1.NetworksClient(credentials=creds)

    # List VMs
    vm_list = compute_client.list(project=project_id)

    # Print VM names
    print("Virtual Machines:")
    for vm in vm_list:
        print(vm.name)
os.environ["GOOGLE_TEMPORARY_ACCESS_TOKEN"] = "access token copied from above"
    creds, project_id = google.auth.default()    
    list_virtual_machines("bamboo-weft-375813", creds=creds)

It should work fine till the token is valid.

@AmitChotaliya AmitChotaliya requested review from a team as code owners February 6, 2024 10:19
Copy link

google-cla bot commented Feb 6, 2024

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Copy link

🤖 I detect that the PR title and the commit message differ and there's only one commit. To use the PR title for the commit history, you can use Github's automerge feature with squashing, or use automerge label. Good luck human!

-- conventional-commit-lint bot
https://conventionalcommits.org/

@AmitChotaliya AmitChotaliya changed the title Added Temporary token based authentication similar to cli --access-token-file feat: Added Temporary token based authentication similar to cli --access-token-file Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for CLOUDSDK_AUTH_ACCESS_TOKEN environment variable
1 participant