Skip to content
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

feat: Support for universe_domain #23783

Merged
merged 2 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions google-cloud-org_policy-v2/.owlbot-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
".gitignore",
".repo-metadata.json",
".rubocop.yml",
".toys.rb",
".yardopts",
"AUTHENTICATION.md",
"CHANGELOG.md",
Expand Down
28 changes: 28 additions & 0 deletions google-cloud-org_policy-v2/.toys.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

# Copyright 2024 Google LLC
#
# 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
#
# https://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.

# Auto-generated by gapic-generator-ruby. DO NOT EDIT!

toys_version! ">= 0.15.3"

if ENV["RUBY_COMMON_TOOLS"]
common_tools_dir = File.expand_path ENV["RUBY_COMMON_TOOLS"]
load File.join(common_tools_dir, "toys", "gapic")
else
load_git remote: "https://github.com/googleapis/ruby-common-tools.git",
path: "toys/gapic",
update: true
end
173 changes: 72 additions & 101 deletions google-cloud-org_policy-v2/AUTHENTICATION.md
Original file line number Diff line number Diff line change
@@ -1,151 +1,122 @@
# Authentication

In general, the google-cloud-org_policy-v2 library uses
[Service Account](https://cloud.google.com/iam/docs/creating-managing-service-accounts)
credentials to connect to Google Cloud services. When running within
[Google Cloud Platform environments](#google-cloud-platform-environments) the
credentials will be discovered automatically. When running on other
environments, the Service Account credentials can be specified by providing the
path to the
[JSON keyfile](https://cloud.google.com/iam/docs/managing-service-account-keys)
for the account (or the JSON itself) in
[environment variables](#environment-variables). Additionally, Cloud SDK
credentials can also be discovered automatically, but this is only recommended
during development.
The recommended way to authenticate to the google-cloud-org_policy-v2 library is to use
[Application Default Credentials (ADC)](https://cloud.google.com/docs/authentication/application-default-credentials).
To review all of your authentication options, see [Credentials lookup](#credential-lookup).

## Quickstart

1. [Create a service account and credentials](#creating-a-service-account).
2. Set the [environment variable](#environment-variables).
The following example shows how to set up authentication for a local development
environment with your user credentials.

```sh
export ORG_POLICY_CREDENTIALS=path/to/keyfile.json
```

3. Initialize the client.
**NOTE:** This method is _not_ recommended for running in production. User credentials
should be used only during development.

```ruby
require "google/cloud/org_policy/v2"
1. [Download and install the Google Cloud CLI](https://cloud.google.com/sdk).
2. Set up a local ADC file with your user credentials:

client = ::Google::Cloud::OrgPolicy::V2::OrgPolicy::Client.new
```sh
gcloud auth application-default login
```

## Credential Lookup

The google-cloud-org_policy-v2 library aims to make authentication
as simple as possible, and provides several mechanisms to configure your system
without requiring **Service Account Credentials** directly in code.

**Credentials** are discovered in the following order:

1. Specify credentials in method arguments
2. Specify credentials in configuration
3. Discover credentials path in environment variables
4. Discover credentials JSON in environment variables
5. Discover credentials file in the Cloud SDK's path
6. Discover GCP credentials

### Google Cloud Platform environments
3. Write code as if already authenticated.

When running on Google Cloud Platform (GCP), including Google Compute Engine
(GCE), Google Kubernetes Engine (GKE), Google App Engine (GAE), Google Cloud
Functions (GCF) and Cloud Run, **Credentials** are discovered automatically.
Code should be written as if already authenticated.
For more information about setting up authentication for a local development environment, see
[Set up Application Default Credentials](https://cloud.google.com/docs/authentication/provide-credentials-adc#local-dev).

### Environment Variables
## Credential Lookup

The **Credentials JSON** can be placed in environment variables instead of
declaring them directly in code. Each service has its own environment variable,
allowing for different service accounts to be used for different services. (See
the READMEs for the individual service gems for details.) The path to the
**Credentials JSON** file can be stored in the environment variable, or the
**Credentials JSON** itself can be stored for environments such as Docker
containers where writing files is difficult or not encouraged.
The google-cloud-org_policy-v2 library provides several mechanisms to configure your system.
Generally, using Application Default Credentials to facilitate automatic
credentials discovery is the easist method. But if you need to explicitly specify
credentials, there are several methods available to you.

The environment variables that google-cloud-org_policy-v2
checks for credentials are configured on the service Credentials class (such as
{::Google::Cloud::OrgPolicy::V2::OrgPolicy::Credentials}):
Credentials are accepted in the following ways, in the following order or precedence:

* `ORG_POLICY_CREDENTIALS` - Path to JSON file, or JSON contents
* `ORG_POLICY_KEYFILE` - Path to JSON file, or JSON contents
* `GOOGLE_CLOUD_CREDENTIALS` - Path to JSON file, or JSON contents
* `GOOGLE_CLOUD_KEYFILE` - Path to JSON file, or JSON contents
* `GOOGLE_APPLICATION_CREDENTIALS` - Path to JSON file
1. Credentials specified in method arguments
2. Credentials specified in configuration
3. Credentials pointed to or included in environment variables
4. Credentials found in local ADC file
5. Credentials returned by the metadata server for the attached service account (GCP)

```ruby
require "google/cloud/org_policy/v2"

ENV["ORG_POLICY_CREDENTIALS"] = "path/to/keyfile.json"
### Configuration

client = ::Google::Cloud::OrgPolicy::V2::OrgPolicy::Client.new
```
You can configure a path to a JSON credentials file, either for an individual client object or
globally, for all client objects. The JSON file can contain credentials created for
[workload identity federation](https://cloud.google.com/iam/docs/workload-identity-federation),
[workforce identity federation](https://cloud.google.com/iam/docs/workforce-identity-federation), or a
[service account key](https://cloud.google.com/docs/authentication/provide-credentials-adc#local-key).

### Configuration
Note: Service account keys are a security risk if not managed correctly. You should
[choose a more secure alternative to service account keys](https://cloud.google.com/docs/authentication#auth-decision-tree)
whenever possible.

The path to the **Credentials JSON** file can be configured instead of storing
it in an environment variable. Either on an individual client initialization:
To configure a credentials file for an individual client initialization:

```ruby
require "google/cloud/org_policy/v2"

client = ::Google::Cloud::OrgPolicy::V2::OrgPolicy::Client.new do |config|
config.credentials = "path/to/keyfile.json"
config.credentials = "path/to/credentialfile.json"
end
```

Or globally for all clients:
To configure a credentials file globally for all clients:

```ruby
require "google/cloud/org_policy/v2"

::Google::Cloud::OrgPolicy::V2::OrgPolicy::Client.configure do |config|
config.credentials = "path/to/keyfile.json"
config.credentials = "path/to/credentialfile.json"
end

client = ::Google::Cloud::OrgPolicy::V2::OrgPolicy::Client.new
```

### Cloud SDK
### Environment Variables

This option allows for an easy way to authenticate during development. If
credentials are not provided in code or in environment variables, then Cloud SDK
credentials are discovered.
You can also use an environment variable to provide a JSON credentials file.
The environment variable can contain a path to the credentials file or, for
environments such as Docker containers where writing files is not encouraged,
you can include the credentials file itself.

To configure your system for this, simply:
The JSON file can contain credentials created for
[workload identity federation](https://cloud.google.com/iam/docs/workload-identity-federation),
[workforce identity federation](https://cloud.google.com/iam/docs/workforce-identity-federation), or a
[service account key](https://cloud.google.com/docs/authentication/provide-credentials-adc#local-key).

1. [Download and install the Cloud SDK](https://cloud.google.com/sdk)
2. Authenticate using OAuth 2.0 `$ gcloud auth application-default login`
3. Write code as if already authenticated.
Note: Service account keys are a security risk if not managed correctly. You should
[choose a more secure alternative to service account keys](https://cloud.google.com/docs/authentication#auth-decision-tree)
whenever possible.

The environment variables that google-cloud-org_policy-v2
checks for credentials are:

**NOTE:** This is _not_ recommended for running in production. The Cloud SDK
*should* only be used during development.
* `GOOGLE_CLOUD_CREDENTIALS` - Path to JSON file, or JSON contents
* `GOOGLE_APPLICATION_CREDENTIALS` - Path to JSON file

## Creating a Service Account
```ruby
require "google/cloud/org_policy/v2"

Google Cloud requires **Service Account Credentials** to
connect to the APIs. You will use the **JSON key file** to
connect to most services with google-cloud-org_policy-v2.
ENV["GOOGLE_APPLICATION_CREDENTIALS"] = "path/to/credentialfile.json"

If you are not running this client within
[Google Cloud Platform environments](#google-cloud-platform-environments), you
need a Google Developers service account.
client = ::Google::Cloud::OrgPolicy::V2::OrgPolicy::Client.new
```

1. Visit the [Google Cloud Console](https://console.cloud.google.com/project).
2. Create a new project or click on an existing project.
3. Activate the menu in the upper left and select **APIs & Services**. From
here, you will enable the APIs that your application requires.
### Local ADC file

*Note: You may need to enable billing in order to use these services.*
You can set up a local ADC file with your user credentials for authentication during
development. If credentials are not provided in code or in environment variables,
then the local ADC credentials are discovered.

4. Select **Credentials** from the side navigation.
Follow the steps in [Quickstart](#quickstart) to set up a local ADC file.

Find the "Create credentials" drop down near the top of the page, and select
"Service account" to be guided through downloading a new JSON key file.
### Google Cloud Platform environments

If you want to re-use an existing service account, you can easily generate a
new key file. Just select the account you wish to re-use, click the pencil
tool on the right side to edit the service account, select the **Keys** tab,
and then select **Add Key**.
When running on Google Cloud Platform (GCP), including Google Compute Engine
(GCE), Google Kubernetes Engine (GKE), Google App Engine (GAE), Google Cloud
Functions (GCF) and Cloud Run, credentials are retrieved from the attached
service account automatically. Code should be written as if already authenticated.

The key file you download will be used by this library to authenticate API
requests and should be stored in a secure location.
For more information, see
[Set up ADC for Google Cloud services](https://cloud.google.com/docs/authentication/provide-credentials-adc#attached-sa).
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Gem::Specification.new do |gem|

gem.required_ruby_version = ">= 2.6"

gem.add_dependency "gapic-common", ">= 0.20.0", "< 2.a"
gem.add_dependency "gapic-common", ">= 0.21.1", "< 2.a"
gem.add_dependency "google-cloud-errors", "~> 1.0"

gem.add_development_dependency "google-style", "~> 1.26.3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ module OrgPolicy
# particular resource and its child resources.
#
class Client
# @private
DEFAULT_ENDPOINT_TEMPLATE = "orgpolicy.$UNIVERSE_DOMAIN$"

include Paths

# @private
Expand Down Expand Up @@ -169,6 +172,15 @@ def configure
@config
end

##
# The effective universe domain
#
# @return [String]
#
def universe_domain
@org_policy_stub.universe_domain
end

##
# Create a new OrgPolicy client object.
#
Expand Down Expand Up @@ -202,8 +214,9 @@ def initialize
credentials = @config.credentials
# Use self-signed JWT if the endpoint is unchanged from default,
# but only if the default endpoint does not have a region prefix.
enable_self_signed_jwt = @config.endpoint == Configuration::DEFAULT_ENDPOINT &&
!@config.endpoint.split(".").first.include?("-")
enable_self_signed_jwt = @config.endpoint.nil? ||
(@config.endpoint == Configuration::DEFAULT_ENDPOINT &&
!@config.endpoint.split(".").first.include?("-"))
credentials ||= Credentials.default scope: @config.scope,
enable_self_signed_jwt: enable_self_signed_jwt
if credentials.is_a?(::String) || credentials.is_a?(::Hash)
Expand All @@ -214,8 +227,10 @@ def initialize

@org_policy_stub = ::Gapic::ServiceStub.new(
::Google::Cloud::OrgPolicy::V2::OrgPolicy::Stub,
credentials: credentials,
endpoint: @config.endpoint,
credentials: credentials,
endpoint: @config.endpoint,
endpoint_template: DEFAULT_ENDPOINT_TEMPLATE,
universe_domain: @config.universe_domain,
channel_args: @config.channel_args,
interceptors: @config.interceptors,
channel_pool_config: @config.channel_pool
Expand Down Expand Up @@ -1395,9 +1410,9 @@ def delete_custom_constraint request, options = nil
# end
#
# @!attribute [rw] endpoint
# The hostname or hostname:port of the service endpoint.
# Defaults to `"orgpolicy.googleapis.com"`.
# @return [::String]
# A custom service endpoint, as a hostname or hostname:port. The default is
# nil, indicating to use the default endpoint in the current universe domain.
# @return [::String,nil]
# @!attribute [rw] credentials
# Credentials to send with calls. You may provide any of the following types:
# * (`String`) The path to a service account key file in JSON format
Expand Down Expand Up @@ -1443,13 +1458,20 @@ def delete_custom_constraint request, options = nil
# @!attribute [rw] quota_project
# A separate project against which to charge quota.
# @return [::String]
# @!attribute [rw] universe_domain
# The universe domain within which to make requests. This determines the
# default endpoint URL. The default value of nil uses the environment
# universe (usually the default "googleapis.com" universe).
# @return [::String,nil]
#
class Configuration
extend ::Gapic::Config

# @private
# The endpoint specific to the default "googleapis.com" universe. Deprecated.
DEFAULT_ENDPOINT = "orgpolicy.googleapis.com"

config_attr :endpoint, DEFAULT_ENDPOINT, ::String
config_attr :endpoint, nil, ::String, nil
config_attr :credentials, nil do |value|
allowed = [::String, ::Hash, ::Proc, ::Symbol, ::Google::Auth::Credentials, ::Signet::OAuth2::Client, nil]
allowed += [::GRPC::Core::Channel, ::GRPC::Core::ChannelCredentials] if defined? ::GRPC
Expand All @@ -1464,6 +1486,7 @@ class Configuration
config_attr :metadata, nil, ::Hash, nil
config_attr :retry_policy, nil, ::Hash, ::Proc, nil
config_attr :quota_project, nil, ::String, nil
config_attr :universe_domain, nil, ::String, nil

# @private
def initialize parent_config = nil
Expand Down
Loading