VirtualRepository | Line # 102 | 39 | 10 | 77.3% |
0.77272725
|
(1) | |||
Result | |||
0.57575756
|
issrg.test.ds.TestDS.testIssuing issrg.test.ds.TestDS.testIssuing | 1 PASS | |
1 | /* | |
2 | * Copyright (c) 2000-2005, University of Salford | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions are met: | |
7 | * | |
8 | * Redistributions of source code must retain the above copyright notice, this | |
9 | * list of conditions and the following disclaimer. | |
10 | * | |
11 | * Redistributions in binary form must reproduce the above copyright notice, | |
12 | * this list of conditions and the following disclaimer in the documentation | |
13 | * and/or other materials provided with the distribution. | |
14 | * | |
15 | * Neither the name of the University of Salford nor the names of its | |
16 | * contributors may be used to endorse or promote products derived from this | |
17 | * software without specific prior written permission. | |
18 | * | |
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
29 | * POSSIBILITY OF SUCH DAMAGE. | |
30 | */ | |
31 | /* | |
32 | * Copyright (c) 2006, University of Kent | |
33 | * All rights reserved. | |
34 | * | |
35 | * Redistribution and use in source and binary forms, with or without | |
36 | * modification, are permitted provided that the following conditions are met: | |
37 | * | |
38 | * Redistributions of source code must retain the above copyright notice, this | |
39 | * list of conditions and the following disclaimer. | |
40 | * | |
41 | * Redistributions in binary form must reproduce the above copyright notice, | |
42 | * this list of conditions and the following disclaimer in the documentation | |
43 | * and/or other materials provided with the distribution. | |
44 | * | |
45 | * 1. Neither the name of the University of Kent nor the names of its | |
46 | * contributors may be used to endorse or promote products derived from this | |
47 | * software without specific prior written permission. | |
48 | * | |
49 | * 2. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | |
50 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | |
51 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
52 | * PURPOSE ARE DISCLAIMED. | |
53 | * | |
54 | * 3. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
55 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
56 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
57 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
58 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
59 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
60 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
61 | * POSSIBILITY OF SUCH DAMAGE. | |
62 | * | |
63 | * 4. YOU AGREE THAT THE EXCLUSIONS IN PARAGRAPHS 2 AND 3 ABOVE ARE REASONABLE | |
64 | * IN THE CIRCUMSTANCES. IN PARTICULAR, YOU ACKNOWLEDGE (1) THAT THIS | |
65 | * SOFTWARE HAS BEEN MADE AVAILABLE TO YOU FREE OF CHARGE, (2) THAT THIS | |
66 | * SOFTWARE IS NOT "PRODUCT" QUALITY, BUT HAS BEEN PRODUCED BY A RESEARCH | |
67 | * GROUP WHO DESIRE TO MAKE THIS SOFTWARE FREELY AVAILABLE TO PEOPLE WHO WISH | |
68 | * TO USE IT, AND (3) THAT BECAUSE THIS SOFTWARE IS NOT OF "PRODUCT" QUALITY | |
69 | * IT IS INEVITABLE THAT THERE WILL BE BUGS AND ERRORS, AND POSSIBLY MORE | |
70 | * SERIOUS FAULTS, IN THIS SOFTWARE. | |
71 | * | |
72 | * 5. This license is governed, except to the extent that local laws | |
73 | * necessarily apply, by the laws of England and Wales. | |
74 | */ | |
75 | ||
76 | package issrg.utils.repository; | |
77 | ||
78 | import issrg.utils.ToRawCredential; | |
79 | import java.util.Map; | |
80 | import java.util.Hashtable; | |
81 | ||
82 | import java.security.Principal; | |
83 | ||
84 | import javax.naming.directory.Attribute; | |
85 | import javax.naming.directory.Attributes; | |
86 | import javax.naming.directory.BasicAttribute; | |
87 | import javax.naming.directory.BasicAttributes; | |
88 | import javax.naming.NamingEnumeration; | |
89 | import javax.naming.NamingException; | |
90 | ||
91 | import issrg.pba.rbac.CustomisePERMIS; | |
92 | import issrg.pba.MultiAuthzTokenParser; | |
93 | ||
94 | import org.apache.log4j.*; | |
95 | /** | |
96 | * This is the class for representing the virtual repository of ACs and ACRLs | |
97 | * for the push model. It can be used to store other attributes, too. | |
98 | * | |
99 | * @author A Otenko | |
100 | * @version 1.0 | |
101 | */ | |
102 | public class VirtualRepository implements issrg.utils.repository.AttributeRepository { | |
103 | ||
104 | private static Logger logger = Logger.getLogger(VirtualRepository.class); | |
105 | /** | |
106 | * This is the actual repository. It contains the | |
107 | * javax.naming.directory.Attributes | |
108 | * objects indexed by the String entry DN. | |
109 | */ | |
110 | protected Map repository = new Hashtable(); | |
111 | ||
112 | /** | |
113 | * This is the diagnostic message reference; it is null, if everything was OK | |
114 | */ | |
115 | protected final RepositoryException diagnosis=null; | |
116 | ||
117 | 698 | public VirtualRepository() { |
118 | } | |
119 | ||
120 | /** | |
121 | * This method allows the caller to place the given value of the named | |
122 | * attribute in the | |
123 | * specified entry. If the entry does not exist, it is created. If the | |
124 | * attribute does not exist, it is created. No checks for duplicate values are | |
125 | * done. | |
126 | * | |
127 | * @param DN is the canonical LDAP DN of the entry | |
128 | * @param attributeName is the name of the attribute | |
129 | * @param value is the value to add to the attribute | |
130 | */ | |
131 | 750 | public void populate(String DN, String attributeName, Object value){ |
132 | 750 | javax.naming.directory.Attributes as; |
133 | 750 | DN = DN.toUpperCase(); |
134 | 0 | if ((as=(javax.naming.directory.Attributes)repository.get(DN))==null){ |
135 | 725 | repository.put(DN, as=new BasicAttributes()); |
136 | } | |
137 | ||
138 | 750 | Attribute a = as.get(attributeName); |
139 | 750 | if (a==null){ |
140 | 731 | as.put(a=new BasicAttribute(attributeName)); |
141 | } | |
142 | 750 | a.add(value); |
143 | } | |
144 | ||
145 | /** | |
146 | * This method allows the caller to access the Attributes of the given entry, | |
147 | * which can be useful for updating the entry's contents (remove or replace | |
148 | * attributes or | |
149 | * their values). It is equal to the getAllAttributes with a few unapparent | |
150 | * differences: it does not affect the repository status; it gives access to | |
151 | * the repository, whilst getAllAttributes returns a <i>copy</i> of the data, | |
152 | * so tampering with it is safe after getAllAttributes, and is not after | |
153 | * get(). | |
154 | * | |
155 | * @param DN is the canonical LDAP DN of the entry to retrieve | |
156 | * | |
157 | * @return the Attributes of that entry; is null if no such entry exists | |
158 | */ | |
159 | 0 | public Attributes get(String DN) { |
160 | 0 | Attributes atts = (Attributes)repository.get(DN.toUpperCase()); |
161 | 0 | return atts; |
162 | } | |
163 | ||
164 | /** | |
165 | * This method destroys the whole entry: all the data is lost; the former | |
166 | * contents of the entry is returned. | |
167 | * | |
168 | * @param DN is the canonical LDAP DN of the entry to remove | |
169 | * | |
170 | * @return the Attributes of that entry, the repository no longer contains | |
171 | * any of the attributes in that entry; can be null, if no such entry | |
172 | * existed | |
173 | */ | |
174 | 1 | public Attributes remove(String DN){ |
175 | 1 | return (Attributes)repository.remove(DN); |
176 | } | |
177 | ||
178 | /** | |
179 | * This method returns the values of the requested attribute from a given | |
180 | * entry. | |
181 | * | |
182 | * @param DN is the name of the entry | |
183 | * @param attributeID is the name of the attribute to return | |
184 | * | |
185 | * @return the Attribute with values or null, if no such attribute was there | |
186 | */ | |
187 | 9 | public Attribute getAttribute(java.security.Principal DN, String attributeID) { |
188 | 9 | Attributes as = getAttributes(DN, new String[]{attributeID}); |
189 | 9 | if (as==null){ |
190 | 0 | return null; // we are working as if all the DNs are present in the Repository, only they can be empty |
191 | //throw diagnosis=new PbaException("No such entry exists"); | |
192 | } | |
193 | ||
194 | //diagnosis=null; | |
195 | 9 | return as.get(attributeID); |
196 | } | |
197 | ||
198 | /** | |
199 | * This method returns the attributes from the given entry. | |
200 | * | |
201 | * @param DN - the entry name; its getName() should return the canonical name | |
202 | * @param attributeIDs - the array of attribute names; if null, all available | |
203 | * attributes are returned | |
204 | * | |
205 | * @return the requested Attributes, or null, if no such entry exists | |
206 | */ | |
207 | 3234 | public Attributes getAttributes(java.security.Principal DN, String [] attributeIDs) { |
208 | ||
209 | 3234 | Attributes as = getAllAttributes(DN); |
210 | 3234 | if (as==null){ |
211 | 361 | return null; |
212 | //throw diagnosis=new PbaException("No such entry exists"); | |
213 | } | |
214 | 0 | if (attributeIDs==null) return as; // it is already a clone |
215 | ||
216 | 2873 | Attributes result = new BasicAttributes(); |
217 | 5746 | for (int i=0; i<attributeIDs.length; i++){ |
218 | 2873 | Attribute a = as.get(attributeIDs[i]); |
219 | 2873 | if (a!=null){ |
220 | // note that the way the repository is populated ensures that the names | |
221 | // of the attributes are unique in any entry; therefore this put() | |
222 | // operation always returns null | |
223 | 2873 | result.put(a); |
224 | } | |
225 | } | |
226 | ||
227 | //diagnosis=null; | |
228 | 2873 | return result; |
229 | } | |
230 | ||
231 | /** | |
232 | * This method returns all available attributes from the entry. It does the | |
233 | * same as getAttributes(DN, null) | |
234 | * | |
235 | * @param DN - the entry name; its getName method should return the canonical | |
236 | * name | |
237 | * | |
238 | * @return all available Attributes, or null, if no such entry exists | |
239 | */ | |
240 | 3234 | public Attributes getAllAttributes(java.security.Principal DN) { |
241 | 3234 | logger.debug("get a credential for "+DN.getName()+" from "+repository.getClass().getName()); |
242 | 3234 | Attributes as = (Attributes)repository.get(DN.getName().toUpperCase()); |
243 | 3234 | if (as==null){ |
244 | 361 | logger.debug("nothing is retrieved"); |
245 | 361 | return null; |
246 | //throw diagnosis=new PbaException("No such entry exists: "+DN.getName()); | |
247 | } | |
248 | //diagnosis=null; | |
249 | 2873 | ToRawCredential toRaw = new ToRawCredential(as,this.getClass().getName()); |
250 | 2873 | try { |
251 | 2873 | as = toRaw.convert(); |
252 | 2873 | logger.debug("successful"); |
253 | } catch (RepositoryException re) { | |
254 | 0 | return null; |
255 | } | |
256 | 2873 | return (Attributes)as.clone(); |
257 | } | |
258 | ||
259 | 0 | public int getStatus(){ |
260 | 0 | return diagnosis==null? SUCCESS_STATUS: FAILURE_STATUS; |
261 | } | |
262 | ||
263 | 3225 | public Throwable getDiagnosis(){ |
264 | 3225 | return diagnosis; |
265 | } | |
266 | ||
267 | } | |
268 |
|