Search in sources :

Example 1 with TopicAcls

use of io.hops.hopsworks.persistence.entity.kafka.TopicAcls in project hopsworks by logicalclocks.

the class KafkaController method removeAclFromTopic.

public void removeAclFromTopic(String topicName, Integer aclId) throws KafkaException {
    TopicAcls ta = topicAclsFacade.find(aclId);
    if (ta == null) {
        throw new KafkaException(RESTCodes.KafkaErrorCode.ACL_NOT_FOUND, Level.FINE, "topic: " + topicName);
    }
    if (!ta.getProjectTopics().getTopicName().equals(topicName)) {
        throw new KafkaException(RESTCodes.KafkaErrorCode.ACL_NOT_FOR_TOPIC, Level.FINE, "topic: " + topicName);
    }
    topicAclsFacade.remove(ta);
}
Also used : TopicAcls(io.hops.hopsworks.persistence.entity.kafka.TopicAcls) KafkaException(io.hops.hopsworks.exceptions.KafkaException)

Example 2 with TopicAcls

use of io.hops.hopsworks.persistence.entity.kafka.TopicAcls in project hopsworks by logicalclocks.

the class KafkaController method addAclsToTopic.

private Pair<TopicAcls, Response.Status> addAclsToTopic(String topicName, Integer projectId, String selectedProjectName, String userEmail, String permissionType, String operationType, String host, String role) throws ProjectException, KafkaException, UserException {
    if (Strings.isNullOrEmpty(topicName) || userEmail == null) {
        throw new IllegalArgumentException("Topic and userEmail must be provided.");
    }
    // get the project id
    Project topicOwnerProject = Optional.ofNullable(projectFacade.find(projectId)).orElseThrow(() -> new ProjectException(RESTCodes.ProjectErrorCode.PROJECT_NOT_FOUND, Level.FINE, "projectId: " + projectId));
    if (!topicOwnerProject.getName().equals(selectedProjectName)) {
        if (projectFacade.findByName(selectedProjectName) == null) {
            throw new ProjectException(RESTCodes.ProjectErrorCode.PROJECT_NOT_FOUND, Level.FINE, "The specified project " + "for the topic" + topicName + " was not found");
        }
    }
    ProjectTopics pt = projectTopicsFacade.findTopicByNameAndProject(topicOwnerProject, topicName).orElseThrow(() -> new KafkaException(RESTCodes.KafkaErrorCode.TOPIC_NOT_FOUND, Level.FINE, "Topic: " + topicName));
    // should not be able to create multiple ACLs at the same time
    if (userEmail.equals("*")) {
        throw new KafkaException(RESTCodes.KafkaErrorCode.ACL_FOR_ANY_USER, Level.FINE, "topic: " + topicName);
    }
    // fetch the user name from database
    Users user = Optional.ofNullable(userFacade.findByEmail(userEmail)).orElseThrow(() -> new UserException(RESTCodes.UserErrorCode.USER_WAS_NOT_FOUND, Level.FINE, "user: " + userEmail));
    String principalName = KafkaConst.buildPrincipalName(selectedProjectName, user.getUsername());
    Optional<TopicAcls> optionalAcl = topicAclsFacade.getTopicAcls(topicName, principalName, permissionType, operationType, host, role);
    if (optionalAcl.isPresent()) {
        return Pair.of(optionalAcl.get(), Response.Status.OK);
    }
    TopicAcls acl = topicAclsFacade.addAclsToTopic(pt, user, permissionType, operationType, host, role, principalName);
    return Pair.of(acl, Response.Status.CREATED);
}
Also used : ProjectException(io.hops.hopsworks.exceptions.ProjectException) Project(io.hops.hopsworks.persistence.entity.project.Project) ProjectTopics(io.hops.hopsworks.persistence.entity.kafka.ProjectTopics) TopicAcls(io.hops.hopsworks.persistence.entity.kafka.TopicAcls) KafkaException(io.hops.hopsworks.exceptions.KafkaException) Users(io.hops.hopsworks.persistence.entity.user.Users) UserException(io.hops.hopsworks.exceptions.UserException)

Example 3 with TopicAcls

use of io.hops.hopsworks.persistence.entity.kafka.TopicAcls in project hopsworks by logicalclocks.

the class TopicAclsFacade method addAclsToTopic.

public TopicAcls addAclsToTopic(ProjectTopics pt, Users user, String permissionType, String operationType, String host, String role, String principalName) {
    TopicAcls ta = new TopicAcls(pt, user, permissionType, operationType, host, role, principalName);
    save(ta);
    em.flush();
    return ta;
}
Also used : TopicAcls(io.hops.hopsworks.persistence.entity.kafka.TopicAcls)

Example 4 with TopicAcls

use of io.hops.hopsworks.persistence.entity.kafka.TopicAcls in project hopsworks by logicalclocks.

the class KafkaResource method addAclsToTopic.

@ApiOperation(value = "Add a new ACL for a specified topic.")
@POST
@Path("/topics/{topic}/acls")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@AllowedProjectRoles({ AllowedProjectRoles.DATA_OWNER })
@JWTRequired(acceptedTokens = { Audience.API }, allowedUserRoles = { "HOPS_ADMIN", "HOPS_USER" })
@ApiKeyRequired(acceptedScopes = { ApiScope.KAFKA }, allowedUserRoles = { "HOPS_ADMIN", "HOPS_USER" })
public Response addAclsToTopic(@Context UriInfo uriInfo, @PathParam("topic") String topicName, AclDTO aclDto, @Context SecurityContext sc) throws KafkaException, ProjectException, UserException {
    Pair<TopicAcls, Response.Status> aclTuple = kafkaController.addAclsToTopic(topicName, project.getId(), aclDto);
    AclDTO dto = aclBuilder.build(uriInfo, aclTuple.getLeft());
    return Response.status(aclTuple.getRight()).entity(dto).build();
}
Also used : AclDTO(io.hops.hopsworks.common.dao.kafka.AclDTO) TopicAcls(io.hops.hopsworks.persistence.entity.kafka.TopicAcls) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) JWTRequired(io.hops.hopsworks.jwt.annotation.JWTRequired) ApiOperation(io.swagger.annotations.ApiOperation) ApiKeyRequired(io.hops.hopsworks.api.filter.apiKey.ApiKeyRequired) AllowedProjectRoles(io.hops.hopsworks.api.filter.AllowedProjectRoles)

Example 5 with TopicAcls

use of io.hops.hopsworks.persistence.entity.kafka.TopicAcls in project hopsworks by logicalclocks.

the class AclBuilder method getAclByTopicAndId.

public AclDTO getAclByTopicAndId(UriInfo uriInfo, Project project, String topicName, Integer aclId) throws KafkaException {
    AclDTO dto = new AclDTO();
    URI uri = getAclUri(uriInfo, project, topicName, aclId).build();
    dto.setHref(uri);
    Optional<TopicAcls> aclsOptional = kafkaController.findAclByIdAndTopic(topicName, aclId);
    if (aclsOptional.isPresent()) {
        TopicAcls acl = aclsOptional.get();
        dto.setId(acl.getId());
        dto.setProjectName(KafkaConst.getProjectNameFromPrincipal(acl.getPrincipal()));
        dto.setUserEmail(acl.getUser().getEmail());
        dto.setPermissionType(acl.getPermissionType());
        dto.setOperationType(acl.getOperationType());
        dto.setHost(acl.getHost());
        dto.setRole(acl.getRole());
    }
    return dto;
}
Also used : AclDTO(io.hops.hopsworks.common.dao.kafka.AclDTO) TopicAcls(io.hops.hopsworks.persistence.entity.kafka.TopicAcls) URI(java.net.URI)

Aggregations

TopicAcls (io.hops.hopsworks.persistence.entity.kafka.TopicAcls)7 KafkaException (io.hops.hopsworks.exceptions.KafkaException)3 AclDTO (io.hops.hopsworks.common.dao.kafka.AclDTO)2 AllowedProjectRoles (io.hops.hopsworks.api.filter.AllowedProjectRoles)1 ApiKeyRequired (io.hops.hopsworks.api.filter.apiKey.ApiKeyRequired)1 ProjectException (io.hops.hopsworks.exceptions.ProjectException)1 UserException (io.hops.hopsworks.exceptions.UserException)1 JWTRequired (io.hops.hopsworks.jwt.annotation.JWTRequired)1 ProjectTopics (io.hops.hopsworks.persistence.entity.kafka.ProjectTopics)1 Project (io.hops.hopsworks.persistence.entity.project.Project)1 Users (io.hops.hopsworks.persistence.entity.user.Users)1 ApiOperation (io.swagger.annotations.ApiOperation)1 URI (java.net.URI)1 Query (javax.persistence.Query)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1