interception

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2020 License: Apache-2.0 Imports: 27 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TopicConnect is the earliest event triggered in an intercepted API.
	// It is used to validate the endpoint URL, regardless of the Request which
	// will be sent to it.
	TopicConnect events.Topic = "connect"

	// TopicRequest is the second event triggered in an intercepted API.
	// It is used to validate the Request itself, as well as its context.
	TopicRequest events.Topic = "request"

	// TopicResponse is the third event triggered in an intercepted API.
	// It is used to react to the response headers and possibly start of resBody
	// being received. Note that at this point, there is no guarantee that either
	// the Request or Response bodies are actually entirely available, due to
	// HTTP advanced features like request/response interleaving. It is not
	// triggered if the round-trip returns an error, as the associated response
	// is not guaranteed to be well-formed.
	TopicResponse events.Topic = "response"

	// TopicBodies is the fourth and last event triggered in an intercepted API.
	// It is used once the bodies on both Request and Response have been closed
	// by the API client. It does NOT mean that these bodies are necessarily
	// complete, as a client may have closed a request early.
	TopicBodies events.Topic = "bodies"

	// TopicReport is the event used to request transmission of a ReportLog to
	// the logs platform. Unlike its four siblings, it can be triggered at any
	// stage of the API call lifecycle.
	TopicReport events.Topic = "report_log"
)
View Source
const (
	// BodyTooLong is the replacement string for bodies beyond MaximumBodySize.
	BodyTooLong = `(omitted due to size)`

	// BodyIsBinary is the replacement string for unparseable bodies.
	BodyIsBinary = `(not showing binary data)`

	// BodyUndecodable is the replacement string for bodies which were expected to be parsable but failed decoding.
	BodyUndecodable = `(could not decode data)`

	// MaximumBodySize is the largest resBody size to store whole.
	MaximumBodySize = 1 << 20
)
View Source
const Filtered = `[FILTERED]`

Filtered is a well-known string replacing filtered-out content.

Variables

View Source
var (
	ShapeDescriptor_PrimitiveType_name = map[int32]string{
		0: "OBJECT",
		1: "ARRAY",
		2: "STRING",
		3: "NUMBER",
		4: "BOOLEAN",
		5: "NULL",
	}
	ShapeDescriptor_PrimitiveType_value = map[string]int32{
		"OBJECT":  0,
		"ARRAY":   1,
		"STRING":  2,
		"NUMBER":  3,
		"BOOLEAN": 4,
		"NULL":    5,
	}
)

Enum value maps for ShapeDescriptor_PrimitiveType.

View Source
var DefaultSensitiveData = regexp.MustCompile("(?i)[a-z0-9]{1}[a-z0-9.!#$%&’*+=?^_\"{|}~-]+@[a-z0-9-]+(?:\\.[a-z0-9-]+)*|(?:\\d[ -]*?){13,16}")

DefaultSensitiveData is the expression used for sensitive data if no other value is set.

View Source
var DefaultSensitiveKeys = regexp.MustCompile(`(?i)^(authorization|password|secret|passwd|api.?key|access.?token|auth.?token|credentials|mysql_pwd|stripetoken|card.?number.?|secret|client.?id|client.?secret)$`)

DefaultSensitiveKeys is the expression used for sensitive keys if no other value is set.

View Source
var File_shape_hash_proto protoreflect.FileDescriptor
View Source
var FormContentType = regexp.MustCompile(`(?i)x-www-form-urlencoded`)

FormContentType is a regexp definint the content types to handle as traditional web forms.

View Source
var JSONContentType = regexp.MustCompile(`(?i)json`)

JSONContentType is a regexp defining the content types to handle as JSON.

View Source
var ParsableContentType = regexp.MustCompile(`(?i)(json|text|xml|x-www-form-urlencoded)`)

ParsableContentType is a regexp defining the types to attempt to parse.

View Source
var StringContentType = regexp.MustCompile(`(?i)(text|xml)`)

StringContentType is a regexp defininig the types to return as plain strings.

Functions

func ParseFormData

func ParseFormData(reader io.Reader) (map[string][]string, error)

ParseFormData parses form data

func PrepareTriggeredRulesForReport

func PrepareTriggeredRulesForReport(triggeredRules []*DataCollectionRule) []proxy.ReportDataCollectionRule

PrepareTriggeredRulesForReport translates DataCollectionRule objects representing triggered rules into the format used for reporting

func RFCListener

func RFCListener(_ context.Context, e events.Event) error

RFCListener validates the destination URL under RFC793, RFC1384, RFC1738 and RFC3986 before entering the standard Bearer multistage API wrapping.

It is hard-coded in the round-tripper to avoid its being disabled.

func ToBytes

func ToBytes(x interface{}) ([]byte, error)

ToBytes builds a hex-encoded representation of the shape of its argument.

func ToHash

func ToHash(j interface{}) string

ToHash builds a NewShapeDescriptor of its argument.

func ToSha

func ToSha(j interface{}) string

ToSha builds a SHA256 of the NewShapeDescriptor of its argument.

Types

type APIEvent

type APIEvent interface {
	events.Event
	Config() *APIEventConfig
	SetConfig(value *APIEventConfig) APIEvent
	TriggeredDataCollectionRules() []*DataCollectionRule
	SetTriggeredDataCollectionRules(rules []*DataCollectionRule) APIEvent
}

APIEvent is the type common to all API call lifecycle events.

type APIEventConfig

type APIEventConfig struct {
	IsActive bool
	LogLevel
}

APIEventConfig represents configuration values derived from all triggered DataCollectionRule objects.

type BodiesEvent

type BodiesEvent struct {
	RequestBody, ResponseBody interface{}
	RequestSha, ResponseSha   string
	// contains filtered or unexported fields
}

BodiesEvent is the type of events dispatched at the TopicBodies stage.

func (*BodiesEvent) Config

func (ae *BodiesEvent) Config() *APIEventConfig

func (*BodiesEvent) SetConfig

func (ae *BodiesEvent) SetConfig(value *APIEventConfig) APIEvent

func (*BodiesEvent) SetTriggeredDataCollectionRules

func (ae *BodiesEvent) SetTriggeredDataCollectionRules(rules []*DataCollectionRule) APIEvent

func (*BodiesEvent) TriggeredDataCollectionRules

func (ae *BodiesEvent) TriggeredDataCollectionRules() []*DataCollectionRule

type BodyParsingProvider

type BodyParsingProvider struct{}

BodyParsingProvider is an events.Listener provider returning listeners performing data collection, hashing, and sanitization on request/reponse bodies.

func (BodyParsingProvider) Listeners

func (p BodyParsingProvider) Listeners(e events.Event) (l []events.Listener)

Listeners implements events.ListenerProvider.

func (BodyParsingProvider) RequestBodyParser

func (BodyParsingProvider) RequestBodyParser(_ context.Context, e events.Event) error

RequestBodyParser is an events.Listener performing eager resBody loading on API requests, to perform sanitization and bandwidth reduction.

func (BodyParsingProvider) ResponseBodyParser

func (p BodyParsingProvider) ResponseBodyParser(_ context.Context, e events.Event) error

ResponseBodyParser is an events.Listener performing eager resBody loading on API responses, to perform sanitization and bandwidth reduction.

type BodyReadCloser

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

BodyReadCloser wraps a io.ReadCloser to give access to the first peekSize bytes without interfering with the normal behaviour

func NewBodyReadCloser

func NewBodyReadCloser(readCloser io.ReadCloser, peekSize int) *BodyReadCloser

NewBodyReadCloser constructs a BodyReadCloser wrapper

func (*BodyReadCloser) Close

func (r *BodyReadCloser) Close() error

Close closes the underlying io.ReadCloser

func (*BodyReadCloser) Peek

func (r *BodyReadCloser) Peek() ([]byte, error)

Peek returns the result of reading the first peek bytes block

func (*BodyReadCloser) Read

func (r *BodyReadCloser) Read(p []byte) (int, error)

Read gives the usual io.Reader behaviour

type ConnectEvent

type ConnectEvent struct {

	// Host is the host to which the request is sent. It may be an IPv6 braced address.
	Host string

	// Port is the TCP port number, in the uint16 range by RFC793.
	Port uint16

	// Scheme, also known as "protocol", is the first part of RFC3986 URL syntax.
	Scheme string
	// contains filtered or unexported fields
}

ConnectEvent is the type of events dispatched at the TopicConnect stage.

Its Data() is a URL. Recommended use is to set the URL

func NewConnectEvent

func NewConnectEvent(url *url.URL) *ConnectEvent

NewConnectEvent builds a ConnectEvent for a url.URL.

func (*ConnectEvent) Config

func (ae *ConnectEvent) Config() *APIEventConfig

func (ConnectEvent) Request

func (re ConnectEvent) Request() *http.Request

Request overrides the events.EventBase.Request method, building an on-the-fly request from the event fields. It accepts building partial URLs, which may be invalid.

func (*ConnectEvent) SetConfig

func (ae *ConnectEvent) SetConfig(value *APIEventConfig) APIEvent

func (*ConnectEvent) SetTriggeredDataCollectionRules

func (ae *ConnectEvent) SetTriggeredDataCollectionRules(rules []*DataCollectionRule) APIEvent

func (ConnectEvent) Topic

func (re ConnectEvent) Topic() events.Topic

Topic is part of the Event interface.

func (*ConnectEvent) TriggeredDataCollectionRules

func (ae *ConnectEvent) TriggeredDataCollectionRules() []*DataCollectionRule

type ContextKey

type ContextKey string

ContextKey is the type to use for any key added to the context by this package.

type DCRProvider

type DCRProvider struct {
	DCRs []*DataCollectionRule
}

DCRProvider is an events.Listener provider returning listeners based on the active data collection rules.

func (DCRProvider) Listeners

func (p DCRProvider) Listeners(e events.Event) []events.Listener

Listeners implements the events.ListenerProvider interface.

type DataCollectionRule

type DataCollectionRule struct {
	filters.Filter
	*LogLevel
	IsActive   *bool
	FilterHash string
	Params     map[string]interface{}
	Signature  string
}

DataCollectionRule represents a data collection rule.

Inactive rules descriptions generate nil *DataCollectionRule values.

func NewDCRFromDescription

func NewDCRFromDescription(filterMap filters.FilterMap, d DataCollectionRuleDescription) *DataCollectionRule

NewDCRFromDescription creates a DataCollectionRule from a DataCollectionRuleDescription and a valid filters.FilterMap.

type DataCollectionRuleDescription

type DataCollectionRuleDescription struct {
	FilterHash string
	Params     map[string]interface{}
	Config     DynamicConfigDescription
	Signature  string
}

DataCollectionRuleDescription is a serialization-friendly description for a data collection rule.

func (DataCollectionRuleDescription) String

type DynamicConfigDescription

type DynamicConfigDescription struct {
	LogLevel *string // ALL, RESTRICTED, or DETECTED.
	Active   *bool
}

DynamicConfigDescription provides a serialization-friendy description of DynamicConfig.

type FieldDescriptor

type FieldDescriptor struct {
	Hash *ShapeDescriptor `protobuf:"bytes,1,opt,name=hash,proto3" json:"hash,omitempty"`
	Key  string           `protobuf:"bytes,2,opt,name=key,proto3" json:"key,omitempty"`
	// contains filtered or unexported fields
}

func (*FieldDescriptor) Descriptor deprecated

func (*FieldDescriptor) Descriptor() ([]byte, []int)

Deprecated: Use FieldDescriptor.ProtoReflect.Descriptor instead.

func (*FieldDescriptor) GetHash

func (x *FieldDescriptor) GetHash() *ShapeDescriptor

func (*FieldDescriptor) GetKey

func (x *FieldDescriptor) GetKey() string

func (*FieldDescriptor) ProtoMessage

func (*FieldDescriptor) ProtoMessage()

func (*FieldDescriptor) ProtoReflect

func (x *FieldDescriptor) ProtoReflect() protoreflect.Message

func (*FieldDescriptor) Reset

func (x *FieldDescriptor) Reset()

func (*FieldDescriptor) String

func (x *FieldDescriptor) String() string

type LogLevel

type LogLevel int

LogLevel represents the log levels defined by the Bearer platform.

const (
	// Detected specifies that the agent should send log level and connection data only.
	Detected LogLevel = iota - 1

	// Restricted specifies that the agent should send common data and all available stage data,
	// excluding request and response headers and bodies.
	Restricted

	// All specifies that the agent should send common data and all available stage data.
	All
)

func LogLevelFromInt

func LogLevelFromInt(n int) LogLevel

LogLevelFromInt converts any int to a valid LogLevel, adjusting non-valid values to the default LogLevel: Restricted.

func LogLevelFromString

func LogLevelFromString(s string) LogLevel

LogLevelFromString builds a LogLevel from a string, defaulting to Restricted for all invalid strings.

func (*LogLevel) Prepare

func (ll *LogLevel) Prepare(re *ReportEvent) proxy.ReportLog

Prepare extract the ReportLog information from the API call, depending on the LogLevel.

func (LogLevel) String

func (i LogLevel) String() string

type ProxyProvider

type ProxyProvider struct {
	*proxy.Sender
}

ProxyProvider is an events.ListenerProvider returning a proxy listener.

func (ProxyProvider) Listeners

func (p ProxyProvider) Listeners(e events.Event) []events.Listener

Listeners implements the events.ListenerProvider interface.

type ReportEvent

type ReportEvent struct {
	*BodiesEvent
	proxy.Stage
	T0, T1 time.Time
}

ReportEvent is emitted to publish a call proxy.ReportLog.

func NewReportEvent

func NewReportEvent(stage proxy.Stage, err error) *ReportEvent

NewReportEvent builds a ReportEvent, empty but for stage, and error.

func (ReportEvent) Config

func (ae ReportEvent) Config() *APIEventConfig

func (ReportEvent) SetConfig

func (ae ReportEvent) SetConfig(value *APIEventConfig) APIEvent

func (ReportEvent) SetTriggeredDataCollectionRules

func (ae ReportEvent) SetTriggeredDataCollectionRules(rules []*DataCollectionRule) APIEvent

func (ReportEvent) Topic

func (ReportEvent) Topic() events.Topic

Topic is part of the Event interface.

func (ReportEvent) TriggeredDataCollectionRules

func (ae ReportEvent) TriggeredDataCollectionRules() []*DataCollectionRule

type RequestEvent

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

RequestEvent is the type of events dispatched at the TopicRequest stages.

func (*RequestEvent) Config

func (ae *RequestEvent) Config() *APIEventConfig

func (*RequestEvent) SetConfig

func (ae *RequestEvent) SetConfig(value *APIEventConfig) APIEvent

func (*RequestEvent) SetTriggeredDataCollectionRules

func (ae *RequestEvent) SetTriggeredDataCollectionRules(rules []*DataCollectionRule) APIEvent

func (RequestEvent) Topic

func (re RequestEvent) Topic() events.Topic

Topic is part of the Event interface.

func (*RequestEvent) TriggeredDataCollectionRules

func (ae *RequestEvent) TriggeredDataCollectionRules() []*DataCollectionRule

type ResponseEvent

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

ResponseEvent is the type of events dispatched at the TopicResponse stage.

func (*ResponseEvent) Config

func (ae *ResponseEvent) Config() *APIEventConfig

func (*ResponseEvent) SetConfig

func (ae *ResponseEvent) SetConfig(value *APIEventConfig) APIEvent

func (*ResponseEvent) SetTriggeredDataCollectionRules

func (ae *ResponseEvent) SetTriggeredDataCollectionRules(rules []*DataCollectionRule) APIEvent

func (ResponseEvent) Topic

func (ResponseEvent) Topic() events.Topic

Topic is part of the Event interface.

func (*ResponseEvent) TriggeredDataCollectionRules

func (ae *ResponseEvent) TriggeredDataCollectionRules() []*DataCollectionRule

type RoundTripper

type RoundTripper struct {
	events.Dispatcher
	Underlying http.RoundTripper
}

RoundTripper is the instrumented implementation of http.RoundTripper.

It triggers events for the TopicConnect, TopicRequest, and TopicResponse stages.

func (*RoundTripper) RoundTrip

func (rt *RoundTripper) RoundTrip(request *http.Request) (*http.Response, error)

RoundTrip implements the http.RoundTripper interface.

type SanitizationProvider

type SanitizationProvider struct {
	SensitiveKeys    []*regexp.Regexp
	SensitiveRegexps []*regexp.Regexp
}

SanitizationProvider is an events.Listener provider returning listeners based on the sensitive keys and regexps.

func (SanitizationProvider) BodySanitizer

func (p SanitizationProvider) BodySanitizer(k interface{}, v *interface{}, accu *interface{}) error

BodySanitizer applies sanitization rules to data.

func (SanitizationProvider) Listeners

func (p SanitizationProvider) Listeners(e events.Event) []events.Listener

Listeners implements the events.ListenerProvider interface.

func (SanitizationProvider) SanitizeQueryAndPaths

func (p SanitizationProvider) SanitizeQueryAndPaths(_ context.Context, e events.Event) error

SanitizeQueryAndPaths sanitizes the URL query parameters and paths in both the original request and the request present in the response, which may or may not be the same.

func (SanitizationProvider) SanitizeRequestBody

func (p SanitizationProvider) SanitizeRequestBody(_ context.Context, e events.Event) error

SanitizeRequestBody sanitized the Request resBody in a ReportEvent.

func (SanitizationProvider) SanitizeRequestHeaders

func (p SanitizationProvider) SanitizeRequestHeaders(_ context.Context, e events.Event) error

SanitizeRequestHeaders sanitizes Request headers and trailers.

func (SanitizationProvider) SanitizeResponseBody

func (p SanitizationProvider) SanitizeResponseBody(_ context.Context, e events.Event) error

SanitizeResponseBody sanitizes the Response resBody in a ReportEvent.

func (SanitizationProvider) SanitizeResponseHeaders

func (p SanitizationProvider) SanitizeResponseHeaders(_ context.Context, e events.Event) error

SanitizeResponseHeaders sanitizes Response headers and trailers.

type ShapeDescriptor

type ShapeDescriptor struct {

	// Order in schema is used for JSON, while numbers are used for protobuf.
	Fields []*FieldDescriptor            `protobuf:"bytes,4,rep,name=fields,proto3" json:"fields,omitempty"`
	Items  []*ShapeDescriptor            `protobuf:"bytes,2,rep,name=items,proto3" json:"items,omitempty"`
	Rules  []string                      `protobuf:"bytes,3,rep,name=rules,proto3" json:"rules,omitempty"`
	Type   ShapeDescriptor_PrimitiveType `protobuf:"varint,1,opt,name=type,proto3,enum=bearer_agent_shape_hash.ShapeDescriptor_PrimitiveType" json:"type,omitempty"`
	// contains filtered or unexported fields
}

func NewShapeDescriptor

func NewShapeDescriptor(typ ShapeDescriptor_PrimitiveType, fields []*FieldDescriptor, items []*ShapeDescriptor) *ShapeDescriptor

NewShapeDescriptor builds a new ShapeDescriptor from its fields.

func (*ShapeDescriptor) Descriptor deprecated

func (*ShapeDescriptor) Descriptor() ([]byte, []int)

Deprecated: Use ShapeDescriptor.ProtoReflect.Descriptor instead.

func (*ShapeDescriptor) GetFields

func (x *ShapeDescriptor) GetFields() []*FieldDescriptor

func (*ShapeDescriptor) GetItems

func (x *ShapeDescriptor) GetItems() []*ShapeDescriptor

func (*ShapeDescriptor) GetRules

func (x *ShapeDescriptor) GetRules() []string

func (*ShapeDescriptor) GetType

func (*ShapeDescriptor) ProtoMessage

func (*ShapeDescriptor) ProtoMessage()

func (*ShapeDescriptor) ProtoReflect

func (x *ShapeDescriptor) ProtoReflect() protoreflect.Message

func (*ShapeDescriptor) Reset

func (x *ShapeDescriptor) Reset()

func (*ShapeDescriptor) String

func (x *ShapeDescriptor) String() string

type ShapeDescriptor_PrimitiveType

type ShapeDescriptor_PrimitiveType int32
const (
	ShapeDescriptor_OBJECT  ShapeDescriptor_PrimitiveType = 0
	ShapeDescriptor_ARRAY   ShapeDescriptor_PrimitiveType = 1
	ShapeDescriptor_STRING  ShapeDescriptor_PrimitiveType = 2
	ShapeDescriptor_NUMBER  ShapeDescriptor_PrimitiveType = 3
	ShapeDescriptor_BOOLEAN ShapeDescriptor_PrimitiveType = 4
	ShapeDescriptor_NULL    ShapeDescriptor_PrimitiveType = 5
)

func (ShapeDescriptor_PrimitiveType) Descriptor

func (ShapeDescriptor_PrimitiveType) Enum

func (ShapeDescriptor_PrimitiveType) EnumDescriptor deprecated

func (ShapeDescriptor_PrimitiveType) EnumDescriptor() ([]byte, []int)

Deprecated: Use ShapeDescriptor_PrimitiveType.Descriptor instead.

func (ShapeDescriptor_PrimitiveType) Number

func (ShapeDescriptor_PrimitiveType) String

func (ShapeDescriptor_PrimitiveType) Type

type WalkFn

type WalkFn func(ik interface{}, iv *interface{}, accu *interface{}) error

WalkFn is the type for visitor functions used with a Walker.

type Walker

type Walker interface {
	fmt.Stringer
	Walk(accu *interface{}, visitor WalkFn) error
	Value() interface{}
}

Walker is able to walk a visitor WalkFn in preorder across the whole tree of a value unmarshalled from JSON, which is far from being any type of Go data.

func NewWalker

func NewWalker(x interface{}) Walker

NewWalker builds an initialized Walker.

Jump to

Keyboard shortcuts

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