|
1 | 1 | # Authentication
|
2 | 2 |
|
3 |
| -In general, the google-analytics-admin-v1alpha 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. |
| 3 | +The recommended way to authenticate to the google-analytics-admin-v1alpha library is to use |
| 4 | +[Application Default Credentials (ADC)](https://cloud.google.com/docs/authentication/application-default-credentials). |
| 5 | +To review all of your authentication options, see [Credentials lookup](#credential-lookup). |
15 | 6 |
|
16 | 7 | ## Quickstart
|
17 | 8 |
|
18 |
| -1. [Create a service account and credentials](#creating-a-service-account). |
19 |
| -2. Set the [environment variable](#environment-variables). |
| 9 | +The following example shows how to set up authentication for a local development |
| 10 | +environment with your user credentials. |
20 | 11 |
|
21 |
| -```sh |
22 |
| -export ANALYTICS_ADMIN_CREDENTIALS=path/to/keyfile.json |
23 |
| -``` |
24 |
| - |
25 |
| -3. Initialize the client. |
| 12 | +**NOTE:** This method is _not_ recommended for running in production. User credentials |
| 13 | +should be used only during development. |
26 | 14 |
|
27 |
| -```ruby |
28 |
| -require "google/analytics/admin/v1alpha" |
| 15 | +1. [Download and install the Google Cloud CLI](https://cloud.google.com/sdk). |
| 16 | +2. Set up a local ADC file with your user credentials: |
29 | 17 |
|
30 |
| -client = ::Google::Analytics::Admin::V1alpha::AnalyticsAdminService::Client.new |
| 18 | +```sh |
| 19 | +gcloud auth application-default login |
31 | 20 | ```
|
32 | 21 |
|
33 |
| -## Credential Lookup |
34 |
| - |
35 |
| -The google-analytics-admin-v1alpha 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 |
| 22 | +3. Write code as if already authenticated. |
49 | 23 |
|
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. |
| 24 | +For more information about setting up authentication for a local development environment, see |
| 25 | +[Set up Application Default Credentials](https://cloud.google.com/docs/authentication/provide-credentials-adc#local-dev). |
54 | 26 |
|
55 |
| -### Environment Variables |
| 27 | +## Credential Lookup |
56 | 28 |
|
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. |
| 29 | +The google-analytics-admin-v1alpha library provides several mechanisms to configure your system. |
| 30 | +Generally, using Application Default Credentials to facilitate automatic |
| 31 | +credentials discovery is the easist method. But if you need to explicitly specify |
| 32 | +credentials, there are several methods available to you. |
64 | 33 |
|
65 |
| -The environment variables that google-analytics-admin-v1alpha |
66 |
| -checks for credentials are configured on the service Credentials class (such as |
67 |
| -{::Google::Analytics::Admin::V1alpha::AnalyticsAdminService::Credentials}): |
| 34 | +Credentials are accepted in the following ways, in the following order or precedence: |
68 | 35 |
|
69 |
| -* `ANALYTICS_ADMIN_CREDENTIALS` - Path to JSON file, or JSON contents |
70 |
| -* `ANALYTICS_ADMIN_KEYFILE` - Path to JSON file, or JSON contents |
71 |
| -* `GOOGLE_CLOUD_CREDENTIALS` - Path to JSON file, or JSON contents |
72 |
| -* `GOOGLE_CLOUD_KEYFILE` - Path to JSON file, or JSON contents |
73 |
| -* `GOOGLE_APPLICATION_CREDENTIALS` - Path to JSON file |
| 36 | +1. Credentials specified in method arguments |
| 37 | +2. Credentials specified in configuration |
| 38 | +3. Credentials pointed to or included in environment variables |
| 39 | +4. Credentials found in local ADC file |
| 40 | +5. Credentials returned by the metadata server for the attached service account (GCP) |
74 | 41 |
|
75 |
| -```ruby |
76 |
| -require "google/analytics/admin/v1alpha" |
77 |
| - |
78 |
| -ENV["ANALYTICS_ADMIN_CREDENTIALS"] = "path/to/keyfile.json" |
| 42 | +### Configuration |
79 | 43 |
|
80 |
| -client = ::Google::Analytics::Admin::V1alpha::AnalyticsAdminService::Client.new |
81 |
| -``` |
| 44 | +You can configure a path to a JSON credentials file, either for an individual client object or |
| 45 | +globally, for all client objects. The JSON file can contain credentials created for |
| 46 | +[workload identity federation](https://cloud.google.com/iam/docs/workload-identity-federation), |
| 47 | +[workforce identity federation](https://cloud.google.com/iam/docs/workforce-identity-federation), or a |
| 48 | +[service account key](https://cloud.google.com/docs/authentication/provide-credentials-adc#local-key). |
82 | 49 |
|
83 |
| -### Configuration |
| 50 | +Note: Service account keys are a security risk if not managed correctly. You should |
| 51 | +[choose a more secure alternative to service account keys](https://cloud.google.com/docs/authentication#auth-decision-tree) |
| 52 | +whenever possible. |
84 | 53 |
|
85 |
| -The path to the **Credentials JSON** file can be configured instead of storing |
86 |
| -it in an environment variable. Either on an individual client initialization: |
| 54 | +To configure a credentials file for an individual client initialization: |
87 | 55 |
|
88 | 56 | ```ruby
|
89 | 57 | require "google/analytics/admin/v1alpha"
|
90 | 58 |
|
91 | 59 | client = ::Google::Analytics::Admin::V1alpha::AnalyticsAdminService::Client.new do |config|
|
92 |
| - config.credentials = "path/to/keyfile.json" |
| 60 | + config.credentials = "path/to/credentialfile.json" |
93 | 61 | end
|
94 | 62 | ```
|
95 | 63 |
|
96 |
| -Or globally for all clients: |
| 64 | +To configure a credentials file globally for all clients: |
97 | 65 |
|
98 | 66 | ```ruby
|
99 | 67 | require "google/analytics/admin/v1alpha"
|
100 | 68 |
|
101 | 69 | ::Google::Analytics::Admin::V1alpha::AnalyticsAdminService::Client.configure do |config|
|
102 |
| - config.credentials = "path/to/keyfile.json" |
| 70 | + config.credentials = "path/to/credentialfile.json" |
103 | 71 | end
|
104 | 72 |
|
105 | 73 | client = ::Google::Analytics::Admin::V1alpha::AnalyticsAdminService::Client.new
|
106 | 74 | ```
|
107 | 75 |
|
108 |
| -### Cloud SDK |
| 76 | +### Environment Variables |
109 | 77 |
|
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. |
| 78 | +You can also use an environment variable to provide a JSON credentials file. |
| 79 | +The environment variable can contain a path to the credentials file or, for |
| 80 | +environments such as Docker containers where writing files is not encouraged, |
| 81 | +you can include the credentials file itself. |
113 | 82 |
|
114 |
| -To configure your system for this, simply: |
| 83 | +The JSON file can contain credentials created for |
| 84 | +[workload identity federation](https://cloud.google.com/iam/docs/workload-identity-federation), |
| 85 | +[workforce identity federation](https://cloud.google.com/iam/docs/workforce-identity-federation), or a |
| 86 | +[service account key](https://cloud.google.com/docs/authentication/provide-credentials-adc#local-key). |
115 | 87 |
|
116 |
| -1. [Download and install the Cloud SDK](https://cloud.google.com/sdk) |
117 |
| -2. Authenticate using OAuth 2.0 `$ gcloud auth application-default login` |
118 |
| -3. Write code as if already authenticated. |
| 88 | +Note: Service account keys are a security risk if not managed correctly. You should |
| 89 | +[choose a more secure alternative to service account keys](https://cloud.google.com/docs/authentication#auth-decision-tree) |
| 90 | +whenever possible. |
| 91 | + |
| 92 | +The environment variables that google-analytics-admin-v1alpha |
| 93 | +checks for credentials are: |
119 | 94 |
|
120 |
| -**NOTE:** This is _not_ recommended for running in production. The Cloud SDK |
121 |
| -*should* only be used during development. |
| 95 | +* `GOOGLE_CLOUD_CREDENTIALS` - Path to JSON file, or JSON contents |
| 96 | +* `GOOGLE_APPLICATION_CREDENTIALS` - Path to JSON file |
122 | 97 |
|
123 |
| -## Creating a Service Account |
| 98 | +```ruby |
| 99 | +require "google/analytics/admin/v1alpha" |
124 | 100 |
|
125 |
| -Google Cloud requires **Service Account Credentials** to |
126 |
| -connect to the APIs. You will use the **JSON key file** to |
127 |
| -connect to most services with google-analytics-admin-v1alpha. |
| 101 | +ENV["GOOGLE_APPLICATION_CREDENTIALS"] = "path/to/credentialfile.json" |
128 | 102 |
|
129 |
| -If you are not running this client within |
130 |
| -[Google Cloud Platform environments](#google-cloud-platform-environments), you |
131 |
| -need a Google Developers service account. |
| 103 | +client = ::Google::Analytics::Admin::V1alpha::AnalyticsAdminService::Client.new |
| 104 | +``` |
132 | 105 |
|
133 |
| -1. Visit the [Google Cloud Console](https://console.cloud.google.com/project). |
134 |
| -2. Create a new project or click on an existing project. |
135 |
| -3. Activate the menu in the upper left and select **APIs & Services**. From |
136 |
| - here, you will enable the APIs that your application requires. |
| 106 | +### Local ADC file |
137 | 107 |
|
138 |
| - *Note: You may need to enable billing in order to use these services.* |
| 108 | +You can set up a local ADC file with your user credentials for authentication during |
| 109 | +development. If credentials are not provided in code or in environment variables, |
| 110 | +then the local ADC credentials are discovered. |
139 | 111 |
|
140 |
| -4. Select **Credentials** from the side navigation. |
| 112 | +Follow the steps in [Quickstart](#quickstart) to set up a local ADC file. |
141 | 113 |
|
142 |
| - Find the "Create credentials" drop down near the top of the page, and select |
143 |
| - "Service account" to be guided through downloading a new JSON key file. |
| 114 | +### Google Cloud Platform environments |
144 | 115 |
|
145 |
| - If you want to re-use an existing service account, you can easily generate a |
146 |
| - new key file. Just select the account you wish to re-use, click the pencil |
147 |
| - tool on the right side to edit the service account, select the **Keys** tab, |
148 |
| - and then select **Add Key**. |
| 116 | +When running on Google Cloud Platform (GCP), including Google Compute Engine |
| 117 | +(GCE), Google Kubernetes Engine (GKE), Google App Engine (GAE), Google Cloud |
| 118 | +Functions (GCF) and Cloud Run, credentials are retrieved from the attached |
| 119 | +service account automatically. Code should be written as if already authenticated. |
149 | 120 |
|
150 |
| - The key file you download will be used by this library to authenticate API |
151 |
| - requests and should be stored in a secure location. |
| 121 | +For more information, see |
| 122 | +[Set up ADC for Google Cloud services](https://cloud.google.com/docs/authentication/provide-credentials-adc#attached-sa). |
0 commit comments