use of org.forgerock.opendj.ldap.Connection in project OpenAM by OpenRock.
the class SMSRepositoryMig method migrate.
private static void migrate(ConnectionFactory factory, String host, int port, String binddn, String pw, String basedn, String flatfiledir) throws Exception {
// check args
if (port < 0 || binddn == null || binddn.length() == 0 || pw == null || pw.length() == 0 || basedn == null || basedn.length() == 0 || flatfiledir == null || flatfiledir.length() == 0) {
throw new IllegalArgumentException("SMSRepositoryMig: One or more invalid " + "arguments in constructor");
}
// Create the SMSFlatFileObject
SMSFlatFileObject smsFlatFileObject = new SMSFlatFileObject();
try (Connection conn = factory.getConnection()) {
// Loop through LDAP attributes, create SMS object for each.
ConnectionEntryReader res = conn.search(LDAPRequests.newSearchRequest("ou=services," + basedn, SearchScope.BASE_OBJECT, "(objectclass=*)", "*"));
while (res.hasNext()) {
if (res.isReference()) {
//ignore
res.readReference();
System.out.println("ERROR: LDAP Referral not supported.");
System.out.println("LDAPReferralException received");
} else {
SearchResultEntry entry;
try {
entry = res.readEntry();
createSMSEntry(smsFlatFileObject, entry.getName().toString(), entry.getAllAttributes());
} catch (LdapException e) {
System.out.println("ERROR: LDAP Exception encountered: " + e.toString());
e.printStackTrace();
}
}
}
}
}
use of org.forgerock.opendj.ldap.Connection in project OpenAM by OpenRock.
the class ImportServiceConfiguration method getLDAPConnection.
private Connection getLDAPConnection() throws CLIException {
IOutput outputWriter = getOutputWriter();
if (isVerbose()) {
outputWriter.printlnMessage(getResourceString("import-service-configuration-connecting-to-ds"));
}
try {
Connection conn;
DSConfigMgr dsCfg = DSConfigMgr.getDSConfigMgr();
ServerGroup sg = dsCfg.getServerGroup("sms");
if (sg != null) {
conn = dsCfg.getNewConnectionFactory("sms", LDAPUser.Type.AUTH_ADMIN).getConnection();
} else {
throw new CLIException(getResourceString("import-service-configuration-not-connect-to-ds"), ExitCodes.REQUEST_CANNOT_BE_PROCESSED, null);
}
if (isVerbose()) {
outputWriter.printlnMessage(getResourceString("import-service-configuration-connected-to-ds"));
}
return conn;
} catch (LDAPServiceException | LdapException e) {
throw new CLIException(getResourceString("import-service-configuration-not-connect-to-ds"), ExitCodes.REQUEST_CANNOT_BE_PROCESSED, null);
}
}
use of org.forgerock.opendj.ldap.Connection in project OpenAM by OpenRock.
the class ImportServiceConfiguration method handleRequest.
/**
* Services a Commandline Request.
*
* @param rc Request Context.
* @throws CLIException if the request cannot serviced.
*/
public void handleRequest(RequestContext rc) throws CLIException {
super.handleRequest(rc);
String xmlFile = getStringOptionValue(IArgument.XML_FILE);
String encryptSecret = getStringOptionValue(IArgument.ENCRYPT_SECRET);
try {
encryptSecret = CLIUtil.getFileContent(getCommandManager(), encryptSecret).trim();
} catch (CLIException clie) {
//There is no encryptSecret file
}
validateEncryptSecret(xmlFile, encryptSecret);
// disable notification
SystemProperties.initializeProperties(Constants.SMS_ENABLE_DB_NOTIFICATION, "true");
SystemProperties.initializeProperties("com.sun.am.event.connection.disable.list", "sm,aci,um");
// disable error debug messsage
SystemProperties.initializeProperties(Constants.SYS_PROPERTY_INSTALL_TIME, "true");
IOutput outputWriter = getOutputWriter();
try (Connection ldConnection = getLDAPConnection()) {
InitializeSystem initSys = CommandManager.initSys;
SSOToken ssoToken = initSys.getSSOToken(getAdminPassword());
DirectoryServerVendor.Vendor vendor = DirectoryServerVendor.getInstance().query(ldConnection);
if (!vendor.name.equals(DirectoryServerVendor.OPENDJ) && !vendor.name.equals(DirectoryServerVendor.OPENDS) && !vendor.name.equals(DirectoryServerVendor.ODSEE)) {
throw new CLIException(getResourceString("import-service-configuration-unknown-ds"), ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
}
loadLDIF(vendor, ldConnection);
String ouServices = "ou=services," + initSys.getRootSuffix();
if (this.isOuServicesExists(ssoToken, ouServices)) {
System.out.print(getResourceString("import-service-configuration-prompt-delete") + " ");
String value = (new BufferedReader(new InputStreamReader(System.in))).readLine();
value = value.trim();
if (value.equalsIgnoreCase("y") || value.equalsIgnoreCase("yes")) {
outputWriter.printlnMessage(getResourceString("import-service-configuration-processing"));
deleteOuServicesDescendents(ssoToken, ouServices);
importData(xmlFile, encryptSecret, ssoToken);
}
} else {
outputWriter.printlnMessage(getResourceString("import-service-configuration-processing"));
importData(xmlFile, encryptSecret, ssoToken);
}
} catch (SMSException e) {
throw new CLIException(e.getMessage(), ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
} catch (LdapException e) {
throw new CLIException(e.getMessage(), ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
} catch (SSOException e) {
throw new CLIException(e.getMessage(), ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
} catch (IOException e) {
throw new CLIException(e.getMessage(), ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
} catch (LoginException e) {
throw new CLIException(getCommandManager().getResourceBundle().getString("exception-LDAP-login-failed"), ExitCodes.LDAP_LOGIN_FAILED);
} catch (InvalidAuthContextException e) {
throw new CLIException(getCommandManager().getResourceBundle().getString("exception-LDAP-login-failed"), ExitCodes.LDAP_LOGIN_FAILED);
}
}
use of org.forgerock.opendj.ldap.Connection in project OpenAM by OpenRock.
the class AddAMSDKIdRepoPlugin method loadLDIFs.
private void loadLDIFs() throws Exception {
CommandManager mgr = getCommandManager();
List ldifs = getLDIFs();
for (Iterator i = directoryServers.iterator(); i.hasNext(); ) {
String dshost = (String) i.next();
try (ConnectionFactory factory = getLDAPConnection(new DSEntry(dshost));
Connection ld = factory.getConnection()) {
String dbName = LDAPUtils.getDBName(basedn, ld);
for (Iterator j = ldifs.iterator(); j.hasNext(); ) {
String file = (String) j.next();
String content = CLIUtil.getFileContent(mgr, file);
String swapped = tagswap(content, dbName);
loadLDIF(ld, swapped);
}
}
}
}
use of org.forgerock.opendj.ldap.Connection in project OpenAM by OpenRock.
the class AMCertStore method getCertificate.
/**
* Return matched certificate from ldap certificate store
*/
public X509Certificate getCertificate() {
/*
* Lookup the certificate in the LDAP certificate
* directory and compare the values.
*/
try (Connection ldc = getConnection()) {
if (ldc == null) {
return null;
}
ConnectionEntryReader results = getSearchResults(ldc, USERCERTIFICATE, USERCERTIFICATE_BINARY, CACERTIFICATE, CACERTIFICATE_BINARY);
while (results != null && results.hasNext()) {
// "Found search results for: " + cn , 2);
if (results.isEntry()) {
SearchResultEntry entry = results.readEntry();
/*
* Retrieve the certificate from the store
*/
Attribute certAttribute = entry.getAttribute(USERCERTIFICATE);
if (certAttribute == null) {
certAttribute = entry.getAttribute(USERCERTIFICATE_BINARY);
if (certAttribute == null) {
// an end-entity certificate can be a CA certificate
certAttribute = entry.getAttribute(CACERTIFICATE);
if (certAttribute == null) {
certAttribute = entry.getAttribute(CACERTIFICATE_BINARY);
}
if (certAttribute == null) {
debug.message("AMCertStore.getCertificate: Certificate - get usercertificate is null ");
continue;
}
}
}
for (ByteString value : certAttribute) {
byte[] bytes = value.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
X509Certificate c = null;
try {
c = (X509Certificate) cf.generateCertificate(bis);
} catch (CertificateParsingException e) {
debug.error("AMCertStore.getCertificate : " + "Error in Certificate parsing : ", e);
}
if (c != null) {
return c;
}
}
// inner while
} else {
SearchResultReference reference = results.readReference();
debug.warning("Got an LDAP reference - only expected entries. Ignoring: {}", reference);
}
}
// outer while
} catch (Exception e) {
debug.error("AMCertStore.getCertificate : " + "Certificate - Error finding registered certificate = ", e);
}
return null;
}
Aggregations