-
Notifications
You must be signed in to change notification settings - Fork 55
/
permissiontarget.go
122 lines (112 loc) · 3.7 KB
/
permissiontarget.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
package permissiontarget
import (
"encoding/json"
"github.com/jfrog/jfrog-cli-core/v2/artifactory/commands/utils"
rtUtils "github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-client-go/artifactory/services"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"strings"
)
const DefaultBuildRepositoriesValue = "artifactory-build-info"
type PermissionTargetCommand struct {
serverDetails *config.ServerDetails
templatePath string
vars string
}
func (ptc *PermissionTargetCommand) Vars() string {
return ptc.vars
}
func (ptc *PermissionTargetCommand) TemplatePath() string {
return ptc.templatePath
}
func (ptc *PermissionTargetCommand) PerformPermissionTargetCmd(isUpdate bool) (err error) {
permissionTargetConfigMap, err := utils.ConvertTemplateToMap(ptc)
if err != nil {
return err
}
// Go over the confMap and write the values with the correct types
for key, value := range permissionTargetConfigMap {
isBuildSection := false
switch key {
case Name:
if _, ok := value.(string); !ok {
return errorutils.CheckErrorf("template syntax error: the value for the key: \"Name\" is not a string type.")
}
case Build:
isBuildSection = true
fallthrough
case Repo:
fallthrough
case ReleaseBundle:
permissionSection, err := covertPermissionSection(value, isBuildSection)
if err != nil {
return err
}
permissionTargetConfigMap[key] = permissionSection
default:
return errorutils.CheckErrorf("template syntax error: unknown key: \"" + key + "\".")
}
}
// Convert the new JSON with the correct types to params struct
content, err := json.Marshal(permissionTargetConfigMap)
if errorutils.CheckError(err) != nil {
return err
}
params := services.NewPermissionTargetParams()
err = json.Unmarshal(content, ¶ms)
if errorutils.CheckError(err) != nil {
return err
}
servicesManager, err := rtUtils.CreateServiceManager(ptc.serverDetails, -1, 0, false)
if err != nil {
return err
}
if isUpdate {
return servicesManager.UpdatePermissionTarget(params)
}
return servicesManager.CreatePermissionTarget(params)
}
// Each section is a map of string->interface{}. We need to convert each value to its correct type
func covertPermissionSection(value interface{}, isBuildSection bool) (*services.PermissionTargetSection, error) {
content, err := json.Marshal(value)
if errorutils.CheckError(err) != nil {
return nil, err
}
var answer PermissionSectionAnswer
err = json.Unmarshal(content, &answer)
if errorutils.CheckError(err) != nil {
return nil, err
}
var pts services.PermissionTargetSection
if len(answer.IncludePatterns) > 0 {
pts.IncludePatterns = strings.Split(answer.IncludePatterns, ",")
}
if len(answer.ExcludePatterns) > 0 {
pts.ExcludePatterns = strings.Split(answer.ExcludePatterns, ",")
}
// 'build' permission target must include repositories with a default value that cannot be changed.
if isBuildSection {
answer.Repositories = DefaultBuildRepositoriesValue
}
if len(answer.Repositories) > 0 {
pts.Repositories = strings.Split(answer.Repositories, ",")
}
if answer.ActionsUsers != nil || answer.ActionsGroups != nil {
pts.Actions = &services.Actions{}
}
if answer.ActionsUsers != nil {
convertActionMap(answer.ActionsUsers, &pts.Actions.Users)
}
if answer.ActionsGroups != nil {
convertActionMap(answer.ActionsGroups, &pts.Actions.Groups)
}
return &pts, nil
}
// actionMap is map of string->string. We need to convert each value to []string
func convertActionMap(srcMap map[string]string, tgtMap *map[string][]string) {
*tgtMap = make(map[string][]string)
for key, permissionsStr := range srcMap {
(*tgtMap)[key] = strings.Split(permissionsStr, ",")
}
}