Search in sources :

Example 21 with FeedMetadata

use of com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata in project kylo by Teradata.

the class FeedImporter method validateEntityAccess.

private boolean validateEntityAccess(FeedMetadata existingFeed, String feedCategory) {
    FeedMetadata importingFeed = importFeed.getFeedToImport();
    if (existingFeed != null) {
        FeedMetadata userAccessFeed = metadataAccess.read(() -> {
            return metadataService.getFeedByName(feedCategory, importingFeed.getSystemFeedName());
        });
        if (userAccessFeed == null || !userAccessFeed.hasAction(FeedAccessControl.EDIT_DETAILS.getSystemName())) {
            // error
            importFeed.setValid(false);
            if (importFeed.getTemplate() == null) {
                ImportTemplate importTemplate = new ImportTemplate(importFeed.getFileName());
                importFeed.setTemplate(importTemplate);
            }
            String msg = "Access Denied.  You do not have access to edit this feed.";
            importFeed.getImportOptions().addErrorMessage(ImportComponent.FEED_DATA, msg);
            importFeed.addErrorMessage(existingFeed, msg);
            importFeed.setValid(false);
            return false;
        } else {
            return true;
        }
    } else {
        // ensure the user can create under the category
        Category category = metadataAccess.read(() -> {
            return categoryProvider.findBySystemName(feedCategory);
        }, MetadataAccess.SERVICE);
        if (category == null) {
            // ensure the user has functional access to create categories
            boolean hasPermission = accessController.hasPermission(AccessController.SERVICES, FeedServicesAccessControl.EDIT_CATEGORIES);
            if (!hasPermission) {
                String msg = "Access Denied. The category for this feed," + feedCategory + ", doesn't exist and you do not have access to create a new category.";
                importFeed.getImportOptions().addErrorMessage(ImportComponent.FEED_DATA, msg);
                importFeed.addErrorMessage(existingFeed, msg);
                importFeed.setValid(false);
                return false;
            }
            return true;
        } else {
            // if the feed is new ensure the user has write access to create feeds
            return metadataAccess.read(() -> {
                // Query for Category and ensure the user has access to create feeds on that category
                Category domainCategory = categoryProvider.findBySystemName(feedCategory);
                if (domainCategory == null || (!domainCategory.getAllowedActions().hasPermission(CategoryAccessControl.CREATE_FEED))) {
                    String msg = "Access Denied. You do not have access to create feeds under the category " + feedCategory + ". Attempt made to create feed " + FeedNameUtil.fullName(feedCategory, importingFeed.getSystemFeedName()) + ".";
                    importFeed.getImportOptions().addErrorMessage(ImportComponent.FEED_DATA, msg);
                    importFeed.addErrorMessage(existingFeed, msg);
                    importFeed.setValid(false);
                    return false;
                }
                /*
                       TemplateAccessControl.CREATE_FEED permission is not being used right now.
                       Uncomment this code once/if we should be checking it

                    // Query for Template and ensure the user has access to create feeds
                    final RegisteredTemplate domainTemplate = registeredTemplateService.findRegisteredTemplate(
                        new RegisteredTemplateRequest.Builder().templateName(importingFeed.getTemplateName()).isFeedEdit(true).build());
                    if (domainTemplate != null && !registeredTemplateService.hasTemplatePermission(domainTemplate.getId(), TemplateAccessControl.CREATE_FEED)) {
                        final String msg = "Access Denied. You do not have access to create feeds using the template " + importingFeed.getTemplateName()
                                           + ". Attempt made to create feed " + FeedNameUtil.fullName(feedCategory, importingFeed.getSystemFeedName()) + ".";
                        feed.getImportOptions().addErrorMessage(ImportComponent.FEED_DATA, msg);
                        feed.addErrorMessage(existingFeed, msg);
                        feed.setValid(false);
                        return false;
                    }
                    */
                return true;
            });
        }
    }
}
Also used : Category(com.thinkbiganalytics.metadata.api.category.Category) FeedCategory(com.thinkbiganalytics.feedmgr.rest.model.FeedCategory) FeedMetadata(com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata) ImportTemplate(com.thinkbiganalytics.feedmgr.service.template.importing.model.ImportTemplate)

Example 22 with FeedMetadata

use of com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata in project kylo by Teradata.

the class FeedImporter method validateUserDatasources.

/**
 * Validates that user data sources can be imported with provided properties.
 *
 * @return {@code true} if the feed can be imported, or {@code false} otherwise
 */
private boolean validateUserDatasources() {
    FeedMetadata metadata = importFeed.getFeedToImport();
    final UploadProgressMessage statusMessage = uploadProgressService.addUploadStatus(importFeed.getImportOptions().getUploadKey(), "Validating data sources.");
    // Get data sources needing to be created
    final Set<String> availableDatasources = metadataAccess.read(() -> datasourceProvider.getDatasources(datasourceProvider.datasetCriteria().type(UserDatasource.class)).stream().map(com.thinkbiganalytics.metadata.api.datasource.Datasource::getId).map(Object::toString).collect(Collectors.toSet()));
    final ImportComponentOption componentOption = importFeedOptions.findImportComponentOption(ImportComponent.USER_DATASOURCES);
    final List<Datasource> providedDatasources = Optional.ofNullable(metadata.getUserDatasources()).orElse(Collections.emptyList());
    if (componentOption.getProperties().isEmpty()) {
        componentOption.setProperties(providedDatasources.stream().filter(datasource -> !availableDatasources.contains(datasource.getId())).map(datasource -> new ImportProperty(datasource.getName(), datasource.getId(), null, null, null)).collect(Collectors.toList()));
    }
    // Update feed with re-mapped data sources
    final boolean valid = componentOption.getProperties().stream().allMatch(property -> {
        if (property.getPropertyValue() != null) {
            ImportUtil.replaceDatasource(metadata, property.getProcessorId(), property.getPropertyValue());
            return true;
        } else {
            return false;
        }
    });
    if (valid) {
        statusMessage.update("Validated data sources.", true);
    } else {
        statusMessage.update("Validation Error. Additional properties are needed before uploading the feed.", false);
        importFeed.setValid(false);
    }
    uploadProgressService.completeSection(importFeed.getImportOptions(), ImportSection.Section.VALIDATE_USER_DATASOURCES);
    return valid;
}
Also used : UserDatasource(com.thinkbiganalytics.metadata.api.datasource.UserDatasource) Datasource(com.thinkbiganalytics.metadata.rest.model.data.Datasource) UploadProgressService(com.thinkbiganalytics.feedmgr.service.UploadProgressService) RegisteredTemplateService(com.thinkbiganalytics.feedmgr.service.template.RegisteredTemplateService) Category(com.thinkbiganalytics.metadata.api.category.Category) LoggerFactory(org.slf4j.LoggerFactory) DatasourceModelTransform(com.thinkbiganalytics.feedmgr.service.datasource.DatasourceModelTransform) TemplateImporter(com.thinkbiganalytics.feedmgr.service.template.importing.TemplateImporter) StringUtils(org.apache.commons.lang3.StringUtils) ByteArrayInputStream(java.io.ByteArrayInputStream) FeedAccessControl(com.thinkbiganalytics.metadata.api.feed.security.FeedAccessControl) NifiPropertyUtil(com.thinkbiganalytics.nifi.rest.support.NifiPropertyUtil) ImportUtil(com.thinkbiganalytics.feedmgr.util.ImportUtil) AccessController(com.thinkbiganalytics.security.AccessController) NifiFeed(com.thinkbiganalytics.feedmgr.rest.model.NifiFeed) CategoryAccessControl(com.thinkbiganalytics.metadata.api.category.security.CategoryAccessControl) FeedServicesAccessControl(com.thinkbiganalytics.feedmgr.security.FeedServicesAccessControl) MetadataAccess(com.thinkbiganalytics.metadata.api.MetadataAccess) ZipEntry(java.util.zip.ZipEntry) MetadataService(com.thinkbiganalytics.feedmgr.service.MetadataService) PolicyPropertyTypes(com.thinkbiganalytics.policy.PolicyPropertyTypes) MetadataFieldAnnotationFieldNameResolver(com.thinkbiganalytics.feedmgr.MetadataFieldAnnotationFieldNameResolver) ImportTemplate(com.thinkbiganalytics.feedmgr.service.template.importing.model.ImportTemplate) NifiProperty(com.thinkbiganalytics.nifi.rest.model.NifiProperty) Set(java.util.Set) UploadProgress(com.thinkbiganalytics.feedmgr.rest.model.UploadProgress) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) UploadProgressMessage(com.thinkbiganalytics.feedmgr.rest.model.UploadProgressMessage) ZipFileUtil(com.thinkbiganalytics.feedmgr.support.ZipFileUtil) List(java.util.List) CategoryProvider(com.thinkbiganalytics.metadata.api.category.CategoryProvider) ImportTemplateOptions(com.thinkbiganalytics.feedmgr.rest.model.ImportTemplateOptions) ImportFeed(com.thinkbiganalytics.feedmgr.service.feed.importing.model.ImportFeed) Optional(java.util.Optional) ImportFeedException(com.thinkbiganalytics.feedmgr.service.feed.ImportFeedException) ImportComponent(com.thinkbiganalytics.feedmgr.rest.ImportComponent) ZipInputStream(java.util.zip.ZipInputStream) FeedMetadata(com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata) ImportSection(com.thinkbiganalytics.feedmgr.rest.ImportSection) FeedCategory(com.thinkbiganalytics.feedmgr.rest.model.FeedCategory) Inject(javax.inject.Inject) ImportProperty(com.thinkbiganalytics.feedmgr.rest.model.ImportProperty) ImportType(com.thinkbiganalytics.feedmgr.rest.ImportType) ImportFeedOptions(com.thinkbiganalytics.feedmgr.rest.model.ImportFeedOptions) RegisteredTemplate(com.thinkbiganalytics.feedmgr.rest.model.RegisteredTemplate) TemplateImporterFactory(com.thinkbiganalytics.feedmgr.service.template.importing.TemplateImporterFactory) UserDatasource(com.thinkbiganalytics.metadata.api.datasource.UserDatasource) ImportTemplateRoutineFactory(com.thinkbiganalytics.feedmgr.service.template.importing.importprocess.ImportTemplateRoutineFactory) Logger(org.slf4j.Logger) FeedNameUtil(com.thinkbiganalytics.support.FeedNameUtil) Datasource(com.thinkbiganalytics.metadata.rest.model.data.Datasource) ImportTemplateRoutine(com.thinkbiganalytics.feedmgr.service.template.importing.importprocess.ImportTemplateRoutine) IOException(java.io.IOException) ImportException(com.thinkbiganalytics.feedmgr.service.template.importing.ImportException) DatasourceProvider(com.thinkbiganalytics.metadata.api.datasource.DatasourceProvider) Collections(java.util.Collections) LegacyNifiRestClient(com.thinkbiganalytics.nifi.rest.client.LegacyNifiRestClient) ImportComponentOption(com.thinkbiganalytics.feedmgr.rest.model.ImportComponentOption) InputStream(java.io.InputStream) FeedMetadata(com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata) ImportComponentOption(com.thinkbiganalytics.feedmgr.rest.model.ImportComponentOption) UploadProgressMessage(com.thinkbiganalytics.feedmgr.rest.model.UploadProgressMessage) ImportProperty(com.thinkbiganalytics.feedmgr.rest.model.ImportProperty)

Example 23 with FeedMetadata

use of com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata in project kylo by Teradata.

the class FeedImporter method validate.

public ImportFeed validate() {
    UploadProgressMessage feedImportStatusMessage = uploadProgressService.addUploadStatus(importFeedOptions.getUploadKey(), "Validating Feed import.");
    try {
        init();
        // read the JSON into the Feed object
        FeedMetadata metadata = importFeed.getFeedToImport();
        // validate the incoming category exists
        validateFeedCategory();
        // verify if we should overwrite the feed if it already exists
        String feedCategory = StringUtils.isNotBlank(importFeedOptions.getCategorySystemName()) ? importFeedOptions.getCategorySystemName() : metadata.getSystemCategoryName();
        // query for this feed.
        // first read in the feed as a service account
        FeedMetadata existingFeed = metadataAccess.read(() -> {
            return metadataService.getFeedByName(feedCategory, metadata.getSystemFeedName());
        }, MetadataAccess.SERVICE);
        if (!validateOverwriteExistingFeed(existingFeed)) {
            // exit
            return importFeed;
        }
        if (accessController.isEntityAccessControlled()) {
            if (!validateEntityAccess(existingFeed, feedCategory)) {
                return importFeed;
            }
        }
        // sensitive properties
        if (!validateSensitiveProperties()) {
            return importFeed;
        }
        // Valid data sources
        if (!validateUserDatasources()) {
            return importFeed;
        }
        // UploadProgressMessage statusMessage = uploadProgressService.addUploadStatus(options.getUploadKey(),"Validating the template data");
        TemplateImporter templateImporter = templateImporterFactory.apply(importFeed.getFileName(), file, importFeedOptions);
        ImportTemplate importTemplate = templateImporter.validate();
        // need to set the importOptions back to the feed options
        // find importOptions for the Template and add them back to the set of options
        // importFeed.getImportOptions().updateOptions(importTemplate.getImportOptions().getImportComponentOptions());
        importFeed.setTemplate(importTemplate);
        // statusMessage.update("Validated the template data",importTemplate.isValid());
        if (!importTemplate.isValid()) {
            importFeed.setValid(false);
            List<String> errorMessages = importTemplate.getTemplateResults().getAllErrors().stream().map(nifiError -> nifiError.getMessage()).collect(Collectors.toList());
            if (!errorMessages.isEmpty()) {
                for (String msg : errorMessages) {
                    importFeed.addErrorMessage(metadata, msg);
                }
            }
        }
    // statusMessage = uploadProgressService.addUploadStatus(options.getUploadKey(),"Validation complete: the feed is "+(importFeed.isValid() ? "valid" : "invalid"),true,importFeed.isValid());
    } catch (Exception e) {
        feedImportStatusMessage.update("Validation error. Feed import error: " + e.getMessage(), false);
        throw new UnsupportedOperationException("Error importing template  " + fileName + ".  " + e.getMessage());
    }
    feedImportStatusMessage.update("Validated Feed import.", importFeed.isValid());
    return this.importFeed;
}
Also used : UploadProgressMessage(com.thinkbiganalytics.feedmgr.rest.model.UploadProgressMessage) UploadProgressService(com.thinkbiganalytics.feedmgr.service.UploadProgressService) RegisteredTemplateService(com.thinkbiganalytics.feedmgr.service.template.RegisteredTemplateService) Category(com.thinkbiganalytics.metadata.api.category.Category) LoggerFactory(org.slf4j.LoggerFactory) DatasourceModelTransform(com.thinkbiganalytics.feedmgr.service.datasource.DatasourceModelTransform) TemplateImporter(com.thinkbiganalytics.feedmgr.service.template.importing.TemplateImporter) StringUtils(org.apache.commons.lang3.StringUtils) ByteArrayInputStream(java.io.ByteArrayInputStream) FeedAccessControl(com.thinkbiganalytics.metadata.api.feed.security.FeedAccessControl) NifiPropertyUtil(com.thinkbiganalytics.nifi.rest.support.NifiPropertyUtil) ImportUtil(com.thinkbiganalytics.feedmgr.util.ImportUtil) AccessController(com.thinkbiganalytics.security.AccessController) NifiFeed(com.thinkbiganalytics.feedmgr.rest.model.NifiFeed) CategoryAccessControl(com.thinkbiganalytics.metadata.api.category.security.CategoryAccessControl) FeedServicesAccessControl(com.thinkbiganalytics.feedmgr.security.FeedServicesAccessControl) MetadataAccess(com.thinkbiganalytics.metadata.api.MetadataAccess) ZipEntry(java.util.zip.ZipEntry) MetadataService(com.thinkbiganalytics.feedmgr.service.MetadataService) PolicyPropertyTypes(com.thinkbiganalytics.policy.PolicyPropertyTypes) MetadataFieldAnnotationFieldNameResolver(com.thinkbiganalytics.feedmgr.MetadataFieldAnnotationFieldNameResolver) ImportTemplate(com.thinkbiganalytics.feedmgr.service.template.importing.model.ImportTemplate) NifiProperty(com.thinkbiganalytics.nifi.rest.model.NifiProperty) Set(java.util.Set) UploadProgress(com.thinkbiganalytics.feedmgr.rest.model.UploadProgress) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) UploadProgressMessage(com.thinkbiganalytics.feedmgr.rest.model.UploadProgressMessage) ZipFileUtil(com.thinkbiganalytics.feedmgr.support.ZipFileUtil) List(java.util.List) CategoryProvider(com.thinkbiganalytics.metadata.api.category.CategoryProvider) ImportTemplateOptions(com.thinkbiganalytics.feedmgr.rest.model.ImportTemplateOptions) ImportFeed(com.thinkbiganalytics.feedmgr.service.feed.importing.model.ImportFeed) Optional(java.util.Optional) ImportFeedException(com.thinkbiganalytics.feedmgr.service.feed.ImportFeedException) ImportComponent(com.thinkbiganalytics.feedmgr.rest.ImportComponent) ZipInputStream(java.util.zip.ZipInputStream) FeedMetadata(com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata) ImportSection(com.thinkbiganalytics.feedmgr.rest.ImportSection) FeedCategory(com.thinkbiganalytics.feedmgr.rest.model.FeedCategory) Inject(javax.inject.Inject) ImportProperty(com.thinkbiganalytics.feedmgr.rest.model.ImportProperty) ImportType(com.thinkbiganalytics.feedmgr.rest.ImportType) ImportFeedOptions(com.thinkbiganalytics.feedmgr.rest.model.ImportFeedOptions) RegisteredTemplate(com.thinkbiganalytics.feedmgr.rest.model.RegisteredTemplate) TemplateImporterFactory(com.thinkbiganalytics.feedmgr.service.template.importing.TemplateImporterFactory) UserDatasource(com.thinkbiganalytics.metadata.api.datasource.UserDatasource) ImportTemplateRoutineFactory(com.thinkbiganalytics.feedmgr.service.template.importing.importprocess.ImportTemplateRoutineFactory) Logger(org.slf4j.Logger) FeedNameUtil(com.thinkbiganalytics.support.FeedNameUtil) Datasource(com.thinkbiganalytics.metadata.rest.model.data.Datasource) ImportTemplateRoutine(com.thinkbiganalytics.feedmgr.service.template.importing.importprocess.ImportTemplateRoutine) IOException(java.io.IOException) ImportException(com.thinkbiganalytics.feedmgr.service.template.importing.ImportException) DatasourceProvider(com.thinkbiganalytics.metadata.api.datasource.DatasourceProvider) Collections(java.util.Collections) LegacyNifiRestClient(com.thinkbiganalytics.nifi.rest.client.LegacyNifiRestClient) ImportComponentOption(com.thinkbiganalytics.feedmgr.rest.model.ImportComponentOption) InputStream(java.io.InputStream) TemplateImporter(com.thinkbiganalytics.feedmgr.service.template.importing.TemplateImporter) FeedMetadata(com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata) ImportTemplate(com.thinkbiganalytics.feedmgr.service.template.importing.model.ImportTemplate) ImportFeedException(com.thinkbiganalytics.feedmgr.service.feed.ImportFeedException) IOException(java.io.IOException) ImportException(com.thinkbiganalytics.feedmgr.service.template.importing.ImportException)

Example 24 with FeedMetadata

use of com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata in project kylo by Teradata.

the class FeedImporter method validateOverwriteExistingFeed.

private boolean validateOverwriteExistingFeed(FeedMetadata existingFeed) {
    FeedMetadata importingFeed = importFeed.getFeedToImport();
    if (existingFeed != null && !importFeedOptions.isImportAndOverwrite(ImportComponent.FEED_DATA)) {
        UploadProgressMessage statusMessage = uploadProgressService.addUploadStatus(importFeedOptions.getUploadKey(), "Validation error. " + importingFeed.getCategoryAndFeedName() + " already exists.", true, false);
        // if we dont have permission to overwrite then return with error that feed already exists
        importFeed.setValid(false);
        ImportTemplate importTemplate = new ImportTemplate(importFeed.getFileName());
        importFeed.setTemplate(importTemplate);
        String msg = "The feed " + existingFeed.getCategoryAndFeedName() + " already exists.";
        importFeed.getImportOptions().addErrorMessage(ImportComponent.FEED_DATA, msg);
        importFeed.addErrorMessage(existingFeed, msg);
        importFeed.setValid(false);
        return false;
    } else {
        String message = "Validated Feed data.  This import will " + (existingFeed != null ? "overwrite" : "create") + " the feed " + importingFeed.getCategoryAndFeedName();
        uploadProgressService.addUploadStatus(importFeedOptions.getUploadKey(), message, true, true);
    }
    uploadProgressService.completeSection(importFeedOptions, ImportSection.Section.VALIDATE_FEED);
    return true;
}
Also used : UploadProgressMessage(com.thinkbiganalytics.feedmgr.rest.model.UploadProgressMessage) FeedMetadata(com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata) ImportTemplate(com.thinkbiganalytics.feedmgr.service.template.importing.model.ImportTemplate)

Example 25 with FeedMetadata

use of com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata in project kylo by Teradata.

the class FeedImporter method validateFeedCategory.

private boolean validateFeedCategory() {
    FeedMetadata metadata = importFeed.getFeedToImport();
    boolean valid = true;
    if (StringUtils.isNotBlank(importFeedOptions.getCategorySystemName())) {
        UploadProgressMessage statusMessage = uploadProgressService.addUploadStatus(importFeedOptions.getUploadKey(), "Validating the newly specified category. Ensure " + importFeedOptions.getCategorySystemName() + " exists.");
        FeedCategory optionsCategory = metadataService.getCategoryBySystemName(importFeedOptions.getCategorySystemName());
        if (optionsCategory == null) {
            importFeed.setValid(false);
            statusMessage.update("Validation Error. The category " + importFeedOptions.getCategorySystemName() + " does not exist, or you dont have access to it.", false);
            valid = false;
        } else {
            if (valid) {
                metadata.getCategory().setSystemName(importFeedOptions.getCategorySystemName());
                statusMessage.update("Validated. The category " + importFeedOptions.getCategorySystemName() + " exists.", true);
            }
        }
    }
    uploadProgressService.completeSection(importFeedOptions, ImportSection.Section.VALIDATE_FEED_CATEGORY);
    return valid;
}
Also used : UploadProgressMessage(com.thinkbiganalytics.feedmgr.rest.model.UploadProgressMessage) FeedCategory(com.thinkbiganalytics.feedmgr.rest.model.FeedCategory) FeedMetadata(com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata)

Aggregations

FeedMetadata (com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata)76 NifiProperty (com.thinkbiganalytics.nifi.rest.model.NifiProperty)26 NifiFeed (com.thinkbiganalytics.feedmgr.rest.model.NifiFeed)23 FeedCategory (com.thinkbiganalytics.feedmgr.rest.model.FeedCategory)21 ArrayList (java.util.ArrayList)21 List (java.util.List)20 Collectors (java.util.stream.Collectors)20 RegisteredTemplate (com.thinkbiganalytics.feedmgr.rest.model.RegisteredTemplate)18 Set (java.util.Set)18 StringUtils (org.apache.commons.lang3.StringUtils)18 UIFeed (com.thinkbiganalytics.feedmgr.rest.model.UIFeed)17 Feed (com.thinkbiganalytics.metadata.api.feed.Feed)17 FeedSummary (com.thinkbiganalytics.feedmgr.rest.model.FeedSummary)16 Map (java.util.Map)16 Inject (javax.inject.Inject)16 Logger (org.slf4j.Logger)16 LoggerFactory (org.slf4j.LoggerFactory)16 FeedServicesAccessControl (com.thinkbiganalytics.feedmgr.security.FeedServicesAccessControl)15 AccessController (com.thinkbiganalytics.security.AccessController)15 Optional (java.util.Optional)15