Skip to content

Commit

Permalink
Add CreatedAt and UpdatedAt field to IAM user
Browse files Browse the repository at this point in the history
  • Loading branch information
ioppermann committed Sep 18, 2023
1 parent f31cb8e commit 92f2f6d
Show file tree
Hide file tree
Showing 27 changed files with 3,912 additions and 3,145 deletions.
2 changes: 1 addition & 1 deletion app/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (v versionInfo) MinorString() string {
// Version of the app
var Version = versionInfo{
Major: 16,
Minor: 15,
Minor: 16,
Patch: 0,
}

Expand Down
6 changes: 6 additions & 0 deletions cluster/docs/ClusterAPI_docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions cluster/docs/ClusterAPI_swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1995,11 +1995,17 @@
"auth": {
"$ref": "#/definitions/identity.UserAuth"
},
"created_at": {
"type": "string"
},
"name": {
"type": "string"
},
"superuser": {
"type": "boolean"
},
"updated_at": {
"type": "string"
}
}
},
Expand Down
4 changes: 4 additions & 0 deletions cluster/docs/ClusterAPI_swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -587,10 +587,14 @@ definitions:
type: string
auth:
$ref: '#/definitions/identity.UserAuth'
created_at:
type: string
name:
type: string
superuser:
type: boolean
updated_at:
type: string
type: object
identity.UserAuth:
properties:
Expand Down
127 changes: 127 additions & 0 deletions cluster/store/identity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package store

import (
"fmt"
"time"
)

func (s *store) addIdentity(cmd CommandAddIdentity) error {
s.lock.Lock()
defer s.lock.Unlock()

err := s.data.Users.userlist.Add(cmd.Identity)
if err != nil {
return fmt.Errorf("the identity with the name '%s' already exists", cmd.Identity.Name)
}

now := time.Now()

s.data.Users.UpdatedAt = now

cmd.Identity.CreatedAt = now
cmd.Identity.UpdatedAt = now
s.data.Users.Users[cmd.Identity.Name] = cmd.Identity

return nil
}

func (s *store) updateIdentity(cmd CommandUpdateIdentity) error {
s.lock.Lock()
defer s.lock.Unlock()

if cmd.Name == "$anon" {
return fmt.Errorf("the identity with the name '%s' can't be updated", cmd.Name)
}

oldUser, err := s.data.Users.userlist.Get(cmd.Name)
if err != nil {
return fmt.Errorf("the identity with the name '%s' doesn't exist", cmd.Name)
}

o, ok := s.data.Users.Users[oldUser.Name]
if !ok {
return fmt.Errorf("the identity with the name '%s' doesn't exist", cmd.Name)
}

err = s.data.Users.userlist.Update(cmd.Name, cmd.Identity)
if err != nil {
return err
}

user, err := s.data.Users.userlist.Get(cmd.Identity.Name)
if err != nil {
return fmt.Errorf("the identity with the name '%s' doesn't exist", cmd.Identity.Name)
}

now := time.Now()

user.CreatedAt = o.CreatedAt
user.UpdatedAt = now

s.data.Users.UpdatedAt = now
delete(s.data.Users.Users, oldUser.Name)
s.data.Users.Users[user.Name] = user

s.data.Policies.UpdatedAt = now
policies := s.data.Policies.Policies[oldUser.Name]
delete(s.data.Policies.Policies, oldUser.Name)
s.data.Policies.Policies[user.Name] = policies

return nil
}

func (s *store) removeIdentity(cmd CommandRemoveIdentity) error {
s.lock.Lock()
defer s.lock.Unlock()

user, err := s.data.Users.userlist.Get(cmd.Name)
if err != nil {
return nil
}

s.data.Users.userlist.Delete(user.Name)

delete(s.data.Users.Users, user.Name)
s.data.Users.UpdatedAt = time.Now()
delete(s.data.Policies.Policies, user.Name)
s.data.Policies.UpdatedAt = time.Now()

return nil
}

func (s *store) ListUsers() Users {
s.lock.RLock()
defer s.lock.RUnlock()

u := Users{
UpdatedAt: s.data.Users.UpdatedAt,
}

for _, user := range s.data.Users.Users {
u.Users = append(u.Users, user)
}

return u
}

func (s *store) GetUser(name string) Users {
s.lock.RLock()
defer s.lock.RUnlock()

u := Users{
UpdatedAt: s.data.Users.UpdatedAt,
}

user, err := s.data.Users.userlist.Get(name)
if err != nil {
return u
}

u.UpdatedAt = user.UpdatedAt

if user, ok := s.data.Users.Users[user.Name]; ok {
u.Users = append(u.Users, user)
}

return u
}
Loading

0 comments on commit 92f2f6d

Please sign in to comment.