Search in sources :

Example 1 with TenantsEntity

use of org.apache.nifi.web.api.entity.TenantsEntity in project nifi by apache.

the class SearchUsersEndpointMerger method merge.

@Override
public final NodeResponse merge(final URI uri, final String method, final Set<NodeResponse> successfulResponses, final Set<NodeResponse> problematicResponses, final NodeResponse clientResponse) {
    if (!canHandle(uri, method)) {
        throw new IllegalArgumentException("Cannot use Endpoint Mapper of type " + getClass().getSimpleName() + " to map responses for URI " + uri + ", HTTP Method " + method);
    }
    final TenantsEntity responseEntity = clientResponse.getClientResponse().readEntity(TenantsEntity.class);
    final Collection<TenantEntity> userEntities = responseEntity.getUsers();
    final Collection<TenantEntity> userGroupEntities = responseEntity.getUserGroups();
    for (final NodeResponse nodeResponse : successfulResponses) {
        final TenantsEntity nodeResponseEntity = nodeResponse == clientResponse ? responseEntity : nodeResponse.getClientResponse().readEntity(TenantsEntity.class);
        // only retain users/groups that all nodes agree on
        userEntities.retainAll(nodeResponseEntity.getUsers());
        userGroupEntities.retainAll(nodeResponseEntity.getUserGroups());
    }
    // create a new client response
    return new NodeResponse(clientResponse, responseEntity);
}
Also used : TenantsEntity(org.apache.nifi.web.api.entity.TenantsEntity) TenantEntity(org.apache.nifi.web.api.entity.TenantEntity) NodeResponse(org.apache.nifi.cluster.manager.NodeResponse)

Example 2 with TenantsEntity

use of org.apache.nifi.web.api.entity.TenantsEntity in project nifi by apache.

the class TenantsResource method searchTenants.

// ------------
// search users
// ------------
/**
 * Searches for a tenant with a given identity.
 *
 * @param value Search value that will be matched against a user/group identity
 * @return Tenants match the specified criteria
 */
@GET
@Consumes(MediaType.WILDCARD)
@Produces(MediaType.APPLICATION_JSON)
@Path("search-results")
@ApiOperation(value = "Searches for a tenant with the specified identity", notes = NON_GUARANTEED_ENDPOINT, response = TenantsEntity.class, authorizations = { @Authorization(value = "Read - /tenants") })
@ApiResponses(value = { @ApiResponse(code = 400, message = "NiFi was unable to complete the request because it was invalid. The request should not be retried without modification."), @ApiResponse(code = 401, message = "Client could not be authenticated."), @ApiResponse(code = 403, message = "Client is not authorized to make this request."), @ApiResponse(code = 404, message = "The specified resource could not be found."), @ApiResponse(code = 409, message = "The request was valid but NiFi was not in the appropriate state to process it. Retrying the same request later may be successful.") })
public Response searchTenants(@ApiParam(value = "Identity to search for.", required = true) @QueryParam("q") @DefaultValue(StringUtils.EMPTY) String value) {
    // ensure we're running with a configurable authorizer
    if (!AuthorizerCapabilityDetection.isManagedAuthorizer(authorizer)) {
        throw new IllegalStateException(AccessPolicyDAO.MSG_NON_MANAGED_AUTHORIZER);
    }
    if (isReplicateRequest()) {
        return replicate(HttpMethod.GET);
    }
    // authorize access
    serviceFacade.authorizeAccess(lookup -> {
        final Authorizable tenants = lookup.getTenant();
        tenants.authorize(authorizer, RequestAction.READ, NiFiUserUtils.getNiFiUser());
    });
    final List<TenantEntity> userMatches = new ArrayList<>();
    final List<TenantEntity> userGroupMatches = new ArrayList<>();
    // get the users
    for (final UserEntity userEntity : serviceFacade.getUsers()) {
        final UserDTO user = userEntity.getComponent();
        if (StringUtils.isBlank(value) || StringUtils.containsIgnoreCase(user.getIdentity(), value)) {
            final TenantDTO tenant = new TenantDTO();
            tenant.setId(user.getId());
            tenant.setIdentity(user.getIdentity());
            tenant.setConfigurable(user.getConfigurable());
            final TenantEntity entity = new TenantEntity();
            entity.setPermissions(userEntity.getPermissions());
            entity.setRevision(userEntity.getRevision());
            entity.setId(userEntity.getId());
            entity.setComponent(tenant);
            userMatches.add(entity);
        }
    }
    // get the user groups
    for (final UserGroupEntity userGroupEntity : serviceFacade.getUserGroups()) {
        final UserGroupDTO userGroup = userGroupEntity.getComponent();
        if (StringUtils.isBlank(value) || StringUtils.containsIgnoreCase(userGroup.getIdentity(), value)) {
            final TenantDTO tenant = new TenantDTO();
            tenant.setId(userGroup.getId());
            tenant.setIdentity(userGroup.getIdentity());
            tenant.setConfigurable(userGroup.getConfigurable());
            final TenantEntity entity = new TenantEntity();
            entity.setPermissions(userGroupEntity.getPermissions());
            entity.setRevision(userGroupEntity.getRevision());
            entity.setId(userGroupEntity.getId());
            entity.setComponent(tenant);
            userGroupMatches.add(entity);
        }
    }
    // build the response
    final TenantsEntity results = new TenantsEntity();
    results.setUsers(userMatches);
    results.setUserGroups(userGroupMatches);
    // generate an 200 - OK response
    return noCache(Response.ok(results)).build();
}
Also used : TenantsEntity(org.apache.nifi.web.api.entity.TenantsEntity) TenantEntity(org.apache.nifi.web.api.entity.TenantEntity) UserDTO(org.apache.nifi.web.api.dto.UserDTO) TenantDTO(org.apache.nifi.web.api.dto.TenantDTO) ArrayList(java.util.ArrayList) UserGroupDTO(org.apache.nifi.web.api.dto.UserGroupDTO) Authorizable(org.apache.nifi.authorization.resource.Authorizable) UserGroupEntity(org.apache.nifi.web.api.entity.UserGroupEntity) UserEntity(org.apache.nifi.web.api.entity.UserEntity) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

TenantEntity (org.apache.nifi.web.api.entity.TenantEntity)2 TenantsEntity (org.apache.nifi.web.api.entity.TenantsEntity)2 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 ArrayList (java.util.ArrayList)1 Consumes (javax.ws.rs.Consumes)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 Authorizable (org.apache.nifi.authorization.resource.Authorizable)1 NodeResponse (org.apache.nifi.cluster.manager.NodeResponse)1 TenantDTO (org.apache.nifi.web.api.dto.TenantDTO)1 UserDTO (org.apache.nifi.web.api.dto.UserDTO)1 UserGroupDTO (org.apache.nifi.web.api.dto.UserGroupDTO)1 UserEntity (org.apache.nifi.web.api.entity.UserEntity)1 UserGroupEntity (org.apache.nifi.web.api.entity.UserGroupEntity)1