-
Notifications
You must be signed in to change notification settings - Fork 498
/
Copy pathAcl.java
443 lines (376 loc) · 12.8 KB
/
Acl.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*******************************************************************************
* Copyright 2002-2024, OpenNebula Project, OpenNebula Systems
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package org.opennebula.client.acl;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.opennebula.client.Client;
import org.opennebula.client.OneResponse;
import org.opennebula.client.PoolElement;
import org.w3c.dom.Node;
/**
* This class represents an OpenNebula ACL rule.
* It also offers static XML-RPC call wrappers.
* <br>
* There is not a public constructor, because the information for an individual
* ACL rule cannot be retrieved from OpenNebula.
* <br>
* Instead, Acl objects should be obtained using AclPool.getById, after the
* info method has been called.
*
* @see AclPool#getById
*/
public class Acl extends PoolElement{
private static final String METHOD_PREFIX = "acl.";
private static final String ADDRULE = METHOD_PREFIX + "addrule";
private static final String DELRULE = METHOD_PREFIX + "delrule";
private static final Map<String, Long> USERS;
private static final Map<String, Long> RESOURCES;
private static final Map<String, Long> RIGHTS;
static {
HashMap<String, Long> tmpUsers = new HashMap<String, Long>();
tmpUsers.put("#", 0x0000000100000000L);
tmpUsers.put("@", 0x0000000200000000L);
tmpUsers.put("*", 0x0000000400000000L);
tmpUsers.put("%", 0x0000000800000000L);
USERS = Collections.unmodifiableMap(tmpUsers);
HashMap<String, Long> tmpResources = new HashMap<String, Long>();
tmpResources.put("VM" , 0x0000001000000000L);
tmpResources.put("HOST" , 0x0000002000000000L);
tmpResources.put("NET" , 0x0000004000000000L);
tmpResources.put("IMAGE" , 0x0000008000000000L);
tmpResources.put("USER" , 0x0000010000000000L);
tmpResources.put("TEMPLATE" , 0x0000020000000000L);
tmpResources.put("GROUP" , 0x0000040000000000L);
tmpResources.put("DATASTORE", 0x0000100000000000L);
tmpResources.put("CLUSTER" , 0x0000200000000000L);
tmpResources.put("DOCUMENT" , 0x0000400000000000L);
tmpResources.put("ZONE" , 0x0000800000000000L);
tmpResources.put("SECGROUP" , 0x0001000000000000L);
tmpResources.put("VDC" , 0x0002000000000000L);
tmpResources.put("VROUTER" , 0x0004000000000000L);
tmpResources.put("MARKETPLACE", 0x0008000000000000L);
tmpResources.put("MARKETPLACEAPP", 0x0010000000000000L);
tmpResources.put("VMGROUP", 0x0020000000000000L);
tmpResources.put("VNTEMPLATE", 0x0040000000000000L);
RESOURCES = Collections.unmodifiableMap(tmpResources);
HashMap<String, Long> tmpRights = new HashMap<String, Long>();
tmpRights.put("USE" , 0x1L);
tmpRights.put("MANAGE" , 0x2L);
tmpRights.put("ADMIN" , 0x4L);
tmpRights.put("CREATE" , 0x8L);
RIGHTS = Collections.unmodifiableMap(tmpRights);
}
/**
* @see PoolElement
*/
protected Acl(Node xmlElement, Client client)
{
super(xmlElement, client);
}
// =================================
// Static XML-RPC methods
// =================================
/**
* Allocates a new ACl rule in OpenNebula
*
* @param client XML-RPC Client.
* @param user A string containing a hex number, e.g. 0x100000001
* @param resource A string containing a hex number, e.g. 0x2100000001
* @param rights A string containing a hex number, e.g. 0x10
* @return If successful the message contains the associated
* id generated for this rule.
*/
public static OneResponse allocate(Client client, String user,
String resource, String rights)
{
return client.call(ADDRULE, user, resource, rights);
}
/**
* Allocates a new ACl rule in OpenNebula
*
* @param client XML-RPC Client.
* @param user A string containing a hex number, e.g. 0x100000001
* @param resource A string containing a hex number, e.g. 0x2100000001
* @param rights A string containing a hex number, e.g. 0x10
* @param zone A string containing a hex number, e.g. 0x10
* @return If successful the message contains the associated
* id generated for this rule.
*/
public static OneResponse allocate(Client client, String user,
String resource, String rights, String zone)
{
return client.call(ADDRULE, user, resource, rights, zone);
}
/**
* Allocates a new ACl rule in OpenNebula
*
* @param client XML-RPC Client.
* @param user 64b encoded user
* @param resource 64b encoded user
* @param rights 64b encoded user
* @return If successful the message contains the associated
* id generated for this rule.
*/
public static OneResponse allocate(Client client, long user, long resource,
long rights)
{
return allocate(client,
Long.toHexString(user),
Long.toHexString(resource),
Long.toHexString(rights));
}
/**
* Allocates a new ACl rule in OpenNebula
*
* @param client XML-RPC Client.
* @param user 64b encoded user
* @param resource 64b encoded
* @param rights 64b encoded
* @param zone 64b encoded
* @return If successful the message contains the associated
* id generated for this rule.
*/
public static OneResponse allocate(Client client, long user, long resource,
long rights, long zone)
{
return allocate(client,
Long.toHexString(user),
Long.toHexString(resource),
Long.toHexString(rights),
Long.toHexString(zone));
}
/**
* Allocates a new ACl rule in OpenNebula
*
* @param client XML-RPC Client.
* @param rule a rule string, e.g. "#5 HOST+VM/@12 INFO+CREATE+DELETE"
* @return If successful the message contains the associated
* id generated for this rule.
* @throws RuleParseException If the rule syntax is wrong.
*/
public static OneResponse allocate(Client client, String rule)
throws RuleParseException
{
String[] components = parseRule(rule);
if (components.length > 3)
{
return allocate(client, components[0], components[1],
components[2], components[3]);
}
else
{
return allocate(client, components[0], components[1], components[2]);
}
}
/**
* Deletes an ACL rule from OpenNebula.
*
* @param client XML-RPC Client.
* @param id The ACL rule id.
* @return A encapsulated response.
*/
public static OneResponse delete(Client client, int id)
{
return client.call(DELRULE, id);
}
// =================================
// Instanced object XML-RPC methods
// =================================
/**
* Deletes the ACL rule from OpenNebula.
*
* @see Acl#delete(Client, int)
*/
public OneResponse delete()
{
return delete(client, id);
}
// =================================
// Helpers
// =================================
public long user()
{
long ret = 0;
try
{
ret = Long.parseLong( xpath("USER"), 16 );
}
catch (NumberFormatException e)
{}
return ret;
}
public long resource()
{
long ret = 0;
try
{
ret = Long.parseLong( xpath("RESOURCE"), 16 );
}
catch (NumberFormatException e)
{}
return ret;
}
public long rights()
{
long ret = 0;
try
{
ret = Long.parseLong( xpath("RIGHTS"), 16 );
}
catch (NumberFormatException e)
{}
return ret;
}
public long zone()
{
long ret = 0;
try
{
ret = Long.parseLong( xpath("ZONE"), 16 );
}
catch (NumberFormatException e)
{}
return ret;
}
public String toString()
{
String st = xpath("STRING");
if( st == null )
{
st = "";
}
return st;
}
// =================================
// Rule parsing
// =================================
/**
* Parses a rule string, e.g. "#5 HOST+VM/@12 INFO+CREATE+DELETE"
*
* @param rule an ACL rule in string format
* @return an Array containing 4 Strings (hex 64b numbers). 3 if the rule
* does not have the zone component, for compatibility
* @throws RuleParseException If the rule syntax is wrong.
*/
public static String[] parseRule(String rule) throws RuleParseException
{
String [] components = rule.split(" ");
if( components.length != 3 && components.length != 4 )
{
throw new RuleParseException(
"String needs three components: User, Resource, Rights");
}
String [] ret = new String[components.length];
ret[0] = parseUsers(components[0]);
ret[1] = parseResources(components[1]);
ret[2] = parseRights(components[2]);
if(components.length > 3)
{
ret[3] = parseZone(components[3]);
}
return ret;
}
/**
* Converts a string in the form [#<id>, @<id>, *] to a hex. number
*
* @param users Users component string
* @return A string containing a hex number
*/
private static String parseUsers(String users) throws RuleParseException
{
return Long.toHexString( calculateIds(users) );
}
/**
* Converts a resources string to a hex. number
*
* @param resources Resources component string
* @return A string containing a hex number
*/
private static String parseResources(String resources)
throws RuleParseException
{
long ret = 0;
String[] resourcesComponents = resources.split("/");
if( resourcesComponents.length != 2 )
{
throw new RuleParseException("Resource '"+resources+"' malformed");
}
for( String resource : resourcesComponents[0].split("\\+") )
{
resource = resource.toUpperCase();
if( !RESOURCES.containsKey(resource) )
{
throw new RuleParseException("Resource '" + resource
+ "' does not exist");
}
ret += RESOURCES.get(resource);
}
ret += calculateIds(resourcesComponents[1]);
return Long.toHexString(ret);
}
/**
* Converts a rights string to a hex. number
*
* @param rights Rights component string
* @return A string containing a hex number
*/
private static String parseRights(String rights) throws RuleParseException
{
long ret = 0;
for( String right : rights.split("\\+") )
{
right = right.toUpperCase();
if( !RIGHTS.containsKey(right) )
{
throw new RuleParseException("Right '" + right
+ "' does not exist");
}
ret = ret | RIGHTS.get(right);
}
return Long.toHexString(ret);
}
/**
* Converts a string in the form [#<id>, *] to a hex. number
*
* @param zone Zone component string
* @return A string containing a hex number
*/
private static String parseZone(String zone) throws RuleParseException
{
return Long.toHexString( calculateIds(zone) );
}
/**
* Calculates the numeric value for a String containing an individual
* (#id), group (@id) or all (*) ID component
*
* @param id Rule Id string
* @return the numeric value for the given id_str
*/
private static long calculateIds(String id) throws RuleParseException
{
if( !id.matches("^([#@%]\\d+|\\*)$") )
{
throw new RuleParseException("ID string '" + id + "' malformed");
}
long value = USERS.get( "" + id.charAt(0) );
if( id.charAt(0) != '*' )
{
value += Long.parseLong( id.substring(1) );
}
return value;
}
}