Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
a8e26b9
oauth2/google: implement support for token downscoping to allow for r…
gIthuriel Jun 9, 2021
52684dc
First set of comment changes.
gIthuriel Jun 9, 2021
eb57311
Add some validity testing for AccessBoundaryRules and add documentation.
gIthuriel Jun 10, 2021
add9801
Add exmaple showing how NewTokenSource should be called.
gIthuriel Jun 10, 2021
e035bf9
go fmt
gIthuriel Jun 10, 2021
69736ff
downscope: make changes related to comments, including adding another…
gIthuriel Jun 11, 2021
be467ee
downscope: update comment formatting
gIthuriel Jun 11, 2021
c4c64d5
downscope: add some context to returned error
gIthuriel Jun 14, 2021
776a9ed
downscope: move example files to a separate file & package
gIthuriel Jun 14, 2021
b594a60
downscope: minor tweaks
gIthuriel Jun 16, 2021
cbbc506
downscope: fixing nits and renaming
gIthuriel Jun 17, 2021
1d9ea0c
downscope: refactor main functionality into a method on a tokenSource…
gIthuriel Jun 17, 2021
a362f28
downscope: fix grammar and punctuation.
gIthuriel Jun 17, 2021
304d28b
downscope: further updates and nits
gIthuriel Jun 22, 2021
1024258
downscope: refactor some code to remove an extraneous function and in…
gIthuriel Jun 22, 2021
1888dba
downscope: change return type of NewTokenSource
gIthuriel Jun 23, 2021
fec7137
downscope: fix some nits
gIthuriel Jun 24, 2021
941cf10
downscope: move validation checks
gIthuriel Jun 24, 2021
c976479
downscope: update documentation
gIthuriel Jul 29, 2021
d921d8f
Merge pull request #1 from Galadros/downscopeDocumentation
Galadros Jul 29, 2021
e4ec8cd
Removed some code that's not yet finished
gIthuriel Jul 29, 2021
3045b9f
Merge branch 'master' into master
Galadros Jul 29, 2021
0bd54f5
downscope: documentation tweaks
gIthuriel Jul 31, 2021
e4caaa9
Merge branch 'master' of github.com:Galadros/oauth2
gIthuriel Jul 31, 2021
e1c4f01
downscope: add new examples and update existing ones.
gIthuriel Aug 2, 2021
db8a139
downscope: update examples
gIthuriel Aug 4, 2021
63894e5
Update example_test.go
Galadros Aug 4, 2021
b74b094
more nits
gIthuriel Aug 5, 2021
387bb65
Merge branch 'golang:master' into master
Galadros Aug 5, 2021
6338e2b
Merge branch 'master' into downscope-example-additions
gIthuriel Aug 5, 2021
c56d618
Removed extraneous newline.
gIthuriel Aug 5, 2021
2149795
Removed tokenconsumer example file; will add the examples elsewhere.
gIthuriel Aug 9, 2021
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
20 changes: 10 additions & 10 deletions google/downscope/downscoping.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ can use. Please note that only Google Cloud Storage supports this feature.
For complete documentation, see https://cloud.google.com/iam/docs/downscoping-short-lived-credentials

To downscope permissions of a source credential, you need to define
a Credential Access Boundary. Said Boundary specifies which resources
a Credential Access Boundary. Said Boundary specifies which resources
the newly created credential can access, an upper bound on the permissions
it has over those resources, and optionally attribute-based conditional
access to the aforementioned resources. For more information on IAM
it has over those resources, and optionally attribute-based conditional
access to the aforementioned resources. For more information on IAM
Conditions, see https://cloud.google.com/iam/docs/conditions-overview.

This functionality would typically be used to provide a third party with
This functionality can be used to provide a third party with
limited access to and permissions on resources held by the owner of the root
credential or internally in conjunction with the principle of least privilege
to ensure that internal services only hold the minimum necessary privileges
Expand All @@ -24,13 +24,13 @@ for their function.
For example, a token broker can be set up on a server in a private network.
Various workloads (token consumers) in the same network will send authenticated
requests to that broker for downscoped tokens to access or modify specific google
cloud storage buckets. See the NewTokenSource example for an example of how a
cloud storage buckets. See the NewTokenSource example for an example of how a
token broker would use this package.

The broker will use the functionality in this package to generate a downscoped
token with the requested configuration, and then pass it back to the token
consumer. These downscoped access tokens can then be used to access Google
Storage resources. For instance, you can create a NewClient from the
consumer. These downscoped access tokens can then be used to access Google
Storage resources. For instance, you can create a NewClient from the
"cloud.google.com/go/storage" package and pass in option.WithTokenSource(yourTokenSource))
*/
package downscope
Expand Down Expand Up @@ -81,7 +81,7 @@ type AccessBoundaryRule struct {
// An Condition restricts the availability of permissions
// to specific Cloud Storage objects. Optional.
//
// A Condition can be used to make permissions available for specific objects,
// A Condition can be used to make permissions available for specific objects,
// rather than all objects in a Cloud Storage bucket.
Condition *AvailabilityCondition `json:"availabilityCondition,omitempty"`
}
Expand Down Expand Up @@ -183,9 +183,9 @@ func (dts downscopingTokenSource) Token() (*oauth2.Token, error) {
if resp.StatusCode != http.StatusOK {
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("downscope: unable to exchange token; %v. Failed to read response body: %v", resp.StatusCode, err)
return nil, fmt.Errorf("downscope: unable to exchange token; %v. Failed to read response body: %v", resp.StatusCode, err)
}
return nil, fmt.Errorf("downscope: unable to exchange token; %v. Server responsed: %v", resp.StatusCode, string(b))
return nil, fmt.Errorf("downscope: unable to exchange token; %v. Server responsed: %v", resp.StatusCode, string(b))
}

var tresp downscopedTokenResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ import (
"context"
"fmt"

"golang.org/x/oauth2/google"

"golang.org/x/oauth2"
"golang.org/x/oauth2/google/downscope"
)

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

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

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

// Enables automatic token refreshing
_ = oauth2.ReuseTokenSource(nil, dts)
tok, err := dts.Token()
if err != nil {
fmt.Printf("failed to generate token: %v", err)
return
}
_ = tok
// You can now pass tok to a token consumer however you wish, such as exposing
// a REST API and sending it over HTTP.

// You can now use the token held in myTokenSource to make
// You can instead use the token held in dts to make
// Google Cloud Storage calls, as follows:

// storageClient, err := storage.NewClient(ctx, option.WithTokenSource(myTokenSource))
// storageClient, err := storage.NewClient(ctx, option.WithTokenSource(dts))

}