forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_bootstrappolicy_file.go
185 lines (157 loc) · 5.57 KB
/
create_bootstrappolicy_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
183
184
185
package admin
import (
"bytes"
"errors"
"io"
"io/ioutil"
"os"
"path"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/util/sets"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/apis/rbac"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
kprinters "k8s.io/kubernetes/pkg/printers"
"github.com/openshift/origin/pkg/api/latest"
authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
"github.com/openshift/origin/pkg/cmd/server/bootstrappolicy"
templateapi "github.com/openshift/origin/pkg/template/apis/template"
)
const (
DefaultPolicyFile = "openshift.local.config/master/policy.json"
CreateBootstrapPolicyFileCommand = "create-bootstrap-policy-file"
CreateBootstrapPolicyFileFullCommand = "openshift admin " + CreateBootstrapPolicyFileCommand
)
type CreateBootstrapPolicyFileOptions struct {
File string
OpenShiftSharedResourcesNamespace string
}
func NewCommandCreateBootstrapPolicyFile(commandName string, fullName string, out io.Writer) *cobra.Command {
options := &CreateBootstrapPolicyFileOptions{}
cmd := &cobra.Command{
Use: commandName,
Short: "Create the default bootstrap policy",
Run: func(cmd *cobra.Command, args []string) {
if err := options.Validate(args); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageError(cmd, err.Error()))
}
if err := options.CreateBootstrapPolicyFile(); err != nil {
kcmdutil.CheckErr(err)
}
},
}
flags := cmd.Flags()
flags.StringVar(&options.File, "filename", DefaultPolicyFile, "The policy template file that will be written with roles and bindings.")
flags.StringVar(&options.OpenShiftSharedResourcesNamespace, "openshift-namespace", "openshift", "Namespace for shared resources.")
flags.MarkDeprecated("openshift-namespace", "this field is no longer supported and using it can lead to undefined behavior")
// autocompletion hints
cmd.MarkFlagFilename("filename")
return cmd
}
func (o CreateBootstrapPolicyFileOptions) Validate(args []string) error {
if len(args) != 0 {
return errors.New("no arguments are supported")
}
if len(o.File) == 0 {
return errors.New("filename must be provided")
}
if len(o.OpenShiftSharedResourcesNamespace) == 0 {
return errors.New("openshift-namespace must be provided")
}
return nil
}
func (o CreateBootstrapPolicyFileOptions) CreateBootstrapPolicyFile() error {
if err := os.MkdirAll(path.Dir(o.File), os.FileMode(0755)); err != nil {
return err
}
policyTemplate := &templateapi.Template{}
policy := bootstrappolicy.Policy()
for i := range policy.ClusterRoles {
originObject := &authorizationapi.ClusterRole{}
if err := kapi.Scheme.Convert(&policy.ClusterRoles[i], originObject, nil); err != nil {
return err
}
versionedObject, err := kapi.Scheme.ConvertToVersion(originObject, latest.Version)
if err != nil {
return err
}
policyTemplate.Objects = append(policyTemplate.Objects, versionedObject)
}
for i := range policy.ClusterRoleBindings {
originObject := &authorizationapi.ClusterRoleBinding{}
if err := kapi.Scheme.Convert(&policy.ClusterRoleBindings[i], originObject, nil); err != nil {
return err
}
versionedObject, err := kapi.Scheme.ConvertToVersion(originObject, latest.Version)
if err != nil {
return err
}
policyTemplate.Objects = append(policyTemplate.Objects, versionedObject)
}
openshiftRoles := map[string][]rbac.Role{}
for namespace, roles := range policy.Roles {
if namespace == bootstrappolicy.DefaultOpenShiftSharedResourcesNamespace {
r := make([]rbac.Role, len(roles))
for i := range roles {
r[i] = roles[i]
r[i].Namespace = o.OpenShiftSharedResourcesNamespace
}
openshiftRoles[o.OpenShiftSharedResourcesNamespace] = r
} else {
openshiftRoles[namespace] = roles
}
}
// iterate in a defined order
for _, namespace := range sets.StringKeySet(openshiftRoles).List() {
roles := openshiftRoles[namespace]
for i := range roles {
originObject := &authorizationapi.Role{}
if err := kapi.Scheme.Convert(&roles[i], originObject, nil); err != nil {
return err
}
versionedObject, err := kapi.Scheme.ConvertToVersion(originObject, latest.Version)
if err != nil {
return err
}
policyTemplate.Objects = append(policyTemplate.Objects, versionedObject)
}
}
openshiftRoleBindings := map[string][]rbac.RoleBinding{}
for namespace, roleBindings := range policy.RoleBindings {
if namespace == bootstrappolicy.DefaultOpenShiftSharedResourcesNamespace {
rb := make([]rbac.RoleBinding, len(roleBindings))
for i := range roleBindings {
rb[i] = roleBindings[i]
rb[i].Namespace = o.OpenShiftSharedResourcesNamespace
}
openshiftRoleBindings[o.OpenShiftSharedResourcesNamespace] = rb
} else {
openshiftRoleBindings[namespace] = roleBindings
}
}
// iterate in a defined order
for _, namespace := range sets.StringKeySet(openshiftRoleBindings).List() {
roleBindings := openshiftRoleBindings[namespace]
for i := range roleBindings {
originObject := &authorizationapi.RoleBinding{}
if err := kapi.Scheme.Convert(&roleBindings[i], originObject, nil); err != nil {
return err
}
versionedObject, err := kapi.Scheme.ConvertToVersion(originObject, latest.Version)
if err != nil {
return err
}
policyTemplate.Objects = append(policyTemplate.Objects, versionedObject)
}
}
versionedPolicyTemplate, err := kapi.Scheme.ConvertToVersion(policyTemplate, latest.Version)
if err != nil {
return err
}
buffer := &bytes.Buffer{}
(&kprinters.JSONPrinter{}).PrintObj(versionedPolicyTemplate, buffer)
if err := ioutil.WriteFile(o.File, buffer.Bytes(), 0644); err != nil {
return err
}
return nil
}