Skip to content

Commit

Permalink
Add ACLViewer Window + Delete functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
an0r0c committed Dec 18, 2020
1 parent a504c3e commit 85990b7
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 47 deletions.
24 changes: 16 additions & 8 deletions src/main/java/at/esque/kafka/acl/viewer/Acl.java
Expand Up @@ -2,6 +2,7 @@

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import org.apache.kafka.common.acl.AclBinding;
import org.apache.kafka.common.acl.AclOperation;
import org.apache.kafka.common.acl.AclPermissionType;
import org.apache.kafka.common.resource.PatternType;
Expand All @@ -15,16 +16,18 @@ public class Acl {
private StringProperty operation = new SimpleStringProperty();
private StringProperty permissionType = new SimpleStringProperty();
private StringProperty host = new SimpleStringProperty();
private AclBinding aclBinding;

public Acl(ResourceType resourceType, String resourceName, PatternType patternType, String principal, AclOperation operation, AclPermissionType permissionType, String host)
public Acl(AclBinding aclBinding)
{
this.resourceType.set(resourceType.toString());
this.resourceName.set(resourceName);
this.patternType.set(patternType.toString());
this.principal.set(principal);
this.operation.set(operation.toString());
this.permissionType.set(permissionType.toString());
this.host.set(host);
this.aclBinding = aclBinding;
this.resourceType.set(aclBinding.pattern().resourceType().toString());
this.resourceName.set(aclBinding.pattern().name());
this.patternType.set(aclBinding.pattern().patternType().toString());
this.principal.set(aclBinding.entry().principal());
this.operation.set(aclBinding.entry().operation().toString());
this.permissionType.set(aclBinding.entry().permissionType().toString());
this.host.set(aclBinding.entry().host());
}

public String getResourceType() {
Expand Down Expand Up @@ -82,6 +85,11 @@ public String getHost() {
public StringProperty hostProperty() {
return host;
}

public AclBinding getAclBinding()
{
return aclBinding;
}
}


51 changes: 44 additions & 7 deletions src/main/java/at/esque/kafka/acl/viewer/AclViewerController.java
Expand Up @@ -11,9 +11,11 @@
import javafx.beans.property.SimpleBooleanProperty;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.util.Callback;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.consumer.OffsetAndMetadata;
import org.apache.kafka.common.TopicPartition;
Expand All @@ -22,6 +24,8 @@
import org.apache.kafka.common.resource.ResourcePattern;
import org.apache.kafka.common.resource.ResourceType;

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.util.*;

public class AclViewerController {
Expand Down Expand Up @@ -80,6 +84,44 @@ public void initialize() {
permissionTypeColumn.setCellValueFactory(new PropertyValueFactory<>("permissionType"));
hostColumn.setCellValueFactory(new PropertyValueFactory<>("host"));

MenuItem mi1 = new MenuItem("Menu item 1");
mi1.setOnAction((ActionEvent event) -> {
System.out.println("Menu item 1");
Object item = resultView.getSelectionModel().getSelectedItem();
System.out.println("Selected item: " + item);
});

// Add Delete Context Menu to Table Rows
resultView.setRowFactory(
new Callback<TableView<Acl>, TableRow<Acl>>() {
@Override
public TableRow<Acl> call(TableView<Acl> tableView) {
final TableRow<Acl> row = new TableRow<>();
final ContextMenu rowMenu = new ContextMenu();
MenuItem removeItem = new MenuItem("Delete");
removeItem.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent event) {
Object item = resultView.getSelectionModel().getSelectedItem();

if (item instanceof Acl)
{
Acl selectedAcl = (Acl) item;
adminClient.deleteAcl(selectedAcl.getAclBinding());
}

startSearch(null);

}
});
rowMenu.getItems().addAll(removeItem);
row.contextMenuProperty().set(rowMenu);

return row;
}
});

}

public void setup(KafkaesqueAdminClient adminClient, KafkaConsumer kafkaConsumer) {
Expand All @@ -97,20 +139,15 @@ private void startSearch(ActionEvent actionEvent) {
List<Acl> aclList = new ArrayList<>();

adminClient.getACLs(resourceTypeCombo.getValue(), resourcePatternCombo.getValue(), resourceName.getText())
.forEach(acl -> aclList.add(new Acl(acl.pattern().resourceType(),
acl.pattern().name(),
acl.pattern().patternType(),
acl.entry().principal(),
acl.entry().operation(),
acl.entry().permissionType(),
acl.entry().host())));
.forEach(acl -> aclList.add(new Acl(acl)));

resultView.setItems(FXCollections.observableArrayList(aclList));
});
Platform.runLater(() -> refreshRunning.setValue(false));
});
}


public void stop(){
kafkaConsumer = null;
}
Expand Down
22 changes: 14 additions & 8 deletions src/main/java/at/esque/kafka/cluster/KafkaesqueAdminClient.java
Expand Up @@ -12,14 +12,7 @@
import org.apache.kafka.common.resource.ResourcePatternFilter;
import org.apache.kafka.common.resource.ResourceType;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -124,6 +117,19 @@ public List<AclBinding> getACLs(ResourceType resourceType, PatternType resourceP
return Collections.EMPTY_LIST;
}

public void deleteAcl(AclBinding aclBinding)
{
try {
AclBindingFilter aclBindingFilter = new AclBindingFilter(new ResourcePatternFilter(aclBinding.pattern().resourceType(), aclBinding.pattern().name(), aclBinding.pattern().patternType()),
new AccessControlEntryFilter(aclBinding.entry().principal(), aclBinding.entry().host(), aclBinding.entry().operation(), aclBinding.entry().permissionType()));

adminClient.deleteAcls(Collections.singletonList(aclBindingFilter));

} catch (Exception e) {
ErrorAlert.show(e);
}
}

public ListConsumerGroupOffsetsResult listConsumerGroupOffsets(String groupId){
return adminClient.listConsumerGroupOffsets(groupId);
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/at/esque/kafka/dialogs/ClusterConfigDialog.java
Expand Up @@ -89,6 +89,12 @@ public static Optional<ClusterConfig> show(ClusterConfig existingConfig) {
.bind(copy.trustStorePasswordProperty())
),
Group.of(
Field.ofStringType(copy.getSaslSecurityProtocol()==null?"":copy.getSaslSecurityProtocol())
.label("Security Protocol")
.tooltip("SecurityProtocol")
.placeholder("SASL_PLAIN")
.format(new NullFormatStringConverter())
.bind(copy.saslSecurityProtocolProperty()),
Field.ofStringType(copy.getSaslMechanism()==null?"":copy.getSaslMechanism())
.label("SASL Mechanism")
.tooltip("SASL Mechanism")
Expand Down
43 changes: 19 additions & 24 deletions src/main/resources/fxml/aclViewer.fxml
Expand Up @@ -7,7 +7,7 @@
<?import org.kordamp.ikonli.javafx.FontIcon?>
<SplitPane fx:controller="at.esque.kafka.acl.viewer.AclViewerController" dividerPositions="0.2236180904522613" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" orientation="VERTICAL" prefHeight="600.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="264.0" prefWidth="598.0">
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="150.0" prefWidth="598.0">
<children>
<ComboBox fx:id="resourceTypeCombo" layoutX="109.0" layoutY="14.0" prefHeight="25.0" prefWidth="176.0" />
<Label layoutX="14.0" layoutY="18.0" text="Resource Type:" />
Expand All @@ -23,28 +23,23 @@
<FontIcon iconColor="WHITE" iconLiteral="fa-search" iconSize="20" />
</graphic>
</Button>
</children></AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="400.0" prefWidth="1000.0" >
<children>
<ScrollPane prefHeight="350.0" prefWidth="1000.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="350.0" prefWidth="1000.0" maxHeight="-Infinity" maxWidth="-Infinity">
<children>
<TableView fx:id="resultView" prefHeight="350.0" prefWidth="1000.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity">
<columns>
<TableColumn fx:id="resourceTypeColumn" editable="false" prefWidth="70.0" text="Res. Type" />
<TableColumn fx:id="resourceNameColumn" editable="false" prefWidth="350.0" text="Resource Name" />
<TableColumn fx:id="patternTypeColumn" editable="false" prefWidth="75.0" text="Pattern Type" />
<TableColumn fx:id="principalColumn" editable="false" prefWidth="300.0" text="Principal" />
<TableColumn fx:id="operationColumn" editable="false" prefWidth="60.0" text="Oper." />
<TableColumn fx:id="permissionTypeColumn" editable="false" prefWidth="60.0" text="PermTyp" />
<TableColumn fx:id="hostColumn" editable="false" prefWidth="75.0" text="Host" />
</columns>
</TableView>
</children>
</AnchorPane>
</content>
</ScrollPane>
</children></AnchorPane>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="350.0" prefWidth="1000.0" maxHeight="-Infinity" maxWidth="-Infinity" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0"
AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<TableView fx:id="resultView" prefHeight="350.0" prefWidth="1000.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity">
<columns>
<TableColumn fx:id="resourceTypeColumn" editable="false" prefWidth="70.0" text="Res. Type" />
<TableColumn fx:id="resourceNameColumn" editable="false" prefWidth="350.0" text="Resource Name" />
<TableColumn fx:id="patternTypeColumn" editable="false" prefWidth="75.0" text="Pattern Type" />
<TableColumn fx:id="principalColumn" editable="false" prefWidth="300.0" text="Principal" />
<TableColumn fx:id="operationColumn" editable="false" prefWidth="60.0" text="Oper." />
<TableColumn fx:id="permissionTypeColumn" editable="false" prefWidth="60.0" text="PermTyp" />
<TableColumn fx:id="hostColumn" editable="false" prefWidth="75.0" text="Host" />
</columns>
</TableView>
</children>
</AnchorPane>
</items>
</SplitPane>

0 comments on commit 85990b7

Please sign in to comment.