Merge pull request #402 from ericchiang/static_users

server: add more validation to --no-db static file parsing
This commit is contained in:
Eric Chiang 2016-04-13 09:39:27 -07:00
commit 804d06c44e
2 changed files with 38 additions and 2 deletions

View file

@ -184,6 +184,10 @@ func loadUsersFromReader(r io.Reader) (users []user.UserWithRemoteIdentities, pw
user.User user.User
Password string `json:"password"` Password string `json:"password"`
RemoteIdentities []user.RemoteIdentity `json:"remoteIdentities"` RemoteIdentities []user.RemoteIdentity `json:"remoteIdentities"`
// The old format stored all user data under the "user" key.
// Attempt to detect that, and print an better error.
OldUserFields map[string]string `json:"user"`
} }
if err := json.NewDecoder(r).Decode(&configUsers); err != nil { if err := json.NewDecoder(r).Decode(&configUsers); err != nil {
return nil, nil, err return nil, nil, err
@ -193,6 +197,10 @@ func loadUsersFromReader(r io.Reader) (users []user.UserWithRemoteIdentities, pw
pwis = make([]user.PasswordInfo, len(configUsers)) pwis = make([]user.PasswordInfo, len(configUsers))
for i, u := range configUsers { for i, u := range configUsers {
if u.OldUserFields != nil {
return nil, nil, fmt.Errorf("Static user file is using an outdated format. Please refer to example in static/fixtures.")
}
users[i] = user.UserWithRemoteIdentities{ users[i] = user.UserWithRemoteIdentities{
User: u.User, User: u.User,
RemoteIdentities: u.RemoteIdentities, RemoteIdentities: u.RemoteIdentities,

View file

@ -15,6 +15,8 @@ func TestLoadUsers(t *testing.T) {
expUsers []user.UserWithRemoteIdentities expUsers []user.UserWithRemoteIdentities
// userid -> plaintext password // userid -> plaintext password
expPasswds map[string]string expPasswds map[string]string
wantErr bool
}{ }{
{ {
raw: `[ raw: `[
@ -50,13 +52,39 @@ func TestLoadUsers(t *testing.T) {
"elroy-id": "bones", "elroy-id": "bones",
}, },
}, },
{
// using old format.
raw: `[
{
"user": {
"id": "elroy-id",
"email": "elroy77@example.com",
"displayName": "Elroy Jonez",
"password": "bones"
},
"remoteIdentities": [
{
"connectorId": "local",
"id": "elroy-id"
}
]
}
]`,
wantErr: true,
},
} }
for i, tt := range tests { for i, tt := range tests {
users, pwInfos, err := loadUsersFromReader(strings.NewReader(tt.raw)) users, pwInfos, err := loadUsersFromReader(strings.NewReader(tt.raw))
if err != nil { if err != nil {
if !tt.wantErr {
t.Errorf("case %d: failed to load user: %v", i, err) t.Errorf("case %d: failed to load user: %v", i, err)
return }
continue
}
if tt.wantErr {
t.Errorf("case %d: wanted parsing error, didn't get one", i)
continue
} }
if diff := pretty.Compare(tt.expUsers, users); diff != "" { if diff := pretty.Compare(tt.expUsers, users); diff != "" {