-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock.go
198 lines (169 loc) · 4.55 KB
/
mock.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// +build !prod
package mock
import (
"context"
"fmt"
"time"
"go.uber.org/zap"
"github.com/chef/automate/components/authn-service/constants"
pg "github.com/chef/automate/components/authn-service/tokens/pg"
tokens "github.com/chef/automate/components/authn-service/tokens/types"
tutil "github.com/chef/automate/components/authn-service/tokens/util"
"github.com/chef/automate/lib/tls/certs"
uuid "github.com/chef/automate/lib/uuid4"
)
// Config is used for configuring mock adapters
type Config struct {
Tokens []*tokens.Token `json:"tokens"`
}
type mock struct {
tokens []*tokens.Token
}
// Open initializes the mock adapter
func (cfg *Config) Open(_ *certs.ServiceCerts, logger *zap.Logger) (tokens.Storage, error) {
return &mock{tokens: cfg.Tokens}, nil
}
func (m *mock) GetTokens(ctx context.Context) ([]*tokens.Token, error) {
tokensToRet := []*tokens.Token{}
for _, tok := range m.tokens {
if projectsIntersect(ctx, tok) {
tokensToRet = append(tokensToRet, tok)
}
}
return tokensToRet, nil
}
func (m *mock) GetTokenIDWithValue(ctx context.Context, value string) (string, error) {
for i, t := range m.tokens {
if (t.Value == value) && t.Active && projectsIntersect(ctx, t) {
return m.tokens[i].ID, nil
}
}
return "", &tokens.NotFoundError{}
}
func (m *mock) GetToken(ctx context.Context, id string) (*tokens.Token, error) {
for i, t := range m.tokens {
if t.ID == id && projectsIntersect(ctx, t) {
return m.tokens[i], nil
}
}
return nil, &tokens.NotFoundError{}
}
func (m *mock) CreateToken(_ context.Context, id, description string,
active bool, projects []string) (*tokens.Token, error) {
if id == "" {
id = uuid.Must(uuid.NewV4()).String()
}
return m.createTokenWithValue(mockToken(id), description, active, id, projects)
}
func (m *mock) CreateTokenWithValue(_ context.Context,
id, value, description string, active bool, projects []string) (*tokens.Token, error) {
if id == "" {
id = uuid.Must(uuid.NewV4()).String()
}
if err := tutil.IsValidToken(value); err != nil {
return nil, err
}
return m.createTokenWithValue(value, description, active, id, projects)
}
func (m *mock) CreateLegacyTokenWithValue(_ context.Context, value string) (*tokens.Token, error) {
if err := tutil.IsValidLegacyToken(value); err != nil {
return nil, err
}
return m.createTokenWithValue(value, tokens.LegacyTokenDescription,
true, uuid.Must(uuid.NewV4()).String(), []string{})
}
func (m *mock) createTokenWithValue(value string,
description string, active bool, id string, projects []string) (*tokens.Token, error) {
if len(projects) == 0 {
projects = []string{}
}
now := time.Now().UTC()
tNew := tokens.Token{
Description: description,
Value: value,
Active: active,
Created: now,
Updated: now,
ID: id,
Projects: projects,
}
m.tokens = append(m.tokens, &tNew)
return &tNew, nil
}
func (m *mock) DeleteToken(ctx context.Context, id string) error {
found := false
newTokens := []*tokens.Token{}
for i, t := range m.tokens {
if t.ID == id && projectsIntersect(ctx, t) {
found = true
continue
}
newTokens = append(newTokens, m.tokens[i])
}
m.tokens = newTokens
if !found {
return &tokens.NotFoundError{}
}
return nil
}
func (m *mock) UpdateToken(ctx context.Context,
id, description string,
active bool,
projects []string) (*tokens.Token, error) {
t, err := m.GetToken(ctx, id)
if err != nil {
return nil, err
}
if t == nil {
return nil, &tokens.NotFoundError{} // not deleted, because not found
}
now := time.Now().UTC()
var newToken *tokens.Token
newTokens := []*tokens.Token{}
for _, t := range m.tokens {
t := t
if t.ID == id {
t.Active = active
t.Updated = now
if description != "" {
t.Description = description
}
if len(projects) != 0 {
t.Projects = projects
} else if projects == nil {
t.Projects = []string{}
}
newToken = t
}
newTokens = append(newTokens, t)
}
m.tokens = newTokens
return newToken, nil
}
func mockToken(id string) string {
return fmt.Sprintf("%v-token", id)
}
func projectsIntersect(ctx context.Context, token *tokens.Token) bool {
projectsFilter, err := pg.ProjectsListFromContext(ctx)
if err != nil {
return false
}
if len(projectsFilter) == 0 {
return true
}
tokenProjects := token.Projects
if len(tokenProjects) == 0 {
tokenProjects = []string{constants.UnassignedProjectID}
}
for _, projectFilter := range projectsFilter {
for _, project := range tokenProjects {
if projectFilter == project {
return true
}
}
}
return false
}
func (m *mock) Reset() {
m.tokens = []*tokens.Token{}
}