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));
}
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));
}
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));
}
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();
}
Aggregations