Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove principal + Allow updates to built-in users + Fix ccn/issues/66, ccn/issues/68 #44

Merged
merged 3 commits into from
Dec 14, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions common/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,17 @@ func Role(roleStr string) (RoleType, error) {
//
// Fields:
// UserName: of the user. Read only field. Must be unique.
// FirstName: of the user
// LastName: of the user
// Password: of the user. Not stored anywhere. Used only for updates.
// Disable: if authorizations for this local user is disabled.
// PasswordHash: of the password string.
//
type LocalUser struct {
Username string `json:"username"`
Password string `json:"password,omitempty"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Disable bool `json:"disable"`
PasswordHash []byte `json:"password_hash,omitempty"`
}
Expand Down
1 change: 1 addition & 0 deletions db/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ func AddDefaultUsers() error {
// default user accounts are `enabled` always; it cannot be disabled
Disable: false,
Password: userR.String(),
// FirstName, LastName = "" for built-in users
}

err := AddLocalUser(&localUser)
Expand Down
99 changes: 68 additions & 31 deletions db/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,28 @@ type dbSuite struct{}
var _ = Suite(&dbSuite{})

var (
newUsers = []types.LocalUser{
{
Username: "aaa",
FirstName: "Temp",
LastName: "User",
Disable: true,
},
{
Username: "bbb",
FirstName: "Temp",
LastName: "User",
Disable: true,
},
{
Username: "ccc",
FirstName: "Temp",
LastName: "User",
Disable: true,
},
}

invalidUsers = []string{"xxx", "yyy", "zzz"}
newUsers = []string{"aaa", "bbb", "ccc"}
builtInUsers = []string{types.Admin.String(), types.Ops.String()}
datastore = ""
datastorePaths = []string{
Expand Down Expand Up @@ -87,7 +107,6 @@ func (s *dbSuite) TestGetLocalUser(c *C) {

c.Assert(user.Username, Equals, username)
c.Assert(user.Password, Equals, "")
c.Assert(user.PasswordHash, DeepEquals, []byte{})
}

// invalid users
Expand All @@ -102,18 +121,12 @@ func (s *dbSuite) TestGetLocalUser(c *C) {
// TestAddLocalUser tests `AddLocalUser(...)`
func (s *dbSuite) TestAddLocalUser(c *C) {
// add new users
for _, username := range newUsers {
user := &types.LocalUser{
Username: username,
Disable: false,
Password: username,
}

err := AddLocalUser(user)
for _, user := range newUsers {
err := AddLocalUser(&user)
c.Assert(err, IsNil)

// add existing usernames and check for error
err = AddLocalUser(user)
err = AddLocalUser(&user)
c.Assert(err, Equals, ccnerrors.ErrKeyExists)
}

Expand All @@ -131,12 +144,12 @@ func (s *dbSuite) TestDeleteLocalUser(c *C) {
}

// delete the added new users
for _, username := range newUsers {
err := DeleteLocalUser(username)
for _, user := range newUsers {
err := DeleteLocalUser(user.Username)
c.Assert(err, IsNil)

// delete the same user again
err = DeleteLocalUser(username)
err = DeleteLocalUser(user.Username)
c.Assert(err, Equals, ccnerrors.ErrKeyNotFound)
}
}
Expand All @@ -145,64 +158,88 @@ func (s *dbSuite) TestDeleteLocalUser(c *C) {
func (s *dbSuite) TestUpdateLocalUser(c *C) {
s.TestAddLocalUser(c)

for _, username := range newUsers {
user, err := GetLocalUser(username)
for _, user := range newUsers {
uUser, err := GetLocalUser(user.Username)
c.Assert(err, IsNil)

// change the username and update
user.Username = username + "_u"
err = UpdateLocalUser(username, user)
newObj := &types.LocalUser{
Username: user.Username + "_u",
PasswordHash: uUser.PasswordHash,
Disable: uUser.Disable,
FirstName: uUser.FirstName,
LastName: uUser.LastName,
}

err = UpdateLocalUser(user.Username, newObj)
c.Assert(err, IsNil)
newObj.PasswordHash = uUser.PasswordHash

// search the data store for old username
oldUser, err := GetLocalUser(username)
oldUser, err := GetLocalUser(user.Username)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see earlier comments. username should not be updatable, specifically since it might have authorizations against it.

c.Assert(err, Equals, ccnerrors.ErrKeyNotFound)
c.Assert(oldUser, IsNil)

// search the data store for new username
newUser, err := GetLocalUser(username + "_u")
newUser, err := GetLocalUser(user.Username + "_u")
c.Assert(err, IsNil)
c.Assert(newUser, DeepEquals, user)
c.Assert(newUser, DeepEquals, newObj)
}

// revert the changes
for _, username := range newUsers {
updateTo := username
username = username + "_u"
for _, user := range newUsers {
updateTo := user.Username
user.Username = user.Username + "_u"

user, err := GetLocalUser(username)
uUser, err := GetLocalUser(user.Username)
c.Assert(err, IsNil)

// change the username and update
user.Username = updateTo
err = UpdateLocalUser(username, user)
newObj := &types.LocalUser{
Username: updateTo,
PasswordHash: uUser.PasswordHash,
Disable: uUser.Disable,
FirstName: uUser.FirstName,
LastName: uUser.LastName,
}
err = UpdateLocalUser(user.Username, newObj)
c.Assert(err, IsNil)
newObj.PasswordHash = uUser.PasswordHash

// search the data store for old username
oldUser, err := GetLocalUser(username)
oldUser, err := GetLocalUser(user.Username)
c.Assert(err, Equals, ccnerrors.ErrKeyNotFound)
c.Assert(oldUser, IsNil)

newUser, err := GetLocalUser(updateTo)
c.Assert(err, IsNil)
c.Assert(newUser, DeepEquals, user)
c.Assert(newUser, DeepEquals, newObj)
}
}

// TestGetLocalUsers tests `GetLocalUsers(...)`
func (s *dbSuite) TestGetLocalUsers(c *C) {
users, err := GetLocalUsers()
c.Assert(err, IsNil)
c.Assert(users, DeepEquals, []*types.LocalUser{})

s.TestAddLocalUser(c)
s.addBuiltInUsers(c)

users, err := GetLocalUsers()
users, err = GetLocalUsers()
c.Assert(err, IsNil)

usernames := []string{}
for _, user := range users {
usernames = append(usernames, user.Username)
}

allUsers := append(newUsers, builtInUsers...)
newUserNames := []string{}
for _, user := range newUsers {
newUserNames = append(newUserNames, user.Username)
}

allUsers := append(newUserNames, builtInUsers...)
sort.Strings(usernames)
sort.Strings(allUsers)

Expand Down
18 changes: 16 additions & 2 deletions proxy/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,10 @@ func getLocalUsersHelper() (int, []byte) {
localUsers := []types.LocalUser{}
for _, user := range users {
lu := types.LocalUser{
Username: user.Username,
Disable: user.Disable,
Username: user.Username,
FirstName: user.FirstName,
LastName: user.LastName,
Disable: user.Disable,
}

localUsers = append(localUsers, lu)
Expand All @@ -272,11 +274,23 @@ func updateLocalUserInfo(username string, updateReq *types.LocalUser, actual *ty
updatedUserObj := &types.LocalUser{
// username == actual.Username
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of this comment? It doesn't seem to communicate anything different than the line below it... if it's legitimately trying to communicate something non-obvious, writing it as a normal sentence would be more clear

Username: actual.Username,
FirstName: actual.FirstName,
LastName: actual.LastName,
Disable: actual.Disable,
PasswordHash: actual.PasswordHash,
// `Password` will be empty
}

// Update `first_name`
if !common.IsEmpty(updateReq.FirstName) {
updatedUserObj.FirstName = updateReq.FirstName
}

// Update `last_name`
if !common.IsEmpty(updateReq.LastName) {
updatedUserObj.LastName = updateReq.LastName
}

// Update `disable`
if actual.Disable != updateReq.Disable {
updatedUserObj.Disable = updateReq.Disable
Expand Down