random

package
v0.0.0-...-e8682ef Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2024 License: MPL-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const APIMaxBytes = 128 * 1024

Variables

View Source
var (
	LowercaseCharset   = sortCharset("abcdefghijklmnopqrstuvwxyz")
	UppercaseCharset   = sortCharset("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
	NumericCharset     = sortCharset("0123456789")
	FullSymbolCharset  = sortCharset("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~")
	ShortSymbolCharset = sortCharset("-")

	AlphabeticCharset              = sortCharset(UppercaseCharset + LowercaseCharset)
	AlphaNumericCharset            = sortCharset(AlphabeticCharset + NumericCharset)
	AlphaNumericShortSymbolCharset = sortCharset(AlphaNumericCharset + ShortSymbolCharset)
	AlphaNumericFullSymbolCharset  = sortCharset(AlphaNumericCharset + FullSymbolCharset)

	LowercaseRuneset   = []rune(LowercaseCharset)
	UppercaseRuneset   = []rune(UppercaseCharset)
	NumericRuneset     = []rune(NumericCharset)
	FullSymbolRuneset  = []rune(FullSymbolCharset)
	ShortSymbolRuneset = []rune(ShortSymbolCharset)

	AlphabeticRuneset              = []rune(AlphabeticCharset)
	AlphaNumericRuneset            = []rune(AlphaNumericCharset)
	AlphaNumericShortSymbolRuneset = []rune(AlphaNumericShortSymbolCharset)
	AlphaNumericFullSymbolRuneset  = []rune(AlphaNumericFullSymbolCharset)

	// DefaultStringGenerator has reasonable default rules for generating strings
	DefaultStringGenerator = &StringGenerator{
		Length: 20,
		Rules: []Rule{
			CharsetRule{
				Charset:  LowercaseRuneset,
				MinChars: 1,
			},
			CharsetRule{
				Charset:  UppercaseRuneset,
				MinChars: 1,
			},
			CharsetRule{
				Charset:  NumericRuneset,
				MinChars: 1,
			},
			CharsetRule{
				Charset:  ShortSymbolRuneset,
				MinChars: 1,
			},
		},
	}
)

Functions

func HandleRandomAPI

func HandleRandomAPI(d *framework.FieldData, additionalSource io.Reader) (*logical.Response, error)

Types

type CharsetRule

type CharsetRule struct {
	// CharsetRule is the list of rules that candidate strings must contain a minimum number of.
	Charset runes `mapstructure:"charset" json:"charset"`

	// MinChars indicates the minimum (inclusive) number of characters from the charset that should appear in the string.
	MinChars int `mapstructure:"min-chars" json:"min-chars"`
}

CharsetRule requires a certain number of characters from the specified charset.

func (CharsetRule) Chars

func (c CharsetRule) Chars() []rune

Chars returns the charset that this rule is looking for.

func (CharsetRule) MinLength

func (c CharsetRule) MinLength() int

func (CharsetRule) Pass

func (c CharsetRule) Pass(value []rune) bool

Pass returns true if the provided candidate string has a minimum number of chars in it. This adheres to the Rule interface

func (CharsetRule) Type

func (c CharsetRule) Type() string

type PolicyParser

type PolicyParser struct {
	// RuleRegistry maps rule names in HCL to Rule constructors.
	RuleRegistry Registry
}

PolicyParser parses string generator configuration from HCL.

func (PolicyParser) ParsePolicy

func (p PolicyParser) ParsePolicy(raw string) (sg StringGenerator, err error)

ParsePolicy parses the provided HCL into a StringGenerator.

type Registry

type Registry struct {
	// Rules maps names of rules to a constructor for the rule
	Rules map[string]ruleConstructor
}

Registry of HCL rule names to rule constructors.

type Rule

type Rule interface {
	// Pass should return true if the provided value passes any assertions this Rule is making.
	Pass(value []rune) bool

	// Type returns the name of the rule as associated in the registry
	Type() string
}

Rule to assert on string values.

func ParseCharset

func ParseCharset(data map[string]interface{}) (rule Rule, err error)

ParseCharset from the provided data map. The data map is expected to be parsed from HCL.

type StringGenerator

type StringGenerator struct {
	// Length of the string to generate.
	Length int `mapstructure:"length" json:"length"`

	// Rules the generated strings must adhere to.
	Rules serializableRules `mapstructure:"-" json:"rule"` // This is "rule" in JSON so it matches the HCL property type
	// contains filtered or unexported fields
}

StringGenerator generates random strings from the provided charset & adhering to a set of rules. The set of rules are things like CharsetRule which requires a certain number of characters from a sub-charset.

func ParsePolicy

func ParsePolicy(raw string) (gen StringGenerator, err error)

ParsePolicy is a convenience function for parsing HCL into a StringGenerator. See PolicyParser.ParsePolicy for details.

func ParsePolicyBytes

func ParsePolicyBytes(raw []byte) (gen StringGenerator, err error)

ParsePolicyBytes is a convenience function for parsing HCL into a StringGenerator. See PolicyParser.ParsePolicy for details.

func (*StringGenerator) Generate

func (g *StringGenerator) Generate(ctx context.Context, rng io.Reader) (str string, err error)

Generate a random string from the charset and adhering to the provided rules. The io.Reader is optional. If not provided, it will default to the reader from crypto/rand

Jump to

Keyboard shortcuts

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