...

Source file src/cloud.google.com/go/auth/credentials/impersonate/example_test.go

Documentation: cloud.google.com/go/auth/credentials/impersonate

     1  // Copyright 2023 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package impersonate_test
    16  
    17  import (
    18  	"log"
    19  
    20  	"cloud.google.com/go/auth/credentials/impersonate"
    21  	"cloud.google.com/go/auth/httptransport"
    22  )
    23  
    24  func ExampleNewCredentials_serviceAccount() {
    25  	// Base credentials sourced from ADC or provided client options
    26  	creds, err := impersonate.NewCredentials(&impersonate.CredentialsOptions{
    27  		TargetPrincipal: "foo@project-id.iam.gserviceaccount.com",
    28  		Scopes:          []string{"https://www.googleapis.com/auth/cloud-platform"},
    29  		// Optionally supply delegates
    30  		Delegates: []string{"bar@project-id.iam.gserviceaccount.com"},
    31  	})
    32  	if err != nil {
    33  		log.Fatal(err)
    34  	}
    35  
    36  	// TODO(codyoss): link to option once it exists.
    37  
    38  	// Use this Credentials with a client library
    39  	_ = creds
    40  }
    41  
    42  func ExampleNewCredentials_adminUser() {
    43  	// Base credentials sourced from ADC or provided client options
    44  	creds, err := impersonate.NewCredentials(&impersonate.CredentialsOptions{
    45  		TargetPrincipal: "foo@project-id.iam.gserviceaccount.com",
    46  		Scopes:          []string{"https://www.googleapis.com/auth/cloud-platform"},
    47  		// Optionally supply delegates
    48  		Delegates: []string{"bar@project-id.iam.gserviceaccount.com"},
    49  		// Specify user to impersonate
    50  		Subject: "admin@example.com",
    51  	})
    52  	if err != nil {
    53  		log.Fatal(err)
    54  	}
    55  
    56  	// Use this Credentials with a client library like
    57  	// "google.golang.org/api/admin/directory/v1"
    58  	_ = creds
    59  }
    60  
    61  func ExampleNewIDTokenCredentials() {
    62  	// Base credentials sourced from ADC or provided client options.
    63  	creds, err := impersonate.NewIDTokenCredentials(&impersonate.IDTokenOptions{
    64  		Audience:        "http://example.com/",
    65  		TargetPrincipal: "foo@project-id.iam.gserviceaccount.com",
    66  		IncludeEmail:    true,
    67  		// Optionally supply delegates.
    68  		Delegates: []string{"bar@project-id.iam.gserviceaccount.com"},
    69  	})
    70  	if err != nil {
    71  		log.Fatal(err)
    72  	}
    73  
    74  	// Create an authenticated client
    75  	client, err := httptransport.NewClient(&httptransport.Options{
    76  		Credentials: creds,
    77  	})
    78  	if err != nil {
    79  		log.Fatal(err)
    80  	}
    81  
    82  	// Use your client that is authenticated with impersonated credentials to
    83  	// make requests.
    84  	client.Get("http://example.com/")
    85  }
    86  

View as plain text