-
Notifications
You must be signed in to change notification settings - Fork 58
/
template.go
238 lines (212 loc) · 7.79 KB
/
template.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
package permissiontarget
import (
"encoding/json"
"fmt"
"os"
"sort"
"strings"
"github.com/c-bata/go-prompt"
"github.com/jfrog/jfrog-cli-core/v2/artifactory/commands/utils"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-cli-core/v2/utils/ioutils"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"github.com/jfrog/jfrog-client-go/utils/log"
)
type PermissionTargetTemplateCommand struct {
path string
}
const (
// Strings for prompt questions
SelectPermissionTargetSectionMsg = "Select the permission target section to configure" + ioutils.PressTabMsg
LeaveEmptyForDefault = "(press enter for default)"
// Yes,No answers
Yes = "yes"
No = "no"
// Main permission target configuration JSON keys
Name = "name"
Repo = "repo"
Build = "build"
ReleaseBundle = "releaseBundle"
IncludePatternsDefault = "**"
ExcludePatternsDefault = ""
// Possible permissions
read = "read"
write = "write"
annotate = "annotate"
delete = "delete"
manage = "manage"
managedXrayMeta = "managedXrayMeta"
distribute = "distribute"
permissionSelectEnd = ioutils.DummyDefaultAnswer
)
func NewPermissionTargetTemplateCommand() *PermissionTargetTemplateCommand {
return &PermissionTargetTemplateCommand{}
}
func (pttc *PermissionTargetTemplateCommand) SetTemplatePath(path string) *PermissionTargetTemplateCommand {
pttc.path = path
return pttc
}
func (pttc *PermissionTargetTemplateCommand) ServerDetails() (*config.ServerDetails, error) {
// Since it's a local command, usage won't be reported.
return nil, nil
}
func (pttc *PermissionTargetTemplateCommand) Run() (err error) {
err = utils.ValidateTemplatePath(pttc.path)
if err != nil {
return
}
permissionTargetTemplateQuestionnaire := &ioutils.InteractiveQuestionnaire{
MandatoryQuestionsKeys: []string{Name},
QuestionsMap: questionMap,
OptionalKeysSuggests: optionalSuggestsMap,
}
err = permissionTargetTemplateQuestionnaire.Perform()
if err != nil {
return err
}
resBytes, err := json.Marshal(permissionTargetTemplateQuestionnaire.AnswersMap)
if err != nil {
return errorutils.CheckError(err)
}
if err = os.WriteFile(pttc.path, resBytes, 0644); err != nil {
return errorutils.CheckError(err)
}
log.Info(fmt.Sprintf("Permission target configuration template successfully created at %s.", pttc.path))
return nil
}
func (pttc *PermissionTargetTemplateCommand) CommandName() string {
return "rt_permission_target_template"
}
var optionalSuggestsMap = []prompt.Suggest{
{Text: ioutils.SaveAndExit},
{Text: Repo},
{Text: Build},
{Text: ReleaseBundle},
}
// Each permission target section (repo/build/releaseBundle) can have the following keys:
// - repos - Mandatory for repo and releaseBundle. Has a const default value for build.
// - include/exclude-patterns - Optional, has a default value.
// - actions - Optional,includes two maps (users and groups): user/group name -> permissions array.
func permissionSectionCallBack(iq *ioutils.InteractiveQuestionnaire, section string) (value string, err error) {
if section == ioutils.SaveAndExit {
return
}
var sectionAnswer PermissionSectionAnswer
if section != Build {
sectionAnswer.Repositories = ioutils.AskString(reposQuestionInfo.Msg, reposQuestionInfo.PromptPrefix, false, reposQuestionInfo.AllowVars)
}
sectionAnswer.IncludePatterns = ioutils.AskStringWithDefault(includePatternsQuestionInfo.Msg, includePatternsQuestionInfo.PromptPrefix, IncludePatternsDefault)
sectionAnswer.ExcludePatterns = ioutils.AskString(excludePatternsQuestionInfo.Msg, excludePatternsQuestionInfo.PromptPrefix, true, excludePatternsQuestionInfo.AllowVars)
configureActions := ioutils.AskFromList("", configureActionsQuestionInfo.PromptPrefix+"users?"+ioutils.PressTabMsg, false, configureActionsQuestionInfo.Options, Yes)
if configureActions == Yes {
sectionAnswer.ActionsUsers = make(map[string]string)
readActionsMap("user", sectionAnswer.ActionsUsers)
}
configureActions = ioutils.AskFromList("", configureActionsQuestionInfo.PromptPrefix+"groups?", false, configureActionsQuestionInfo.Options, Yes)
if configureActions == Yes {
sectionAnswer.ActionsGroups = make(map[string]string)
readActionsMap("group", sectionAnswer.ActionsGroups)
}
iq.AnswersMap[section] = sectionAnswer
return
}
// We will read (user/group name, permissions) pairs until empty name is read.
func readActionsMap(actionsType string, actionsMap map[string]string) {
customKeyPrompt := "Insert " + actionsType + " name (press enter to finish) >"
for {
key := ioutils.AskString("", customKeyPrompt, true, false)
if key == "" {
return
}
value := strings.Join(readPermissionList(key), ",")
actionsMap[key] = value
}
}
func readPermissionList(permissionsOwner string) (permissions []string) {
var permissionsMap = map[string]bool{
read: false,
write: false,
annotate: false,
delete: false,
manage: false,
managedXrayMeta: false,
distribute: false,
}
for {
answer := ioutils.AskFromList("", "Select permission value for "+permissionsOwner+" (press tab for options or enter to finish) >", true, buildPermissionSuggestArray(permissionsMap), permissionSelectEnd)
if answer == permissionSelectEnd {
break
}
// If the answer is a valid key, we will mark it with true to remove it from the suggestion list
if _, ok := permissionsMap[answer]; ok {
permissionsMap[answer] = true
} else {
// answer is a var, we will add it to the final permissions slice result
permissions = append(permissions, answer)
}
}
for key, value := range permissionsMap {
if value {
permissions = append(permissions, key)
}
}
return
}
func buildPermissionSuggestArray(permissionMap map[string]bool) (permissions []prompt.Suggest) {
for key, value := range permissionMap {
if !value {
permissions = append(permissions, prompt.Suggest{Text: key})
}
}
sort.Slice(permissions, func(i, j int) bool {
return permissions[i].Text < permissions[j].Text
})
return
}
var questionMap = map[string]ioutils.QuestionInfo{
Name: {
Msg: "",
PromptPrefix: "Insert the permission target name >",
AllowVars: false,
Writer: ioutils.WriteStringAnswer,
MapKey: Name,
Callback: nil,
},
ioutils.OptionalKey: {
Msg: "",
PromptPrefix: SelectPermissionTargetSectionMsg,
AllowVars: false,
Writer: nil,
MapKey: "",
Callback: permissionSectionCallBack,
},
Repo: ioutils.FreeStringQuestionInfo,
Build: ioutils.FreeStringQuestionInfo,
ReleaseBundle: ioutils.FreeStringQuestionInfo,
}
var reposQuestionInfo = ioutils.QuestionInfo{
Msg: "Insert the section's repositories value.\nYou can specify the name \"ANY\" to apply to all repositories, \"ANY REMOTE\" for all remote repositories or \"ANY LOCAL\" for all local repositories",
PromptPrefix: ioutils.CommaSeparatedListMsg + " >",
}
var includePatternsQuestionInfo = ioutils.QuestionInfo{
Msg: "Insert a value for include-patterns",
PromptPrefix: ioutils.CommaSeparatedListMsg + " " + LeaveEmptyForDefault,
}
var excludePatternsQuestionInfo = ioutils.QuestionInfo{
Msg: "Insert value for exclude-patterns",
PromptPrefix: ioutils.CommaSeparatedListMsg + " " + LeaveEmptyForDefault + " []:",
}
var configureActionsQuestionInfo = ioutils.QuestionInfo{
PromptPrefix: "Configure actions for ",
Options: []prompt.Suggest{
{Text: Yes},
{Text: No},
},
}
type PermissionSectionAnswer struct {
Repositories string `json:"repositories,omitempty"`
IncludePatterns string `json:"include-patterns,omitempty"`
ExcludePatterns string `json:"exclude-patterns,omitempty"`
ActionsUsers map[string]string `json:"actions-users,omitempty"`
ActionsGroups map[string]string `json:"actions-groups,omitempty"`
}