001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.ldap;
017:
018: import org.apache.directory.server.core.configuration.Configuration;
019: import org.apache.directory.server.core.configuration.MutablePartitionConfiguration;
020: import org.apache.directory.server.core.configuration.MutableStartupConfiguration;
021: import org.apache.directory.server.core.jndi.CoreContextFactory;
022: import org.apache.directory.server.core.partition.DefaultPartitionNexus;
023:
024: import java.io.File;
025:
026: import java.util.HashSet;
027: import java.util.Properties;
028: import java.util.Set;
029:
030: import javax.naming.Context;
031: import javax.naming.NameAlreadyBoundException;
032: import javax.naming.NamingException;
033: import javax.naming.directory.Attribute;
034: import javax.naming.directory.Attributes;
035: import javax.naming.directory.BasicAttribute;
036: import javax.naming.directory.BasicAttributes;
037: import javax.naming.directory.DirContext;
038: import javax.naming.directory.InitialDirContext;
039:
040: /**
041: * An embedded LDAP test server, complete with test data for running the unit tests against.
042: *
043: * @author Luke Taylor
044: * @version $Id: LdapTestServer.java 1784 2007-02-24 21:00:24Z luke_t $
045: */
046: public class LdapTestServer {
047: //~ Instance fields ================================================================================================
048:
049: private DirContext serverContext;
050:
051: private MutableStartupConfiguration cfg;
052:
053: //~ Constructors ===================================================================================================
054:
055: /**
056: * Starts up and configures ApacheDS.
057: */
058: public LdapTestServer() {
059: startLdapServer();
060: createManagerUser();
061: initTestData();
062: }
063:
064: //~ Methods ========================================================================================================
065:
066: public void createGroup(String cn, String ou, String[] memberDns) {
067: Attributes group = new BasicAttributes("cn", cn);
068: Attribute members = new BasicAttribute("member");
069: Attribute orgUnit = new BasicAttribute("ou", ou);
070:
071: for (int i = 0; i < memberDns.length; i++) {
072: members.add(memberDns[i]);
073: }
074:
075: Attribute objectClass = new BasicAttribute("objectClass");
076: objectClass.add("top");
077: objectClass.add("groupOfNames");
078:
079: group.put(objectClass);
080: group.put(members);
081: group.put(orgUnit);
082:
083: try {
084: serverContext.createSubcontext("cn=" + cn + ",ou=groups",
085: group);
086: } catch (NameAlreadyBoundException ignore) {
087: // System.out.println(" group " + cn + " already exists.");
088: } catch (NamingException ne) {
089: System.err.println("Failed to create group.");
090: ne.printStackTrace();
091: }
092: }
093:
094: private void createManagerUser() {
095: Attributes user = new BasicAttributes("cn", "manager", true);
096: user.put("userPassword", "acegisecurity");
097:
098: Attribute objectClass = new BasicAttribute("objectClass");
099: user.put(objectClass);
100: objectClass.add("top");
101: objectClass.add("person");
102: objectClass.add("organizationalPerson");
103: objectClass.add("inetOrgPerson");
104: user.put("sn", "Manager");
105: user.put("cn", "manager");
106:
107: try {
108: serverContext.createSubcontext("cn=manager", user);
109: } catch (NameAlreadyBoundException ignore) {
110: // System.out.println("Manager user already exists.");
111: } catch (NamingException ne) {
112: System.err.println("Failed to create manager user.");
113: ne.printStackTrace();
114: }
115: }
116:
117: public void createOu(String name) {
118: Attributes ou = new BasicAttributes("ou", name);
119: Attribute objectClass = new BasicAttribute("objectClass");
120: objectClass.add("top");
121: objectClass.add("organizationalUnit");
122: ou.put(objectClass);
123:
124: try {
125: serverContext.createSubcontext("ou=" + name, ou);
126: } catch (NameAlreadyBoundException ignore) {
127: // System.out.println(" ou " + name + " already exists.");
128: } catch (NamingException ne) {
129: System.err.println("Failed to create ou.");
130: ne.printStackTrace();
131: }
132: }
133:
134: public void createUser(String uid, String cn, String password) {
135: Attributes user = new BasicAttributes("uid", uid);
136: user.put("cn", cn);
137: user.put("userPassword", LdapUtils.getUtf8Bytes(password));
138:
139: Attribute objectClass = new BasicAttribute("objectClass");
140: user.put(objectClass);
141: objectClass.add("top");
142: objectClass.add("person");
143: objectClass.add("organizationalPerson");
144: objectClass.add("inetOrgPerson");
145: user.put("sn", uid);
146:
147: try {
148: serverContext.createSubcontext("uid=" + uid + ",ou=people",
149: user);
150: } catch (NameAlreadyBoundException ignore) {
151: // System.out.println(" user " + uid + " already exists.");
152: } catch (NamingException ne) {
153: System.err.println("Failed to create user.");
154: ne.printStackTrace();
155: }
156: }
157:
158: public Configuration getConfiguration() {
159: return cfg;
160: }
161:
162: private void initConfiguration() throws NamingException {
163: // Create the partition for the acegi tests
164: MutablePartitionConfiguration acegiDit = new MutablePartitionConfiguration();
165: acegiDit.setName("acegisecurity");
166: acegiDit.setSuffix("dc=acegisecurity,dc=org");
167:
168: BasicAttributes attributes = new BasicAttributes();
169: BasicAttribute objectClass = new BasicAttribute("objectClass");
170: objectClass.add("top");
171: objectClass.add("domain");
172: objectClass.add("extensibleObject");
173: attributes.put(objectClass);
174: acegiDit.setContextEntry(attributes);
175:
176: Set indexedAttrs = new HashSet();
177: indexedAttrs.add("objectClass");
178: indexedAttrs.add("uid");
179: indexedAttrs.add("cn");
180: indexedAttrs.add("ou");
181: indexedAttrs.add("member");
182:
183: acegiDit.setIndexedAttributes(indexedAttrs);
184:
185: Set partitions = new HashSet();
186: partitions.add(acegiDit);
187:
188: cfg.setContextPartitionConfigurations(partitions);
189: }
190:
191: private void initTestData() {
192: createOu("people");
193: createOu("groups");
194: createUser("bob", "Bob Hamilton", "bobspassword");
195: createUser("ben", "Ben Alex",
196: "{SHA}nFCebWjxfaLbHHG1Qk5UU4trbvQ=");
197:
198: String[] developers = new String[] {
199: "uid=ben,ou=people,dc=acegisecurity,dc=org",
200: "uid=bob,ou=people,dc=acegisecurity,dc=org" };
201: createGroup("developers", "developer", developers);
202: createGroup("managers", "manager",
203: new String[] { developers[0] });
204: }
205:
206: public static void main(String[] args) {
207: LdapTestServer server = new LdapTestServer();
208: }
209:
210: private void startLdapServer() {
211: cfg = new MutableStartupConfiguration();
212:
213: // Attempt to use the maven target directory for the apache ds store. Property is passed
214: // through surefire plugin setup in pom.xml.
215:
216: String apacheWorkDir = System.getProperty("apacheDSWorkDir");
217:
218: if (apacheWorkDir == null) {
219: apacheWorkDir = System.getProperty("java.io.tmpdir")
220: + File.separator + "apacheds-work";
221: }
222:
223: File workingDir = new File(apacheWorkDir);
224:
225: // Delete any previous contents (often not compatible between apache-ds versions).
226: deleteDir(workingDir);
227:
228: ((MutableStartupConfiguration) cfg)
229: .setWorkingDirectory(workingDir);
230:
231: System.out.println("Ldap Server Working directory is "
232: + workingDir.getAbsolutePath());
233:
234: Properties env = new Properties();
235:
236: env
237: .setProperty(Context.PROVIDER_URL,
238: "dc=acegisecurity,dc=org");
239: env.setProperty(Context.INITIAL_CONTEXT_FACTORY,
240: CoreContextFactory.class.getName());
241: env.setProperty(Context.SECURITY_AUTHENTICATION, "simple");
242: env.setProperty(Context.SECURITY_PRINCIPAL,
243: DefaultPartitionNexus.ADMIN_PRINCIPAL);
244: env.setProperty(Context.SECURITY_CREDENTIALS,
245: DefaultPartitionNexus.ADMIN_PASSWORD);
246:
247: try {
248: initConfiguration();
249: env.putAll(cfg.toJndiEnvironment());
250: serverContext = new InitialDirContext(env);
251: } catch (NamingException e) {
252: System.err.println("Failed to start Apache DS");
253: e.printStackTrace();
254: }
255: }
256:
257: /** Recursively deletes a directory */
258: private boolean deleteDir(File dir) {
259: if (dir.isDirectory()) {
260: String[] children = dir.list();
261: for (int i = 0; i < children.length; i++) {
262: boolean success = deleteDir(new File(dir, children[i]));
263: if (!success) {
264: return false;
265: }
266: }
267: }
268:
269: return dir.delete();
270: }
271: }
|