Search in sources :

Example 1 with DeleteRequest

use of org.forgerock.opendj.ldap.requests.DeleteRequest in project OpenAM by OpenRock.

the class RemoveReferralsStep method deleteExistingReferrals.

private void deleteExistingReferrals() throws UpgradeException {
    try (Connection connection = getConnection()) {
        for (DN referral : referralsToBeRemoved) {
            UpgradeProgress.reportStart(AUDIT_REMOVING_REFERRAL_START, referral);
            DeleteRequest request = LDAPRequests.newDeleteRequest(referral);
            connection.delete(request);
            UpgradeProgress.reportEnd(AUDIT_UPGRADE_SUCCESS);
        }
    } catch (DataLayerException | LdapException e) {
        UpgradeProgress.reportEnd(AUDIT_UPGRADE_FAIL);
        throw new UpgradeException("Failed to delete referrals", e);
    }
}
Also used : UpgradeException(org.forgerock.openam.upgrade.UpgradeException) DataLayerException(org.forgerock.openam.sm.datalayer.api.DataLayerException) Connection(org.forgerock.opendj.ldap.Connection) DN(org.forgerock.opendj.ldap.DN) DeleteRequest(org.forgerock.opendj.ldap.requests.DeleteRequest) LdapException(org.forgerock.opendj.ldap.LdapException)

Example 2 with DeleteRequest

use of org.forgerock.opendj.ldap.requests.DeleteRequest in project OpenAM by OpenRock.

the class DataLayer method deleteEntry.

/**
     * Delete entry from the server
     * 
     * @param guid
     *            globally unique identifier for the entry
     * @exception AccessRightsException
     *                insufficient access
     * @exception EntryNotFoundException
     *                if the entry is not found
     * @exception UMSException
     *                Fail to delete the entry
     *
     * @supported.api
     */
public void deleteEntry(java.security.Principal principal, Guid guid) throws UMSException {
    if (guid == null) {
        String msg = i18n.getString(IUMSConstants.BAD_ID);
        throw new IllegalArgumentException(msg);
    }
    String id = guid.getDn();
    ResultCode errorCode;
    try {
        DeleteRequest request = LDAPRequests.newDeleteRequest(id);
        int retry = 0;
        while (retry <= connNumRetry) {
            if (debug.messageEnabled()) {
                debug.message("DataLayer.deleteEntry retry: " + retry);
            }
            try (Connection conn = getConnection(principal)) {
                conn.delete(request);
                return;
            } catch (LdapException e) {
                if (!retryErrorCodes.contains(e.getResult().getResultCode()) || retry == connNumRetry) {
                    throw e;
                }
                retry++;
                try {
                    Thread.sleep(connRetryInterval);
                } catch (InterruptedException ex) {
                }
            }
        }
    } catch (LdapException e) {
        debug.error("Exception in DataLayer.deleteEntry for DN: " + id, e);
        errorCode = e.getResult().getResultCode();
        String[] args = { id };
        if (ResultCode.NO_SUCH_OBJECT.equals(errorCode)) {
            throw new EntryNotFoundException(i18n.getString(IUMSConstants.ENTRY_NOT_FOUND, args), e);
        } else if (ResultCode.INSUFFICIENT_ACCESS_RIGHTS.equals(errorCode)) {
            throw new AccessRightsException(i18n.getString(IUMSConstants.INSUFFICIENT_ACCESS_DELETE, args), e);
        } else {
            throw new UMSException(i18n.getString(IUMSConstants.UNABLE_TO_DELETE_ENTRY, args), e);
        }
    }
}
Also used : Connection(org.forgerock.opendj.ldap.Connection) ByteString(org.forgerock.opendj.ldap.ByteString) DeleteRequest(org.forgerock.opendj.ldap.requests.DeleteRequest) LdapException(org.forgerock.opendj.ldap.LdapException) ResultCode(org.forgerock.opendj.ldap.ResultCode)

Example 3 with DeleteRequest

use of org.forgerock.opendj.ldap.requests.DeleteRequest in project OpenAM by OpenRock.

the class LdapAdapterTest method shouldUseConnectionForDelete.

@Test
public void shouldUseConnectionForDelete() throws Exception {
    // Given
    String tokenId = "badger";
    DN testDN = DN.rootDN();
    Result successResult = mockSuccessfulResult();
    given(mockConnection.delete(any(DeleteRequest.class))).willReturn(successResult);
    given(mockConversion.generateTokenDN(anyString())).willReturn(testDN);
    // When
    adapter.delete(mockConnection, tokenId);
    // Then
    ArgumentCaptor<DeleteRequest> captor = ArgumentCaptor.forClass(DeleteRequest.class);
    verify(mockConnection).delete(captor.capture());
    assertEquals(testDN, captor.getValue().getName());
}
Also used : DN(org.forgerock.opendj.ldap.DN) DeleteRequest(org.forgerock.opendj.ldap.requests.DeleteRequest) Result(org.forgerock.opendj.ldap.responses.Result) Test(org.testng.annotations.Test)

Example 4 with DeleteRequest

use of org.forgerock.opendj.ldap.requests.DeleteRequest in project OpenAM by OpenRock.

the class LdifUtils method createSchemaFromLDIF.

/**
     * Creates LDAP schema from LDIF file.
     *
     * @param ldif LDIF object.
     * @param ld LDAP Connection.
     * @throws IOException If an error occurs when reading the LDIF file.
     */
public static void createSchemaFromLDIF(LDIFChangeRecordReader ldif, final Connection ld) throws IOException {
    while (ldif.hasNext()) {
        final ChangeRecord changeRecord = ldif.readChangeRecord();
        changeRecord.accept(new ChangeRecordVisitor<Void, Void>() {

            @Override
            public Void visitChangeRecord(Void aVoid, AddRequest change) {
                try {
                    change.addControl(TransactionIdControl.newControl(AuditRequestContext.createSubTransactionIdValue()));
                    ld.add(change);
                } catch (LdapException e) {
                    if (ResultCode.ENTRY_ALREADY_EXISTS.equals(e.getResult().getResultCode())) {
                        for (Attribute attr : change.getAllAttributes()) {
                            ModifyRequest modifyRequest = LDAPRequests.newModifyRequest(change.getName());
                            modifyRequest.addModification(new Modification(ModificationType.ADD, attr));
                            try {
                                ld.modify(modifyRequest);
                            } catch (LdapException ex) {
                                DEBUG.warning("LDAPUtils.createSchemaFromLDIF - Could not modify schema: {}", modifyRequest, ex);
                            }
                        }
                    } else {
                        DEBUG.warning("LDAPUtils.createSchemaFromLDIF - Could not add to schema: {}", change, e);
                    }
                }
                return null;
            }

            @Override
            public Void visitChangeRecord(Void aVoid, ModifyRequest change) {
                try {
                    change.addControl(TransactionIdControl.newControl(AuditRequestContext.createSubTransactionIdValue()));
                    ld.modify(change);
                } catch (LdapException e) {
                    DEBUG.warning("LDAPUtils.createSchemaFromLDIF - Could not modify schema: {}", change, e);
                }
                return null;
            }

            @Override
            public Void visitChangeRecord(Void aVoid, ModifyDNRequest change) {
                return null;
            }

            @Override
            public Void visitChangeRecord(Void aVoid, DeleteRequest change) {
                DEBUG.message("Delete request ignored: {}", changeRecord);
                return null;
            }
        }, null);
    }
}
Also used : AddRequest(org.forgerock.opendj.ldap.requests.AddRequest) ModifyDNRequest(org.forgerock.opendj.ldap.requests.ModifyDNRequest) Modification(org.forgerock.opendj.ldap.Modification) Attribute(org.forgerock.opendj.ldap.Attribute) ModifyRequest(org.forgerock.opendj.ldap.requests.ModifyRequest) ChangeRecord(org.forgerock.opendj.ldif.ChangeRecord) LdapException(org.forgerock.opendj.ldap.LdapException) DeleteRequest(org.forgerock.opendj.ldap.requests.DeleteRequest)

Example 5 with DeleteRequest

use of org.forgerock.opendj.ldap.requests.DeleteRequest in project OpenAM by OpenRock.

the class RemoveReferralsStepTest method simpleSuccessfulPassThrough.

@Test
public void simpleSuccessfulPassThrough() throws Exception {
    // Given
    given(connectionFactory.create()).willReturn(connection);
    given(connection.search(isA(SearchRequest.class))).willReturn(entryReader);
    given(entryReader.hasNext()).willReturn(true).willReturn(false);
    given(entryReader.readEntry()).willReturn(resultEntry);
    given(resultEntry.getName()).willReturn(DN.valueOf("ou=test,ou=forgerock,ou=org"));
    JsonValue jsonValue = json(object(field("name", "ref"), field("mapApplNameToResources", object(field("app1", array("*://*:*/*")))), field("realms", array("/a"))));
    Set<String> values = singleton("serializable=" + jsonValue.toString());
    Attribute attribute = new LinkedAttribute("ou", values);
    AttributeParser attributeParser = AttributeParser.parseAttribute(attribute);
    given(resultEntry.parseAttribute("sunKeyValue")).willReturn(attributeParser);
    Application app1 = new Application();
    app1.setName("app1");
    app1.addAllResourceTypeUuids(singleton("123"));
    given(applicationService.getApplication(isA(Subject.class), eq("/"), eq("app1"))).willReturn(app1);
    given(policyServiceFactory.get(eq("/a"), isA(Subject.class))).willReturn(policyService);
    Privilege policy1 = new OpenSSOPrivilege();
    policy1.setName("pol1");
    given(policyService.findAllPoliciesByApplication("app1")).willReturn(singletonList(policy1));
    ResourceType resourceType1 = ResourceType.builder().setName("resourceType1").setUUID("123").build();
    given(resourceTypeService.getResourceType(isA(Subject.class), eq("/"), eq("123"))).willReturn(resourceType1);
    // When
    testStep.initialize();
    boolean isApplicable = testStep.isApplicable();
    testStep.perform();
    String shortReport = testStep.getShortReport("");
    String longReport = testStep.getDetailedReport("");
    // Then
    assertThat(isApplicable).isTrue();
    assertThat(shortReport).containsSequence("applications to be cloned", "Referrals found");
    assertThat(longReport).containsSequence("app1", "ou=test,ou=forgerock,ou=org");
    verify(resourceTypeService).saveResourceType(isA(Subject.class), eq("/a"), resourceTypeCaptor.capture());
    verify(applicationService).saveApplication(isA(Subject.class), eq("/a"), applicationCaptor.capture());
    verify(policyService).modify(policyCaptor.capture());
    ResourceType clonedResourceType = resourceTypeCaptor.getValue();
    assertThat(clonedResourceType).isNotEqualTo(resourceType1);
    assertThat(clonedResourceType.getName()).isEqualTo("resourceType1");
    Application clonedApplication = applicationCaptor.getValue();
    assertThat(clonedApplication).isNotEqualTo(app1);
    assertThat(clonedApplication.getName()).isEqualTo("app1");
    assertThat(clonedApplication.getResourceTypeUuids()).containsExactly(clonedResourceType.getUUID());
    Privilege modifiedPolicy = policyCaptor.getValue();
    assertThat(modifiedPolicy).isEqualTo(modifiedPolicy);
    assertThat(modifiedPolicy.getResourceTypeUuid()).isEqualTo(clonedResourceType.getUUID());
    verify(connection).delete(deleteRequestCaptor.capture());
    DeleteRequest request = deleteRequestCaptor.getValue();
    assertThat(request.getName().toString()).isEqualTo("ou=test,ou=forgerock,ou=org");
}
Also used : SearchRequest(org.forgerock.opendj.ldap.requests.SearchRequest) Attribute(org.forgerock.opendj.ldap.Attribute) LinkedAttribute(org.forgerock.opendj.ldap.LinkedAttribute) JsonValue(org.forgerock.json.JsonValue) OpenSSOPrivilege(com.sun.identity.entitlement.opensso.OpenSSOPrivilege) ResourceType(org.forgerock.openam.entitlement.ResourceType) Subject(javax.security.auth.Subject) LinkedAttribute(org.forgerock.opendj.ldap.LinkedAttribute) AttributeParser(org.forgerock.opendj.ldap.AttributeParser) OpenSSOPrivilege(com.sun.identity.entitlement.opensso.OpenSSOPrivilege) Privilege(com.sun.identity.entitlement.Privilege) Application(com.sun.identity.entitlement.Application) DeleteRequest(org.forgerock.opendj.ldap.requests.DeleteRequest) Test(org.testng.annotations.Test)

Aggregations

DeleteRequest (org.forgerock.opendj.ldap.requests.DeleteRequest)5 LdapException (org.forgerock.opendj.ldap.LdapException)3 Attribute (org.forgerock.opendj.ldap.Attribute)2 Connection (org.forgerock.opendj.ldap.Connection)2 DN (org.forgerock.opendj.ldap.DN)2 Test (org.testng.annotations.Test)2 Application (com.sun.identity.entitlement.Application)1 Privilege (com.sun.identity.entitlement.Privilege)1 OpenSSOPrivilege (com.sun.identity.entitlement.opensso.OpenSSOPrivilege)1 Subject (javax.security.auth.Subject)1 JsonValue (org.forgerock.json.JsonValue)1 ResourceType (org.forgerock.openam.entitlement.ResourceType)1 DataLayerException (org.forgerock.openam.sm.datalayer.api.DataLayerException)1 UpgradeException (org.forgerock.openam.upgrade.UpgradeException)1 AttributeParser (org.forgerock.opendj.ldap.AttributeParser)1 ByteString (org.forgerock.opendj.ldap.ByteString)1 LinkedAttribute (org.forgerock.opendj.ldap.LinkedAttribute)1 Modification (org.forgerock.opendj.ldap.Modification)1 ResultCode (org.forgerock.opendj.ldap.ResultCode)1 AddRequest (org.forgerock.opendj.ldap.requests.AddRequest)1