|
| 1 | +# Authentication |
| 2 | + |
| 3 | +In general, the google-cloud-access_approval library uses |
| 4 | +[Service Account](https://cloud.google.com/iam/docs/creating-managing-service-accounts) |
| 5 | +credentials to connect to Google Cloud services. When running within |
| 6 | +[Google Cloud Platform environments](#google-cloud-platform-environments) the |
| 7 | +credentials will be discovered automatically. When running on other |
| 8 | +environments, the Service Account credentials can be specified by providing the |
| 9 | +path to the |
| 10 | +[JSON keyfile](https://cloud.google.com/iam/docs/managing-service-account-keys) |
| 11 | +for the account (or the JSON itself) in |
| 12 | +[environment variables](#environment-variables). Additionally, Cloud SDK |
| 13 | +credentials can also be discovered automatically, but this is only recommended |
| 14 | +during development. |
| 15 | + |
| 16 | +## Quickstart |
| 17 | + |
| 18 | +1. [Create a service account and credentials](#creating-a-service-account). |
| 19 | +2. Set the [environment variable](#environment-variables). |
| 20 | + |
| 21 | +```sh |
| 22 | +export ACCESS_APPROVAL_CREDENTIALS=path/to/keyfile.json |
| 23 | +``` |
| 24 | + |
| 25 | +3. Initialize the client. |
| 26 | + |
| 27 | +```ruby |
| 28 | +require "google/cloud/access_approval" |
| 29 | + |
| 30 | +client = Google::Cloud::AccessApproval.access_approval |
| 31 | +``` |
| 32 | + |
| 33 | +## Credential Lookup |
| 34 | + |
| 35 | +The google-cloud-access_approval library aims to make authentication |
| 36 | +as simple as possible, and provides several mechanisms to configure your system |
| 37 | +without requiring **Service Account Credentials** directly in code. |
| 38 | + |
| 39 | +**Credentials** are discovered in the following order: |
| 40 | + |
| 41 | +1. Specify credentials in method arguments |
| 42 | +2. Specify credentials in configuration |
| 43 | +3. Discover credentials path in environment variables |
| 44 | +4. Discover credentials JSON in environment variables |
| 45 | +5. Discover credentials file in the Cloud SDK's path |
| 46 | +6. Discover GCP credentials |
| 47 | + |
| 48 | +### Google Cloud Platform environments |
| 49 | + |
| 50 | +When running on Google Cloud Platform (GCP), including Google Compute Engine |
| 51 | +(GCE), Google Kubernetes Engine (GKE), Google App Engine (GAE), Google Cloud |
| 52 | +Functions (GCF) and Cloud Run, **Credentials** are discovered automatically. |
| 53 | +Code should be written as if already authenticated. |
| 54 | + |
| 55 | +### Environment Variables |
| 56 | + |
| 57 | +The **Credentials JSON** can be placed in environment variables instead of |
| 58 | +declaring them directly in code. Each service has its own environment variable, |
| 59 | +allowing for different service accounts to be used for different services. (See |
| 60 | +the READMEs for the individual service gems for details.) The path to the |
| 61 | +**Credentials JSON** file can be stored in the environment variable, or the |
| 62 | +**Credentials JSON** itself can be stored for environments such as Docker |
| 63 | +containers where writing files is difficult or not encouraged. |
| 64 | + |
| 65 | +The environment variables that google-cloud-access_approval |
| 66 | +checks for credentials are configured on the service Credentials class (such as |
| 67 | +`Google::Cloud::AccessApproval::V1::AccessApproval::Credentials`): |
| 68 | + |
| 69 | +1. `ACCESS_APPROVAL_CREDENTIALS` - Path to JSON file, or JSON contents |
| 70 | +2. `ACCESS_APPROVAL_KEYFILE` - Path to JSON file, or JSON contents |
| 71 | +3. `GOOGLE_CLOUD_CREDENTIALS` - Path to JSON file, or JSON contents |
| 72 | +4. `GOOGLE_CLOUD_KEYFILE` - Path to JSON file, or JSON contents |
| 73 | +5. `GOOGLE_APPLICATION_CREDENTIALS` - Path to JSON file |
| 74 | + |
| 75 | +```ruby |
| 76 | +require "google/cloud/access_approval" |
| 77 | + |
| 78 | +ENV["ACCESS_APPROVAL_CREDENTIALS"] = "path/to/keyfile.json" |
| 79 | + |
| 80 | +client = Google::Cloud::AccessApproval.access_approval |
| 81 | +``` |
| 82 | + |
| 83 | +### Configuration |
| 84 | + |
| 85 | +The **Credentials JSON** can be configured instead of placing them in |
| 86 | +environment variables. Either on an individual client initialization: |
| 87 | + |
| 88 | +```ruby |
| 89 | +require "google/cloud/access_approval" |
| 90 | + |
| 91 | +client = Google::Cloud::AccessApproval.access_approval do |config| |
| 92 | + config.credentials = "path/to/keyfile.json" |
| 93 | +end |
| 94 | +``` |
| 95 | + |
| 96 | +Or configured globally for all clients: |
| 97 | + |
| 98 | +```ruby |
| 99 | +require "google/cloud/access_approval" |
| 100 | + |
| 101 | +Google::Cloud::AccessApproval.configure do |config| |
| 102 | + config.credentials = "path/to/keyfile.json" |
| 103 | +end |
| 104 | + |
| 105 | +client = Google::Cloud::AccessApproval.access_approval |
| 106 | +``` |
| 107 | + |
| 108 | +### Cloud SDK |
| 109 | + |
| 110 | +This option allows for an easy way to authenticate during development. If |
| 111 | +credentials are not provided in code or in environment variables, then Cloud SDK |
| 112 | +credentials are discovered. |
| 113 | + |
| 114 | +To configure your system for this, simply: |
| 115 | + |
| 116 | +1. [Download and install the Cloud SDK](https://cloud.google.com/sdk) |
| 117 | +2. Authenticate using OAuth 2.0 `$ gcloud auth login` |
| 118 | +3. Write code as if already authenticated. |
| 119 | + |
| 120 | +**NOTE:** This is _not_ recommended for running in production. The Cloud SDK |
| 121 | +*should* only be used during development. |
| 122 | + |
| 123 | +[gce-how-to]: https://cloud.google.com/compute/docs/authentication#using |
| 124 | +[dev-console]: https://console.cloud.google.com/project |
| 125 | + |
| 126 | +[enable-apis]: https://raw.githubusercontent.com/GoogleCloudPlatform/gcloud-common/master/authentication/enable-apis.png |
| 127 | + |
| 128 | +[create-new-service-account]: https://raw.githubusercontent.com/GoogleCloudPlatform/gcloud-common/master/authentication/create-new-service-account.png |
| 129 | +[create-new-service-account-existing-keys]: https://raw.githubusercontent.com/GoogleCloudPlatform/gcloud-common/master/authentication/create-new-service-account-existing-keys.png |
| 130 | +[reuse-service-account]: https://raw.githubusercontent.com/GoogleCloudPlatform/gcloud-common/master/authentication/reuse-service-account.png |
| 131 | + |
| 132 | +## Creating a Service Account |
| 133 | + |
| 134 | +Google Cloud requires **Service Account Credentials** to |
| 135 | +connect to the APIs. You will use the **JSON key file** to |
| 136 | +connect to most services with google-cloud-access_approval. |
| 137 | + |
| 138 | +If you are not running this client within |
| 139 | +[Google Cloud Platform environments](#google-cloud-platform-environments), you |
| 140 | +need a Google Developers service account. |
| 141 | + |
| 142 | +1. Visit the [Google Developers Console][dev-console]. |
| 143 | +2. Create a new project or click on an existing project. |
| 144 | +3. Activate the slide-out navigation tray and select **API Manager**. From |
| 145 | + here, you will enable the APIs that your application requires. |
| 146 | + |
| 147 | + ![Enable the APIs that your application requires][enable-apis] |
| 148 | + |
| 149 | + *Note: You may need to enable billing in order to use these services.* |
| 150 | + |
| 151 | +4. Select **Credentials** from the side navigation. |
| 152 | + |
| 153 | + You should see a screen like one of the following. |
| 154 | + |
| 155 | + ![Create a new service account][create-new-service-account] |
| 156 | + |
| 157 | + ![Create a new service account With Existing Keys][create-new-service-account-existing-keys] |
| 158 | + |
| 159 | + Find the "Add credentials" drop down and select "Service account" to be |
| 160 | + guided through downloading a new JSON key file. |
| 161 | + |
| 162 | + If you want to re-use an existing service account, you can easily generate a |
| 163 | + new key file. Just select the account you wish to re-use, and click "Generate |
| 164 | + new JSON key": |
| 165 | + |
| 166 | + ![Re-use an existing service account][reuse-service-account] |
| 167 | + |
| 168 | + The key file you download will be used by this library to authenticate API |
| 169 | + requests and should be stored in a secure location. |
0 commit comments