-
Notifications
You must be signed in to change notification settings - Fork 33
/
JAASAccessControlModel.java
189 lines (168 loc) · 7.53 KB
/
JAASAccessControlModel.java
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
/*
* Copyright (c) 2000, 2017 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2022 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.messaging.jmq.jmsserver.auth.acl;
import java.util.Map;
import java.util.Properties;
import java.security.Principal;
import java.security.Permission;
import java.security.AccessControlException;
import java.security.Policy;
import java.security.PrivilegedAction;
import javax.security.auth.Subject;
import com.sun.messaging.jmq.io.PacketType;
import com.sun.messaging.jmq.util.log.Logger;
import com.sun.messaging.jmq.jmsserver.Globals;
import com.sun.messaging.jmq.jmsserver.auth.AccessController;
import com.sun.messaging.jmq.jmsserver.resources.BrokerResources;
import com.sun.messaging.jmq.auth.jaas.*;
import com.sun.messaging.jmq.auth.api.server.model.*;
public class JAASAccessControlModel implements AccessControlModel {
public static final String TYPE = "jaas";
public static final String PROP_PERMISSION_FACTORY = TYPE + ".permissionFactory";
public static final String PROP_PERMISSION_FACTORY_PRIVATE = TYPE + ".permissionFactoryPrivate";
public static final String PROP_POLICY_PROVIDER = TYPE + ".policyProvider";
private Logger logger = Globals.getLogger();
private Properties authProps;
private PermissionFactory permFactory = null;
private String permFactoryPrivate = null;
/**
* This method is called immediately after this AccessControlModel has been instantiated and prior to any calls to its
* other public methods.
*
* @param type the jmq.accesscontrol.type
* @param authProperties broker auth properties
*
* @throws AccessControlException
*/
@Override
public void initialize(String type, Properties authProperties) {
// this.type = type;
if (!type.equals(TYPE)) {
String[] args = { type, TYPE, this.getClass().getName() };
String emsg = Globals.getBrokerResources().getKString(BrokerResources.X_ACCESSCONTROL_TYPE_MISMATCH, args);
logger.log(Logger.ERROR, emsg);
throw new AccessControlException(emsg);
}
authProps = authProperties;
String pfclass = authProps.getProperty(AccessController.PROP_ACCESSCONTROL_PREFIX + PROP_PERMISSION_FACTORY);
String ppclass = authProps.getProperty(AccessController.PROP_ACCESSCONTROL_PREFIX + PROP_POLICY_PROVIDER);
try {
if (pfclass != null) {
permFactory = (PermissionFactory) Class.forName(pfclass).getDeclaredConstructor().newInstance();
}
// if (ppclass != null) policyProvider = (Policy)Class.forName(ppclass).newInstance();
if (ppclass != null) {
Class.forName(ppclass).getDeclaredConstructor().newInstance();
}
} catch (Exception e) {
logger.logStack(Logger.ERROR, e.getMessage(), e);
throw new AccessControlException(e.getClass().getName() + ": " + e.getMessage());
}
permFactoryPrivate = authProps.getProperty(AccessController.PROP_ACCESSCONTROL_PREFIX + PROP_PERMISSION_FACTORY_PRIVATE);
load();
}
/** @throws AccessControlException */
@Override
public void load() {
try {
Policy.getPolicy().refresh();
} catch (SecurityException e) {
AccessControlException ace = new AccessControlException(e.toString());
ace.initCause(e);
throw ace;
}
}
/**
*
* Check connection permission
*
* @param clientUser The Principal represents the client user that is associated with the subject
* @param serviceName the service instance name (eg. "broker", "admin")
* @param serviceType the service type for the service instance ("NORMAL" or "ADMIN")
* @param subject the authenticated subject
*
* @throws AccessControlException
*/
@Override
public void checkConnectionPermission(Principal clientUser, String serviceName, String serviceType, Subject subject) {
Permission perm;
try {
perm = permFactory.newPermission(permFactoryPrivate, PermissionFactory.CONN_RESOURCE_PREFIX + serviceType, (String) null, (Map) null);
} catch (Exception e) {
logger.logStack(Logger.ERROR, e.toString(), e);
AccessControlException ace = new AccessControlException(e.toString());
ace.initCause(e);
throw ace;
}
try {
checkPermission(subject, perm);
} catch (AccessControlException e) {
AccessControlException ace = new AccessControlException(e.getMessage() + ": " + clientUser + " [" + subject.getPrincipals() + "]");
ace.initCause(e);
throw ace;
}
}
/**
* Check permission for an operation on a destination for this role
*
* @param clientUser The Principal represents the client user that is associated with the subject
* @param serviceName the service instance name (eg. "broker", "admin")
* @param serviceType the service type for the service instance ("NORMAL" or "ADMIN")
* @param subject the authenticated subject
* @param operation the operaction
* @param destination the destination
*
* @throws AccessControlException
*/
@Override
public void checkDestinationPermission(Principal clientUser, String serviceName, String serviceType, Subject subject, String operation, String destination,
String destinationType) {
Permission perm;
try {
if (operation.equals(PacketType.AC_DESTCREATE)) {
perm = permFactory.newPermission(permFactoryPrivate, PermissionFactory.AUTO_RESOURCE_PREFIX + PermissionFactory.DEST_QUEUE, (String) null,
(Map) null);
} else {
perm = permFactory.newPermission(permFactoryPrivate, PermissionFactory.DEST_RESOURCE_PREFIX + PermissionFactory.DEST_QUEUE_PREFIX + destination,
operation, (Map) null);
}
} catch (Exception e) {
logger.logStack(Logger.ERROR, e.toString(), e);
AccessControlException ace = new AccessControlException(e.toString());
ace.initCause(e);
throw ace;
}
try {
checkPermission(subject, perm);
} catch (AccessControlException e) {
AccessControlException ace = new AccessControlException(e.getMessage() + ": " + clientUser + " [" + subject.getPrincipals() + "]");
ace.initCause(e);
throw ace;
}
}
/** @throws AccessControlException */
private void checkPermission(Subject subject, Permission p) {
final Permission perm = p;
Subject.doAsPrivileged(subject, new PrivilegedAction() {
@Override
public Object run() {
java.security.AccessController.checkPermission(perm);
return null; // nothing to return
}
}, null);
}
}