Auth class for requests used to authenticate HTTP requests to Google Cloud Identity-Aware Proxy using user credentials.
This is in contrast to most other IAP authentication libraries which use service account credentials.
Original inspiration came from https://github.com/kiwicom/requests-iap
Typical installation via pip:
pip install requests-iap2
Alternatively you can install from source:
git clone https://github.com/climateengine/requests-iap2.git
cd requests-iap2
pip install .
You will need to have a Google Cloud project with IAP enabled and a user account with IAP Webapp User
role.
Additionally, you will need to create a OAuth 2.0 client ID and secret in the Google Cloud Console: Instructions for creating a new OAuth 2.0 client.
- This must be in the same GCP project as the IAP server.
- The OAuth client must be created as a Desktop application
In most cases, IAP will have already creates a "Web application" client ID for you, so you will have 2 clients, the "Web application" created by IAP, and the "Desktop application" you just created.
import requests
from requests_iap2 import IAPAuth
# This is the URL of the IAP-protected resource
url = "https://api.climateengine.net/"
# Create a requests Session object and set the authentication handler
session = requests.Session()
session.auth = IAPAuth(
client_oauth_client_id="something_else.apps.googleusercontent.com", # "Desktop" client
client_oauth_client_secret="client_secret_key", # "Desktop" client secret
server_oauth_client_id="something.apps.googleusercontent.com", # optional, "Web" client created by IAP
use_adc=False, # optional, set to True to use ADC instead of user credentials
oob=False, # optional, set to True if running in a headless environment or cannot run a webserver (e.g. on Vertex AI)
)
# Use the session to make requests
r = session.get(url)
# Alternatively, you can use the IAPAuth without a Session object
r = requests.get(url,
auth=IAPAuth(
server_oauth_client_id="something.apps.googleusercontent.com", # optional
client_oauth_client_id="something_else.apps.googleusercontent.com",
client_oauth_client_secret="client_secret_key"),
)
If you are running on Vertex AI or Cloud AI Platform Notebooks, you will need to set oob=True
to specify Out of Band (OOB) authentication when creating the IAPAuth
object.
Other reasons that you may need to set oob=True
would be for security readons or if you are running the program on a restricted VM.
Please note that setting oob=True
will generate an "unable to connect" or "host unreachable" error. This is expected and does not indicate authentication failure, so if you have received this error be sure and continue on to test the API authentication by making a GET request to an API endpoint.
ADC credentials only work within the same project as the IAP resource.
If you are running in Vertex AI, you can change the project that ADC uses, but the process can be a bit cumbersome.
In the Vertex AI notebook (Python), run the following, replacing client_oauth_client_id
and
client_oauth_client_secret
with the values from the "Desktop" OAuth2 client.
from requests_iap2.create_client_id_file import create_client_id_file
create_client_id_file(client_id, client_secret)
This will create a file called client_id.json
in the current directory.
Then in the Vertex AI notebook, create a Terminal and run the following (this wii not work in the Python notebook):
gcloud auth application-default login --no-browser --client-id-file=client_id.json
You will be given a very long command to copy. You will need to run this command in a Terminal outside of the Vertex AI notebook.
Copy and paste the command into a terminal running on your local machine. You will be required to go through multiple prompts to authenticate.
You may receive an error message in a browser that says "Google hasn't verified this app". To continue, click "Advanced" and then "Go to (unsafe)".
Check the boxes to allow the app to access your Google account and then click "Continue".
After allowing access, your local terminal will display a code that you will need to copy and paste into the
terminal running in Vertex AI notebook. Note: this code may look like a url starting with https://localhost:8085/...
Your Vertex AI notebook should now be able to authenticate to an IAP resource in a different project. You may need to restart the notebook kernel to pick up the new credentials.
- Add tests
- Add support for service account credentials
- Add support for ADC (Application Default Credentials)
Versioning of this package is done through setuptools-scm, which auto-generates the version number based on git tags and commits. setuptools-scm generates a unique version number for each commit in the repository according to this scheme.
The version of the package is read from requests_iap2/_version.py
(which is generated by setuptools_scm during the package build) when running as a package, and derived
from git when running from source.
See scripts/gen_requirements.sh
.
This project uses semantic versioning.
For a new minor version release (X.X.0
), create a vX.X.0
tag in main branch,
and create a vX.X
branch from the same commit for future patches to the minor version.
For patch versions, commit to and create vX.X.Y
tags in the respective minor version branch.
(e.g v1.1.1
, v1.1.2
.. tags in the v1.1
branch)
For building the package and publishing it on PyPI, see scripts/build_package.sh
and scripts/publish_package.sh
.