Documentation ¶
Overview ¶
Package fakemetadata provides the fake GCE compute metadata server for testing.
Index ¶
- Constants
- Variables
- func DisableImpersonate()
- func DisableWorkloadIdentityFederation()
- func EnableImpersonate()
- func EnableWorkloadIdentityFederation()
- func IsRunning() bool
- func OnTest() bool
- func SetDelegateServiceAccount(delegates []string)
- func Shutdown(ctx context.Context) error
- func StartServer()
- func WriteJSON(w safehttp.ResponseWriter, data interface{}) safehttp.Result
- type Dispatcher
- type InstanceHandler
- func (InstanceHandler) Attributes(m map[string]bool) safehttp.Handler
- func (InstanceHandler) CPUPlatform() safehttp.Handler
- func (InstanceHandler) Description() safehttp.Handler
- func (InstanceHandler) Disks() safehttp.Handler
- func (InstanceHandler) GuestAttributes(m map[string]bool) safehttp.Handler
- func (InstanceHandler) Hostname() safehttp.Handler
- func (InstanceHandler) ID() safehttp.Handler
- func (InstanceHandler) Image() safehttp.Handler
- func (InstanceHandler) LegacyEndpointAccess() safehttp.Handler
- func (InstanceHandler) Licenses() safehttp.Handler
- func (InstanceHandler) MachineType() safehttp.Handler
- func (InstanceHandler) MaintenanceEvent() safehttp.Handler
- func (InstanceHandler) Name() safehttp.Handler
- func (InstanceHandler) NetworkInterfaces() safehttp.Handler
- func (InstanceHandler) Preempted() safehttp.Handler
- func (InstanceHandler) Region() safehttp.Handler
- func (h *InstanceHandler) RegisterHandlers(mux *safehttp.ServeMux)
- func (InstanceHandler) RemainingCPUTime() safehttp.Handler
- func (InstanceHandler) Scheduling() safehttp.Handler
- func (h *InstanceHandler) ServiceAccounts() safehttp.Handler
- func (InstanceHandler) Tags() safehttp.Handler
- func (InstanceHandler) VirtualClock() safehttp.Handler
- func (InstanceHandler) Zone() safehttp.Handler
- type JSONResponse
- type ProjectHandler
- type Server
- func (s *Server) Addr() string
- func (s *Server) Close() error
- func (s *Server) DisableImpersonate()
- func (s *Server) DisableWorkloadIdentityFederation()
- func (s *Server) EnableImpersonate()
- func (s *Server) EnableWorkloadIdentityFederation()
- func (s *Server) ListenAndServe() error
- func (s *Server) ListenAndServeTLS(certFile, keyFile string) error
- func (s *Server) Serve(l net.Listener) error
- func (s *Server) ServeTLS(l net.Listener, certFile, keyFile string) error
- func (s *Server) SetDelegateServiceAccount(delegates []string)
- func (s *Server) Shutdown(ctx context.Context) error
- type StatusError
- type TokenResponse
- type X86Microarchitecture
Constants ¶
const ( // RequestHeader is the required http header for access to the metadata server. // // This header indicates that the request was sent with the intention of retrieving metadata values, // rather than unintentionally from an insecure source, and lets the metadata server return the data you requested. // If you don't provide this header, the metadata server denies your request. RequestHeader = "Metadata-Flavor: Google" // LegacyRequestHeader is the legacy (but still supported) required http header for access to the metadata server. LegacyRequestHeader = "X-Google-Metadata-Request: True" )
List of request http header constants.
See also: https://cloud.google.com/compute/docs/metadata/overview
const ( // EnvGoogleApplicationCredentials environment variable name for overrides application default credentials JSON path. EnvGoogleApplicationCredentials = "GOOGLE_APPLICATION_CREDENTIALS" // EnvGoogleAccountEmail environment variable name for overrides service account email address. EnvGoogleAccountEmail = "GOOGLE_ACCOUNT_EMAIL" )
const ( MetadataFlavorHeader = "Metadata-Flavor" MetadataFlavorValue = "Google" )
const ( ServerHeader = "Server" ServerValue = "Metadata Server for VM" )
const ( XXSSProtectionHeader = "X-XSS-Protection" XXSSProtectionValue = "0" XFrameOptionsHeader = "X-Frame-Options" XFrameOptionsValue = "SAMEORIGIN" )
const ( // EnvGoogleCloudNumericProject one of environment variable name for overrides numeric project id. EnvGoogleCloudNumericProject = "GOOGLE_CLOUD_NUMERIC_PROJECT" // EnvGCPNumericProject one of environment variable name for overrides numeric project id. EnvGCPNumericProject = "GCP_NUMERIC_PROJECT" // EnvGoogleGCPNumericProject one of environment variable name for overrides numeric project id. EnvGoogleGCPNumericProject = "GOOGLE_GCP_NUMERIC_PROJECT" )
const ( // EnvGoogleCloudProject one of environment variable name for overrides project id. EnvGoogleCloudProject = "GOOGLE_CLOUD_PROJECT" // EnvGCPProject one of environment variable name for overrides project id. EnvGCPProject = "GCP_PROJECT" // EnvGoogleGCPProject one of environment variable name for overrides project id. EnvGoogleGCPProject = "GOOGLE_GCP_PROJECT" )
const EnvGoogleInstanceRegion = "GOOGLE_INSTANCE_REGION"
EnvGoogleInstanceRegion environment variable name for overrides instance region.
const EnvGoogleProjectDefaultZone = "GOOGLE_PROJECT_DEFAULT_ZONE"
EnvGoogleProjectDefaultZone environment variable name for overrides default zone.
const EnvInstanceHostname = "GOOGLE_INSTANCE_HOSTNAME"
EnvInstanceHostname environment variable name for overrides instance hostname.
const EnvInstanceID = "GOOGLE_INSTANCE_ID"
EnvInstanceID environment variable name for overrides instance id.
Variables ¶
var ( // metadataIP is the documented metadata server IP address. MetadataIP = "169.254.169.254" // MetadataHostEnv is the environment variable specifying the GCE metadata hostname. // If empty, the default value of metadataIP ("169.254.169.254") is used instead. // // The cloud.google.com/go/compute/metadata package maintainer said: // > This is variable name is not defined by any spec, as far as I know; it was made up for the Go package. // // So this environment variable is helpful to replace the server that the cloud.google.com/go/compute/metadata package accesses during testing. MetadataHostEnv = "GCE_METADATA_HOST" )
List of metadata server variables.
Those are exported and don't use constant types so can be replaced.
See details: https://cloud.google.com/compute/docs/metadata/overview#parts-of-a-request
var InstanceAttributeMap = map[string]bool{ "enable-oslogin": true, "vmdnssetting": true, "ssh-keys": true, }
InstanceAttributeMap map of instance attributes.
See: https://cloud.google.com/compute/docs/metadata/default-metadata-values#instance-attributes-metadata
var InstanceGuestAttributeMap = map[string]bool{ "guestInventory": true, "hostkeys": true, }
InstanceGuestAttributeMap map of instance guest attributes.
var ProjectAttributeMap = map[string]bool{ "disable-legacy-endpoints": true, "enable-guest-attributes": true, "enable-os-inventory": true, "enable-oslogin": true, "google-compute-default-region": true, "google-compute-default-zone": true, "ssh-keys": true, "sshKeys": true, "vmdnssetting": true, }
ProjectAttributeMap map of porject attributes.
The project attributes are stored under the following directory:
http://metadata.google.internal/computeMetadata/v1/project/attributes/
Functions ¶
func DisableImpersonate ¶
func DisableImpersonate()
DisableImpersonate disable impersonate service account.
func DisableWorkloadIdentityFederation ¶
func DisableWorkloadIdentityFederation()
DisableWorkloadIdentityFederation disable Workload Identity Federation ADC.
func EnableImpersonate ¶
func EnableImpersonate()
EnableImpersonate enable impersonate service account.
func EnableWorkloadIdentityFederation ¶
func EnableWorkloadIdentityFederation()
EnableWorkloadIdentityFederation enable Workload Identity Federation ADC.
func SetDelegateServiceAccount ¶
func SetDelegateServiceAccount(delegates []string)
SetDelegateServiceAccount sets sequence of service accounts in a delegation chain.
Types ¶
type Dispatcher ¶
type Dispatcher struct {
safehttp.DefaultDispatcher
}
Dispatcher is a custom safehttp.Dispatcher implementation. See:
https://pkg.go.dev/github.com/google/go-safeweb/safehttp#hdr-Dispatcher.
func (Dispatcher) Error ¶
func (d Dispatcher) Error(rw http.ResponseWriter, resp safehttp.ErrorResponse) error
Error implemens safehttp.Dispatcher.Error.
func (Dispatcher) Write ¶
func (d Dispatcher) Write(rw http.ResponseWriter, resp safehttp.Response) error
Write implemens safehttp.Dispatcher.Write.
type InstanceHandler ¶
type InstanceHandler struct {
// contains filtered or unexported fields
}
InstanceHandler holds instance metadata handlers.
See: https://cloud.google.com/compute/docs/metadata/default-metadata-values#vm_instance_metadata
func (InstanceHandler) Attributes ¶
func (InstanceHandler) Attributes(m map[string]bool) safehttp.Handler
Attributes a directory of custom metadata values passed to the VM during startup or shutdown. These custom values can either be Google Cloud attributes or user-created metadata values.
For a list of instance-level Google Cloud attributes that you can set, see Instance attributes.
For more information about setting custom metadata, see Setting custom metadata.
func (InstanceHandler) CPUPlatform ¶
func (InstanceHandler) CPUPlatform() safehttp.Handler
CPUPlatform CPU platform of the VM.
For information about CPU platforms, see CPU platforms.
func (InstanceHandler) Description ¶
func (InstanceHandler) Description() safehttp.Handler
Description is the free-text description of an instance that is assigned using the "--description" flag by using the Google Cloud CLI or the API.
func (InstanceHandler) Disks ¶
func (InstanceHandler) Disks() safehttp.Handler
Disks a directory of disks that are attached to the VM.
For each disk, the following information is available:
device-name index interface mode type
For more information about disks, see Storage options.
func (InstanceHandler) GuestAttributes ¶
func (InstanceHandler) GuestAttributes(m map[string]bool) safehttp.Handler
GuestAttributes sets guest attributes for the VM. These custom values can either be Google Cloud attributes or user-created metadata values.
For a list of instance-level Google Cloud attributes that you can set, see Instance guest attributes.
Note: Any user or process on your VM instance can read and write to the namespaces and keys in guest-attributes metadata.
For more information about guest attributes, see Setting and querying guest attributes.
func (InstanceHandler) Hostname ¶
func (InstanceHandler) Hostname() safehttp.Handler
Hostname is the hostname of the VM.
func (InstanceHandler) ID ¶
func (InstanceHandler) ID() safehttp.Handler
ID the ID of the VM. This is a unique, numerical ID that is generated by Compute Engine. This is useful for identifying VMs if you don't use VM names.
func (InstanceHandler) Image ¶
func (InstanceHandler) Image() safehttp.Handler
Image is the operating system image used by the VM. This value has the following format:
projects/IMAGE_PROJECT/global/images/IMAGE_NAME
func (InstanceHandler) LegacyEndpointAccess ¶
func (InstanceHandler) LegacyEndpointAccess() safehttp.Handler
LegacyEndpointAccess stores the list of legacy endpoints. Values are 0.1 and v1beta1.
func (InstanceHandler) Licenses ¶
func (InstanceHandler) Licenses() safehttp.Handler
Licenses a list of license code IDs that are used to attach the licenses to images, snapshots, and disks. directory
func (InstanceHandler) MachineType ¶
func (InstanceHandler) MachineType() safehttp.Handler
MachineType is the machine type for this VM. This value has the following format: projects/PROJECT_NUM/machineTypes/MACHINE_TYPE
func (InstanceHandler) MaintenanceEvent ¶
func (InstanceHandler) MaintenanceEvent() safehttp.Handler
MaintenanceEvent indicates whether a maintenance event is affecting this VM. For more information, see Live migrate.
func (InstanceHandler) Name ¶
func (InstanceHandler) Name() safehttp.Handler
Name is the name of the VM.
func (InstanceHandler) NetworkInterfaces ¶
func (InstanceHandler) NetworkInterfaces() safehttp.Handler
NetworkInterfaces a directory of network interfaces. For each network interface the following information is available:
access-configs/ external-ip type dns-servers forwarded-ips/ gateway ip ip-aliases/ mac mtu network subnetmask target-instance-ips
For more information about network interfaces, see Multiple network interfaces overview.
func (InstanceHandler) Preempted ¶
func (InstanceHandler) Preempted() safehttp.Handler
Preempted a boolean value that indicates whether a VM is about to be preempted.
func (InstanceHandler) Region ¶
func (InstanceHandler) Region() safehttp.Handler
Region returns a region of GCP services.
This value has the following format:
projects/PROJECT-NUMBER/regions/REGION
Note that when using this function, you also need to fake the GCP project number as this package emulates the behavior of the real metadata server.
Requires sets one of the below environment variables: - GOOGLE_CLOUD_NUMERIC_PROJECT - GCP_NUMERIC_PROJECT - GOOGLE_GCP_NUMERIC_PROJECT
func (*InstanceHandler) RegisterHandlers ¶
func (h *InstanceHandler) RegisterHandlers(mux *safehttp.ServeMux)
RegisterHandlers registers instance handlers to mux.
func (InstanceHandler) RemainingCPUTime ¶
func (InstanceHandler) RemainingCPUTime() safehttp.Handler
func (InstanceHandler) Scheduling ¶
func (InstanceHandler) Scheduling() safehttp.Handler
Scheduling sets the scheduling options for the VM.
Scheduling metadata values include the following:
on-host-maintenance
indicates whether the VM terminates or live migrates during host maintenance.
automatic-restart
If this value is TRUE, the VM automatically restarts after a maintenance event or crash.
preemptible
If this value is TRUE, the VM is preemptible. This value is set when you create a VM, and it can't be changed.
For more information about scheduling options, see Setting instance availability policies.
func (*InstanceHandler) ServiceAccounts ¶
func (h *InstanceHandler) ServiceAccounts() safehttp.Handler
ServiceAccounts a directory of service accounts associated with the VM. For each service account, the following information is available:
aliases
The service accounts alias.
The email address for the service account.
identity
A JSON Web Token that is unique to the VM. You must include the audience parameter in your request for this VM metadata value. For example, "?audience=http://www.example.com".
For information about how to request and verify instance identity tokens, see Verifying the identity of instances.
scopes
The access scopes assigned to the service account.
token
The OAuth2 access token that can be used to authenticate applications.
For information about access tokens, see Authenticating applications directly with access tokens.
For more information about service accounts, see Creating and enabling service accounts for instances.
func (InstanceHandler) Tags ¶
func (InstanceHandler) Tags() safehttp.Handler
Tags lists any network tags associated with the VM.
For more information about network tags, see Configuring network tags.
func (InstanceHandler) VirtualClock ¶
func (InstanceHandler) VirtualClock() safehttp.Handler
func (InstanceHandler) Zone ¶
func (InstanceHandler) Zone() safehttp.Handler
Zone is the zone where this VM is located.
This value has the following format: projects/PROJECT_NUM/zones/ZONE.
type JSONResponse ¶
type JSONResponse struct {
Data interface{}
}
JSONResponse should encapsulate a valid JSON object that will be serialised and written to the http.ResponseWriter using a JSON encoder.
type ProjectHandler ¶
type ProjectHandler struct{}
ProjectHandler holds project metadata handlers.
Project metadata entries are stored under the following directory:
http://metadata.google.internal/computeMetadata/v1/project/
See: https://cloud.google.com/compute/docs/metadata/default-metadata-values#project_metadata
func (ProjectHandler) Attributes ¶
func (ProjectHandler) Attributes(m map[string]bool) safehttp.Handler
Attributes a directory of custom metadata values passed to the VMs in your project during startup or shutdown. These custom values can either be Google Cloud attributes or user-created metadata values.
For a list of project-level Google Cloud attributes that you can set, see Project attributes.
For more information about setting custom metadata, see Setting VM metadata.
func (ProjectHandler) NumericProjectID ¶
func (ProjectHandler) NumericProjectID() safehttp.Handler
NumericProjectID is the numeric project ID (project number) of the instance, which is not the same as the project name that is visible in the Google Cloud console. This value is different from the project-id metadata entry value.
func (ProjectHandler) ProjectID ¶
func (ProjectHandler) ProjectID() safehttp.Handler
ProjectID is the project ID.
func (ProjectHandler) RegisterHandlers ¶
func (h ProjectHandler) RegisterHandlers(mux *safehttp.ServeMux)
RegisterHandlers registers project handlers to mux.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server represents a fake metadata server.
func NewServerWithPort ¶
NewServer returns the new fake metadata server.
func (*Server) Close ¶
Close is a wrapper for https://pkg.go.dev/pkg/net/http/#Server.Close
func (*Server) DisableImpersonate ¶
func (s *Server) DisableImpersonate()
DisableImpersonate disable impersonate service account.
func (*Server) DisableWorkloadIdentityFederation ¶
func (s *Server) DisableWorkloadIdentityFederation()
DisableWorkloadIdentityFederation disable Workload Identity Federation ADC.
func (*Server) EnableImpersonate ¶
func (s *Server) EnableImpersonate()
EnableImpersonate enable impersonate service account.
func (*Server) EnableWorkloadIdentityFederation ¶
func (s *Server) EnableWorkloadIdentityFederation()
EnableWorkloadIdentityFederation enable Workload Identity Federation ADC.
func (*Server) ListenAndServe ¶
ListenAndServe is a wrapper for https://pkg.go.dev/pkg/net/http/#Server.ListenAndServe
func (*Server) ListenAndServeTLS ¶
ListenAndServeTLS is a wrapper for https://pkg.go.dev/pkg/net/http/#Server.ListenAndServeTLS
func (*Server) Serve ¶
Serve is a wrapper for https://pkg.go.dev/pkg/net/http/#Server.Serve
func (*Server) ServeTLS ¶
ServeTLS is a wrapper for https://pkg.go.dev/pkg/net/http/#Server.ServeTLS
func (*Server) SetDelegateServiceAccount ¶
SetDelegateServiceAccount sets sequence of service accounts in a delegation chain.
type StatusError ¶
type StatusError struct {
// contains filtered or unexported fields
}
StatusError represents an error and safehttp.StatusCode.
This error requires custom safehttp dispatcher.
func NewStatusError ¶
func NewStatusError(err error, status safehttp.StatusCode) StatusError
NewStatusError returns the new StatusError from err and status args.
func (StatusError) Code ¶
func (e StatusError) Code() safehttp.StatusCode
Code implements safehttp.ErrorResponse.Code.
func (StatusError) Error ¶
func (e StatusError) Error(w http.ResponseWriter, resp safehttp.ErrorResponse) error
Error implements safehttp.Dispatcher.Error.
type TokenResponse ¶
type TokenResponse struct { AccessToken string `json:"access_token"` ExpiresIn int `json:"expires_in"` TokenType string `json:"token_type"` }
TokenResponse represents a JSON response of service account token.
type X86Microarchitecture ¶
type X86Microarchitecture int
const ( X86_UNKNOWN X86Microarchitecture = iota INTEL_80486 // https://en.wikichip.org/wiki/intel/microarchitectures/80486 INTEL_P5 // https://en.wikichip.org/wiki/intel/microarchitectures/p5 INTEL_LAKEMONT // https://en.wikichip.org/wiki/intel/quark INTEL_CORE // https://en.wikipedia.org/wiki/Intel_Core_(microarchitecture) INTEL_PNR // https://en.wikipedia.org/wiki/Penryn_(microarchitecture) INTEL_NHM // https://en.wikipedia.org/wiki/Nehalem_(microarchitecture) INTEL_ATOM_BNL // https://en.wikipedia.org/wiki/Bonnell_(microarchitecture) INTEL_WSM // https://en.wikipedia.org/wiki/Westmere_(microarchitecture) INTEL_SNB // https://en.wikipedia.org/wiki/Sandy_Bridge#Models_and_steppings INTEL_IVB // https://en.wikipedia.org/wiki/Ivy_Bridge_(microarchitecture)#Models_and_steppings INTEL_ATOM_SMT // https://en.wikipedia.org/wiki/Silvermont INTEL_HSW // https://en.wikipedia.org/wiki/Haswell_(microarchitecture) INTEL_BDW // https://en.wikipedia.org/wiki/Broadwell_(microarchitecture) INTEL_SKL // https://en.wikipedia.org/wiki/Skylake_(microarchitecture) INTEL_ATOM_GMT // https://en.wikipedia.org/wiki/Goldmont INTEL_KBL // https://en.wikipedia.org/wiki/Kaby_Lake INTEL_CFL // https://en.wikipedia.org/wiki/Coffee_Lake INTEL_WHL // https://en.wikipedia.org/wiki/Whiskey_Lake_(microarchitecture) INTEL_CML // https://en.wikichip.org/wiki/intel/microarchitectures/comet_lake INTEL_CNL // https://en.wikipedia.org/wiki/Cannon_Lake_(microarchitecture) INTEL_ICL // https://en.wikipedia.org/wiki/Ice_Lake_(microprocessor) INTEL_TGL // https://en.wikipedia.org/wiki/Tiger_Lake_(microarchitecture) INTEL_SPR // https://en.wikipedia.org/wiki/Sapphire_Rapids INTEL_ADL // https://en.wikichip.org/wiki/intel/microarchitectures/alder_lake INTEL_RCL // https://en.wikichip.org/wiki/intel/microarchitectures/rocket_lake INTEL_KNIGHTS_M // https://en.wikichip.org/wiki/intel/microarchitectures/knights_mill INTEL_KNIGHTS_L // https://en.wikichip.org/wiki/intel/microarchitectures/knights_landing INTEL_KNIGHTS_F // https://en.wikichip.org/wiki/intel/microarchitectures/knights_ferry INTEL_KNIGHTS_C // https://en.wikichip.org/wiki/intel/microarchitectures/knights_corner INTEL_NETBURST // https://en.wikichip.org/wiki/intel/microarchitectures/netburst AMD_HAMMER // K8 HAMMER AMD_K10 // K10 AMD_K11 // http://developer.amd.com/wordpress/media/2012/10/41788.pdf AMD_K12 // https://www.amd.com/system/files/TechDocs/44739_12h_Rev_Gd.pdf AMD_BOBCAT // https://www.amd.com/system/files/TechDocs/47534_14h_Mod_00h-0Fh_Rev_Guide.pdf AMD_PILEDRIVER // https://en.wikichip.org/wiki/amd/microarchitectures/piledriver AMD_STREAMROLLER // https://en.wikichip.org/wiki/amd/microarchitectures/steamroller AMD_EXCAVATOR // https://en.wikichip.org/wiki/amd/microarchitectures/excavator AMD_BULLDOZER // https://en.wikichip.org/wiki/amd/microarchitectures/bulldozer AMD_JAGUAR // K16 JAGUAR AMD_PUMA // K16 PUMA AMD_ZEN // https://en.wikichip.org/wiki/amd/microarchitectures/zen AMD_ZEN_PLUS // https://en.wikichip.org/wiki/amd/microarchitectures/zen%2B AMD_ZEN2 // https://en.wikichip.org/wiki/amd/microarchitectures/zen_2 AMD_ZEN3 // https://en.wikichip.org/wiki/amd/microarchitectures/zen_3 AMD_ZEN4 // https://en.wikichip.org/wiki/amd/microarchitectures/zen_4 )
func (X86Microarchitecture) String ¶
func (x86 X86Microarchitecture) String() string