-
Notifications
You must be signed in to change notification settings - Fork 43
/
file.go
182 lines (151 loc) · 4.17 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package clientsauth
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"sort"
"time"
"github.com/patrickmn/go-cache"
"github.com/openrport/openrport/share/enums"
"github.com/openrport/openrport/share/query"
)
// FileProvider is file based client provider.
// It is not thread save so should be surrounded by CachedProvider.
type FileProvider struct {
fileName string
cache *cache.Cache
}
var _ Provider = &FileProvider{}
func NewFileProvider(fileName string, cache *cache.Cache) *FileProvider {
return &FileProvider{
fileName: fileName,
cache: cache,
}
}
// GetAll returns rport clients auth credentials from a given file.
func (c *FileProvider) getAll() ([]*ClientAuth, error) {
idPswdPairs, err := c.load()
if err != nil {
return nil, fmt.Errorf("failed to decode rport clients auth file: %v", err)
}
var res []*ClientAuth
for id, pswd := range idPswdPairs {
if id == "" || pswd == "" {
return nil, errors.New("empty client auth ID or password is not allowed")
}
res = append(res, &ClientAuth{ID: id, Password: pswd})
}
return res, nil
}
func (c *FileProvider) GetFiltered(filter *query.ListOptions) ([]*ClientAuth, int, error) {
var ca []*ClientAuth
ca, err := c.getAll()
if err != nil {
return nil, 0, err
}
if len(filter.Filters) > 0 {
var filtered = []*ClientAuth{}
for _, v := range ca {
match, err := query.MatchesFilters(v, filter.Filters)
if err != nil {
return nil, 0, err
}
if match {
filtered = append(filtered, &ClientAuth{v.ID, v.Password})
}
}
ca = filtered
}
c.SortByID(ca, false)
l := len(ca)
start, end := filter.Pagination.GetStartEnd(l)
return ca[start:end], l, nil
}
func (c *FileProvider) Get(id string) (*ClientAuth, error) {
if val, _ := c.cache.Get(c.CacheKey(id)); val != nil {
return val.(*ClientAuth), nil
}
idPswdPairs, err := c.load()
if err != nil {
return nil, fmt.Errorf("failed to decode rport clients auth file: %v", err)
}
if _, ok := idPswdPairs[id]; ok {
ca := &ClientAuth{ID: id, Password: idPswdPairs[id]}
if err := c.cache.Add(c.CacheKey(id), ca, 60*time.Minute); err != nil {
return nil, err
}
return ca, nil
}
return nil, nil
}
func (c *FileProvider) Add(clientAuth *ClientAuth) (bool, error) {
idPswdPairs, err := c.load()
if err != nil {
return false, fmt.Errorf("failed to decode rport clients auth file: %v", err)
}
clientID := clientAuth.ID
if _, ok := idPswdPairs[clientID]; ok {
return false, nil
}
idPswdPairs[clientID] = clientAuth.Password
if err := c.save(idPswdPairs); err != nil {
return false, fmt.Errorf("failed to encode rport clients auth file: %v", err)
}
return true, nil
}
func (c *FileProvider) Delete(id string) error {
idPswdPairs, err := c.load()
if err != nil {
return fmt.Errorf("failed to decode rport clients auth file: %v", err)
}
delete(idPswdPairs, id)
c.cache.Delete(c.CacheKey(id))
if err := c.save(idPswdPairs); err != nil {
return fmt.Errorf("failed to encode rport clients auth file: %v", err)
}
return nil
}
func (c *FileProvider) IsWriteable() bool {
return true
}
func (c *FileProvider) load() (map[string]string, error) {
b, err := ioutil.ReadFile(c.fileName)
if err != nil {
return nil, fmt.Errorf("failed to read rport clients auth file %q: %s", c.fileName, err)
}
var idPswdPairs map[string]string
if err := json.Unmarshal(b, &idPswdPairs); err != nil {
return nil, err
}
return idPswdPairs, nil
}
func (c *FileProvider) save(idPswdPairs map[string]string) error {
file, err := os.OpenFile(c.fileName, os.O_RDWR|os.O_TRUNC, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to open rport clients auth file: %v", err)
}
defer file.Close()
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
if err := encoder.Encode(idPswdPairs); err != nil {
return fmt.Errorf("failed to write rport clients auth: %v", err)
}
return nil
}
func (c *FileProvider) Source() enums.ProviderSource {
return enums.ProviderSourceFile
}
func (c *FileProvider) SortByID(a []*ClientAuth, desc bool) {
sort.Slice(a, func(i, j int) bool {
less := a[i].ID < a[j].ID
if desc {
return !less
}
return less
})
}
func (c *FileProvider) CacheKey(id string) string {
return "client-auth-" + id
}