util

package
v1.10.0-beta10 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2024 License: Apache-2.0 Imports: 44 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TagDefault is the tag name for a default value of a field as recognized
	// by RegisterFlags.
	TagDefault = "def"
	// TagHelp is the tag name for a help message of a field as recognized
	// by RegisterFlags.
	TagHelp = "help"
	// TagOpt is the tag name for a one character option of a field as recognized
	// by RegisterFlags.  For example, a value of "d" reserves "-d" for the
	// command line argument.
	TagOpt = "opt"
	// TagSkip is the tag name which causes the field to be skipped by
	// RegisterFlags.
	TagSkip = "skip"
	// TagHide is the tag name which causes the field to be hidden
	TagHide = "hide"
)
View Source
const SecretTag = "mask"

SecretTag to tag a field as secret as in password, token

Variables

View Source
var (

	// ErrNotImplemented used to return errors for functions not implemented
	ErrNotImplemented = errors.New("NOT YET IMPLEMENTED")
)
View Source
var URLRegex = regexp.MustCompile(`(ldap|http)s*://(\S+):(\S+)@`)

URLRegex is the regular expression to check if a value is an URL

Functions

func B64Decode

func B64Decode(str string) (buf []byte, err error)

B64Decode base64 decodes a string

func B64Encode

func B64Encode(buf []byte) string

B64Encode base64 encodes bytes

func BCCSPKeyRequestGenerate

func BCCSPKeyRequestGenerate(req *csr.CertificateRequest, myCSP bccsp.BCCSP) (bccsp.Key, crypto.Signer, error)

BCCSPKeyRequestGenerate generates keys through BCCSP somewhat mirroring to cfssl/req.KeyRequest.Generate()

func BccspBackedSigner

func BccspBackedSigner(caFile, keyFile string, policy *config.Signing, csp bccsp.BCCSP) (signer.Signer, error)

BccspBackedSigner attempts to create a signer using csp bccsp.BCCSP. This csp could be SW (golang crypto) PKCS11 or whatever BCCSP-conformant library is configured

func CheckHostsInCert

func CheckHostsInCert(certFile string, hosts ...string) error

CheckHostsInCert checks to see if host correctly inserted into certificate

func CmdRunBegin

func CmdRunBegin(v *viper.Viper)

CmdRunBegin is called at the beginning of each cobra run function

func ConfigureBCCSP

func ConfigureBCCSP(optsPtr **factory.FactoryOpts, mspDir, homeDir string) error

ConfigureBCCSP configures BCCSP, using

func CopyMissingValues

func CopyMissingValues(src, dst interface{})

CopyMissingValues checks the dst interface for missing values and replaces them with value from src config struct. This does a deep copy of pointers.

func CreateClientHome

func CreateClientHome() (string, error)

CreateClientHome will create a home directory if it does not exist

func CreateToken

func CreateToken(csp bccsp.BCCSP, cert []byte, key bccsp.Key, method, uri string, body []byte) (string, error)

CreateToken creates a JWT-like token. In a normal JWT token, the format of the token created is:

<algorithm,claims,signature>

where each part is base64-encoded string separated by a period. In this JWT-like token, there are two differences:

  1. the claims section is a certificate, so the format is: <certificate,signature>
  2. the signature uses the private key associated with the certificate, and the signature is across both the certificate and the "body" argument, which is the body of an HTTP request, though could be any arbitrary bytes.

@param cert The pem-encoded certificate @param key The pem-encoded key @param method http method of the request @param uri URI of the request @param body The body of an HTTP request

func ErrorContains

func ErrorContains(t *testing.T, err error, contains, msg string, args ...interface{})

ErrorContains will check to see if an error occurred, if so it will check that it contains the appropriate error message

func Fatal

func Fatal(format string, v ...interface{})

Fatal logs a fatal message and exits

func FatalError

func FatalError(t *testing.T, err error, msg string, args ...interface{})

FatalError will check to see if an error occurred if so it will cause the test cases exit

func FileExists

func FileExists(name string) bool

FileExists checks to see if a file exists

func FlagString

func FlagString(v *viper.Viper, flags *pflag.FlagSet, name, short string, def string, desc string)

FlagString sets up a flag for a string, binding it to its name

func GenECDSAToken

func GenECDSAToken(csp bccsp.BCCSP, cert []byte, key bccsp.Key, method, uri string, body []byte) (string, error)

GenECDSAToken signs the http body and cert with ECDSA using EC private key

func GetBCCSP

func GetBCCSP(opts *factory.FactoryOpts, homeDir string) (bccsp.BCCSP, error)

GetBCCSP returns BCCSP

func GetCertificateDuration

func GetCertificateDuration(cert *x509.Certificate) time.Duration

GetCertificateDuration returns the validity duration for a certificate

func GetCertificateDurationFromFile

func GetCertificateDurationFromFile(file string) (time.Duration, error)

GetCertificateDurationFromFile returns the validity duration for a certificate in a file.

func GetDefaultBCCSP

func GetDefaultBCCSP() bccsp.BCCSP

GetDefaultBCCSP returns the default BCCSP

func GetDefaultConfigFile

func GetDefaultConfigFile(cmdName string) string

GetDefaultConfigFile gets the default path for the config file to display in usage message

func GetECPrivateKey

func GetECPrivateKey(raw []byte) (*ecdsa.PrivateKey, error)

GetECPrivateKey get *ecdsa.PrivateKey from key pem

func GetEnrollmentIDFromPEM

func GetEnrollmentIDFromPEM(cert []byte) (string, error)

GetEnrollmentIDFromPEM returns the EnrollmentID from a PEM buffer

func GetEnrollmentIDFromX509Certificate

func GetEnrollmentIDFromX509Certificate(cert *x509.Certificate) string

GetEnrollmentIDFromX509Certificate returns the EnrollmentID from the X509 certificate

func GetMaskedURL

func GetMaskedURL(url string) string

GetMaskedURL returns masked URL. It masks username and password from the URL if present

func GetRSAPrivateKey

func GetRSAPrivateKey(raw []byte) (*rsa.PrivateKey, error)

GetRSAPrivateKey get *rsa.PrivateKey from key pem

func GetSerialAsHex

func GetSerialAsHex(serial *big.Int) string

GetSerialAsHex returns the serial number from certificate as hex format

func GetServerPort

func GetServerPort() string

GetServerPort returns the server's listening port

func GetServerURL

func GetServerURL() string

GetServerURL returns the server's URL

func GetSignerFromCert

func GetSignerFromCert(cert *x509.Certificate, csp bccsp.BCCSP) (bccsp.Key, crypto.Signer, error)

GetSignerFromCert load private key represented by ski and return bccsp signer that conforms to crypto.Signer

func GetSignerFromCertFile

func GetSignerFromCertFile(certFile string, csp bccsp.BCCSP) (bccsp.Key, crypto.Signer, *x509.Certificate, error)

GetSignerFromCertFile load skiFile and load private key represented by ski and return bccsp signer that conforms to crypto.Signer

func GetSliceFromList

func GetSliceFromList(split string, delim string) []string

GetSliceFromList will return a slice from a list

func GetUser

func GetUser(v *viper.Viper) (string, string, error)

GetUser returns username and password from CLI input

func GetX509CertificateFromPEM

func GetX509CertificateFromPEM(cert []byte) (*x509.Certificate, error)

GetX509CertificateFromPEM get an X509 certificate from bytes in PEM format

func GetX509CertificateFromPEMFile

func GetX509CertificateFromPEMFile(file string) (*x509.Certificate, error)

GetX509CertificateFromPEMFile gets an X509 certificate from a file

func GetX509CertificatesFromPEM

func GetX509CertificatesFromPEM(pemBytes []byte) ([]*x509.Certificate, error)

GetX509CertificatesFromPEM returns X509 certificates from bytes in PEM format

func HTTPRequestToString

func HTTPRequestToString(req *http.Request) string

HTTPRequestToString returns a string for an HTTP request for debugging

func HTTPResponseToString

func HTTPResponseToString(resp *http.Response) string

HTTPResponseToString returns a string for an HTTP response for debugging

func Hostname

func Hostname() string

Hostname name returns the hostname of the machine

func ImportBCCSPKeyFromPEM

func ImportBCCSPKeyFromPEM(keyFile string, myCSP bccsp.BCCSP, temporary bool) (bccsp.Key, error)

ImportBCCSPKeyFromPEM attempts to create a private BCCSP key from a pem file keyFile

func InitBCCSP

func InitBCCSP(optsPtr **factory.FactoryOpts, mspDir, homeDir string) (bccsp.BCCSP, error)

InitBCCSP initializes BCCSP

func ListContains

func ListContains(list, find string) bool

ListContains looks through a comma separated list to see if a string exists

func LoadX509KeyPair

func LoadX509KeyPair(certFile, keyFile string, csp bccsp.BCCSP) (*tls.Certificate, error)

LoadX509KeyPair reads and parses a public/private key pair from a pair of files. The files must contain PEM encoded data. The certificate file may contain intermediate certificates following the leaf certificate to form a certificate chain. On successful return, Certificate.Leaf will be nil because the parsed form of the certificate is not retained.

This function originated from crypto/tls/tls.go and was adapted to use a BCCSP Signer

func MakeFileAbs

func MakeFileAbs(file, dir string) (string, error)

MakeFileAbs makes 'file' absolute relative to 'dir' if not already absolute

func MakeFileNamesAbsolute

func MakeFileNamesAbsolute(files []*string, home string) error

MakeFileNamesAbsolute makes all file names in the list absolute, relative to home

func Marshal

func Marshal(from interface{}, what string) ([]byte, error)

Marshal to bytes

func NormalizeFileList

func NormalizeFileList(files []string, homeDir string) ([]string, error)

NormalizeFileList provides absolute pathing for the list of files

func NormalizeStringSlice

func NormalizeStringSlice(slice []string) []string

NormalizeStringSlice checks for seperators

func ParseObj

func ParseObj(obj interface{}, cb func(*Field) error, tags map[string]string) error

ParseObj parses an object structure, calling back with field info for each field

func RandomString

func RandomString(n int) string

RandomString returns a random string

func Read

func Read(r io.Reader, data []byte) ([]byte, error)

Read reads from Reader into a byte array

func ReadFile

func ReadFile(file string) ([]byte, error)

ReadFile reads a file

func RegisterFlags

func RegisterFlags(v *viper.Viper, flags *pflag.FlagSet, config interface{},
	tags map[string]string) error

RegisterFlags registers flags for all fields in an arbitrary 'config' object. This method recognizes the following field tags: "def" - the default value of the field; "opt" - the optional one character short name to use on the command line; "help" - the help message to display on the command line; "skip" - to skip the field.

func StructToString

func StructToString(si interface{}) string

StructToString converts a struct to a string. If a field has a 'secret' tag, it is masked in the returned string

func Unmarshal

func Unmarshal(from []byte, to interface{}, what string) error

Unmarshal from bytes

func ValidateAndReturnAbsConf

func ValidateAndReturnAbsConf(configFilePath, homeDir, cmdName string) (string, string, error)

ValidateAndReturnAbsConf checks to see that there are no conflicts between the configuration file path and home directory. If no conflicts, returns back the absolute path for the configuration file and home directory.

func VerifyToken

func VerifyToken(csp bccsp.BCCSP, token string, method, uri string, body []byte, compMode1_3 bool) (*x509.Certificate, error)

VerifyToken verifies token signed by either ECDSA or RSA and returns the associated user ID

TODO(mjs): Move to consumer (lib/serverRequestContextImpl#verifyX509Token)

func WriteFile

func WriteFile(file string, buf []byte, perm os.FileMode) error

WriteFile writes a file

Types

type Field

type Field struct {
	Name  string
	Path  string
	Type  reflect.Type
	Kind  reflect.Kind
	Leaf  bool
	Depth int
	Tag   reflect.StructTag
	Value interface{}
	Addr  interface{}
	Hide  string
}

Field is a field of an arbitrary struct

Jump to

Keyboard shortcuts

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