Search in sources :

Example 1 with SubjectUtils

use of ddf.security.service.impl.SubjectUtils in project ddf by codice.

the class RoleClaimsHandlerTest method testRetrieveClaimsValuesIgnoredReferences.

@Test
public void testRetrieveClaimsValuesIgnoredReferences() throws LdapException, SearchResultReferenceIOException {
    BindResult bindResult = mock(BindResult.class);
    ClaimsParameters claimsParameters;
    Connection connection = mock(Connection.class);
    ConnectionEntryReader membershipReader = mock(ConnectionEntryReader.class);
    ConnectionEntryReader groupNameReader = mock(ConnectionEntryReader.class);
    LinkedAttribute membershipAttribute = new LinkedAttribute("uid");
    LinkedAttribute groupNameAttribute = new LinkedAttribute("cn");
    ClaimsCollection processedClaims;
    RoleClaimsHandler claimsHandler;
    SearchResultEntry membershipSearchResult = mock(SearchResultEntry.class);
    DN resultDN = DN.valueOf("uid=tstark,");
    SearchResultEntry groupNameSearchResult = mock(SearchResultEntry.class);
    String groupName = "avengers";
    when(bindResult.isSuccess()).thenReturn(true);
    membershipAttribute.add("tstark");
    when(membershipSearchResult.getAttribute(anyString())).thenReturn(membershipAttribute);
    // simulate two items in the list (a reference and an entry)
    when(membershipReader.hasNext()).thenReturn(true, true, false);
    // test a reference followed by entries thereafter
    when(membershipReader.isEntry()).thenReturn(false, true);
    when(membershipReader.readEntry()).thenReturn(membershipSearchResult);
    when(membershipSearchResult.getName()).thenReturn(resultDN);
    groupNameAttribute.add(groupName);
    when(groupNameSearchResult.getAttribute(anyString())).thenReturn(groupNameAttribute);
    when(groupNameReader.hasNext()).thenReturn(true, true, false);
    when(groupNameReader.isEntry()).thenReturn(false, true);
    when(groupNameReader.readEntry()).thenReturn(groupNameSearchResult);
    when(connection.bind(any())).thenReturn(bindResult);
    when(connection.search(any(), any(), eq("(&(objectClass=groupOfNames)(|(member=uid=tstark,)(member=uid=tstark,)))"), any())).thenReturn(groupNameReader);
    when(connection.search(anyString(), any(), anyString(), matches("uid"))).thenReturn(membershipReader);
    claimsHandler = new RoleClaimsHandler(new AttributeMapLoader(new SubjectUtils()));
    ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class);
    when(mockConnectionFactory.getConnection()).thenReturn(connection);
    claimsHandler.setLdapConnectionFactory(mockConnectionFactory);
    claimsHandler.setBindMethod("Simple");
    claimsHandler.setBindUserCredentials("foo");
    claimsHandler.setBindUserDN("bar");
    claimsParameters = new ClaimsParametersImpl(new UserPrincipal(USER_CN), new HashSet<>(), new HashMap<>());
    processedClaims = claimsHandler.retrieveClaims(claimsParameters);
    assertThat(processedClaims, hasSize(1));
    Claim claim = processedClaims.get(0);
    assertThat(claim.getValues(), hasSize(1));
    assertThat(claim.getValues().get(0), equalTo(groupName));
}
Also used : SubjectUtils(ddf.security.service.impl.SubjectUtils) HashMap(java.util.HashMap) Connection(org.forgerock.opendj.ldap.Connection) DN(org.forgerock.opendj.ldap.DN) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) UserPrincipal(org.apache.karaf.jaas.boot.principal.UserPrincipal) ClaimsParameters(ddf.security.claims.ClaimsParameters) LinkedAttribute(org.forgerock.opendj.ldap.LinkedAttribute) ConnectionEntryReader(org.forgerock.opendj.ldif.ConnectionEntryReader) ConnectionFactory(org.forgerock.opendj.ldap.ConnectionFactory) ClaimsParametersImpl(ddf.security.claims.impl.ClaimsParametersImpl) BindResult(org.forgerock.opendj.ldap.responses.BindResult) ClaimsCollection(ddf.security.claims.ClaimsCollection) Claim(ddf.security.claims.Claim) SearchResultEntry(org.forgerock.opendj.ldap.responses.SearchResultEntry) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with SubjectUtils

use of ddf.security.service.impl.SubjectUtils in project ddf by codice.

the class ReliableResourceDownloaderTest method testNullReliableResourceCallableAndStatus.

@Test
public void testNullReliableResourceCallableAndStatus() throws Exception {
    ResourceResponse mockResponse = getMockResourceResponse(mockStream);
    ResourceRetriever mockResourceRetriever = mock(ResourceRetriever.class);
    when(mockResourceRetriever.retrieveResource(anyLong())).thenReturn(mockResponse);
    ReliableResourceStatus resourceStatus = new ReliableResourceStatus(DownloadStatus.RESOURCE_DOWNLOAD_INTERRUPTED, 0L);
    ReliableResourceCallable mockCallable = mock(ReliableResourceCallable.class);
    when(mockCallable.getReliableResourceStatus()).thenReturn(resourceStatus);
    int retries = 5;
    downloaderConfig.setMaxRetryAttempts(retries);
    ReliableResourceDownloader downloader = spy(new ReliableResourceDownloader(downloaderConfig, new AtomicBoolean(), DOWNLOAD_ID, mockResponse, mockResourceRetriever));
    doReturn(mockCallable).when(downloader).constructReliableResourceCallable(any(InputStream.class), any(CountingOutputStream.class), any(), anyInt(), any(Object.class));
    doThrow(new CancellationException()).when(downloader).constructResourceRetrievalMonitor();
    DownloadStatusInfoImpl downloadStatusInfo = new DownloadStatusInfoImpl();
    downloadStatusInfo.setSubjectOperations(new SubjectUtils());
    downloader.setupDownload(mockMetacard, downloadStatusInfo);
    downloader.run();
    verify(mockPublisher, times(retries)).postRetrievalStatus(any(ResourceResponse.class), eq(ProductRetrievalStatus.RETRYING), any(Metacard.class), anyString(), anyLong(), eq(DOWNLOAD_ID));
}
Also used : SubjectUtils(ddf.security.service.impl.SubjectUtils) DownloadStatusInfoImpl(ddf.catalog.event.retrievestatus.DownloadStatusInfoImpl) ResourceRetriever(ddf.catalog.resourceretriever.ResourceRetriever) MockInputStream(ddf.catalog.cache.MockInputStream) InputStream(java.io.InputStream) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CountingOutputStream(com.google.common.io.CountingOutputStream) Metacard(ddf.catalog.data.Metacard) ResourceResponse(ddf.catalog.operation.ResourceResponse) CancellationException(java.util.concurrent.CancellationException) Test(org.junit.Test)

Example 3 with SubjectUtils

use of ddf.security.service.impl.SubjectUtils in project ddf by codice.

the class ReliableResourceDownloaderTest method testBadKeyName.

@Test
public void testBadKeyName() throws Exception {
    Metacard metacard = getMockMetacard(DOWNLOAD_ID, ":badsourcename");
    downloaderConfig.setCacheEnabled(true);
    ResourceResponse mockResponse = getMockResourceResponse(mockStream);
    ResourceCacheImpl mockCache = mock(ResourceCacheImpl.class);
    when(mockCache.isPending(anyString())).thenReturn(false);
    when(mockCache.getProductCacheDirectory()).thenReturn(productCacheDirectory);
    downloaderConfig.setResourceCache(mockCache);
    ReliableResourceDownloader downloader = new ReliableResourceDownloader(downloaderConfig, new AtomicBoolean(), DOWNLOAD_ID, mockResponse, getMockRetriever());
    DownloadStatusInfoImpl downloadStatusInfo = new DownloadStatusInfoImpl();
    downloadStatusInfo.setSubjectOperations(new SubjectUtils());
    downloader.setupDownload(metacard, downloadStatusInfo);
    verify(mockCache, never()).addPendingCacheEntry(any(ReliableResource.class));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Metacard(ddf.catalog.data.Metacard) SubjectUtils(ddf.security.service.impl.SubjectUtils) ResourceCacheImpl(ddf.catalog.cache.impl.ResourceCacheImpl) ResourceResponse(ddf.catalog.operation.ResourceResponse) DownloadStatusInfoImpl(ddf.catalog.event.retrievestatus.DownloadStatusInfoImpl) ReliableResource(ddf.catalog.resource.data.ReliableResource) Test(org.junit.Test)

Example 4 with SubjectUtils

use of ddf.security.service.impl.SubjectUtils in project ddf by codice.

the class DownloadsStatusEventListenerTest method setUp.

@Before
public void setUp() throws IOException {
    testFolder.create();
    String productCacheDir = testFolder.newFolder("cache").toString();
    localResourcePath = testFolder.newFolder("resources").toPath();
    ReliableResourceDownloaderConfig downloaderConfig = new ReliableResourceDownloaderConfig();
    testDownloadStatusInfo = new DownloadStatusInfoImpl();
    testDownloadStatusInfo.setSubjectOperations(new SubjectUtils());
    ResourceCacheImpl testResourceCache = new ResourceCacheImpl();
    DownloadsStatusEventPublisher testEventPublisher = mock(DownloadsStatusEventPublisher.class);
    DownloadsStatusEventListener testEventListener = new DownloadsStatusEventListener();
    downloaderConfig.setResourceCache(testResourceCache);
    downloaderConfig.setEventPublisher(testEventPublisher);
    downloaderConfig.setEventListener(testEventListener);
    testDownloadManager = new ReliableResourceDownloadManager(downloaderConfig, testDownloadStatusInfo, Executors.newSingleThreadExecutor());
    testDownloadManager.setMaxRetryAttempts(1);
    testDownloadManager.setDelayBetweenAttempts(0);
    testDownloadManager.setMonitorPeriod(5);
}
Also used : SubjectUtils(ddf.security.service.impl.SubjectUtils) ResourceCacheImpl(ddf.catalog.cache.impl.ResourceCacheImpl) ReliableResourceDownloadManager(ddf.catalog.resource.download.ReliableResourceDownloadManager) ReliableResourceDownloaderConfig(ddf.catalog.resource.download.ReliableResourceDownloaderConfig) Before(org.junit.Before)

Example 5 with SubjectUtils

use of ddf.security.service.impl.SubjectUtils in project ddf by codice.

the class AbstractDownloadsStatusEventPublisherTest method setupPublisherWithNoNotifications.

private void setupPublisherWithNoNotifications() {
    actionProvider = mock(ActionProvider.class);
    eventAdmin = mock(EventAdmin.class);
    publisher = new DownloadsStatusEventPublisher(eventAdmin, ImmutableList.of(actionProvider));
    publisher.setSubjectOperations(new SubjectUtils());
    publisher.setNotificationEnabled(false);
    publisher.setActivityEnabled(false);
}
Also used : ActionProvider(ddf.action.ActionProvider) SubjectUtils(ddf.security.service.impl.SubjectUtils) EventAdmin(org.osgi.service.event.EventAdmin)

Aggregations

SubjectUtils (ddf.security.service.impl.SubjectUtils)20 Test (org.junit.Test)10 Metacard (ddf.catalog.data.Metacard)5 DownloadStatusInfoImpl (ddf.catalog.event.retrievestatus.DownloadStatusInfoImpl)5 Before (org.junit.Before)5 Action (ddf.action.Action)4 ResourceCacheImpl (ddf.catalog.cache.impl.ResourceCacheImpl)4 ResourceResponse (ddf.catalog.operation.ResourceResponse)4 HashMap (java.util.HashMap)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 ActionProvider (ddf.action.ActionProvider)3 Claim (ddf.security.claims.Claim)3 ClaimsCollection (ddf.security.claims.ClaimsCollection)3 ClaimsParameters (ddf.security.claims.ClaimsParameters)3 ClaimsParametersImpl (ddf.security.claims.impl.ClaimsParametersImpl)3 HashSet (java.util.HashSet)3 UserPrincipal (org.apache.karaf.jaas.boot.principal.UserPrincipal)3 MockInputStream (ddf.catalog.cache.MockInputStream)2 SecurityLoggerImpl (ddf.security.audit.impl.SecurityLoggerImpl)2 IOException (java.io.IOException)2