Search in sources :

Example 36 with ResourceRequest

use of ddf.catalog.operation.ResourceRequest in project ddf by codice.

the class FilterPluginTest method setup.

@Before
public void setup() {
    AuthorizingRealm realm = mock(AuthorizingRealm.class);
    when(realm.getName()).thenReturn("mockRealm");
    when(realm.isPermitted(any(PrincipalCollection.class), any(Permission.class))).then(makeDecision());
    Collection<org.apache.shiro.realm.Realm> realms = new ArrayList<>();
    realms.add(realm);
    DefaultSecurityManager manager = new DefaultSecurityManager();
    manager.setRealms(realms);
    SimplePrincipalCollection principalCollection = new SimplePrincipalCollection(new Principal() {

        @Override
        public String getName() {
            return "testuser";
        }
    }, realm.getName());
    Subject systemSubject = new MockSubject(manager, principalCollection);
    plugin = new FilterPlugin(new Security()) {

        @Override
        protected Subject getSystemSubject() {
            return systemSubject;
        }
    };
    plugin.setPermissions(new PermissionsImpl());
    plugin.setSubjectOperations(new SubjectUtils());
    plugin.setSecurityLogger(mock(SecurityLogger.class));
    QueryRequestImpl request = getSampleRequest();
    Map<String, Serializable> properties = new HashMap<>();
    Subject subject = new MockSubject(manager, principalCollection);
    properties.put(SecurityConstants.SECURITY_SUBJECT, subject);
    request.setProperties(properties);
    incomingResponse = new QueryResponseImpl(request);
    ResourceRequest resourceRequest = mock(ResourceRequest.class);
    when(resourceRequest.getProperties()).thenReturn(properties);
    resourceResponse = new ResourceResponseImpl(resourceRequest, mock(Resource.class));
    resourceResponse.setProperties(properties);
    DeleteRequest deleteRequest = mock(DeleteRequest.class);
    when(deleteRequest.getProperties()).thenReturn(properties);
    List<Metacard> deletedMetacards = new ArrayList<>();
    deletedMetacards.add(getExactRolesMetacard());
    deleteResponse = new DeleteResponseImpl(deleteRequest, properties, deletedMetacards);
    List<Metacard> badDeletedMetacards = new ArrayList<>();
    badDeletedMetacards.add(getMoreRolesMetacard());
    badDeleteResponse = new DeleteResponseImpl(deleteRequest, properties, badDeletedMetacards);
    createRequest = new CreateRequestImpl(getExactRolesMetacard());
    createRequest.setProperties(properties);
    badCreateRequest = new CreateRequestImpl(getMoreRolesMetacard());
    badCreateRequest.setProperties(properties);
    updateRequest = new UpdateRequestImpl(getExactRolesMetacard().getId(), getExactRolesMetacard());
    updateRequest.setProperties(properties);
    ResultImpl result1 = new ResultImpl(getMoreRolesMetacard());
    ResultImpl result2 = new ResultImpl(getMissingRolesMetacard());
    ResultImpl result3 = new ResultImpl(getExactRolesMetacard());
    ResultImpl result4 = new ResultImpl(getNoRolesMetacard());
    ResultImpl result5 = new ResultImpl(getNoSecurityAttributeMetacard());
    incomingResponse.addResult(result1, false);
    incomingResponse.addResult(result2, false);
    incomingResponse.addResult(result3, false);
    incomingResponse.addResult(result4, false);
    incomingResponse.addResult(result5, true);
}
Also used : SubjectUtils(ddf.security.service.impl.SubjectUtils) Serializable(java.io.Serializable) FilterPlugin(ddf.catalog.security.filter.plugin.FilterPlugin) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) ResultImpl(ddf.catalog.data.impl.ResultImpl) Security(org.codice.ddf.security.impl.Security) AuthorizingRealm(org.apache.shiro.realm.AuthorizingRealm) PermissionsImpl(ddf.security.permission.impl.PermissionsImpl) CollectionPermission(ddf.security.permission.CollectionPermission) Permission(org.apache.shiro.authz.Permission) KeyValueCollectionPermission(ddf.security.permission.KeyValueCollectionPermission) AuthorizingRealm(org.apache.shiro.realm.AuthorizingRealm) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) ResourceResponseImpl(ddf.catalog.operation.impl.ResourceResponseImpl) DefaultSecurityManager(org.apache.shiro.mgt.DefaultSecurityManager) DelegatingSubject(org.apache.shiro.subject.support.DelegatingSubject) Subject(ddf.security.Subject) QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) Metacard(ddf.catalog.data.Metacard) DeleteResponseImpl(ddf.catalog.operation.impl.DeleteResponseImpl) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) ResourceRequest(ddf.catalog.operation.ResourceRequest) UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl) DeleteRequest(ddf.catalog.operation.DeleteRequest) Principal(java.security.Principal) SecurityLogger(ddf.security.audit.SecurityLogger) Before(org.junit.Before)

Example 37 with ResourceRequest

use of ddf.catalog.operation.ResourceRequest in project ddf by codice.

the class CatalogPolicyTest method testPreResource.

@Test
public void testPreResource() throws StopProcessingException {
    policyPlugin.setReadPermissions(new String[] { "role=admin" });
    ResourceRequest mock = mock(ResourceRequest.class);
    Map<String, Serializable> properties = new HashMap<>();
    properties.put(Constants.LOCAL_DESTINATION_KEY, true);
    when(mock.getProperties()).thenReturn(properties);
    PolicyResponse response = policyPlugin.processPreResource(mock);
    assertThat(response.itemPolicy().size(), equalTo(0));
    assertThat(response.operationPolicy().size(), equalTo(1));
    properties.put(Constants.LOCAL_DESTINATION_KEY, false);
    response = policyPlugin.processPreResource(mock);
    assertThat(response.itemPolicy().size(), equalTo(0));
    assertThat(response.operationPolicy().size(), equalTo(0));
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) ResourceRequest(ddf.catalog.operation.ResourceRequest) PolicyResponse(ddf.catalog.plugin.PolicyResponse) Test(org.junit.Test)

Example 38 with ResourceRequest

use of ddf.catalog.operation.ResourceRequest in project ddf by codice.

the class ResourceOperations method getResource.

// 
// 
// 
@SuppressWarnings("javadoc")
ResourceResponse getResource(ResourceRequest resourceRequest, boolean isEnterprise, String resourceSiteName, boolean fanoutEnabled) throws IOException, ResourceNotFoundException, ResourceNotSupportedException {
    ResourceResponse resourceResponse = null;
    ResourceRequest resourceReq = resourceRequest;
    ResourceRetriever retriever = null;
    if (fanoutEnabled) {
        isEnterprise = true;
    }
    if (resourceSiteName == null && !isEnterprise) {
        throw new ResourceNotFoundException("resourceSiteName cannot be null when obtaining resource.");
    }
    validateGetResourceRequest(resourceReq);
    try {
        resourceReq = preProcessPreAuthorizationPlugins(resourceReq);
        resourceReq = processPreResourcePolicyPlugins(resourceReq);
        resourceReq = processPreResourceAccessPlugins(resourceReq);
        resourceReq = processPreResourcePlugins(resourceReq);
        Map<String, Serializable> requestProperties = resourceReq.getProperties();
        LOGGER.debug("Attempting to get resource from siteName: {}", resourceSiteName);
        // At this point we pull out the properties and use them.
        Serializable sourceIdProperty = requestProperties.get(ResourceRequest.SOURCE_ID);
        final String namedSource = (sourceIdProperty != null) ? sourceIdProperty.toString() : resourceSiteName;
        Serializable enterpriseProperty = requestProperties.get(ResourceRequest.IS_ENTERPRISE);
        if (enterpriseProperty != null && Boolean.parseBoolean(enterpriseProperty.toString())) {
            isEnterprise = true;
        }
        // check if the resourceRequest has an ID only
        // If so, the metacard needs to be found and the Resource URI
        StringBuilder resolvedSourceIdHolder = new StringBuilder();
        ResourceInfo resourceInfo = getResourceInfo(resourceReq, namedSource, isEnterprise, resolvedSourceIdHolder, requestProperties, fanoutEnabled);
        if (resourceInfo == null) {
            throw new ResourceNotFoundException("Resource could not be found for the given attribute value: " + resourceReq.getAttributeValue());
        }
        final URI responseURI = resourceInfo.getResourceUri();
        final Metacard metacard = resourceInfo.getMetacard();
        final String resolvedSourceId = resolvedSourceIdHolder.toString();
        LOGGER.debug("resolvedSourceId = {}", resolvedSourceId);
        LOGGER.debug("ID = {}", getId());
        // since resolvedSourceId specifies what source the product
        // metacard resides on, we can just
        // change resourceSiteName to be that value, and then the
        // following if-else statements will
        // handle retrieving the product on the correct source
        final String resourceSourceName = (isEnterprise) ? resolvedSourceId : namedSource;
        // retrieve product from specified federated site if not in cache
        if (!resourceSourceName.equals(getId())) {
            LOGGER.debug("Searching federatedSource {} for resource.", resourceSourceName);
            LOGGER.debug("metacard for product found on source: {}", resolvedSourceId);
            final List<FederatedSource> sources = frameworkProperties.getFederatedSources().stream().filter(e -> e.getId().equals(resourceSourceName)).collect(Collectors.toList());
            if (!sources.isEmpty()) {
                if (sources.size() != 1) {
                    LOGGER.debug("Found multiple federatedSources for id: {}", resourceSourceName);
                }
                FederatedSource source = sources.get(0);
                LOGGER.debug("Adding federated site to federated query: {}", source.getId());
                LOGGER.debug("Retrieving product from remote source {}", source.getId());
                retriever = new RemoteResourceRetriever(source, responseURI, requestProperties);
            } else {
                LOGGER.debug("Could not find federatedSource: {}", resourceSourceName);
            }
        } else {
            LOGGER.debug("Retrieving product from local source {}", resourceSourceName);
            retriever = new LocalResourceRetriever(frameworkProperties.getResourceReaders(), responseURI, metacard, requestProperties);
        }
        try {
            resourceResponse = frameworkProperties.getDownloadManager().download(resourceRequest, metacard, retriever);
        } catch (DownloadException e) {
            LOGGER.info("Unable to download resource", e);
        }
        resourceResponse = putPropertiesInResponse(resourceRequest, resourceResponse);
        resourceResponse = validateFixGetResourceResponse(resourceResponse, resourceReq);
        resourceResponse = postProcessPreAuthorizationPlugins(resourceResponse, metacard);
        resourceResponse = processPostResourcePolicyPlugins(resourceResponse, metacard);
        resourceResponse = processPostResourceAccessPlugins(resourceResponse, metacard);
        resourceResponse = processPostResourcePlugins(resourceResponse);
        resourceResponse.getProperties().put(Constants.METACARD_PROPERTY, metacard);
    } catch (DataUsageLimitExceededException e) {
        LOGGER.info("RuntimeException caused by: ", e);
        throw e;
    } catch (RuntimeException e) {
        LOGGER.info("RuntimeException caused by: ", e);
        throw new ResourceNotFoundException("Unable to find resource");
    } catch (StopProcessingException e) {
        LOGGER.info("Resource not supported", e);
        throw new ResourceNotSupportedException(FAILED_BY_GET_RESOURCE_PLUGIN + e.getMessage());
    }
    return resourceResponse;
}
Also used : LiteralImpl(ddf.catalog.filter.impl.LiteralImpl) StringUtils(org.apache.commons.lang.StringUtils) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) ResourceRetriever(ddf.catalog.resourceretriever.ResourceRetriever) DataUsageLimitExceededException(ddf.catalog.resource.DataUsageLimitExceededException) PluginExecutionException(ddf.catalog.plugin.PluginExecutionException) SystemInfo(org.codice.ddf.configuration.SystemInfo) Map(java.util.Map) PropertyIsEqualToLiteral(ddf.catalog.filter.impl.PropertyIsEqualToLiteral) PreResourcePlugin(ddf.catalog.plugin.PreResourcePlugin) URI(java.net.URI) PostResourcePlugin(ddf.catalog.plugin.PostResourcePlugin) PolicyPlugin(ddf.catalog.plugin.PolicyPlugin) FederatedSource(ddf.catalog.source.FederatedSource) Set(java.util.Set) ResourceReader(ddf.catalog.resource.ResourceReader) Collectors(java.util.stream.Collectors) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) Serializable(java.io.Serializable) QueryResponse(ddf.catalog.operation.QueryResponse) List(java.util.List) PreAuthorizationPlugin(ddf.catalog.plugin.PreAuthorizationPlugin) ResourceResponseImpl(ddf.catalog.operation.impl.ResourceResponseImpl) AccessPlugin(ddf.catalog.plugin.AccessPlugin) FilterDelegate(ddf.catalog.filter.FilterDelegate) ResourceResponse(ddf.catalog.operation.ResourceResponse) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) PropertyNameImpl(ddf.catalog.filter.impl.PropertyNameImpl) HashMap(java.util.HashMap) ContentItem(ddf.catalog.content.data.ContentItem) Constants(ddf.catalog.Constants) Metacard(ddf.catalog.data.Metacard) ResourceRequest(ddf.catalog.operation.ResourceRequest) QueryRequest(ddf.catalog.operation.QueryRequest) RemoteResourceRetriever(ddf.catalog.resourceretriever.RemoteResourceRetriever) DownloadException(org.codice.ddf.catalog.resource.download.DownloadException) LocalResourceRetriever(ddf.catalog.resourceretriever.LocalResourceRetriever) Result(ddf.catalog.data.Result) DescribableImpl(ddf.catalog.util.impl.DescribableImpl) QueryImpl(ddf.catalog.operation.impl.QueryImpl) PolicyResponse(ddf.catalog.plugin.PolicyResponse) Logger(org.slf4j.Logger) StopProcessingException(ddf.catalog.plugin.StopProcessingException) IOException(java.io.IOException) FederationException(ddf.catalog.federation.FederationException) FrameworkProperties(ddf.catalog.impl.FrameworkProperties) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) Query(ddf.catalog.operation.Query) Filter(org.opengis.filter.Filter) Collections(java.util.Collections) RemoteResourceRetriever(ddf.catalog.resourceretriever.RemoteResourceRetriever) Serializable(java.io.Serializable) ResourceRetriever(ddf.catalog.resourceretriever.ResourceRetriever) RemoteResourceRetriever(ddf.catalog.resourceretriever.RemoteResourceRetriever) LocalResourceRetriever(ddf.catalog.resourceretriever.LocalResourceRetriever) StopProcessingException(ddf.catalog.plugin.StopProcessingException) URI(java.net.URI) Metacard(ddf.catalog.data.Metacard) FederatedSource(ddf.catalog.source.FederatedSource) ResourceResponse(ddf.catalog.operation.ResourceResponse) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) DataUsageLimitExceededException(ddf.catalog.resource.DataUsageLimitExceededException) DownloadException(org.codice.ddf.catalog.resource.download.DownloadException) LocalResourceRetriever(ddf.catalog.resourceretriever.LocalResourceRetriever) ResourceRequest(ddf.catalog.operation.ResourceRequest) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException)

Example 39 with ResourceRequest

use of ddf.catalog.operation.ResourceRequest in project ddf by codice.

the class MetacardResourceSizePlugin method process.

@Override
public QueryResponse process(QueryResponse input) throws PluginExecutionException, StopProcessingException {
    List<Result> results = input.getResults();
    for (Result result : results) {
        Metacard metacard = result.getMetacard();
        if (metacard != null) {
            // Can only search cache based on Metacard - no way to generate ResourceRequest with
            // any properties for use in generating the CacheKey
            final ResourceRequest resourceRequest = new ResourceRequestById(metacard.getId());
            CacheKey cacheKey;
            String key = null;
            ReliableResource cachedResource = null;
            try {
                cacheKey = new CacheKey(metacard, resourceRequest);
                ClassLoader tccl = Thread.currentThread().getContextClassLoader();
                key = cacheKey.generateKey();
                try {
                    Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
                    cachedResource = (ReliableResource) cache.getValid(key, metacard);
                } finally {
                    Thread.currentThread().setContextClassLoader(tccl);
                }
            } catch (IllegalArgumentException e) {
                LOGGER.debug("Unable to retrieve cached resource for metacard id = {}", metacard.getId());
            }
            if (cachedResource != null) {
                long resourceSize = cachedResource.getSize();
                if (resourceSize > 0 && cachedResource.hasProduct()) {
                    LOGGER.debug("Setting resourceSize = {} for metacard ID = {}", resourceSize, metacard.getId());
                    Attribute resourceSizeAttribute = new AttributeImpl(Metacard.RESOURCE_SIZE, String.valueOf(resourceSize));
                    metacard.setAttribute(resourceSizeAttribute);
                } else {
                    LOGGER.debug("resourceSize <= 0 for metacard ID = {}", metacard.getId());
                }
            } else {
                LOGGER.debug("No cached resource for cache key = {}", key);
            }
        }
    }
    return input;
}
Also used : Attribute(ddf.catalog.data.Attribute) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) ReliableResource(ddf.catalog.resource.data.ReliableResource) Result(ddf.catalog.data.Result) Metacard(ddf.catalog.data.Metacard) ResourceRequestById(ddf.catalog.operation.impl.ResourceRequestById) ResourceRequest(ddf.catalog.operation.ResourceRequest) CacheKey(ddf.catalog.cache.impl.CacheKey)

Example 40 with ResourceRequest

use of ddf.catalog.operation.ResourceRequest in project ddf by codice.

the class CatalogMetricsTest method catalogPostResourceLatencyMetric.

@Test
public void catalogPostResourceLatencyMetric() throws Exception {
    Iterable<Tag> tags = Tags.of("successful", "true");
    ResourceRequest request = mock(ResourceRequest.class);
    ResourceResponse response = mock(ResourceResponse.class);
    when(response.getRequest()).thenReturn(request);
    when(request.getPropertyValue("metrics.catalog.operation.start")).thenReturn(System.currentTimeMillis() - 1000);
    catalogMetrics.process(response);
    assertThat(meterRegistry.summary("ddf.catalog.resource.latency", tags).count(), is(1L));
    assertThat(meterRegistry.summary("ddf.catalog.resource.latency", tags).max(), greaterThanOrEqualTo(1000.0));
}
Also used : ResourceResponse(ddf.catalog.operation.ResourceResponse) Tag(io.micrometer.core.instrument.Tag) ResourceRequest(ddf.catalog.operation.ResourceRequest) Test(org.junit.Test)

Aggregations

ResourceRequest (ddf.catalog.operation.ResourceRequest)42 Test (org.junit.Test)25 ResourceResponse (ddf.catalog.operation.ResourceResponse)16 Metacard (ddf.catalog.data.Metacard)12 Resource (ddf.catalog.resource.Resource)9 IOException (java.io.IOException)9 ResourceRequestById (ddf.catalog.operation.impl.ResourceRequestById)8 ResourceNotFoundException (ddf.catalog.resource.ResourceNotFoundException)8 ResourceNotSupportedException (ddf.catalog.resource.ResourceNotSupportedException)8 Serializable (java.io.Serializable)7 HashMap (java.util.HashMap)7 QueryRequest (ddf.catalog.operation.QueryRequest)6 Result (ddf.catalog.data.Result)5 QueryResponse (ddf.catalog.operation.QueryResponse)5 ResourceRetriever (ddf.catalog.resourceretriever.ResourceRetriever)5 DeleteRequest (ddf.catalog.operation.DeleteRequest)4 Matchers.anyString (org.mockito.Matchers.anyString)4 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)3 FederatedSource (ddf.catalog.source.FederatedSource)3 Subject (ddf.security.Subject)3