forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
roles.go
141 lines (116 loc) · 4.77 KB
/
roles.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
package cluster
import (
"fmt"
"io/ioutil"
kerrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kubernetes/pkg/apis/authorization"
authorizationtypedclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/authorization/internalversion"
authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
oauthorizationtypedclient "github.com/openshift/origin/pkg/authorization/generated/internalclientset/typed/authorization/internalversion"
regutil "github.com/openshift/origin/pkg/authorization/registry/util"
"github.com/openshift/origin/pkg/oc/admin/diagnostics/diagnostics/types"
"github.com/openshift/origin/pkg/oc/admin/diagnostics/diagnostics/util"
policycmd "github.com/openshift/origin/pkg/oc/admin/policy"
rbacregistryvalidation "k8s.io/kubernetes/pkg/registry/rbac/validation"
)
// ClusterRoles is a Diagnostic to check that the default cluster roles match expectations
type ClusterRoles struct {
ClusterRolesClient oauthorizationtypedclient.ClusterRoleInterface
SARClient authorizationtypedclient.SelfSubjectAccessReviewsGetter
}
const (
ClusterRolesName = "ClusterRoles"
clusterRoleMissing = `
clusterrole/%s is missing.
Use the 'oc adm policy reconcile-cluster-roles' command to create the role. For example,
$ oc adm policy reconcile-cluster-roles \
--additive-only=true --confirm
`
clusterRoleReduced = `
clusterrole/%s has changed, but the existing role has more permissions than the new role.
If you can confirm that the extra permissions are not required, you may use the
'oc adm policy reconcile-cluster-roles' command to update the role to reduce permissions.
For example,
$ oc adm policy reconcile-cluster-roles \
--additive-only=false --confirm
`
clusterRoleChanged = `
clusterrole/%s has changed and the existing role does not have enough permissions.
Use the 'oc adm policy reconcile-cluster-roles' command to update the role.
For example,
$ oc adm policy reconcile-cluster-roles \
--additive-only=true --confirm
`
)
func (d *ClusterRoles) Name() string {
return ClusterRolesName
}
func (d *ClusterRoles) Description() string {
return "Check that the default ClusterRoles are present and contain the expected permissions"
}
func (d *ClusterRoles) Requirements() (client bool, host bool) {
return true, false
}
func (d *ClusterRoles) CanRun() (bool, error) {
if d.ClusterRolesClient == nil {
return false, fmt.Errorf("must have client.ClusterRolesInterface")
}
if d.SARClient == nil {
return false, fmt.Errorf("must have client.SubjectAccessReviews")
}
return util.UserCan(d.SARClient, &authorization.ResourceAttributes{
Verb: "list",
Group: authorizationapi.GroupName,
Resource: "clusterroles",
})
}
func (d *ClusterRoles) Check() types.DiagnosticResult {
r := types.NewDiagnosticResult(ClusterRolesName)
reconcileOptions := &policycmd.ReconcileClusterRolesOptions{
Confirmed: false,
Union: false,
Out: ioutil.Discard,
RoleClient: d.ClusterRolesClient,
}
changedClusterRoles, _, err := reconcileOptions.ChangedClusterRoles()
if err != nil {
r.Error("CRD1000", err, fmt.Sprintf("Error inspecting ClusterRoles: %v", err))
return r
}
// success
if len(changedClusterRoles) == 0 {
return r
}
for _, changedClusterRole := range changedClusterRoles {
actualClusterRole, err := d.ClusterRolesClient.Get(changedClusterRole.Name, metav1.GetOptions{})
if kerrs.IsNotFound(err) {
r.Error("CRD1002", nil, fmt.Sprintf(clusterRoleMissing, changedClusterRole.Name))
continue
}
if err != nil {
r.Error("CRD1001", err, fmt.Sprintf("Unable to get clusterrole/%s: %v", changedClusterRole.Name, err))
continue
}
actualRBACClusterRole, err := regutil.ClusterRoleToRBAC(actualClusterRole)
if err != nil {
r.Error("CRD1009", err, fmt.Sprintf("Unable to convert clusterrole/%s to RBAC cluster role: %v", actualClusterRole.Name, err))
continue
}
_, missingRules := rbacregistryvalidation.Covers(actualRBACClusterRole.Rules, changedClusterRole.Rules)
if len(missingRules) == 0 {
r.Info("CRD1003", fmt.Sprintf(clusterRoleReduced, changedClusterRole.Name))
_, extraRules := rbacregistryvalidation.Covers(changedClusterRole.Rules, actualRBACClusterRole.Rules)
for _, extraRule := range extraRules {
r.Info("CRD1008", fmt.Sprintf("clusterrole/%s has extra permission %v.", changedClusterRole.Name, extraRule))
}
continue
}
r.Error("CRD1005", nil, fmt.Sprintf(clusterRoleChanged, changedClusterRole.Name))
for _, missingRule := range missingRules {
r.Info("CRD1007", fmt.Sprintf("clusterrole/%s is missing permission %v.", changedClusterRole.Name, missingRule))
}
r.Debug("CRD1006", fmt.Sprintf("clusterrole/%s is now %v.", changedClusterRole.Name, changedClusterRole))
}
return r
}