-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
example_test.go
97 lines (82 loc) · 2.87 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package impersonate_test
import (
"context"
"log"
admin "google.golang.org/api/admin/directory/v1"
"google.golang.org/api/impersonate"
"google.golang.org/api/option"
"google.golang.org/api/secretmanager/v1"
"google.golang.org/api/transport"
)
func ExampleCredentialsTokenSource_serviceAccount() {
ctx := context.Background()
// Base credentials sourced from ADC or provided client options.
ts, err := impersonate.CredentialsTokenSource(ctx, impersonate.CredentialsConfig{
TargetPrincipal: "[email protected]",
Scopes: []string{"https://www.googleapis.com/auth/cloud-platform"},
// Optionally supply delegates.
Delegates: []string{"[email protected]"},
})
if err != nil {
log.Fatal(err)
}
// Pass an impersonated credential to any function that takes client
// options.
client, err := secretmanager.NewService(ctx, option.WithTokenSource(ts))
if err != nil {
log.Fatal(err)
}
// Use your client that is authenticated with impersonated credentials to
// make requests.
client.Projects.Secrets.Get("...")
}
func ExampleCredentialsTokenSource_adminUser() {
ctx := context.Background()
// Base credentials sourced from ADC or provided client options.
ts, err := impersonate.CredentialsTokenSource(ctx, impersonate.CredentialsConfig{
TargetPrincipal: "[email protected]",
Scopes: []string{"https://www.googleapis.com/auth/cloud-platform"},
// Optionally supply delegates.
Delegates: []string{"[email protected]"},
// Specify user to impersonate
Subject: "[email protected]",
})
if err != nil {
log.Fatal(err)
}
// Pass an impersonated credential to any function that takes client
// options.
client, err := admin.NewService(ctx, option.WithTokenSource(ts))
if err != nil {
log.Fatal(err)
}
// Use your client that is authenticated with impersonated credentials to
// make requests.
client.Groups.Delete("...")
}
func ExampleIDTokenSource() {
ctx := context.Background()
// Base credentials sourced from ADC or provided client options.
ts, err := impersonate.IDTokenSource(ctx, impersonate.IDTokenConfig{
Audience: "http://example.com/",
TargetPrincipal: "[email protected]",
IncludeEmail: true,
// Optionally supply delegates.
Delegates: []string{"[email protected]"},
})
if err != nil {
log.Fatal(err)
}
// Pass an impersonated credential to any function that takes client
// options.
client, _, err := transport.NewHTTPClient(ctx, option.WithTokenSource(ts))
if err != nil {
log.Fatal(err)
}
// Use your client that is authenticated with impersonated credentials to
// make requests.
client.Get("http://example.com/")
}