001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 package org.apache.directory.server.integ; 020 021 022 import java.util.Hashtable; 023 024 import javax.naming.Context; 025 import javax.naming.ldap.InitialLdapContext; 026 import javax.naming.ldap.LdapContext; 027 028 import netscape.ldap.LDAPConnection; 029 030 import org.apache.directory.ldap.client.api.LdapConnection; 031 import org.apache.directory.server.constants.ServerDNConstants; 032 import org.apache.directory.server.core.integ.IntegrationUtils; 033 import org.apache.directory.server.ldap.LdapServer; 034 import org.apache.directory.shared.ldap.jndi.JndiUtils; 035 import org.apache.directory.shared.ldap.message.control.Control; 036 import org.slf4j.Logger; 037 import org.slf4j.LoggerFactory; 038 039 040 public class ServerIntegrationUtils extends IntegrationUtils 041 { 042 /** The class logger */ 043 private static final Logger LOG = LoggerFactory.getLogger( ServerIntegrationUtils.class ); 044 private static final String CTX_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory"; 045 046 private static final String DEFAULT_HOST = "localhost"; 047 private static final int DEFAULT_PORT = 10389; 048 private static final String DEFAULT_ADMIN = ServerDNConstants.ADMIN_SYSTEM_DN; 049 private static final String DEFAULT_PASSWORD = "secret"; 050 051 052 /** 053 * Creates a JNDI LdapContext with a connection over the wire using the 054 * SUN LDAP provider. The connection is made using the administrative 055 * user as the principalDN. The context is to the rootDSE. 056 * 057 * @param ldapServer the LDAP server to get the connection to 058 * @return an LdapContext as the administrative user to the RootDSE 059 * @throws Exception if there are problems creating the context 060 */ 061 public static LdapContext getWiredContext( LdapServer ldapServer ) throws Exception 062 { 063 return getWiredContext( ldapServer, null ); 064 } 065 066 067 /** 068 * Creates a JNDI LdapContext with a connection over the wire using the 069 * SUN LDAP provider. The connection is made using the administrative 070 * user as the principalDN. The context is to the rootDSE. 071 * 072 * @param ldapServer the LDAP server to get the connection to 073 * @return an LdapContext as the administrative user to the RootDSE 074 * @throws Exception if there are problems creating the context 075 */ 076 public static LdapContext getWiredContext( LdapServer ldapServer, String principalDn, String password ) 077 throws Exception 078 { 079 LOG.debug( "Creating a wired context to local LDAP server on port {}", ldapServer.getPort() ); 080 Hashtable<String, String> env = new Hashtable<String, String>(); 081 env.put( Context.INITIAL_CONTEXT_FACTORY, CTX_FACTORY ); 082 env.put( Context.PROVIDER_URL, "ldap://localhost:" + ldapServer.getPort() ); 083 env.put( Context.SECURITY_PRINCIPAL, principalDn ); 084 env.put( Context.SECURITY_CREDENTIALS, password ); 085 env.put( Context.SECURITY_AUTHENTICATION, "simple" ); 086 return new InitialLdapContext( env, null ); 087 } 088 089 090 /** 091 * Creates a JNDI LdapContext with a connection over the wire using the 092 * SUN LDAP provider. The connection is made using the administrative 093 * user as the principalDN. The context is to the rootDSE. 094 * 095 * @param ldapServer the LDAP server to get the connection to 096 * @return an LdapContext as the administrative user to the RootDSE 097 * @throws Exception if there are problems creating the context 098 */ 099 public static LdapContext getWiredContext( LdapServer ldapServer, Control[] controls ) throws Exception 100 { 101 LOG.debug( "Creating a wired context to local LDAP server on port {}", ldapServer.getPort() ); 102 Hashtable<String, String> env = new Hashtable<String, String>(); 103 env.put( Context.INITIAL_CONTEXT_FACTORY, CTX_FACTORY ); 104 env.put( Context.PROVIDER_URL, "ldap://localhost:" + ldapServer.getPort() ); 105 env.put( Context.SECURITY_PRINCIPAL, ServerDNConstants.ADMIN_SYSTEM_DN ); 106 env.put( Context.SECURITY_CREDENTIALS, "secret" ); 107 env.put( Context.SECURITY_AUTHENTICATION, "simple" ); 108 return new InitialLdapContext( env, JndiUtils.toJndiControls( controls ) ); 109 } 110 111 112 /** 113 * Creates a JNDI LdapContext with a connection over the wire using the 114 * SUN LDAP provider. The connection is made using the administrative 115 * user as the principalDN. The context is to the rootDSE. 116 * 117 * @param ldapServer the LDAP server to get the connection to 118 * @return an LdapContext as the administrative user to the RootDSE 119 * @throws Exception if there are problems creating the context 120 */ 121 public static LdapContext getWiredContextThrowOnRefferal( LdapServer ldapServer ) throws Exception 122 { 123 LOG.debug( "Creating a wired context to local LDAP server on port {}", ldapServer.getPort() ); 124 Hashtable<String, String> env = new Hashtable<String, String>(); 125 env.put( Context.INITIAL_CONTEXT_FACTORY, CTX_FACTORY ); 126 env.put( Context.PROVIDER_URL, "ldap://localhost:" + ldapServer.getPort() ); 127 env.put( Context.SECURITY_PRINCIPAL, ServerDNConstants.ADMIN_SYSTEM_DN ); 128 env.put( Context.SECURITY_CREDENTIALS, "secret" ); 129 env.put( Context.SECURITY_AUTHENTICATION, "simple" ); 130 env.put( Context.REFERRAL, "throw" ); 131 return new InitialLdapContext( env, null ); 132 } 133 134 135 /** 136 * Creates a JNDI LdapContext with a connection over the wire using the 137 * SUN LDAP provider. The connection is made using the administrative 138 * user as the principalDN. The context is to the rootDSE. 139 * 140 * @param ldapServer the LDAP server to get the connection to 141 * @return an LdapContext as the administrative user to the RootDSE 142 * @throws Exception if there are problems creating the context 143 */ 144 public static LdapContext getWiredContextRefferalIgnore( LdapServer ldapServer ) throws Exception 145 { 146 LOG.debug( "Creating a wired context to local LDAP server on port {}", ldapServer.getPort() ); 147 Hashtable<String, String> env = new Hashtable<String, String>(); 148 env.put( Context.INITIAL_CONTEXT_FACTORY, CTX_FACTORY ); 149 env.put( Context.PROVIDER_URL, "ldap://localhost:" + ldapServer.getPort() ); 150 env.put( Context.SECURITY_PRINCIPAL, ServerDNConstants.ADMIN_SYSTEM_DN ); 151 env.put( Context.SECURITY_CREDENTIALS, "secret" ); 152 env.put( Context.SECURITY_AUTHENTICATION, "simple" ); 153 env.put( Context.REFERRAL, "ignore" ); 154 return new InitialLdapContext( env, null ); 155 } 156 157 158 /** 159 * Creates a JNDI LdapContext with a connection over the wire using the 160 * SUN LDAP provider. The connection is made using the administrative 161 * user as the principalDN. The context is to the rootDSE. 162 * 163 * @param ldapServer the LDAP server to get the connection to 164 * @return an LdapContext as the administrative user to the RootDSE 165 * @throws Exception if there are problems creating the context 166 */ 167 public static LdapContext getWiredContextFollowOnRefferal( LdapServer ldapServer ) throws Exception 168 { 169 LOG.debug( "Creating a wired context to local LDAP server on port {}", ldapServer.getPort() ); 170 Hashtable<String, String> env = new Hashtable<String, String>(); 171 env.put( Context.INITIAL_CONTEXT_FACTORY, CTX_FACTORY ); 172 env.put( Context.PROVIDER_URL, "ldap://localhost:" + ldapServer.getPort() ); 173 env.put( Context.SECURITY_PRINCIPAL, ServerDNConstants.ADMIN_SYSTEM_DN ); 174 env.put( Context.SECURITY_CREDENTIALS, "secret" ); 175 env.put( Context.SECURITY_AUTHENTICATION, "simple" ); 176 env.put( Context.REFERRAL, "follow" ); 177 return new InitialLdapContext( env, null ); 178 } 179 180 181 public static LDAPConnection getWiredConnection( LdapServer ldapServer ) throws Exception 182 { 183 String testServer = System.getProperty( "ldap.test.server", null ); 184 185 if ( testServer == null ) 186 { 187 return getWiredConnection( ldapServer, ServerDNConstants.ADMIN_SYSTEM_DN, "secret" ); 188 } 189 190 LOG.debug( "ldap.test.server = " + testServer ); 191 192 String admin = System.getProperty( testServer + ".admin", DEFAULT_ADMIN ); 193 LOG.debug( testServer + ".admin = " + admin ); 194 195 String password = System.getProperty( testServer + ".password", DEFAULT_PASSWORD ); 196 LOG.debug( testServer + ".password = " + password ); 197 198 String host = System.getProperty( testServer + ".host", DEFAULT_HOST ); 199 LOG.debug( testServer + ".host = " + host ); 200 201 int port = Integer.parseInt( System.getProperty( testServer + ".port", Integer.toString( DEFAULT_PORT ) ) ); 202 LOG.debug( testServer + ".port = " + port ); 203 204 LDAPConnection conn = new LDAPConnection(); 205 conn.connect( 3, host, port, admin, password ); 206 return conn; 207 } 208 209 210 public static LDAPConnection getWiredConnection( LdapServer ldapServer, String principalDn, String password ) 211 throws Exception 212 { 213 LDAPConnection conn = new LDAPConnection(); 214 conn.connect( 3, "localhost", ldapServer.getPort(), principalDn, password ); 215 return conn; 216 } 217 218 219 public static LdapConnection getClientApiConnection( LdapServer ldapServer ) throws Exception 220 { 221 LdapConnection conn = new LdapConnection( "localhost", ldapServer.getPort() ); 222 conn.bind( ServerDNConstants.ADMIN_SYSTEM_DN, "secret" ); 223 return conn; 224 } 225 226 }