Skip to content

Commit

Permalink
Revert to InitRobotUsers.
Browse files Browse the repository at this point in the history
We cannot properly sync robot users right now, since we don't have
any information about the source of a given user, therefore cannot
distinguish between users coming from the manifest and other sources
when deleteting them. Therefore, we can only add users, which we
would do anyway by calling initUser anew (the code that creates users
in the database already does the diff between the existing users in
the DB and those in the manifest.)
  • Loading branch information
alexeyklyukin committed Nov 3, 2017
1 parent 79288be commit 5c58c8e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 51 deletions.
36 changes: 12 additions & 24 deletions pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (c *Cluster) initUsers() error {
return fmt.Errorf("could not init infrastructure roles: %v", err)
}

if err := c.syncRobotUsers(); err != nil {
if err := c.initRobotUsers(); err != nil {
return fmt.Errorf("could not init robot users: %v", err)
}

Expand Down Expand Up @@ -606,36 +606,24 @@ func (c *Cluster) initSystemUsers() {
}
}

// syncRobotUsers syncs adds users from the operator Spec
// to the list of users known to the operator.
func (c *Cluster) syncRobotUsers() error {
for newUsername, newUserFlags := range c.Spec.Users {
if !isValidUsername(newUsername) {
return fmt.Errorf("invalid username: %q", newUsername)
func (c *Cluster) initRobotUsers() error {
for username, userFlags := range c.Spec.Users {
if !isValidUsername(username) {
return fmt.Errorf("invalid username: '%v'", username)
}

newUserFlagsNormalized, err := normalizeUserFlags(newUserFlags)
flags, err := normalizeUserFlags(userFlags)
if err != nil {
return fmt.Errorf("invalid flags for user %q: %v", newUsername, err)
return fmt.Errorf("invalid flags for user '%v': %v", username, err)
}

if _, ok := c.pgUsers[newUsername]; !ok {
c.pgUsers[newUsername] = spec.PgUser{
Name: newUsername,
Password: util.RandomPassword(constants.PasswordLength),
Flags: newUserFlagsNormalized,
}
} else {
existingUser := c.pgUsers[newUsername]
deletedFlags, addedFlags := util.SymmetricDifferenceStringSlices(existingUser.Flags, newUserFlagsNormalized)
if len(deletedFlags)+len(addedFlags) == 0 {
continue
}
existingUser.Flags = newUserFlagsNormalized
c.logger.Infof("changed flags of user %q, deleted: %v, added: %v",
newUsername, deletedFlags, addedFlags)
c.pgUsers[username] = spec.PgUser{
Name: username,
Password: util.RandomPassword(constants.PasswordLength),
Flags: flags,
}
}

return nil
}

Expand Down
27 changes: 0 additions & 27 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,33 +77,6 @@ OUTER:
return result, len(result) == 0
}

// SymmetricDifferenceStringSlices finds elements in the first slice that are not in the second one and vice versa.
func SymmetricDifferenceStringSlices(listA []string, listB []string) (onlyInFirst []string, onlyInSecond []string) {
mapA := make(map[string]bool)
mapB := make(map[string]bool)

onlyInFirst = []string{}
onlyInSecond = []string{}

for _, val := range listA {
mapA[val] = true
}
for _, val := range listB {
if _, ok := mapA[val]; !ok {
mapB[val] = true
} else {
delete(mapA, val)
}
}
for key := range mapA {
onlyInFirst = append(onlyInFirst, key)
}
for key := range mapB {
onlyInSecond = append(onlyInSecond, key)
}
return
}

// FindNamedStringSubmatch returns a map of strings holding the text of the matches of the r regular expression
func FindNamedStringSubmatch(r *regexp.Regexp, s string) map[string]string {
matches := r.FindStringSubmatch(s)
Expand Down

0 comments on commit 5c58c8e

Please sign in to comment.