-
Notifications
You must be signed in to change notification settings - Fork 14
/
roles_provider.go
202 lines (187 loc) · 7.05 KB
/
roles_provider.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
package server
import (
"crypto/sha1"
"fmt"
"github.com/awcullen/opcua/ua"
)
// RolesProvider selects roles where the user identity and connection information matches the membership criteria.
// Roles are identified by a NodeID. There are a number of well-known roles.
// Later, users are granted Permissions to perform actions based on the user's role memberships.
type RolesProvider interface {
// GetRoles returns the roles where the user matches the membership criteria.
GetRoles(userIdentity interface{}, applicationURI string, endpointURL string) ([]ua.NodeID, error)
}
// IdentityMappingRule ...
type IdentityMappingRule struct {
NodeID ua.NodeID
Identities []ua.IdentityMappingRuleType
ApplicationsExclude bool
Applications []string
EndpointsExclude bool
Endpoints []struct {
EndpointUrl string
SecurityMode string
SecurityPolicyURI string
TransportProfileUri string
}
}
var (
// DefaultRolePermissions returns RolePermissionTypes for the well known roles.
DefaultRolePermissions []ua.RolePermissionType = []ua.RolePermissionType{
{RoleID: ua.ObjectIDWellKnownRoleAnonymous, Permissions: (ua.PermissionTypeBrowse | ua.PermissionTypeRead)},
{RoleID: ua.ObjectIDWellKnownRoleAuthenticatedUser, Permissions: (ua.PermissionTypeBrowse | ua.PermissionTypeRead)},
{RoleID: ua.ObjectIDWellKnownRoleObserver, Permissions: (ua.PermissionTypeBrowse | ua.PermissionTypeRead | ua.PermissionTypeReadHistory | ua.PermissionTypeReceiveEvents)},
{RoleID: ua.ObjectIDWellKnownRoleOperator, Permissions: (ua.PermissionTypeBrowse | ua.PermissionTypeRead | ua.PermissionTypeWrite | ua.PermissionTypeReadHistory | ua.PermissionTypeReceiveEvents | ua.PermissionTypeCall)},
{RoleID: ua.ObjectIDWellKnownRoleEngineer, Permissions: (ua.PermissionTypeBrowse | ua.PermissionTypeRead | ua.PermissionTypeWrite | ua.PermissionTypeReadHistory | ua.PermissionTypeReceiveEvents | ua.PermissionTypeCall | ua.PermissionTypeWriteHistorizing)},
{RoleID: ua.ObjectIDWellKnownRoleSupervisor, Permissions: (ua.PermissionTypeBrowse | ua.PermissionTypeRead | ua.PermissionTypeWrite | ua.PermissionTypeReadHistory | ua.PermissionTypeReceiveEvents | ua.PermissionTypeCall)},
{RoleID: ua.ObjectIDWellKnownRoleConfigureAdmin, Permissions: (ua.PermissionTypeBrowse | ua.PermissionTypeRead | ua.PermissionTypeWriteAttribute)},
{RoleID: ua.ObjectIDWellKnownRoleSecurityAdmin, Permissions: (ua.PermissionTypeBrowse | ua.PermissionTypeReadRolePermissions | ua.PermissionTypeWriteRolePermissions)},
}
// DefaultIdentityMappingRules ...
DefaultIdentityMappingRules []IdentityMappingRule = []IdentityMappingRule{
// WellKnownRoleAnonymous
{
NodeID: ua.ObjectIDWellKnownRoleAnonymous,
Identities: []ua.IdentityMappingRuleType{
{CriteriaType: ua.IdentityCriteriaTypeAnonymous},
},
ApplicationsExclude: true,
EndpointsExclude: true,
},
// WellKnownRoleAuthenticatedUser
{
NodeID: ua.ObjectIDWellKnownRoleAuthenticatedUser,
Identities: []ua.IdentityMappingRuleType{
{CriteriaType: ua.IdentityCriteriaTypeAuthenticatedUser},
},
ApplicationsExclude: true,
EndpointsExclude: true,
},
// WellKnownRoleObserver
{
NodeID: ua.ObjectIDWellKnownRoleObserver,
Identities: []ua.IdentityMappingRuleType{
{CriteriaType: ua.IdentityCriteriaTypeAuthenticatedUser},
},
ApplicationsExclude: true,
EndpointsExclude: true,
},
// WellKnownRoleOperator
{
NodeID: ua.ObjectIDWellKnownRoleOperator,
Identities: []ua.IdentityMappingRuleType{
{CriteriaType: ua.IdentityCriteriaTypeAuthenticatedUser},
},
ApplicationsExclude: true,
EndpointsExclude: true,
},
// WellKnownRoleEngineer
{
NodeID: ua.ObjectIDWellKnownRoleEngineer,
ApplicationsExclude: true,
EndpointsExclude: true,
},
// WellKnownRoleSupervisor
{
NodeID: ua.ObjectIDWellKnownRoleSupervisor,
ApplicationsExclude: true,
EndpointsExclude: true,
},
// WellKnownRoleConfigureAdmin
{
NodeID: ua.ObjectIDWellKnownRoleConfigureAdmin,
ApplicationsExclude: true,
EndpointsExclude: true,
},
// WellKnownRoleSecurityAdmin
{
NodeID: ua.ObjectIDWellKnownRoleSecurityAdmin,
ApplicationsExclude: true,
EndpointsExclude: true,
},
}
)
// IsUserPermitted returns true if the user's role permissions contain a given permissionType.
func IsUserPermitted(userRolePermissions []ua.RolePermissionType, permissionType ua.PermissionType) bool {
for _, rp := range userRolePermissions {
if rp.Permissions&permissionType != 0 {
return true
}
}
return false
}
// RulesBasedRolesProvider returns WellKnownRoles given server identity mapping rules.
type RulesBasedRolesProvider struct {
identityMappingRules []IdentityMappingRule
}
// NewRulesBasedRolesProvider ...
func NewRulesBasedRolesProvider(rules []IdentityMappingRule) RolesProvider {
return &RulesBasedRolesProvider{
identityMappingRules: rules,
}
}
// GetRoles ...
func (p *RulesBasedRolesProvider) GetRoles(userIdentity interface{}, applicationURI string, endpointURL string) ([]ua.NodeID, error) {
roles := []ua.NodeID{}
for _, rule := range p.identityMappingRules {
ok := rule.ApplicationsExclude // true means the following applications should be excluded
for _, uri := range rule.Applications {
if uri == applicationURI {
ok = !rule.ApplicationsExclude
break
}
}
if !ok {
break // continue with next rule
}
ok = rule.EndpointsExclude // true means the following endpoints should be excluded
for _, ep := range rule.Endpoints {
if ep.EndpointUrl == endpointURL {
ok = !rule.EndpointsExclude
break
}
}
if !ok {
break // continue with next role
}
for _, identity := range rule.Identities {
switch id := userIdentity.(type) {
case ua.AnonymousIdentity:
if identity.CriteriaType == ua.IdentityCriteriaTypeAnonymous {
roles = append(roles, rule.NodeID)
break // continue with next identity
}
case ua.UserNameIdentity:
if identity.CriteriaType == ua.IdentityCriteriaTypeAuthenticatedUser {
roles = append(roles, rule.NodeID)
break // continue with next identity
}
if identity.CriteriaType == ua.IdentityCriteriaTypeUserName && identity.Criteria == id.UserName {
roles = append(roles, rule.NodeID)
break // continue with next identity
}
case ua.X509Identity:
if identity.CriteriaType == ua.IdentityCriteriaTypeAuthenticatedUser {
roles = append(roles, rule.NodeID)
break // continue with next identity
}
thumbprint := fmt.Sprintf("%x", sha1.Sum([]byte(id.Certificate)))
if identity.CriteriaType == ua.IdentityCriteriaTypeThumbprint && identity.Criteria == thumbprint {
roles = append(roles, rule.NodeID)
break // continue with next identity
}
case ua.IssuedIdentity:
if identity.CriteriaType == ua.IdentityCriteriaTypeAuthenticatedUser {
roles = append(roles, rule.NodeID)
break // continue with next identity
}
default:
return nil, ua.BadUserAccessDenied
}
}
}
if len(roles) == 0 {
return nil, ua.BadUserAccessDenied
}
return roles, nil
}