gcemeta

package
v0.0.0-...-ff9afc2 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2024 License: Apache-2.0 Imports: 21 Imported by: 3

Documentation

Overview

Package gcemeta implements a subset of GCE metadata server protocol.

It can be used to "trick" Go and Python libraries that use Application Default Credentials into believing they run on GCE so that they request OAuth2 tokens via GCE metadata server (which is implemented by us here).

It implements a significant portion of the GCE metadata protocol, but populates only a small subset of the metadata values that are commonly accessed by tools.

Following features of the protocol are not implemented:

  • "wait-for-change"
  • "https://..." endpoints

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Server

type Server struct {
	// Generator is used to obtain OAuth2 and ID tokens.
	Generator TokenGenerator
	// Email is the email associated with generated tokens.
	Email string
	// Scopes is a list of scopes to put into generated OAuth2 tokens.
	Scopes []string
	// MinTokenLifetime is a minimum lifetime left in returned tokens.
	MinTokenLifetime time.Duration
	// Port is a local TCP port to bind to or 0 to allow the OS to pick one.
	Port int

	// InheritFromGCE enables inheriting some values from the real GCE MD server.
	//
	// Setting this to true will enable probing of the real GCE metadata server
	// via metadata.OnGCE() when starting the emulated server. metadata.OnGCE()
	// caches the first value it observed. Thus setting this field to true in
	// tests that want to emulate GCE metadata server will lead to problems: if
	// the test is not running on **real** GCE, the fact of just starting the
	// emulation server will result in the process thinking that it runs NOT on
	// GCE (even if the emulation server is later put into the process
	// environment).
	//
	// Using this field in tests is likely a mistake.
	InheritFromGCE bool
	// contains filtered or unexported fields
}

Server runs a local fake GCE metadata server.

func (*Server) Start

func (s *Server) Start(ctx context.Context) (string, error)

Start launches background goroutine with the serving loop.

The provided context is used as base context for request handlers and for logging. The server must be eventually stopped with Stop().

Returns "host:port" address of the launched metadata server.

func (*Server) Stop

func (s *Server) Stop(ctx context.Context) error

Stop closes the listening socket, notifies pending requests to abort and stops the internal serving goroutine.

Safe to call multiple times. Once stopped, the server cannot be started again (make a new instance of Server instead).

Uses the given context for the deadline when waiting for the serving loop to stop.

type TokenGenerator

type TokenGenerator interface {
	// GenerateOAuthToken returns an access token for a combination of scopes.
	GenerateOAuthToken(ctx context.Context, scopes []string, lifetime time.Duration) (*oauth2.Token, error)
	// GenerateIDToken returns an ID token with the given audience in `aud` claim.
	GenerateIDToken(ctx context.Context, audience string, lifetime time.Duration) (*oauth2.Token, error)
}

TokenGenerator produces access and ID tokens.

The canonical implementation is &auth.TokenGenerator{}.

Jump to

Keyboard shortcuts

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