Implement postgres version of scopeStore.
Update the authd server to use postgres as its scopeStore, instead of memstore.
panic when starting the authd server if the CreateScopes call fails. This
should, ideally, ignore ErrScopeAlreadyExists errors, but does not as of this
commit.
Update the simple.gotmpl template to properly display scopes, after switching to
the Scope type instead of simply passing around the string the client supplied
broke the template and I never bothered fixing it.
Update the updateScopes method on the scopeStore (and the corresponding
UpdateScopes method on the Context type) to be updateScope/UpdateScope.
Operating on several scopes at a time like that is simply too challenging in
SQL and I can't justify the complexity with a use case.
Add a helper method to ScopeChange called Empty(), which returns true if the
ScopeChange is full of nil values.
Remove the ID from the ScopeChange type, because we're no longer accepting
multiple ScopeChange types in UpdateScope, so we can supply that information
outside the ScopeChange, which matches the rest of our update* methods.
Correct our tests in scope_test.go to correctly use the updateScope method
instead of the old updateScopes method. This generally just resulted in calling
updateScope multiple times, as opposed to just once.
Add a scope table initialization to the sql/postgres_init.sql script.
10 ErrNoScopeStore = errors.New("scopeStore not set in Context")
13 type ErrScopeNotFound struct {
18 func (e ErrScopeNotFound) Error() string {
19 return fmt.Sprintf("scope %s couldn't be found", e.ID)
22 type ErrScopeAlreadyExists struct {
27 func (e ErrScopeAlreadyExists) Error() string {
28 return fmt.Sprintf("scope %s already exists", e.ID)
31 // Scope represents a limit on the access that a grant provides.
38 func (s *Scope) ApplyChange(change ScopeChange) {
39 if change.Name != nil {
42 if change.Description != nil {
43 s.Description = *change.Description
47 type sortedScopes []Scope
49 func (s sortedScopes) Len() int {
53 func (s sortedScopes) Swap(i, j int) {
54 s[i], s[j] = s[j], s[i]
57 func (s sortedScopes) Less(i, j int) bool {
58 return s[i].ID < s[j].ID
61 // ScopeChange represents a change to a Scope.
62 type ScopeChange struct {
67 func (s ScopeChange) Empty() bool {
68 return s.Name == nil && s.Description == nil
71 type scopeStore interface {
72 createScopes(scopes []Scope) error
73 getScopes(ids []string) ([]Scope, error)
74 updateScope(id string, change ScopeChange) error
75 removeScopes(ids []string) error
76 listScopes() ([]Scope, error)
79 func (m *memstore) createScopes(scopes []Scope) error {
81 defer m.scopeLock.Unlock()
83 for pos, scope := range scopes {
84 if _, ok := m.scopes[scope.ID]; ok {
85 return ErrScopeAlreadyExists{Pos: pos, ID: scope.ID}
88 for _, scope := range scopes {
89 m.scopes[scope.ID] = scope
94 func (m *memstore) getScopes(ids []string) ([]Scope, error) {
96 defer m.scopeLock.RUnlock()
99 for pos, id := range ids {
100 scope, ok := m.scopes[id]
102 return []Scope{}, ErrScopeNotFound{ID: id, Pos: pos}
104 scopes = append(scopes, scope)
106 sorted := sortedScopes(scopes)
112 func (m *memstore) updateScope(id string, change ScopeChange) error {
114 defer m.scopeLock.Unlock()
116 scope, ok := m.scopes[id]
118 return ErrScopeNotFound{Pos: 0, ID: id}
120 scope.ApplyChange(change)
125 func (m *memstore) removeScopes(ids []string) error {
127 defer m.scopeLock.Unlock()
129 for pos, id := range ids {
130 if _, ok := m.scopes[id]; !ok {
131 return ErrScopeNotFound{Pos: pos, ID: id}
134 for _, id := range ids {
140 func (m *memstore) listScopes() ([]Scope, error) {
142 defer m.scopeLock.RUnlock()
145 for _, scope := range m.scopes {
146 scopes = append(scopes, scope)
148 sorted := sortedScopes(scopes)