Search in sources :

Example 1 with TenantDTO

use of org.apache.nifi.web.api.dto.TenantDTO in project nifi by apache.

the class UserGroupEntityMergerTest method testMergeAccessPolicy.

@Test
public void testMergeAccessPolicy() throws Exception {
    final NodeIdentifier node1 = new NodeIdentifier("node-1", "host-1", 8080, "host-1", 19998, null, null, null, false);
    final NodeIdentifier node2 = new NodeIdentifier("node-2", "host-2", 8081, "host-2", 19999, null, null, null, false);
    final PermissionsDTO permissed = new PermissionsDTO();
    permissed.setCanRead(true);
    permissed.setCanWrite(true);
    final TenantDTO user1DTO = new TenantDTO();
    user1DTO.setId("user-1");
    final TenantEntity user1Entity = new TenantEntity();
    user1Entity.setPermissions(permissed);
    user1Entity.setId(user1DTO.getId());
    user1Entity.setComponent(user1DTO);
    final TenantDTO user2DTO = new TenantDTO();
    user1DTO.setId("user-2");
    final TenantEntity user2Entity = new TenantEntity();
    user2Entity.setPermissions(permissed);
    user2Entity.setId(user2DTO.getId());
    user2Entity.setComponent(user2DTO);
    final AccessPolicyDTO policy1DTO = new AccessPolicyDTO();
    policy1DTO.setId("policy-1");
    final AccessPolicyEntity policy1Entity = new AccessPolicyEntity();
    policy1Entity.setPermissions(permissed);
    policy1Entity.setId(policy1DTO.getId());
    policy1Entity.setComponent(policy1DTO);
    final AccessPolicyDTO policy2DTO = new AccessPolicyDTO();
    policy2DTO.setId("policy-2");
    final AccessPolicyEntity policy2Entity = new AccessPolicyEntity();
    policy2Entity.setPermissions(permissed);
    policy2Entity.setId(policy2DTO.getId());
    policy2Entity.setComponent(policy2DTO);
    final UserGroupDTO userGroup1DTO = new UserGroupDTO();
    userGroup1DTO.setId("user-1");
    userGroup1DTO.setAccessPolicies(Stream.of(policy1Entity, policy2Entity).collect(Collectors.toSet()));
    userGroup1DTO.setUsers(Stream.of(user2Entity).collect(Collectors.toSet()));
    final UserGroupEntity userGroup1Entity = new UserGroupEntity();
    userGroup1Entity.setPermissions(permissed);
    userGroup1Entity.setId(userGroup1DTO.getId());
    userGroup1Entity.setComponent(userGroup1DTO);
    final UserGroupDTO userGroup2DTO = new UserGroupDTO();
    userGroup2DTO.setId("user-2");
    userGroup2DTO.setAccessPolicies(Stream.of(policy1Entity).collect(Collectors.toSet()));
    userGroup2DTO.setUsers(Stream.of(user1Entity, user2Entity).collect(Collectors.toSet()));
    final UserGroupEntity userGroup2Entity = new UserGroupEntity();
    userGroup2Entity.setPermissions(permissed);
    userGroup2Entity.setId(userGroup2DTO.getId());
    userGroup2Entity.setComponent(userGroup2DTO);
    final Map<NodeIdentifier, UserGroupEntity> nodeMap = new HashMap<>();
    nodeMap.put(node1, userGroup1Entity);
    nodeMap.put(node2, userGroup2Entity);
    final UserGroupEntityMerger merger = new UserGroupEntityMerger();
    merger.merge(userGroup1Entity, nodeMap);
    assertEquals(1, userGroup1DTO.getUsers().size());
    assertTrue(userGroup1DTO.getAccessPolicies().contains(policy1Entity));
    assertEquals(1, userGroup1DTO.getUsers().size());
    assertTrue(userGroup1DTO.getUsers().contains(user2Entity));
}
Also used : TenantEntity(org.apache.nifi.web.api.entity.TenantEntity) HashMap(java.util.HashMap) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) PermissionsDTO(org.apache.nifi.web.api.dto.PermissionsDTO) TenantDTO(org.apache.nifi.web.api.dto.TenantDTO) UserGroupDTO(org.apache.nifi.web.api.dto.UserGroupDTO) UserGroupEntity(org.apache.nifi.web.api.entity.UserGroupEntity) AccessPolicyDTO(org.apache.nifi.web.api.dto.AccessPolicyDTO) AccessPolicyEntity(org.apache.nifi.web.api.entity.AccessPolicyEntity) Test(org.junit.Test)

Example 2 with TenantDTO

use of org.apache.nifi.web.api.dto.TenantDTO in project nifi by apache.

the class AccessPolicyEntityMergerTest method testMergeAccessPolicy.

@Test
public void testMergeAccessPolicy() throws Exception {
    final NodeIdentifier node1 = new NodeIdentifier("node-1", "host-1", 8080, "host-1", 19998, null, null, null, false);
    final NodeIdentifier node2 = new NodeIdentifier("node-2", "host-2", 8081, "host-2", 19999, null, null, null, false);
    final PermissionsDTO permissed = new PermissionsDTO();
    permissed.setCanRead(true);
    permissed.setCanWrite(true);
    final TenantDTO user1DTO = new TenantDTO();
    user1DTO.setId("user-1");
    final TenantEntity user1Entity = new TenantEntity();
    user1Entity.setPermissions(permissed);
    user1Entity.setId(user1DTO.getId());
    user1Entity.setComponent(user1DTO);
    final TenantDTO user2DTO = new TenantDTO();
    user1DTO.setId("user-2");
    final TenantEntity user2Entity = new TenantEntity();
    user2Entity.setPermissions(permissed);
    user2Entity.setId(user2DTO.getId());
    user2Entity.setComponent(user2DTO);
    final AccessPolicyDTO accessPolicy1DTO = new AccessPolicyDTO();
    accessPolicy1DTO.setId("policy-1");
    accessPolicy1DTO.setUsers(Stream.of(user1Entity, user2Entity).collect(Collectors.toSet()));
    accessPolicy1DTO.setUserGroups(Stream.of(user2Entity).collect(Collectors.toSet()));
    final AccessPolicyEntity accessPolicy1Entity = new AccessPolicyEntity();
    accessPolicy1Entity.setPermissions(permissed);
    accessPolicy1Entity.setId(accessPolicy1DTO.getId());
    accessPolicy1Entity.setComponent(accessPolicy1DTO);
    final AccessPolicyDTO accessPolicy2DTO = new AccessPolicyDTO();
    accessPolicy2DTO.setId("policy-2");
    accessPolicy2DTO.setUsers(Stream.of(user1Entity).collect(Collectors.toSet()));
    accessPolicy2DTO.setUserGroups(Stream.of(user1Entity, user2Entity).collect(Collectors.toSet()));
    final AccessPolicyEntity accessPolicy2Entity = new AccessPolicyEntity();
    accessPolicy2Entity.setPermissions(permissed);
    accessPolicy2Entity.setId(accessPolicy2DTO.getId());
    accessPolicy2Entity.setComponent(accessPolicy2DTO);
    final Map<NodeIdentifier, AccessPolicyEntity> nodeMap = new HashMap<>();
    nodeMap.put(node1, accessPolicy1Entity);
    nodeMap.put(node2, accessPolicy2Entity);
    final AccessPolicyEntityMerger merger = new AccessPolicyEntityMerger();
    merger.merge(accessPolicy1Entity, nodeMap);
    assertEquals(1, accessPolicy1DTO.getUserGroups().size());
    assertTrue(accessPolicy1DTO.getUsers().contains(user1Entity));
    assertEquals(1, accessPolicy1DTO.getUserGroups().size());
    assertTrue(accessPolicy1DTO.getUserGroups().contains(user2Entity));
}
Also used : TenantEntity(org.apache.nifi.web.api.entity.TenantEntity) HashMap(java.util.HashMap) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) PermissionsDTO(org.apache.nifi.web.api.dto.PermissionsDTO) TenantDTO(org.apache.nifi.web.api.dto.TenantDTO) AccessPolicyDTO(org.apache.nifi.web.api.dto.AccessPolicyDTO) AccessPolicyEntity(org.apache.nifi.web.api.entity.AccessPolicyEntity) Test(org.junit.Test)

Example 3 with TenantDTO

use of org.apache.nifi.web.api.dto.TenantDTO in project nifi by apache.

the class UserEntityMergerTest method testMergeAccessPolicy.

@Test
public void testMergeAccessPolicy() throws Exception {
    final NodeIdentifier node1 = new NodeIdentifier("node-1", "host-1", 8080, "host-1", 19998, null, null, null, false);
    final NodeIdentifier node2 = new NodeIdentifier("node-2", "host-2", 8081, "host-2", 19999, null, null, null, false);
    final PermissionsDTO permissed = new PermissionsDTO();
    permissed.setCanRead(true);
    permissed.setCanWrite(true);
    final TenantDTO userGroup1DTO = new TenantDTO();
    userGroup1DTO.setId("user-group-1");
    final TenantEntity userGroup1Entity = new TenantEntity();
    userGroup1Entity.setPermissions(permissed);
    userGroup1Entity.setId(userGroup1DTO.getId());
    userGroup1Entity.setComponent(userGroup1DTO);
    final TenantDTO userGroup2DTO = new TenantDTO();
    userGroup1DTO.setId("user-group-2");
    final TenantEntity userGroup2Entity = new TenantEntity();
    userGroup2Entity.setPermissions(permissed);
    userGroup2Entity.setId(userGroup2DTO.getId());
    userGroup2Entity.setComponent(userGroup2DTO);
    final AccessPolicySummaryDTO policy1DTO = new AccessPolicySummaryDTO();
    policy1DTO.setId("policy-1");
    final AccessPolicySummaryEntity policy1Entity = new AccessPolicySummaryEntity();
    policy1Entity.setPermissions(permissed);
    policy1Entity.setId(policy1DTO.getId());
    policy1Entity.setComponent(policy1DTO);
    final AccessPolicySummaryDTO policy2DTO = new AccessPolicySummaryDTO();
    policy2DTO.setId("policy-2");
    final AccessPolicySummaryEntity policy2Entity = new AccessPolicySummaryEntity();
    policy2Entity.setPermissions(permissed);
    policy2Entity.setId(policy2DTO.getId());
    policy2Entity.setComponent(policy2DTO);
    final UserDTO user1DTO = new UserDTO();
    user1DTO.setId("user-1");
    user1DTO.setAccessPolicies(Stream.of(policy1Entity, policy2Entity).collect(Collectors.toSet()));
    user1DTO.setUserGroups(Stream.of(userGroup2Entity).collect(Collectors.toSet()));
    final UserEntity user1Entity = new UserEntity();
    user1Entity.setPermissions(permissed);
    user1Entity.setId(user1DTO.getId());
    user1Entity.setComponent(user1DTO);
    final UserDTO user2DTO = new UserDTO();
    user2DTO.setId("user-2");
    user2DTO.setAccessPolicies(Stream.of(policy1Entity).collect(Collectors.toSet()));
    user2DTO.setUserGroups(Stream.of(userGroup1Entity, userGroup2Entity).collect(Collectors.toSet()));
    final UserEntity user2Entity = new UserEntity();
    user2Entity.setPermissions(permissed);
    user2Entity.setId(user2DTO.getId());
    user2Entity.setComponent(user2DTO);
    final Map<NodeIdentifier, UserEntity> nodeMap = new HashMap<>();
    nodeMap.put(node1, user1Entity);
    nodeMap.put(node2, user2Entity);
    final UserEntityMerger merger = new UserEntityMerger();
    merger.merge(user1Entity, nodeMap);
    assertEquals(1, user1DTO.getUserGroups().size());
    assertTrue(user1DTO.getAccessPolicies().contains(policy1Entity));
    assertEquals(1, user1DTO.getUserGroups().size());
    assertTrue(user1DTO.getUserGroups().contains(userGroup2Entity));
}
Also used : TenantEntity(org.apache.nifi.web.api.entity.TenantEntity) HashMap(java.util.HashMap) NodeIdentifier(org.apache.nifi.cluster.protocol.NodeIdentifier) PermissionsDTO(org.apache.nifi.web.api.dto.PermissionsDTO) TenantDTO(org.apache.nifi.web.api.dto.TenantDTO) UserDTO(org.apache.nifi.web.api.dto.UserDTO) AccessPolicySummaryDTO(org.apache.nifi.web.api.dto.AccessPolicySummaryDTO) AccessPolicySummaryEntity(org.apache.nifi.web.api.entity.AccessPolicySummaryEntity) UserEntity(org.apache.nifi.web.api.entity.UserEntity) Test(org.junit.Test)

Example 4 with TenantDTO

use of org.apache.nifi.web.api.dto.TenantDTO 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

TenantDTO (org.apache.nifi.web.api.dto.TenantDTO)4 TenantEntity (org.apache.nifi.web.api.entity.TenantEntity)4 HashMap (java.util.HashMap)3 NodeIdentifier (org.apache.nifi.cluster.protocol.NodeIdentifier)3 PermissionsDTO (org.apache.nifi.web.api.dto.PermissionsDTO)3 Test (org.junit.Test)3 AccessPolicyDTO (org.apache.nifi.web.api.dto.AccessPolicyDTO)2 UserDTO (org.apache.nifi.web.api.dto.UserDTO)2 UserGroupDTO (org.apache.nifi.web.api.dto.UserGroupDTO)2 AccessPolicyEntity (org.apache.nifi.web.api.entity.AccessPolicyEntity)2 UserEntity (org.apache.nifi.web.api.entity.UserEntity)2 UserGroupEntity (org.apache.nifi.web.api.entity.UserGroupEntity)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