package device import ( "encoding/gob" "encoding/json" "os" ) func init() { gob.Register(LoginPolicy{}) gob.Register(FieldPolicy{}) } type LoginPolicy struct { Username FieldPolicy `json:"username"` Password FieldPolicy `json:"password"` } type FieldPolicy struct { MinLength int `json:"min_len"` MaxLength int `json:"max_len"` NumericOnly bool `json:"num_only"` } func GetLoginPolicy() (LoginPolicy, error) { policyJSON := getLoginPolicyJSON() var devicePolicy LoginPolicy err := json.Unmarshal([]byte(policyJSON), &devicePolicy) if err != nil { return LoginPolicy{}, err } return devicePolicy, nil } func getLoginPolicyJSON() string { defaultPolicy := `{"username":{"min_len":1,"max_len":12,"num_only":false}, "password":{"min_len":1,"max_len":12,"num_only":false}}` fromEnv, exist := os.LookupEnv("IAM_DEVICE_POLICY") if !exist { return defaultPolicy } return fromEnv }