Skip to content

Commit

Permalink
Add import certificate action and toolbar enhancement
Browse files Browse the repository at this point in the history
Introduced a new toolbar action for importing certificates into JKS files. Updated `CertificateHelper` and related UI components to support the new import functionality. Adjusted project settings to use JDK 20.
  • Loading branch information
cortiz committed Sep 11, 2024
1 parent 73dba7c commit e44c8f2
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 50 deletions.
2 changes: 2 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 12 additions & 19 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 46 additions & 30 deletions src/main/java/com/jmpeax/ssltoolbox/jks/JKSView.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.jmpeax.ssltoolbox.jks;

import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.ActionGroup;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.ActionToolbar;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.fileChooser.FileChooser;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.components.JBLabel;
Expand Down Expand Up @@ -152,44 +157,55 @@ private JPanel createUnlockButton() {
}

private JPanel buildToolBar() {
ActionGroup actionGroup = (ActionGroup) ActionManager.getInstance().getAction("JKS-Actions");
ActionToolbar actionToolBar = ActionManager.getInstance().createActionToolbar("JKS-Actions-Toolbar", actionGroup, true);
actionToolBar.setTargetComponent(this);

DataContext dataContext = dataId -> this.file;
actionToolBar.getComponent().putClientProperty(DataContext.class, dataContext);

JPanel panel = new JPanel(new GridLayout(1,1));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = JBUI.insets(5);
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.WEST;

JButton openButton = new JButton(AllIcons.ToolbarDecorator.Import);
openButton.setToolTipText("Open");
openButton.setPreferredSize(new Dimension(AllIcons.ToolbarDecorator.Import.getIconWidth() + 10, AllIcons.ToolbarDecorator.Import.getIconHeight() + 10));
openButton.setBorderPainted(false);
openButton.addActionListener(e -> {
var descriptor = new FileChooserDescriptor(
true, // Choose Files
false,
false,
false,
false,
false
);
VirtualFile file = FileChooser.chooseFile(descriptor, null, null);
if (file != null) {
var str = Messages.showInputDialog("Enter Alias for " + file.getName(), "Alias for Imported Certificate", null);
LoggerFactory.getLogger(JKSView.class).info("Selected file: alias {} {}", str, file.getPath());
this.listModel.addElement(str);
}
});
gbc.gridx = 0;
gbc.gridy = 0;
panel.add(openButton, gbc);
JButton saveButton = new JButton(AllIcons.ToolbarDecorator.Export);
saveButton.setPreferredSize(new Dimension(AllIcons.ToolbarDecorator.Export.getIconWidth() + 10, AllIcons.ToolbarDecorator.Export.getIconHeight() + 10));
saveButton.setToolTipText("Save");
saveButton.addActionListener(e -> {
});
gbc.gridx = 1;
panel.add(saveButton, gbc);

panel.add(actionToolBar.getComponent(), gbc);
return panel;
//
// JButton openButton = new JButton(AllIcons.ToolbarDecorator.Import);
// openButton.setToolTipText("Open");
// openButton.setPreferredSize(new Dimension(AllIcons.ToolbarDecorator.Import.getIconWidth() + 10, AllIcons.ToolbarDecorator.Import.getIconHeight() + 10));
// openButton.setBorderPainted(false);
// openButton.addActionListener(e -> {
// var descriptor = new FileChooserDescriptor(
// true, // Choose Files
// false,
// false,
// false,
// false,
// false
// );
// VirtualFile file = FileChooser.chooseFile(descriptor, null, null);
// if (file != null) {
// var str = Messages.showInputDialog("Enter Alias for " + file.getName(), "Alias for Imported Certificate", null);
// LoggerFactory.getLogger(JKSView.class).info("Selected file: alias {} {}", str, file.getPath());
// this.listModel.addElement(str);
// }
// });
// gbc.gridx = 0;
// gbc.gridy = 0;
// panel.add(openButton, gbc);
// JButton saveButton = new JButton(AllIcons.ToolbarDecorator.Export);
// saveButton.setPreferredSize(new Dimension(AllIcons.ToolbarDecorator.Export.getIconWidth() + 10, AllIcons.ToolbarDecorator.Export.getIconHeight() + 10));
// saveButton.setToolTipText("Save");
// saveButton.addActionListener(e -> {
// });
// gbc.gridx = 1;
// panel.add(saveButton, gbc);
//
// return panel;
}

private void updateView(Map<String, X509Certificate> certs) {
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/com/jmpeax/ssltoolbox/jks/actions/ImportCert.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.jmpeax.ssltoolbox.jks.actions;

import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileChooser.FileChooser;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFile;
import com.jmpeax.ssltoolbox.svc.CertificateHelper;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;

public class ImportCert extends AnAction {

private static final Logger LOGGER = Logger.getInstance(ImportCert.class);

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
try {
var f = e.getData(CommonDataKeys.VIRTUAL_FILE);
if (f == null) {
LOGGER.warn("JKS not send to the Action");
return;
}
var descriptor = new FileChooserDescriptor(
true, // Choose Files
false,
false,
false,
false,
false
);
VirtualFile file = FileChooser.chooseFile(descriptor, null, null);
if (file == null) {
LOGGER.warn("User did not select a certificate");
return;
}
Messages.showInputDialog("Cert alias","Certificate Alias", Messages.getQuestionIcon());
var pwd = Messages.showPasswordDialog("Keystore password", "KeyStore Password");
if (pwd != null && !pwd.isBlank()) {
CertificateHelper.importCertificate(f.getInputStream(), file.getInputStream(), pwd.toCharArray());
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,7 @@ public static Map<String,X509Certificate> getKeyStoreCerts(InputStream keystoreI
return certs;
}

public static void importCertificate(@NotNull InputStream inputStream, @NotNull InputStream inputStream1, char[] charArray) {

}
}
13 changes: 13 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,17 @@
<fileEditorProvider implementation="com.jmpeax.ssltoolbox.jks.JKSFileEditorProvider" />
<fileType name="JKS file" implementationClass="com.jmpeax.ssltoolbox.jks.JKSFileType" extensions="jks;p12"/>
</extensions>

<actions>

<group id="JKS-Actions"
text="JKS Actions" description="JKS actions"
popup="true" icon="AllIcons.Ide.ConfigFile">
<action id="import-cert-jks" class="com.jmpeax.ssltoolbox.jks.actions.ImportCert"
text="Import" description="Import certificate"
icon="AllIcons.Actions.Refresh">
</action>
</group>

</actions>
</idea-plugin>

0 comments on commit e44c8f2

Please sign in to comment.