auth

Paddy 2015-03-24 Parent:d103a598548c Child:3e8964a914ef

152:de5e09680f6b Go to Latest

auth/scope.go

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.

History
1 package auth
3 import (
4 "errors"
5 "fmt"
6 "sort"
7 )
9 var (
10 ErrNoScopeStore = errors.New("scopeStore not set in Context")
11 )
13 type ErrScopeNotFound struct {
14 Pos int
15 ID string
16 }
18 func (e ErrScopeNotFound) Error() string {
19 return fmt.Sprintf("scope %s couldn't be found", e.ID)
20 }
22 type ErrScopeAlreadyExists struct {
23 Pos int
24 ID string
25 }
27 func (e ErrScopeAlreadyExists) Error() string {
28 return fmt.Sprintf("scope %s already exists", e.ID)
29 }
31 // Scope represents a limit on the access that a grant provides.
32 type Scope struct {
33 ID string
34 Name string
35 Description string
36 }
38 func (s *Scope) ApplyChange(change ScopeChange) {
39 if change.Name != nil {
40 s.Name = *change.Name
41 }
42 if change.Description != nil {
43 s.Description = *change.Description
44 }
45 }
47 type sortedScopes []Scope
49 func (s sortedScopes) Len() int {
50 return len(s)
51 }
53 func (s sortedScopes) Swap(i, j int) {
54 s[i], s[j] = s[j], s[i]
55 }
57 func (s sortedScopes) Less(i, j int) bool {
58 return s[i].ID < s[j].ID
59 }
61 // ScopeChange represents a change to a Scope.
62 type ScopeChange struct {
63 Name *string
64 Description *string
65 }
67 func (s ScopeChange) Empty() bool {
68 return s.Name == nil && s.Description == nil
69 }
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)
77 }
79 func (m *memstore) createScopes(scopes []Scope) error {
80 m.scopeLock.Lock()
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}
86 }
87 }
88 for _, scope := range scopes {
89 m.scopes[scope.ID] = scope
90 }
91 return nil
92 }
94 func (m *memstore) getScopes(ids []string) ([]Scope, error) {
95 m.scopeLock.RLock()
96 defer m.scopeLock.RUnlock()
98 scopes := []Scope{}
99 for pos, id := range ids {
100 scope, ok := m.scopes[id]
101 if !ok {
102 return []Scope{}, ErrScopeNotFound{ID: id, Pos: pos}
103 }
104 scopes = append(scopes, scope)
105 }
106 sorted := sortedScopes(scopes)
107 sort.Sort(sorted)
108 scopes = sorted
109 return scopes, nil
110 }
112 func (m *memstore) updateScope(id string, change ScopeChange) error {
113 m.scopeLock.Lock()
114 defer m.scopeLock.Unlock()
116 scope, ok := m.scopes[id]
117 if !ok {
118 return ErrScopeNotFound{Pos: 0, ID: id}
119 }
120 scope.ApplyChange(change)
121 m.scopes[id] = scope
122 return nil
123 }
125 func (m *memstore) removeScopes(ids []string) error {
126 m.scopeLock.Lock()
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}
132 }
133 }
134 for _, id := range ids {
135 delete(m.scopes, id)
136 }
137 return nil
138 }
140 func (m *memstore) listScopes() ([]Scope, error) {
141 m.scopeLock.RLock()
142 defer m.scopeLock.RUnlock()
144 scopes := []Scope{}
145 for _, scope := range m.scopes {
146 scopes = append(scopes, scope)
147 }
148 sorted := sortedScopes(scopes)
149 sort.Sort(sorted)
150 scopes = sorted
151 return scopes, nil
152 }