downscope

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 4, 2023 License: BSD-3-Clause Imports: 8 Imported by: 2

Documentation

Overview

Package downscope implements the ability to downscope, or restrict, the Identity and Access Management permissions that a short-lived Token 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 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 Conditions, see https://cloud.google.com/iam/docs/conditions-overview.

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 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 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 "cloud.google.com/go/storage" package and pass in option.WithTokenSource(yourTokenSource))

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewTokenSource

func NewTokenSource(ctx context.Context, conf DownscopingConfig) (oauth2.TokenSource, error)

NewTokenSource returns a configured downscopingTokenSource.

Example
package main

import (
	"context"
	"fmt"

	"golang.org/x/oauth2/google"

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

func main() {
	// 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 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",
			AvailablePermissions: []string{"inRole:roles/storage.objectViewer"},
		},
	}

	var rootSource oauth2.TokenSource
	// 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")

	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
	}

	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 instead use the token held in dts to make
	// Google Cloud Storage calls, as follows:

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

}
Output:

Types

type AccessBoundaryRule

type AccessBoundaryRule struct {
	// 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.
	AvailableResource string `json:"availableResource"`
	// AvailablePermissions is a list that defines the upper bound on the available permissions
	// for the resource. Each value is the identifier for an IAM predefined role or custom role,
	// with the prefix inRole:. For example: inRole:roles/storage.objectViewer.
	// Only the permissions in these roles will be available.
	AvailablePermissions []string `json:"availablePermissions"`
	// 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,
	// rather than all objects in a Cloud Storage bucket.
	Condition *AvailabilityCondition `json:"availabilityCondition,omitempty"`
}

An AccessBoundaryRule Sets the permissions (and optionally conditions) that the new token has on given resource.

type AvailabilityCondition

type AvailabilityCondition struct {
	// An Expression specifies the Cloud Storage objects where
	// permissions are available. For further documentation, see
	// https://cloud.google.com/iam/docs/conditions-overview
	Expression string `json:"expression"`
	// Title is short string that identifies the purpose of the condition. Optional.
	Title string `json:"title,omitempty"`
	// Description details about the purpose of the condition. Optional.
	Description string `json:"description,omitempty"`
}

An AvailabilityCondition restricts access to a given Resource.

type DownscopingConfig

type DownscopingConfig struct {
	// RootSource is the TokenSource used to create the downscoped token.
	// The downscoped token therefore has some subset of the accesses of
	// the original RootSource.
	RootSource oauth2.TokenSource
	// 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.
	// There can be a maximum of 10 AccessBoundaryRules.
	Rules []AccessBoundaryRule
}

DownscopingConfig specifies the information necessary to request a downscoped token.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL