|
1 | 1 | //! GCP auth provides authentication using service accounts Google Cloud Platform (GCP) |
2 | 2 | //! |
3 | | -//! The library looks for authentication methods in the following order: |
| 3 | +//! GCP auth is a simple, minimal authentication library for Google Cloud Platform (GCP) |
| 4 | +//! providing authentication using service accounts. Once authenticated, the service |
| 5 | +//! account can be used to acquire bearer tokens for use in authenticating against GCP |
| 6 | +//! services. |
4 | 7 | //! |
5 | | -//! 1. Path to service account JSON configuration file using GOOGLE_APPLICATION_CREDENTIALS environment |
6 | | -//! variable. The service account configuration file can be downloaded in the IAM service when displaying service account detail. |
7 | | -//! The downloaded JSON file should be provided without any further modification. |
8 | | -//! 2. Invoking the library inside GCP environment fetches the default service account for the service and |
9 | | -//! the application is authenticated using that particular account |
10 | | -//! 3. Application default credentials. Local user authentication for development purposes created using `gcloud auth` application. |
11 | | -//! 4. If none of the above can be used an error occurs |
| 8 | +//! The library supports the following methods of retrieving tokens: |
12 | 9 | //! |
13 | | -//! The tokens are single-use and as such they shouldn't be cached and for each use a new token should be requested. |
14 | | -//! Library handles token caching for their lifetime and so it won't make a request if a token with appropriate scope |
15 | | -//! is available. |
| 10 | +//! 1. Reading custom service account credentials from the path pointed to by the |
| 11 | +//! `GOOGLE_APPLICATION_CREDENTIALS` environment variable. Alternatively, custom service |
| 12 | +//! account credentials can be read from a JSON file or string. |
| 13 | +//! 2. Retrieving a token from the `gcloud` CLI tool, if it is available on the `PATH`. |
| 14 | +//! 3. Use the default service account by retrieving a token from the metadata server. |
| 15 | +//! 4. Look for credentials in `.config/gcloud/application_default_credentials.json`; |
| 16 | +//! if found, use these credentials to request refresh tokens. |
16 | 17 | //! |
17 | | -//! # Default service account |
| 18 | +//! For more details, see [`AuthenticationManager::new()`]. |
18 | 19 | //! |
19 | | -//! When running inside GCP the library can be asked directly without any further configuration to provide a Bearer token |
20 | | -//! for the current service account of the service. |
| 20 | +//! The `AuthenticationManager` handles caching tokens for their lifetime; it will not make a request if |
| 21 | +//! an appropriate token is already cached. Therefore, the caller should not cache tokens. |
21 | 22 | //! |
22 | | -//! ```async |
23 | | -//! let authentication_manager = gcp_auth::init().await?; |
24 | | -//! let token = authentication_manager.get_token().await?; |
25 | | -//! ``` |
| 23 | +//! ## Simple usage |
26 | 24 | //! |
27 | | -//! # Custom service account |
| 25 | +//! The default way to use this library is to get instantiate an [`AuthenticationManager`]. It will |
| 26 | +//! find the appropriate authentication method and use it to retrieve tokens. |
28 | 27 | //! |
29 | | -//! When running outside of GCP e.g on development laptop to allow finer granularity for permission a |
30 | | -//! custom service account can be used. To use a custom service account a configuration file containing key |
31 | | -//! has to be downloaded in IAM service for the service account you intend to use. The configuration file has to |
32 | | -//! be available to the application at run time. The path to the configuration file is specified by |
33 | | -//! `GOOGLE_APPLICATION_CREDENTIALS` environment variable. |
| 28 | +//! ```rust,no_run |
| 29 | +//! # async fn get_token() -> Result<(), gcp_auth::Error> { |
| 30 | +//! use gcp_auth::AuthenticationManager; |
34 | 31 | //! |
35 | | -//! ```async |
36 | | -//! // GOOGLE_APPLICATION_CREDENTIALS environment variable is set-up |
37 | | -//! let authentication_manager = gcp_auth::init().await?; |
38 | | -//! let token = authentication_manager.get_token().await?; |
| 32 | +//! let authentication_manager = AuthenticationManager::new().await?; |
| 33 | +//! let scopes = &["https://www.googleapis.com/auth/cloud-platform"]; |
| 34 | +//! let token = authentication_manager.get_token(scopes).await?; |
| 35 | +//! # Ok(()) |
| 36 | +//! # } |
39 | 37 | //! ``` |
40 | | -//! You may instantiate `authentication_manager` from a credentials file path using the method `from_credentials_file`: |
41 | 38 | //! |
42 | | -//! ```async |
| 39 | +//! ## Supplying service account credentials |
| 40 | +//! |
| 41 | +//! When running outside of GCP (for example, on a development machine), it can be useful to supply |
| 42 | +//! service account credentials. The first method checked by [`AuthenticationManager::new()`] is to |
| 43 | +//! read a path to a file containing JSON credentials in the `GOOGLE_APPLICATION_CREDENTIALS` |
| 44 | +//! environment variable. However, you may also supply a custom path to read credentials from, or |
| 45 | +//! a `&str` containing the credentials. In both of these cases, you should create a |
| 46 | +//! [`CustomServiceAccount`] directly using one of its associated functions: |
| 47 | +//! |
| 48 | +//! ```rust,no_run |
| 49 | +//! # use std::path::PathBuf; |
| 50 | +//! # |
| 51 | +//! # async fn get_token() -> Result<(), gcp_auth::Error> { |
| 52 | +//! use gcp_auth::{AuthenticationManager, CustomServiceAccount}; |
| 53 | +//! |
43 | 54 | //! // `credentials_path` variable is the path for the credentials `.json` file. |
44 | | -//! let authentication_manager = gcp_auth::from_credentials_file(credentials_path).await?; |
45 | | -//! let token = authentication_manager.get_token().await?; |
| 55 | +//! let credentials_path = PathBuf::from("service-account.json"); |
| 56 | +//! let service_account = CustomServiceAccount::from_file(credentials_path)?; |
| 57 | +//! let authentication_manager = AuthenticationManager::from(service_account); |
| 58 | +//! let scopes = &["https://www.googleapis.com/auth/cloud-platform"]; |
| 59 | +//! let token = authentication_manager.get_token(scopes).await?; |
| 60 | +//! # Ok(()) |
| 61 | +//! # } |
46 | 62 | //! ``` |
47 | 63 | //! |
48 | | -//! # Local user authentication |
49 | | -//! This authentication method allows developers to authenticate again GCP services when developing locally. |
50 | | -//! The method is intended only for development. Credentials can be set-up using `gcloud auth` utility. |
51 | | -//! Credentials are read from file `~/.config/gcloud/application_default_credentials.json`. |
| 64 | +//! ## Getting tokens in multi-thread or async environments |
| 65 | +//! |
| 66 | +//! Using a `OnceCell` makes it easy to reuse the [`AuthenticationManager`] across different |
| 67 | +//! threads or async tasks. |
52 | 68 | //! |
53 | | -//! # FAQ |
| 69 | +//! ```rust,no_run |
| 70 | +//! use gcp_auth::AuthenticationManager; |
| 71 | +//! use tokio::sync::OnceCell; |
54 | 72 | //! |
55 | | -//! ## Does library support windows? |
| 73 | +//! static AUTH_MANAGER: OnceCell<AuthenticationManager> = OnceCell::const_new(); |
56 | 74 | //! |
57 | | -//! No |
| 75 | +//! async fn authentication_manager() -> &'static AuthenticationManager { |
| 76 | +//! AUTH_MANAGER |
| 77 | +//! .get_or_init(|| async { |
| 78 | +//! AuthenticationManager::new() |
| 79 | +//! .await |
| 80 | +//! .expect("unable to initialize authentication manager") |
| 81 | +//! }) |
| 82 | +//! .await |
| 83 | +//! } |
| 84 | +//! ``` |
58 | 85 |
|
59 | 86 | #![deny(missing_docs)] |
60 | 87 | #![deny(warnings)] |
|
0 commit comments