From 7858da565fa301495a3e714b02388b9bc9957d6a Mon Sep 17 00:00:00 2001 From: Eric Chiang Date: Mon, 11 Apr 2016 16:15:44 -0700 Subject: [PATCH] server: add more validation to --no-db static file parsing In #393 the format of the static user file in --no-db mode changed. However, the old format loads without error, which has caused issues for developers with existing user files. Add an explicit check to ensure the file is not using the old format. If they are, print a better error message. --- server/config.go | 8 ++++++++ server/config_test.go | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/server/config.go b/server/config.go index 24e45000..50df485c 100644 --- a/server/config.go +++ b/server/config.go @@ -184,6 +184,10 @@ func loadUsersFromReader(r io.Reader) (users []user.UserWithRemoteIdentities, pw user.User Password string `json:"password"` 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 { return nil, nil, err @@ -193,6 +197,10 @@ func loadUsersFromReader(r io.Reader) (users []user.UserWithRemoteIdentities, pw pwis = make([]user.PasswordInfo, len(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{ User: u.User, RemoteIdentities: u.RemoteIdentities, diff --git a/server/config_test.go b/server/config_test.go index 70ad677d..1f9637ed 100644 --- a/server/config_test.go +++ b/server/config_test.go @@ -15,6 +15,8 @@ func TestLoadUsers(t *testing.T) { expUsers []user.UserWithRemoteIdentities // userid -> plaintext password expPasswds map[string]string + + wantErr bool }{ { raw: `[ @@ -50,13 +52,39 @@ func TestLoadUsers(t *testing.T) { "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 { users, pwInfos, err := loadUsersFromReader(strings.NewReader(tt.raw)) if err != nil { - t.Errorf("case %d: failed to load user: %v", i, err) - return + if !tt.wantErr { + t.Errorf("case %d: failed to load user: %v", i, err) + } + 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 != "" {