Files
vault/sdk/helper/random/registry.go
Michael Golowka ad90e0b39d Add user configurable password policies available to secret engines (#8637)
* Add random string generator with rules engine

This adds a random string generation library that validates random
strings against a set of rules. The library is designed for use as generating
passwords, but can be used to generate any random strings.
2020-05-27 12:28:00 -06:00

36 lines
891 B
Go

package random
import (
"fmt"
)
type ruleConstructor func(map[string]interface{}) (Rule, error)
var (
// defaultRuleNameMapping is the default mapping of HCL rule names to the appropriate rule constructor.
// Add to this map when adding a new Rule type to be recognized in HCL.
defaultRuleNameMapping = map[string]ruleConstructor{
"charset": ParseCharset,
}
defaultRegistry = Registry{
Rules: defaultRuleNameMapping,
}
)
// Registry of HCL rule names to rule constructors.
type Registry struct {
// Rules maps names of rules to a constructor for the rule
Rules map[string]ruleConstructor
}
func (r Registry) parseRule(ruleType string, ruleData map[string]interface{}) (rule Rule, err error) {
constructor, exists := r.Rules[ruleType]
if !exists {
return nil, fmt.Errorf("unrecognized rule type %s", ruleType)
}
rule, err = constructor(ruleData)
return rule, err
}