tools

package
v3.3.3 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2023 License: MIT Imports: 25 Imported by: 12

Documentation

Overview

Package tools provides utilities for all the bus actors

Copyright 2015 Apcera Inc. All rights reserved. Copied from nats.go/encoders/protobuf and changed to use gogo/protobuf

Index

Constants

View Source
const (
	// Alpha contains the letters upper and lower cased
	Alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

	// Num contains the 10 digits
	Num = "0123456789"

	// Dot is the dot char
	Dot = "."

	// Dash is the '-' char
	Dash = "-"

	// Underscore is the '_' char
	Underscore = "_"

	// EventTypeAllowerCharacters constains all the characters allowed in a event
	// type name
	EventTypeAllowerCharacters = Alpha + Num + Dash + Underscore + Dot

	// AccountNameCharacters contains characters allowed in a account name
	AccountNameCharacters = Alpha + Num + Dash + Underscore
)
View Source
const (
	PROTOBUF_ENCODER = "protobuf"
)

Additional index for registered Encoders.

Variables

View Source
var (
	ErrInvalidProtoMsgEncode = errors.New("nats: Invalid protobuf proto.Message object passed to encode")
	ErrInvalidProtoMsgDecode = errors.New("nats: Invalid protobuf proto.Message object passed to decode")
)
View Source
var (
	// ErrWrongEnvelope is returned by validator if an envelope with a wrong
	// ID is passed to it.
	ErrWrongEnvelope = errors.New("Wrong envelope")
)

Functions

func CreateCSR

func CreateCSR(
	priv interface{}, hostDefs string, certSubject pkix.Name,
) (derBytes []byte, err error)

CreateCSR is used when creating CSRs. you need to provide a private Key

func CreateCert

func CreateCert(
	priv interface{}, ca bool,
	extKeyUsage x509.ExtKeyUsage,
	signerCertData []byte, signerPrivKey []byte,
	notbefore, notafter time.Time, hostDefs string,
	organization []string, commonName string,
) (certData []byte, err error)

CreateCert creates either an X.509 certificate (self-signed or to be signed via the specified CA private key) or a PKCS #10 CSR. the result is a PEM encoded byte array

func CreateCertificateFromCSR

func CreateCertificateFromCSR(
	csr []byte, parent *x509.Certificate, priv interface{}, notAfter time.Time,
) ([]byte, error)

CreateCertificateFromCSR creates a certificate using a csr as a template.

func CreateClientCA

func CreateClientCA(
	priv interface{}, notbefore, notafter time.Time, hostDefs string,
) (certData []byte, err error)

CreateClientCA creates a self-signed X.509 CA for client connections.

func CreatePrivateKey

func CreatePrivateKey() (privKeyData []byte, err error)

CreatePrivateKey generates a private key and returns it, PEM-encoded.

func CreateServerRootCA

func CreateServerRootCA(
	priv interface{}, notbefore, notafter time.Time, hostDefs string,
) (certData []byte, err error)

CreateServerRootCA creates a self-signed X.509 certificate used by the Xbus server. it returns a PEM encoded certificate in a byte array

func DumpEventValidator

func DumpEventValidator(ev EventValidator) (string, error)

DumpEventValidator store a EventValidator in a string

func GetCSRHash

func GetCSRHash(csrDataStr string, decodePEM bool) string

GetCSRHash computes a SHA-1 hash of the specified certificate signing request; the output is prettyfied (2-char split). The hash is based on the DER data of the CSR. @param decodePEM: Whether the data is given as PEM-encoded data, in which case the DER data will be extracted from it.

func PKeyToPEM

func PKeyToPEM(key *ecdsa.PrivateKey) ([]byte, error)

PKeyToPEM serialize a key to PEM encoding

func RandomString

func RandomString(n int) string

RandomString returns a random string of length 'n'

func ReadPEM

func ReadPEM(blockType string, pemOrPath string) ([]byte, error)

ReadPEM loads bytes from a PEM content or a file containing a PEM encoded block

func ReadPEMCertificate

func ReadPEMCertificate(pemOrPath string) (*x509.Certificate, error)

ReadPEMCertificate loads a x509.Certificate from a PEM file or content

func ReadPEMPrivateKey

func ReadPEMPrivateKey(pemOrPath string) (*ecdsa.PrivateKey, error)

ReadPEMPrivateKey loads a x509.Certificate from a PEM file or content

func RequestWithInbox

func RequestWithInbox(nc *nats.Conn, subject, inbox string, data []byte, timeout time.Duration) (*nats.Msg, error)

RequestWithInbox is a nats.Conn.Request that takes the reply instead of calculating it automatically

func ValidateAccountName

func ValidateAccountName(name string) bool

ValidateAccountName returns true if the passed account name is valid

func ValidateEventType

func ValidateEventType(eventType string) bool

ValidateEventType returns true if the passed event type is valid

Types

type CustomInboxPrefixConn

type CustomInboxPrefixConn struct {
	*nats.Conn

	InboxPrefix string
}

CustomInboxPrefixConn is a nats.Conn that uses a custom inbox prefix

func NewClientConn

func NewClientConn(c *nats.Conn, ClientID string) *CustomInboxPrefixConn

NewClientConn returns a CustomInboxPrefixConn for a client

func NewServerToClientConn

func NewServerToClientConn(c *nats.Conn, ClientID string) *CustomInboxPrefixConn

NewServerToClientConn returns a CustomInboxPrefixConn for a server talking to a given client

func (CustomInboxPrefixConn) Request

func (c CustomInboxPrefixConn) Request(subject string, data []byte, timeout time.Duration) (*nats.Msg, error)

Request overrides nats.Conn.Request and uses a custom reply subject

type EnvelopeFragmentSorter

type EnvelopeFragmentSorter struct {
	EnvelopeValidator
	// contains filtered or unexported fields
}

EnvelopeFragmentSorter sorts a stream of envelope fragments It is _not_ concurrency safe

func NewEnvelopeFragmentSorter

func NewEnvelopeFragmentSorter() *EnvelopeFragmentSorter

NewEnvelopeFragmentSorter returns a new EnvelopeFragmentSorter

func (*EnvelopeFragmentSorter) Add

func (s *EnvelopeFragmentSorter) Add(fragment *api.Envelope)

Add a fragment to the sorter queue

func (*EnvelopeFragmentSorter) Get

Get returns the current item

func (*EnvelopeFragmentSorter) Next

func (s *EnvelopeFragmentSorter) Next() bool

Next switch to the next fragment if available

type EnvelopeValidator

type EnvelopeValidator struct {
	EnvelopeID      api.UUID
	ExpectedEvents  []api.UUID
	EventTypesKnown bool
	Events          map[api.UUID]EventValidator // Index if the last chunk of each event
	Complete        bool
	Valid           bool
}

EnvelopeValidator handle a stream of chunks of envelope and calculate checksum on the fly. The event chunks in the stream must be ordered. Its state can be easily saved to later continuation

func NewEnvelopeValidator

func NewEnvelopeValidator() EnvelopeValidator

NewEnvelopeValidator returns a new validator, and optionnaly feeds it some envelopes

func (*EnvelopeValidator) Add

func (v *EnvelopeValidator) Add(envelope api.Envelope) error

Add adds an envelope

func (*EnvelopeValidator) Check

func (v *EnvelopeValidator) Check() error

Check compare the current validation state with a final state

func (EnvelopeValidator) Clone added in v3.3.0

Clone returns a copy of the validator

func (*EnvelopeValidator) IsNext

func (v *EnvelopeValidator) IsNext(envelope api.Envelope) bool

IsNext returns true if the given fragment can be next in line, ie its events index are just after the ones already validated

type EnvelopeValidatorWithQueue

type EnvelopeValidatorWithQueue struct {
	EnvelopeValidator
	// contains filtered or unexported fields
}

EnvelopeValidatorWithQueue queues fragments when they are not the next in line

func NewEnvelopeValidatorWithQueue

func NewEnvelopeValidatorWithQueue() *EnvelopeValidatorWithQueue

NewEnvelopeValidatorWithQueue creates a new EnvelopeValidatorWithQueue

func (*EnvelopeValidatorWithQueue) Queue

func (v *EnvelopeValidatorWithQueue) Queue(fragment api.Envelope) error

Queue a fragment for validation as soon as it becomes the next in line

type EventValidator

type EventValidator struct {
	EventID          api.UUID
	Type             string
	ItemCount        uint64
	Checksum         api.Checksum
	CurrentIndex     uint64
	CurrentItemCount uint64
	CurrentChecksum  api.Checksum
	Complete         bool
	Valid            bool
	Error            error `json:"-"`
}

EventValidator handles a stream of event chunks of the same event

func LoadEventValidator

func LoadEventValidator(s string) (ev EventValidator, err error)

LoadEventValidator loads a EventValidator from a string

func NewEventValidator

func NewEventValidator(event api.Event) (EventValidator, error)

NewEventValidator initialize a EventValidator

func (EventValidator) MarshalJSON

func (v EventValidator) MarshalJSON() ([]byte, error)

MarshalJSON serializes to json

func (*EventValidator) UnmarshalJSON

func (v *EventValidator) UnmarshalJSON(data []byte) error

UnmarshalJSON deserialize a jsonified EventValidator

func (EventValidator) Update

func (v EventValidator) Update(event api.Event) (EventValidator, error)

Update calculates a new intermediary state

type ProtobufEncoder

type ProtobufEncoder struct {
}

ProtobufEncoder is a protobuf implementation for EncodedConn This encoder will use the builtin protobuf lib to Marshal and Unmarshal structs.

func (*ProtobufEncoder) Decode

func (pb *ProtobufEncoder) Decode(subject string, data []byte, vPtr interface{}) error

Decode

func (*ProtobufEncoder) Encode

func (pb *ProtobufEncoder) Encode(subject string, v interface{}) ([]byte, error)

Encode

type WhoAmIConn

type WhoAmIConn struct {
	*nats.Conn
}

WhoAmIConn is a conn with a very specific reply

func NewWhoAmIConn

func NewWhoAmIConn(c *nats.Conn) WhoAmIConn

NewWhoAmIConn creates a new WhoAmIConn

func (WhoAmIConn) Request

func (c WhoAmIConn) Request(subject string, data []byte, timeout time.Duration) (*nats.Msg, error)

Request performs a custom REQ/REP for the 'whoami' call that have a specific inbox subject pattern

Jump to

Keyboard shortcuts

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