package asaModels import ( "fmt" "regexp" "strings" "strconv" "github.com/sirupsen/logrus" "github.rackspace.com/gorax/tools/util" ) var ( userNameRegex = regexp.MustCompile(`(?i)username\s+(\S+)\s+password\s+(\S+)\s+(pbkdf2|encrypted)(\s+privilege\s+(\S+)|)`) userServiceTypeRegex = regexp.MustCompile(`(?i)service-type\s+(\S+)`) userGroupLockRegex = regexp.MustCompile(`(?i)group-lock\s+value\s+(\S+)`) ) type ( UserNames struct { UserNames []*UserName `json:"user_names"` } UserName struct { Name *string `json:"name"` Password *string `json:"password"` EncryptionMethod string `json:"encryption_method"` Encrypted bool `json:"encrypted"` Privilege *int `json:"privilege"` Attributes *UserAttributes `json:"attributes"` } UserAttributes struct { ServiceType *string `json:"service-type"` GroupLock *string `json:"group-lock"` } ) func (u *UserNames) Parse(s string) { match := userNameRegex.FindAllStringSubmatch(s, -1) if len(match) == 0 { logrus.Warn("no matches found for userNameRegex.FindAllStringSubmatch on: ", s) return } for _, un := range match { if un[1] == "" { continue } un[1] = strings.Replace(un[1], `\`, `\\`, -1) UserRe := regexp.MustCompile(fmt.Sprintf(`(?i)username\s+%s\s+attributes\n((.|\n)*?)(\n\S|$)`, un[1])) UserM := UserRe.FindStringSubmatch(s) u.UserNames = append(u.UserNames, &UserName{ Name: &un[1], Password: &un[2], Encrypted: func() bool { if un[3] == "encrypted" || un[3] == "pbkdf2" { return true } return false }(), EncryptionMethod: un[3], Privilege: func() *int { i, _ := strconv.Atoi(un[5]) if i == 0 { return nil } return &i }(), Attributes: func() *UserAttributes { if len(UserM) == 0 { return nil } return &UserAttributes{ ServiceType: util.RegexpFindStringPtr(userServiceTypeRegex, UserM[1]), GroupLock: util.RegexpFindStringPtr(userGroupLockRegex, UserM[1]), } }(), }) } } func (u *UserNames) NonPrivilegedUsers(pw bool) []*UserName { return NonPrivilegedUsers(u.UserNames, pw) } func NonPrivilegedUsers(ul []*UserName, pw bool) (nl []*UserName) { for _, u := range ul { if u.Privilege == nil { if pw == false { nl = append(nl, u) continue } nl = append(nl, u) } } return }