-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Copy Endpoints Python Echo sample to a new location. #631
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3140319
Copy Endpoints Python Echo sample to a new location. This will be it…
kdeus f60c166
Added gke.yaml
kdeus a6c26f3
Added HTTPS to gke.yaml
kdeus 172d7bd
Removed dockerfile
kdeus be3439b
Renamed echo to getting-started
kdeus 953e936
Fixed license URLs
kdeus 3b5752c
Added dockerfile.
kdeus c2b04bf
Removed GAE-specific instructions.
kdeus 2f8fe60
Updated GKE image name
kdeus 9f97439
Renamed gke.yaml to container-engine.yaml
kdeus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| FROM debian:jessie | ||
|
|
||
| RUN apt-get update && \ | ||
| apt-get install -y python2.7 python-pip && \ | ||
| apt-get clean && \ | ||
| rm /var/lib/apt/lists/*_* | ||
|
|
||
| ADD . /app | ||
| WORKDIR /app | ||
|
|
||
| RUN pip install -r requirements.txt | ||
| ENTRYPOINT ["gunicorn", "-b", ":8081", "main:app"] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| # Google Cloud Endpoints & App Engine Flexible Environment & Python | ||
|
|
||
| This sample demonstrates how to use Google Cloud Endpoints on Google App Engine Flexible Environment using Python. | ||
|
|
||
| This sample consists of two parts: | ||
|
|
||
| 1. The backend | ||
| 2. The clients | ||
|
|
||
| ## Running locally | ||
|
|
||
| ### Running the backend | ||
|
|
||
| For more info on running Flexible applications locally, see [the getting started documentation](https://cloud.google.com/python/getting-started/hello-world). | ||
|
|
||
| Install all the dependencies: | ||
| ```bash | ||
| $ virtualenv env | ||
| $ source env/bin/activate | ||
| $ pip install -r requirements.txt | ||
| ``` | ||
|
|
||
| Run the application: | ||
| ```bash | ||
| $ python main.py | ||
| ``` | ||
|
|
||
| ### Using the echo client | ||
|
|
||
| With the app running locally, you can execute the simple echo client using: | ||
| ```bash | ||
| $ python clients/echo-client.py http://localhost:8080 APIKEY helloworld | ||
| ``` | ||
|
|
||
| The `APIKEY` doesn't matter as the endpoint proxy is not running to do authentication. | ||
|
|
||
| ## Deploying to Google App Engine | ||
|
|
||
| Open the `swagger.yaml` file and in the `host` property, replace | ||
| `YOUR-PROJECT-ID` with your project's ID. | ||
|
|
||
| Then, deploy the sample using `gcloud`: | ||
| ```bash | ||
| $ gcloud beta app deploy | ||
| ``` | ||
|
|
||
| Once deployed, you can access the application at https://YOUR-PROJECT-ID.appspot.com/. | ||
|
|
||
| ### Using the echo client | ||
|
|
||
| With the project deployed, you'll need to create an API key to access the API. | ||
|
|
||
| 1. Open the Credentials page of the API Manager in the [Cloud Console](https://console.cloud.google.com/apis/credentials). | ||
| 2. Click 'Create credentials'. | ||
| 3. Select 'API Key'. | ||
| 4. Choose 'Server Key' | ||
|
|
||
| With the API key, you can use the echo client to access the API: | ||
| ```bash | ||
| $ python clients/echo-client.py https://YOUR-PROJECT-ID.appspot.com YOUR-API-KEY helloworld | ||
| ``` | ||
|
|
||
| ### Using the JWT client (with key file) | ||
|
|
||
| The JWT client demonstrates how to use a service account to authenticate to endpoints with the service account's private key file. To use the client, you'll need both an API key (as described in the echo client section) and a service account. To create a service account: | ||
|
|
||
| 1. Open the Credentials page of the API Manager in the [Cloud Console](https://console.cloud.google.com/apis/credentials). | ||
| 2. Click 'Create credentials'. | ||
| 3. Select 'Service account key'. | ||
| 4. In the 'Select service account' dropdown, select 'Create new service account'. | ||
| 5. Choose 'JSON' for the key type. | ||
|
|
||
| To use the service account for authentication: | ||
|
|
||
| 1. Update the `google_jwt`'s `x-jwks_uri` in `swagger.yaml` with your service account's email address. | ||
| 2. Redeploy your application. | ||
|
|
||
| Now you can use the JWT client to make requests to the API: | ||
| ```bash | ||
| $ python clients/google-jwt-client.py https://YOUR-PROJECT-ID.appspot.com YOUR-API-KEY /path/to/service-account.json | ||
| ``` | ||
|
|
||
| ### Using the ID Token client (with key file) | ||
|
|
||
| The ID Token client demonstrates how to use user credentials to authenticate to endpoints. To use the client, you'll need both an API key (as described in the echo client section) and a OAuth2 client ID. To create a client ID: | ||
|
|
||
| 1. Open the Credentials page of the API Manager in the [Cloud Console](https://console.cloud.google.com/apis/credentials). | ||
| 2. Click 'Create credentials'. | ||
| 3. Select 'OAuth client ID'. | ||
| 4. Choose 'Other' for the application type. | ||
|
|
||
| To use the client ID for authentication: | ||
|
|
||
| 1. Update the `/auth/info/googleidtoken`'s `audiences` in `swagger.yaml` with your client ID. | ||
| 2. Redeploy your application. | ||
|
|
||
| Now you can use the client ID to make requests to the API: | ||
| ```bash | ||
| $ python clients/google-id-token-client.py https://YOUR-PROJECT-ID.appspot.com YOUR-API-KEY /path/to/client-id.json | ||
| ``` | ||
|
|
||
| ### Using the App Engine default service account client (no key file needed) | ||
|
|
||
| The App Engine default service account client demonstrates how to use the Google App Engine default service account to authenticate to endpoints. | ||
| We refer to the project that serves API requests as the server project. You also need to create a client project in the [Cloud Console](https://console.cloud.google.com). The client project is running Google App Engine standard application. | ||
|
|
||
| To use the App Engine default service account for authentication: | ||
|
|
||
| 1. Update the `gae_default_service_account`'s `x-issuer` and `x-jwks_uri` in `swagger.yaml` with your client project ID. | ||
| 2. Redeploy your server application. | ||
| 3. Update clients/service_to_service_gae_default/main.py, replace 'YOUR-CLIENT-PROJECT-ID' and 'YOUR-SERVER-PROJECT-ID' with your client project ID and your server project ID. | ||
| 4. Upload your application to Google App Engine by invoking the following command. Note that you need to provide project ID in the command because there are two projects (server and client projects) here and gcloud needs to know which project to pick. | ||
| ```bash | ||
| $ gcloud app deploy app.yaml --project=YOUR-CLIENT-PROJECT-ID | ||
| ``` | ||
|
|
||
| Your client app is now deployed at https://YOUR-CLIENT-PROJECT-ID.appspot.com. When you access https://YOUR-CLIENT-PROJECT-ID.appspot.com, your client calls your server project API using | ||
| the client's service account. | ||
|
|
||
| ### Using the service account client (no key file needed) | ||
|
|
||
| The service account client demonstrates how to use a non-default service account to authenticate to endpoints. | ||
| We refer to the project that serves API requests as the server project. You also need to create a client project in the [Cloud Console](https://console.cloud.google.com). | ||
| The client project is running Google App Engine standard application. | ||
|
|
||
| In the example, we use Google Cloud Identity and Access Management (IAM) API to create a JSON Web Token (JWT) for a service account, and use it to call an Endpoints API. | ||
|
|
||
| To use the client, you will need to enable "Service Account Actor" role for App Engine default service account: | ||
|
|
||
| 1. Go to [IAM page](https://console.cloud.google.com/iam-admin/iam) of your client project. | ||
| 2. For App Engine default service account, from “Role(s)” drop-down menu, select “Project”-“Service Account Actor”, and Save. | ||
|
|
||
| You also need to install Google API python library because the client code (main.py) uses googleapiclient, | ||
| which is a python library that needs to be uploaded to App Engine with your application code. After you run "pip install -t lib -r requirements", | ||
| Google API python client library should have already been installed under 'lib' directory. Additional information can be found | ||
| [here](https://cloud.google.com/appengine/docs/python/tools/using-libraries-python-27#requesting_a_library). | ||
|
|
||
| To use the client for authentication: | ||
|
|
||
| 1. Update the `google_service_account`'s `x-issuer` and `x-jwks_uri` in `swagger.yaml` with your service account email. | ||
| 2. Redeploy your server application. | ||
| 3. Update clients/service_to_service_non_default/main.py by replacing 'YOUR-SERVICE-ACCOUNT-EMAIL', 'YOUR-SERVER-PROJECT-ID' and 'YOUR-CLIENT-PROJECT-ID' | ||
| with your service account email, your server project ID, and your client project ID, respectively. | ||
| 4. Upload your application to Google App Engine by invoking the following command. Note that you need to provide project ID in the command because there are two projects (server and client projects) here and gcloud needs to know which project to pick. | ||
| ```bash | ||
| $ gcloud app deploy app.yaml --project=YOUR-CLIENT-PROJECT-ID | ||
| ``` | ||
|
|
||
| Your client app is now deployed at https://YOUR-CLIENT-PROJECT-ID.appspot.com. When you access https://YOUR-CLIENT-PROJECT-ID.appspot.com, your client calls your server project API using | ||
| the client's service account. | ||
|
|
||
| ### Using the ID token client (no key file needed) | ||
|
|
||
| This example demonstrates how to authenticate to endpoints from Google App Engine default service account using Google ID token. | ||
| In the example, we first create a JSON Web Token (JWT) using the App Engine default service account. We then request a Google | ||
| ID token using the JWT, and call an Endpoints API using the Google ID token. | ||
|
|
||
| We refer to the project that serves API requests as the server project. You also need to create a client project in the [Cloud Console](https://console.cloud.google.com). | ||
| The client project is running Google App Engine standard application. | ||
|
|
||
| To use the client for authentication: | ||
|
|
||
| 1. Update the `google_id_token`'s audiences, replace `YOUR-SERVER-PROJECT-ID` with your server project ID. | ||
| 2. Redeploy your server application. | ||
| 3. Update clients/service_to_service_google_id_token/main.py, replace 'YOUR-CLIENT-PROJECT-ID' and 'YOUR-SERVER-PROJECT-ID' with your client project ID and your server project ID. | ||
| 4. Upload your application to Google App Engine by invoking the following command. Note that you need to provide project ID in the command because there are two projects (server and client projects) here and gcloud needs to know which project to pick. | ||
| ```bash | ||
| $ gcloud app deploy app.yaml --project=YOUR-CLIENT-PROJECT-ID | ||
| ``` | ||
|
|
||
| Your client app is now deployed at https://YOUR-CLIENT-PROJECT-ID.appspot.com. When you access https://YOUR-CLIENT-PROJECT-ID.appspot.com, your client calls your server project API from | ||
| the client's service account using Google ID token. | ||
|
|
||
| ## Viewing the Endpoints graphs | ||
|
|
||
| By using Endpoints, you get access to several metrics that are displayed graphically in the Cloud Console. | ||
|
|
||
| To view the Endpoints graphs: | ||
|
|
||
| 1. Go to the [Endpoints section in Cloud Console](https://console.cloud.google.com/endpoints) of the project you deployed your API to. | ||
| 2. Click on your API to view more detailed information about the metrics collected. | ||
|
|
||
| ## Swagger UI | ||
|
|
||
| The Swagger UI is an open source Swagger project that allows you to explore your API through a UI. Find out more about it on the [Swagger site](http://swagger.io/swagger-ui/). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| runtime: python | ||
| vm: true | ||
| entrypoint: gunicorn -b :$PORT main:app | ||
|
|
||
| runtime_config: | ||
| python_version: 3 | ||
|
|
||
| beta_settings: | ||
| # Enable Google Cloud Endpoints API management. | ||
| use_endpoints_api_management: true | ||
| # Specify the Swagger API specification. | ||
| endpoints_swagger_spec_file: swagger.yaml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| # Copyright 2016 Google Inc. All Rights Reserved. | ||
| # | ||
| # 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 | ||
| # | ||
| # http://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. | ||
|
|
||
| """Example of calling a simple Google Cloud Endpoint API.""" | ||
|
|
||
| import argparse | ||
|
|
||
| import requests | ||
| from six.moves import urllib | ||
|
|
||
|
|
||
| def make_request(host, api_key, message): | ||
| """Makes a request to the auth info endpoint for Google ID tokens.""" | ||
| url = urllib.parse.urljoin(host, 'echo') | ||
| params = { | ||
| 'key': api_key | ||
| } | ||
| body = { | ||
| 'message': message | ||
| } | ||
|
|
||
| response = requests.post(url, params=params, json=body) | ||
|
|
||
| response.raise_for_status() | ||
| return response.text | ||
|
|
||
|
|
||
| def main(host, api_key, message): | ||
| response = make_request(host, api_key, message) | ||
| print(response) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| parser = argparse.ArgumentParser( | ||
| description=__doc__, | ||
| formatter_class=argparse.RawDescriptionHelpFormatter) | ||
| parser.add_argument( | ||
| 'host', help='Your API host, e.g. https://your-project.appspot.com.') | ||
| parser.add_argument( | ||
| 'api_key', help='Your API key.') | ||
| parser.add_argument( | ||
| 'message', | ||
| help='Message to echo.') | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| main(args.host, args.api_key, args.message) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| # Copyright 2016 Google Inc. All Rights Reserved. | ||
| # | ||
| # 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 | ||
| # | ||
| # http://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. | ||
|
|
||
| """Example of calling a Google Cloud Endpoint API with an ID token obtained | ||
| using the Google OAuth2 flow.""" | ||
|
|
||
| import argparse | ||
|
|
||
| import oauth2client.client | ||
| import oauth2client.file | ||
| import oauth2client.tools | ||
| import requests | ||
| from six.moves import urllib | ||
|
|
||
|
|
||
| def get_id_token(client_secrets_file, extra_args): | ||
| storage = oauth2client.file.Storage('credentials.dat') | ||
| credentials = storage.get() | ||
|
|
||
| if not credentials or credentials.invalid: | ||
| flow = oauth2client.client.flow_from_clientsecrets( | ||
| client_secrets_file, scope='email') | ||
| credentials = oauth2client.tools.run_flow( | ||
| flow, storage, flags=extra_args) | ||
|
|
||
| # The ID token is used by Cloud Endpoints, not the access token. | ||
| id_token = credentials.token_response['id_token'] | ||
|
|
||
| return id_token | ||
|
|
||
|
|
||
| def make_request(host, api_key, id_token): | ||
| """Makes a request to the auth info endpoint for Google ID tokens.""" | ||
| url = urllib.parse.urljoin(host, '/auth/info/googleidtoken') | ||
| params = { | ||
| 'key': api_key | ||
| } | ||
| headers = { | ||
| 'Authorization': 'Bearer {}'.format(id_token) | ||
| } | ||
|
|
||
| response = requests.get(url, params=params, headers=headers) | ||
|
|
||
| response.raise_for_status() | ||
| return response.text | ||
|
|
||
|
|
||
| def main(host, api_key, client_secrets_file, extra_args): | ||
| id_token = get_id_token(client_secrets_file, extra_args) | ||
| response = make_request(host, api_key, id_token) | ||
| print(response) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| parser = argparse.ArgumentParser( | ||
| description=__doc__, | ||
| formatter_class=argparse.RawDescriptionHelpFormatter, | ||
| parents=[oauth2client.tools.argparser]) | ||
| parser.add_argument( | ||
| 'host', help='Your API host, e.g. https://your-project.appspot.com.') | ||
| parser.add_argument( | ||
| 'api_key', help='Your API key.') | ||
| parser.add_argument( | ||
| 'client_secrets_file', | ||
| help='The path to your OAuth2 client secrets file.') | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| main(args.host, args.api_key, args.client_secrets_file, args) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why not just use
gcr.io/google_appengine/python- it's designed to work on GKE as well.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.
Turns out we don't actually need this Dockerfile. Removed, in a following commit.