/
request-arguments.go
287 lines (265 loc) · 9.36 KB
/
request-arguments.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package rulesvc
import (
"context"
"github.com/common-fate/apikit/logger"
ahtypes "github.com/common-fate/common-fate/accesshandler/pkg/types"
"github.com/common-fate/common-fate/pkg/cache"
"github.com/common-fate/common-fate/pkg/rule"
"github.com/common-fate/common-fate/pkg/storage"
"github.com/common-fate/common-fate/pkg/types"
"github.com/common-fate/ddb"
"github.com/common-fate/provider-registry-sdk-go/pkg/providerregistrysdk"
)
func (s *Service) processArgument(ctx context.Context, targetGroupID string, argument providerregistrysdk.TargetField, value string) types.RequestArgument {
ra := types.RequestArgument{
Description: argument.Description,
Options: []types.WithOption{
{
Label: value,
Valid: false,
Value: value,
},
},
}
if argument.Title != nil {
ra.Title = *argument.Title
}
if argument.Resource != nil {
resourceQuery := &storage.GetCachedTargetGroupResource{TargetGroupID: targetGroupID, ResourceType: *argument.Resource, ResourceID: value}
_, err := s.DB.Query(ctx, resourceQuery)
if err != ddb.ErrNoItems {
ra.Description = argument.Description
ra.Options = []types.WithOption{{
Label: resourceQuery.Result.Resource.Name,
Valid: true,
Value: value,
}}
}
}
return ra
}
func (s *Service) processArguments(ctx context.Context, targetGroupID string, argument providerregistrysdk.TargetField, values []string) types.RequestArgument {
ra := types.RequestArgument{
Description: argument.Description,
RequiresSelection: true,
Options: []types.WithOption{},
}
if argument.Title != nil {
ra.Title = *argument.Title
}
if argument.Resource != nil {
for _, value := range values {
resourceQuery := &storage.GetCachedTargetGroupResource{TargetGroupID: targetGroupID, ResourceType: *argument.Resource, ResourceID: value}
_, err := s.DB.Query(ctx, resourceQuery)
if err != ddb.ErrNoItems {
ra.Options = append(ra.Options, types.WithOption{
Label: resourceQuery.Result.Resource.Name,
Valid: true,
Value: value,
})
} else {
ra.Options = append(ra.Options, types.WithOption{
Label: value,
Valid: false,
Value: value,
})
}
}
} else {
for _, value := range values {
ra.Options = append(ra.Options, types.WithOption{
Label: value,
Valid: false,
Value: value,
})
}
}
return ra
}
// RequestArguments takes an access rule and prepares a list of request arguments which contains all the available options that a user may chose from when creating a request
// this can also be used to validate the input to a create request api call
func (s *Service) RequestArguments(ctx context.Context, accessRuleTarget rule.Target) (map[string]types.RequestArgument, error) {
if accessRuleTarget.TargetGroupID != "" {
targetGroupRequestArguments := make(map[string]types.RequestArgument)
targetGroup := &storage.GetTargetGroup{ID: accessRuleTarget.TargetGroupID}
_, err := s.DB.Query(ctx, targetGroup)
if err != nil && err != ddb.ErrNoItems {
return nil, err
}
for k, v := range targetGroup.Result.Schema.Properties {
if value, ok := accessRuleTarget.With[k]; ok {
targetGroupRequestArguments[k] = s.processArgument(ctx, accessRuleTarget.TargetGroupID, v, value)
}
if values, ok := accessRuleTarget.WithSelectable[k]; ok {
targetGroupRequestArguments[k] = s.processArguments(ctx, accessRuleTarget.TargetGroupID, v, values)
}
}
return targetGroupRequestArguments, nil
}
// prepare request arguments for an access rule
// fetch the schema for the provider
providerSchema, err := s.getProviderArgSchemaByID(ctx, accessRuleTarget.ProviderID)
if err != nil {
if err == ErrProviderNotFound {
logger.Get(ctx).Infow("failed to fetch provider while building request args because the provider no longer exists, falling back to basic request arguments")
} else {
logger.Get(ctx).Errorw("failed to fetch provider while building request args, falling back to basic request arguments", "error", err)
}
// Fall back to using keys as labels
requestArguments := make(map[string]types.RequestArgument)
for k, v := range accessRuleTarget.With {
a := requestArguments[k]
a.Title = k
a.Options = append(a.Options, types.WithOption{Label: v, Value: v})
requestArguments[k] = a
}
for k, v := range accessRuleTarget.WithSelectable {
a := requestArguments[k]
a.Title = k
for _, o := range v {
a.Options = append(a.Options, types.WithOption{Label: o, Value: o})
}
requestArguments[k] = a
}
for k := range accessRuleTarget.WithArgumentGroupOptions {
a := requestArguments[k]
a.Title = k
requestArguments[k] = a
}
return requestArguments, nil
}
// add the arguments from the schema
requestArguments := make(map[string]types.RequestArgument)
for k, v := range providerSchema.AdditionalProperties {
var requestFormElement *types.RequestArgumentFormElement
if v.RequestFormElement != nil {
requestFormElement = (*types.RequestArgumentFormElement)(v.RequestFormElement)
}
requestArguments[k] = types.RequestArgument{
Description: v.Description,
Title: v.Title,
FormElement: requestFormElement,
}
}
// fetch the options from the cache
argOptionsQuery := &storage.ListCachedProviderOptions{ProviderID: accessRuleTarget.ProviderID}
_, err = s.DB.Query(ctx, argOptionsQuery)
if err != nil && err != ddb.ErrNoItems {
return nil, err
}
argGroupOptionsQuery := &storage.ListAllCachedProviderArgGroupOptions{ProviderID: accessRuleTarget.ProviderID}
_, err = s.DB.Query(ctx, argGroupOptionsQuery)
if err != nil && err != ddb.ErrNoItems {
return nil, err
}
// for convenience, convert the list into a maps for easy indexing
argOptionsQueryMap := make(map[string]map[string]cache.ProviderOption)
argGroupOptionsQueryMap := make(map[string]map[string]map[string]cache.ProviderArgGroupOption)
for _, argOption := range argOptionsQuery.Result {
option := argOptionsQueryMap[argOption.Arg]
if option == nil {
option = make(map[string]cache.ProviderOption)
}
option[argOption.Value] = argOption
argOptionsQueryMap[argOption.Arg] = option
}
for _, argOption := range argGroupOptionsQuery.Result {
arg := argGroupOptionsQueryMap[argOption.Arg]
if arg == nil {
arg = make(map[string]map[string]cache.ProviderArgGroupOption)
}
group := arg[argOption.Group]
if group == nil {
group = make(map[string]cache.ProviderArgGroupOption)
}
group[argOption.Value] = argOption
arg[argOption.Group] = group
argGroupOptionsQueryMap[argOption.Arg] = arg
}
// populate the requestArguments with the details from the queries
// for each value on the access rule, add it with details to the request argument
for argId, v := range requestArguments {
options := make(map[string]types.WithOption)
// start with values on the access rule both with and withSelectable
argValues := accessRuleTarget.WithSelectable[argId]
// The field is selectable so set requiresSelection to true
if len(argValues) != 0 {
v.RequiresSelection = true
}
if value, ok := accessRuleTarget.With[argId]; ok {
argValues = append(argValues, value)
}
for _, argValue := range argValues {
matched := false
if values, ok := argOptionsQueryMap[argId]; ok {
if v, ok := values[argValue]; ok {
options[argValue] = types.WithOption{
Description: v.Description,
Label: v.Label,
Valid: true,
Value: v.Value,
}
matched = true
}
}
// value not found in cache so mark it as invalid
if !matched {
options[argValue] = types.WithOption{
Label: argValue,
// If the field is an input, it won't match any options, but its still valid for selection!
// the label and value are the same for an input field
Valid: providerSchema.AdditionalProperties[argId].RuleFormElement == ahtypes.ArgumentRuleFormElementINPUT,
Value: argValue,
}
}
}
// then get all the values related to groups
if argGroups, ok := accessRuleTarget.WithArgumentGroupOptions[argId]; ok {
for argGroupId, argGroupValues := range argGroups {
// if there are any selected groupings, the field is selectable regardless of whether that group produces any options currently so set requiresSelection to true
if len(argGroupValues) != 0 {
v.RequiresSelection = true
}
// add each option from arg group children
for _, argGroupValue := range argGroupValues {
if argGroups, ok := argGroupOptionsQueryMap[argId]; ok {
if argGroup, ok := argGroups[argGroupId]; ok {
if argGroupValue, ok := argGroup[argGroupValue]; ok {
for _, child := range argGroupValue.Children {
matched := false
if values, ok := argOptionsQueryMap[argId]; ok {
if v, ok := values[child]; ok {
options[child] = types.WithOption{
Description: v.Description,
Label: v.Label,
Valid: true,
Value: v.Value,
}
matched = true
}
}
if !matched {
options[child] = types.WithOption{
Label: child,
Valid: false,
Value: child,
}
}
}
}
}
}
}
}
}
// then deduplicate
deduplicatedOptions := make([]types.WithOption, 0, len(options))
for _, v := range options {
deduplicatedOptions = append(deduplicatedOptions, v)
}
v.Options = deduplicatedOptions
requestArguments[argId] = v
}
// return
return requestArguments, nil
}