util

package
v1.0.0-alpha Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2017 License: Apache-2.0 Imports: 28 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"
)
View Source
const SecretTag = "secret"

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 PassExpr = regexp.MustCompile(`[,]?password[,]?`)

PassExpr is the regular expression to check if a tag has 'password'

View Source
var RevocationReasonCodes = map[string]int{
	"unspecified":          ocsp.Unspecified,
	"keycompromise":        ocsp.KeyCompromise,
	"cacompromise":         ocsp.CACompromise,
	"affiliationchanged":   ocsp.AffiliationChanged,
	"superseded":           ocsp.Superseded,
	"cessationofoperation": ocsp.CessationOfOperation,
	"certificatehold":      ocsp.CertificateHold,
	"removefromcrl":        ocsp.RemoveFromCRL,
	"privilegewithdrawn":   ocsp.PrivilegeWithdrawn,
	"aacompromise":         ocsp.AACompromise,
}

RevocationReasonCodes is a map between string reason codes to integers as defined in RFC 5280

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 CmdRunBegin

func CmdRunBegin()

CmdRunBegin is called at the beginning of each cobra run function

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 []byte, 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 body The body of an HTTP request

func DERCertToPEM

func DERCertToPEM(der []byte) []byte

DERCertToPEM converts DER to PEM format

func DecodeToken

func DecodeToken(token string) (*x509.Certificate, string, string, error)

DecodeToken extracts an X509 certificate and base64 encoded signature from a token

func Fatal

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

Fatal logs a fatal message and exits

func FileExists

func FileExists(name string) bool

FileExists checks to see if a file exists

func FlagBool

func FlagBool(flags *pflag.FlagSet, name, short string, def bool, desc string)

FlagBool sets up a flag for a bool, binding it to its name

func FlagInt

func FlagInt(flags *pflag.FlagSet, name, short string, def int, desc string)

FlagInt sets up a flag for an int, binding it to its name

func FlagString

func FlagString(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 []byte, body []byte) (string, error)

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

func GetCommandLineOptValue

func GetCommandLineOptValue(optName string, remove bool) string

GetCommandLineOptValue searches the command line arguments for the specified option and returns the following value if found; otherwise it returns "". If **remove** is true and it is found, the option and its value are removed from os.Args. For example, if command line is:

fabric-ca client enroll -config myconfig.json

GetCommandLineOptValue("-config",true) returns "myconfig.json" and changes os.Args to

fabric-ca client enroll

func GetDB

func GetDB(driver string, dbPath string) (*sqlx.DB, error)

GetDB returns a handle to an established driver-specific database connection

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 GetKeyFromBytes

func GetKeyFromBytes(csp bccsp.BCCSP, key []byte) (bccsp.Key, error)

GetKeyFromBytes returns a BCCSP key given a byte buffer. The byte buffer should always contain the SKI and not the real private key; however, until we have complete BCCSP integration, we tolerate it being the real private key.

func GetSerialAsHex

func GetSerialAsHex(serial *big.Int) string

GetSerialAsHex returns the serial number from certificate as hex format

func GetServerAddr

func GetServerAddr() string

GetServerAddr returns the server's address

func GetServerPort

func GetServerPort() string

GetServerPort returns the server's listening port

func GetServerProtocol

func GetServerProtocol() string

GetServerProtocol returns the server's protocol

func GetServerURL

func GetServerURL() string

GetServerURL returns the server's URL

func GetUser

func GetUser() (string, string, error)

GetUser returns username and password from CLI input

func GetX509CertificateFromPEM

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

GetX509CertificateFromPEM converts a PEM buffer to an X509 Certificate

func HTTPRequestToString

func HTTPRequestToString(req *http.Request) string

HTTPRequestToString returns a string for an HTTP request for debuggging

func HTTPResponseToString

func HTTPResponseToString(resp *http.Response) string

HTTPResponseToString returns a string for an HTTP response for debuggging

func MakeFileAbs

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

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

func Marshal

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

Marshal to bytes

func ParseObj

func ParseObj(obj interface{}, cb func(*Field) error) 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 ReadFile

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

ReadFile reads a file

func RegisterFlags

func RegisterFlags(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 RemoveQuotes

func RemoveQuotes(str string) string

RemoveQuotes removes outer quotes from a string if necessary

func SetDefaultServerPort

func SetDefaultServerPort()

SetDefaultServerPort overrides the default CFSSL server port by adding the "-port" option to the command line if it was not already present.

func StrContained

func StrContained(str string, strs []string) bool

StrContained returns true if 'str' is in 'strs'; otherwise return false

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 VerifyToken

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

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

func WriteFile

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

WriteFile writes a file

Types

type ECDSASignature

type ECDSASignature struct {
	R, S *big.Int
}

ECDSASignature forms the structure for R and S value for ECDSA

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{}
}

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