Skip to content

Commit faf39c7

Browse files
gIthurielbroady
authored andcommitted
google/downscope: additional examples
Updating examples to match the expected token broker & token consumer paradigm. Change-Id: I9f6474e6d433e544dc92d8b1595e9538a5266043 GitHub-Last-Rev: 2149795 GitHub-Pull-Request: #513 Reviewed-on: https://go-review.googlesource.com/c/oauth2/+/339190 Reviewed-by: Leo Siracusa <[email protected]> Reviewed-by: Cody Oss <[email protected]> Trust: Cody Oss <[email protected]> Trust: Chris Broadfoot <[email protected]> Run-TryBot: Cody Oss <[email protected]> TryBot-Result: Go Bot <[email protected]>
1 parent 6f1e639 commit faf39c7

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

google/downscope/downscoping.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ can use. Please note that only Google Cloud Storage supports this feature.
99
For complete documentation, see https://cloud.google.com/iam/docs/downscoping-short-lived-credentials
1010
1111
To downscope permissions of a source credential, you need to define
12-
a Credential Access Boundary. Said Boundary specifies which resources
12+
a Credential Access Boundary. Said Boundary specifies which resources
1313
the newly created credential can access, an upper bound on the permissions
14-
it has over those resources, and optionally attribute-based conditional
15-
access to the aforementioned resources. For more information on IAM
14+
it has over those resources, and optionally attribute-based conditional
15+
access to the aforementioned resources. For more information on IAM
1616
Conditions, see https://cloud.google.com/iam/docs/conditions-overview.
1717
18-
This functionality would typically be used to provide a third party with
18+
This functionality can be used to provide a third party with
1919
limited access to and permissions on resources held by the owner of the root
2020
credential or internally in conjunction with the principle of least privilege
2121
to ensure that internal services only hold the minimum necessary privileges
@@ -24,13 +24,13 @@ for their function.
2424
For example, a token broker can be set up on a server in a private network.
2525
Various workloads (token consumers) in the same network will send authenticated
2626
requests to that broker for downscoped tokens to access or modify specific google
27-
cloud storage buckets. See the NewTokenSource example for an example of how a
27+
cloud storage buckets. See the NewTokenSource example for an example of how a
2828
token broker would use this package.
2929
3030
The broker will use the functionality in this package to generate a downscoped
3131
token with the requested configuration, and then pass it back to the token
32-
consumer. These downscoped access tokens can then be used to access Google
33-
Storage resources. For instance, you can create a NewClient from the
32+
consumer. These downscoped access tokens can then be used to access Google
33+
Storage resources. For instance, you can create a NewClient from the
3434
"cloud.google.com/go/storage" package and pass in option.WithTokenSource(yourTokenSource))
3535
*/
3636
package downscope
@@ -81,7 +81,7 @@ type AccessBoundaryRule struct {
8181
// An Condition restricts the availability of permissions
8282
// to specific Cloud Storage objects. Optional.
8383
//
84-
// A Condition can be used to make permissions available for specific objects,
84+
// A Condition can be used to make permissions available for specific objects,
8585
// rather than all objects in a Cloud Storage bucket.
8686
Condition *AvailabilityCondition `json:"availabilityCondition,omitempty"`
8787
}
@@ -183,9 +183,9 @@ func (dts downscopingTokenSource) Token() (*oauth2.Token, error) {
183183
if resp.StatusCode != http.StatusOK {
184184
b, err := ioutil.ReadAll(resp.Body)
185185
if err != nil {
186-
return nil, fmt.Errorf("downscope: unable to exchange token; %v. Failed to read response body: %v", resp.StatusCode, err)
186+
return nil, fmt.Errorf("downscope: unable to exchange token; %v. Failed to read response body: %v", resp.StatusCode, err)
187187
}
188-
return nil, fmt.Errorf("downscope: unable to exchange token; %v. Server responsed: %v", resp.StatusCode, string(b))
188+
return nil, fmt.Errorf("downscope: unable to exchange token; %v. Server responsed: %v", resp.StatusCode, string(b))
189189
}
190190

191191
var tresp downscopedTokenResponse

google/downscope/example_test.go renamed to google/downscope/tokenbroker_test.go

+19-6
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@ import (
88
"context"
99
"fmt"
1010

11+
"golang.org/x/oauth2/google"
12+
1113
"golang.org/x/oauth2"
1214
"golang.org/x/oauth2/google/downscope"
1315
)
1416

1517
func ExampleNewTokenSource() {
18+
// This shows how to generate a downscoped token. This code would be run on the
19+
// token broker, which holds the root token used to generate the downscoped token.
1620
ctx := context.Background()
17-
// Initializes an accessBoundary with one Rule.
21+
// Initializes an accessBoundary with one Rule which restricts the downscoped
22+
// token to only be able to access the bucket "foo" and only grants it the
23+
// permission "storage.objectViewer".
1824
accessBoundary := []downscope.AccessBoundaryRule{
1925
{
2026
AvailableResource: "//storage.googleapis.com/projects/_/buckets/foo",
@@ -26,19 +32,26 @@ func ExampleNewTokenSource() {
2632
// This Source can be initialized in multiple ways; the following example uses
2733
// Application Default Credentials.
2834

29-
// rootSource, err := google.DefaultTokenSource(ctx, "https://www.googleapis.com/auth/cloud-platform")
35+
rootSource, err := google.DefaultTokenSource(ctx, "https://www.googleapis.com/auth/cloud-platform")
3036

3137
dts, err := downscope.NewTokenSource(ctx, downscope.DownscopingConfig{RootSource: rootSource, Rules: accessBoundary})
3238
if err != nil {
3339
fmt.Printf("failed to generate downscoped token source: %v", err)
3440
return
3541
}
3642

37-
// Enables automatic token refreshing
38-
_ = oauth2.ReuseTokenSource(nil, dts)
43+
tok, err := dts.Token()
44+
if err != nil {
45+
fmt.Printf("failed to generate token: %v", err)
46+
return
47+
}
48+
_ = tok
49+
// You can now pass tok to a token consumer however you wish, such as exposing
50+
// a REST API and sending it over HTTP.
3951

40-
// You can now use the token held in myTokenSource to make
52+
// You can instead use the token held in dts to make
4153
// Google Cloud Storage calls, as follows:
4254

43-
// storageClient, err := storage.NewClient(ctx, option.WithTokenSource(myTokenSource))
55+
// storageClient, err := storage.NewClient(ctx, option.WithTokenSource(dts))
56+
4457
}

0 commit comments

Comments
 (0)