awsrole

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 18 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package awsrole assumes AWS roles for writ serve's runs and hands the credentials to providers. Each run gets temporary credentials from STS, with a role session name and a source identity that name who asked, so CloudTrail shows them. Providers fetch the credentials from a loopback endpoint that speaks the AWS SDKs' container credentials protocol, with a token for each run, so the endpoint can assume the role again before the credentials expire and refuse the token once the run ends.

Index

Constants

View Source
const (
	MinDuration = 15 * time.Minute
	MaxDuration = 12 * time.Hour
)

Bounds AWS sets on a session.

View Source
const CredentialsPath = "/v1/credentials"

CredentialsPath is the endpoint's path.

View Source
const (
	// DefaultRefreshBefore is how long before credentials expire the
	// broker assumes the role again. The AWS SDKs fetch again within a few
	// minutes of expiry.
	DefaultRefreshBefore = 15 * time.Minute
)

Defaults of BrokerOptions.

Variables

This section is empty.

Functions

func AccountOf

func AccountOf(role string) string

AccountOf returns the account id of a role's ARN.

func CheckRoleARN

func CheckRoleARN(s string) error

CheckRoleARN reports whether s is an IAM role's ARN.

func SessionName

func SessionName(name, run string) string

SessionName is writ-NAME-RUN, with characters STS refuses replaced by "-". STS allows 64 characters, so a long NAME is cut to fit and RUN is kept whole.

func SourceIdentity

func SourceIdentity(s string) string

SourceIdentity is s with characters STS refuses replaced by "-", cut to 64 characters.

Types

type AssumedSession

type AssumedSession struct {
	Role, Name, SourceIdentity string
	From, Until                time.Time
}

AssumedSession is a role session the control plane assumed. From is when its first credentials were issued, and Until when its last ones expire.

type Assumer

type Assumer interface {
	Assume(ctx context.Context, s Session) (Credentials, error)
}

Assumer assumes roles. *STS is one.

type Broker

type Broker struct {
	// contains filtered or unexported fields
}

Broker serves each run's credentials on a loopback HTTP endpoint, as the AWS SDKs' container credentials provider reads them: a GET with the run's token in the Authorization header.

func NewBroker

func NewBroker(o BrokerOptions) (*Broker, error)

NewBroker listens on a free loopback port and serves until Close.

func (*Broker) Close

func (b *Broker) Close() error

Close stops the endpoint.

func (*Broker) Handler added in v0.1.6

func (b *Broker) Handler() http.Handler

Handler serves the endpoint as its listener does, with the same tokens, for a run that reaches it on a listener of its own, as one in its own network namespace does.

func (*Broker) Start

func (b *Broker) Start(ctx context.Context, s Session) (*Run, error)

Start assumes s's role at once, so a run whose role STS refuses fails before any provider starts, and registers a fresh token for the run.

func (*Broker) URL

func (b *Broker) URL() string

URL is the endpoint providers fetch credentials from.

type BrokerOptions

type BrokerOptions struct {
	Assumer       Assumer
	RefreshBefore time.Duration
	Now           func() time.Time
	Log           *slog.Logger
	// Assumed is told of each session the broker assumes, before its
	// credentials are served. When it fails at a run's start the run does
	// not start; when it fails at a refresh the credentials are served
	// anyway.
	Assumed func(ctx context.Context, s AssumedSession) error
}

BrokerOptions configures NewBroker.

type Credentials

type Credentials struct {
	AccessKeyID     string
	SecretAccessKey string
	SessionToken    string
	Expires         time.Time
}

Credentials are temporary credentials from STS.

type DeniedError

type DeniedError struct {
	Role string
	Err  error
}

DeniedError is STS refusing to assume a role.

func (*DeniedError) Error

func (e *DeniedError) Error() string

func (*DeniedError) Unwrap

func (e *DeniedError) Unwrap() error

type Keeper

type Keeper struct {
	// contains filtered or unexported fields
}

Keeper holds the credentials of one session, such as the one the change watcher reads its queue with or one run's. It assumes the role again when the credentials have less than RefreshBefore left. When a refresh fails it keeps serving the old credentials while they have a minute left.

func (*Keeper) Get

func (k *Keeper) Get(ctx context.Context) (Credentials, error)

Get returns credentials with more than RefreshBefore left.

type Run

type Run struct {
	// contains filtered or unexported fields
}

Run is one run's credentials and its token.

func (*Run) End

func (r *Run) End()

End stops answering the run's token.

func (*Run) Env

func (r *Run) Env() []string

Env is what a provider needs to fetch the run's credentials. The AWS SDKs read these two variables.

func (*Run) EnvAt added in v0.1.6

func (r *Run) EnvAt(url string) []string

EnvAt is Env with the endpoint at url, where Handler serves it.

func (*Run) Secrets added in v0.1.4

func (r *Run) Secrets() []string

Secrets are the secret access key and session token of every credentials the broker assumed for the run, so far: a provider can echo them in its output, and ending the run's token does not revoke them. The access key id is left out, as it names the session and is no secret.

type STS

type STS struct {
	// contains filtered or unexported fields
}

STS assumes roles through the AWS SDK as the control plane's own identity: the SDK's default chain of environment, shared files, and the instance's role.

func NewSTS

func NewSTS(ctx context.Context, o STSOptions) (*STS, error)

NewSTS loads the control plane's AWS identity. It contacts nothing: the SDK resolves credentials on the first call.

func (*STS) Assume

func (c *STS) Assume(ctx context.Context, s Session) (Credentials, error)

Assume calls sts:AssumeRole with s.

type STSOptions

type STSOptions struct {
	Region string
	// Endpoint replaces the regional STS endpoint, for an interface VPC
	// endpoint or a test. "" uses the SDK's.
	Endpoint string
}

STSOptions configures NewSTS.

type Session

type Session struct {
	Role string // the role's ARN
	// Name is the role session name, which CloudTrail shows with each
	// call the session makes.
	Name string
	// SourceIdentity names the principal behind the session. AWS keeps it
	// on every session chained from this one.
	SourceIdentity string
	Duration       time.Duration
}

Session is one AssumeRole request.

Directories

Path Synopsis
Package ststest is a stand-in for AWS STS in tests.
Package ststest is a stand-in for AWS STS in tests.

Jump to

Keyboard shortcuts

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