2021-06-11 14:40:45 -04:00
|
|
|
// 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.
|
|
|
|
|
2021-06-10 17:11:50 -04:00
|
|
|
/*
|
2021-06-11 14:40:45 -04:00
|
|
|
Package downscope implements the ability to downscope, or restrict, the
|
2021-06-10 17:11:50 -04:00
|
|
|
Identity and AccessManagement permissions that a short-lived Token
|
2021-06-11 14:40:45 -04:00
|
|
|
can use. Please note that only Google Cloud Storage supports this feature.
|
2021-06-10 17:55:56 -04:00
|
|
|
*/
|
2021-06-10 17:11:50 -04:00
|
|
|
package downscope
|
2021-06-09 13:46:53 -04:00
|
|
|
|
|
|
|
import (
|
2021-06-09 17:25:06 -04:00
|
|
|
"context"
|
2021-06-09 13:46:53 -04:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
2021-06-11 14:40:45 -04:00
|
|
|
|
|
|
|
"golang.org/x/oauth2"
|
2021-06-09 13:46:53 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-06-09 17:25:06 -04:00
|
|
|
identityBindingEndpoint = "https://sts.googleapis.com/v1beta/token"
|
2021-06-09 13:46:53 -04:00
|
|
|
)
|
|
|
|
|
2021-06-11 14:40:45 -04:00
|
|
|
type accessBoundary struct {
|
2021-06-09 13:46:53 -04:00
|
|
|
AccessBoundaryRules []AccessBoundaryRule `json:"accessBoundaryRules"`
|
|
|
|
}
|
|
|
|
|
2021-06-10 17:11:50 -04:00
|
|
|
// An AvailabilityCondition restricts access to a given Resource.
|
2021-06-09 13:46:53 -04:00
|
|
|
type AvailabilityCondition struct {
|
2021-06-10 17:11:50 -04:00
|
|
|
// A condition expression that specifies the Cloud Storage objects where
|
|
|
|
// permissions are available. For further documentation, see
|
|
|
|
// https://cloud.google.com/iam/docs/conditions-overview
|
2021-06-10 17:55:56 -04:00
|
|
|
Expression string `json:"expression"`
|
2021-06-11 14:40:45 -04:00
|
|
|
// Title is short string that identifies the purpose of the condition. Optional.
|
2021-06-10 17:55:56 -04:00
|
|
|
Title string `json:"title,omitempty"`
|
2021-06-11 14:40:45 -04:00
|
|
|
// Description details about the purpose of the condition. Optional.
|
2021-06-09 13:46:53 -04:00
|
|
|
Description string `json:"description,omitempty"`
|
|
|
|
}
|
|
|
|
|
2021-06-10 17:11:50 -04:00
|
|
|
// Sets the permissions (and optionally conditions) that the new
|
|
|
|
// token has on given resource.
|
2021-06-09 13:46:53 -04:00
|
|
|
type AccessBoundaryRule struct {
|
2021-06-10 17:11:50 -04:00
|
|
|
// AvailableResource is the full resource name of the Cloud Storage bucket that the rule applies to.
|
|
|
|
// Use the format //storage.googleapis.com/projects/_/buckets/bucket-name.
|
2021-06-10 17:55:56 -04:00
|
|
|
AvailableResource string `json:"availableResource"`
|
2021-06-10 17:11:50 -04:00
|
|
|
// AvailablePermissions is a list that defines the upper bound on the available permissions
|
2021-06-11 14:40:45 -04:00
|
|
|
// for the resource. Each value is the identifier for an IAM predefined role or custom role,
|
2021-06-10 17:11:50 -04:00
|
|
|
// with the prefix inRole:. For example: inRole:roles/storage.objectViewer.
|
|
|
|
// Only the permissions in these roles will be available.
|
2021-06-10 17:55:56 -04:00
|
|
|
AvailablePermissions []string `json:"availablePermissions"`
|
2021-06-11 14:40:45 -04:00
|
|
|
// An Condition restricts the availability of permissions
|
|
|
|
// to specific Cloud Storage objects. Optional.
|
2021-06-10 17:11:50 -04:00
|
|
|
//
|
|
|
|
// Use this field if you want to make permissions available for specific objects,
|
|
|
|
// rather than all objects in a Cloud Storage bucket.
|
2021-06-10 17:55:56 -04:00
|
|
|
Condition *AvailabilityCondition `json:"availabilityCondition,omitempty"`
|
2021-06-09 13:46:53 -04:00
|
|
|
}
|
|
|
|
|
2021-06-09 17:25:06 -04:00
|
|
|
type downscopedTokenResponse struct {
|
2021-06-09 13:46:53 -04:00
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
IssuedTokenType string `json:"issued_token_type"`
|
|
|
|
TokenType string `json:"token_type"`
|
|
|
|
ExpiresIn int `json:"expires_in"`
|
|
|
|
}
|
|
|
|
|
2021-06-11 14:40:45 -04:00
|
|
|
// DownscopingConfigSpecifies the information necessary to request a downscoped token
|
2021-06-09 13:46:53 -04:00
|
|
|
type DownscopingConfig struct {
|
2021-06-10 17:11:50 -04:00
|
|
|
// RootSource is the TokenSource used to create the downscoped token.
|
|
|
|
// The downscoped token therefore has some subset of the accesses of
|
|
|
|
// the original RootSource.
|
2021-06-10 17:55:56 -04:00
|
|
|
RootSource oauth2.TokenSource
|
2021-06-11 14:40:45 -04:00
|
|
|
// Rules defines the accesses held by the new
|
|
|
|
// downscoped Token. One or more AccessBoundaryRules are required to
|
|
|
|
// define permissions for the new downscoped token. Each one defines an
|
|
|
|
// access (or set of accesses) that the new token has to a given resource.
|
|
|
|
Rules []AccessBoundaryRule
|
2021-06-09 13:46:53 -04:00
|
|
|
}
|
|
|
|
|
2021-06-10 17:11:50 -04:00
|
|
|
// downscopedTokenWithEndpoint is a helper function used for unit testing
|
|
|
|
// purposes, as it allows us to pass in a locally mocked endpoint.
|
2021-06-09 17:25:06 -04:00
|
|
|
func downscopedTokenWithEndpoint(ctx context.Context, config DownscopingConfig, endpoint string) (oauth2.TokenSource, error) {
|
2021-06-09 13:46:53 -04:00
|
|
|
if config.RootSource == nil {
|
2021-06-10 17:11:50 -04:00
|
|
|
return nil, fmt.Errorf("downscope: rootTokenSource cannot be nil")
|
2021-06-09 13:46:53 -04:00
|
|
|
}
|
2021-06-11 14:40:45 -04:00
|
|
|
if len(config.Rules) == 0 {
|
2021-06-10 17:11:50 -04:00
|
|
|
return nil, fmt.Errorf("downscope: length of AccessBoundaryRules must be at least 1")
|
|
|
|
}
|
2021-06-11 14:40:45 -04:00
|
|
|
for _, val := range config.Rules {
|
2021-06-10 17:11:50 -04:00
|
|
|
if val.AvailableResource == "" {
|
|
|
|
return nil, fmt.Errorf("downscope: all rules must have a nonempty AvailableResource: %+v", val)
|
|
|
|
}
|
|
|
|
if len(val.AvailablePermissions) == 0 {
|
|
|
|
return nil, fmt.Errorf("downscope: all rules must provide at least one permission: %+v", val)
|
|
|
|
}
|
2021-06-09 13:46:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
downscopedOptions := struct {
|
2021-06-11 14:40:45 -04:00
|
|
|
Boundary accessBoundary `json:"accessBoundary"`
|
2021-06-09 13:46:53 -04:00
|
|
|
}{
|
2021-06-11 14:40:45 -04:00
|
|
|
Boundary: accessBoundary{
|
|
|
|
AccessBoundaryRules: config.Rules,
|
|
|
|
},
|
2021-06-09 13:46:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
tok, err := config.RootSource.Token()
|
|
|
|
if err != nil {
|
2021-06-10 17:11:50 -04:00
|
|
|
return nil, fmt.Errorf("downscope: unable to obtain root token: %v", err)
|
2021-06-09 13:46:53 -04:00
|
|
|
}
|
|
|
|
|
2021-06-09 17:25:06 -04:00
|
|
|
b, err := json.Marshal(downscopedOptions)
|
2021-06-09 13:46:53 -04:00
|
|
|
if err != nil {
|
2021-06-11 14:40:45 -04:00
|
|
|
return nil, fmt.Errorf("downscope: unable to marshall AccessBoundary payload %v", err)
|
2021-06-09 13:46:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
form := url.Values{}
|
|
|
|
form.Add("grant_type", "urn:ietf:params:oauth:grant-type:token-exchange")
|
|
|
|
form.Add("subject_token_type", "urn:ietf:params:oauth:token-type:access_token")
|
|
|
|
form.Add("requested_token_type", "urn:ietf:params:oauth:token-type:access_token")
|
|
|
|
form.Add("subject_token", tok.AccessToken)
|
|
|
|
form.Add("options", url.QueryEscape(string(b)))
|
|
|
|
|
2021-06-09 17:25:06 -04:00
|
|
|
myClient := oauth2.NewClient(ctx, nil)
|
|
|
|
resp, err := myClient.PostForm(endpoint, form)
|
2021-06-09 13:46:53 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to generate POST Request %v", err)
|
|
|
|
}
|
2021-06-09 17:25:06 -04:00
|
|
|
defer resp.Body.Close()
|
2021-06-09 13:46:53 -04:00
|
|
|
|
2021-06-09 17:25:06 -04:00
|
|
|
var tresp downscopedTokenResponse
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&tresp)
|
|
|
|
if err != nil {
|
2021-06-11 14:40:45 -04:00
|
|
|
return nil, fmt.Errorf("downscope: unable to unmarshal response body: %v", err)
|
2021-06-09 17:25:06 -04:00
|
|
|
}
|
2021-06-09 13:46:53 -04:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
2021-06-11 14:40:45 -04:00
|
|
|
return nil, fmt.Errorf("downscope: unable to exchange token; %v", resp.StatusCode)
|
2021-06-09 13:46:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// an exchanged token that is derived from a service account (2LO) has an expired_in value
|
|
|
|
// a token derived from a users token (3LO) does not.
|
|
|
|
// The following code uses the time remaining on rootToken for a user as the value for the
|
|
|
|
// derived token's lifetime
|
|
|
|
var expiry_time time.Time
|
|
|
|
if tresp.ExpiresIn > 0 {
|
|
|
|
expiry_time = time.Now().Add(time.Duration(time.Duration(tresp.ExpiresIn) * time.Second))
|
|
|
|
} else {
|
|
|
|
expiry_time = tok.Expiry
|
|
|
|
}
|
|
|
|
|
|
|
|
newToken := &oauth2.Token{
|
|
|
|
AccessToken: tresp.AccessToken,
|
|
|
|
TokenType: tresp.TokenType,
|
|
|
|
Expiry: expiry_time,
|
|
|
|
}
|
|
|
|
return oauth2.StaticTokenSource(newToken), nil
|
|
|
|
}
|
|
|
|
|
2021-06-10 17:11:50 -04:00
|
|
|
// NewTokenSource takes a root TokenSource and returns a downscoped TokenSource
|
2021-06-11 14:40:45 -04:00
|
|
|
// with a subset of the permissions held by the root source. The
|
2021-06-10 17:11:50 -04:00
|
|
|
// CredentialAccessBoundary in the config defines the permissions held
|
2021-06-11 14:40:45 -04:00
|
|
|
// by the new TokenSource. Do note that the returned TokenSource is
|
|
|
|
// an oauth2.StaticTokenSource. If you wish to refresh this token automatically,
|
2021-06-10 17:55:41 -04:00
|
|
|
// then initialize a locally defined TokenSource struct with the Token held
|
|
|
|
// by the StaticTokenSource and wrap that TokenSource in an oauth2.ReuseTokenSource.
|
2021-06-10 17:11:50 -04:00
|
|
|
func NewTokenSource(ctx context.Context, config DownscopingConfig) (oauth2.TokenSource, error) {
|
2021-06-09 17:25:06 -04:00
|
|
|
return downscopedTokenWithEndpoint(ctx, config, identityBindingEndpoint)
|
2021-06-09 13:46:53 -04:00
|
|
|
}
|