Skip to content

Commit

Permalink
fix: fix correct checking/unlocking radicle identity
Browse files Browse the repository at this point in the history
  • Loading branch information
JChrist committed Jan 20, 2025
1 parent 3b3c6f1 commit 85d9c35
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,52 +92,61 @@ private RadicleProjectSettings getProjectSettings() {
return projectHandler.loadSettings();
}


private ProcessOutput unlockIdentity(String radHome, String radPath, IdentityDialog dialog) {
if (project == null && repo == null) {
return new ProcessOutput(0);
}
var pr = project != null ? project : repo.getProject();
var output = rad.self(radHome, radPath);
var lines = output.getStdoutLines(true);
final var idOutput = rad.self(radHome, radPath);
final var hasIdentity = RadAction.isSuccess(idOutput);
var lines = idOutput.getStdoutLines(true);
var radDetails = new RadDetails(lines);
var isIdentityUnlocked = rad.isIdentityUnlocked(radDetails.keyHash);
var hasIdentity = RadAction.isSuccess(output);
var projectSettings = new RadicleProjectSettingsHandler(pr);
final var isIdentityUnlocked = rad.isIdentityUnlocked(radDetails.keyHash);
if (isIdentityUnlocked) {
return new ProcessOutput(0);
}
final var projectSettings = new RadicleProjectSettingsHandler(pr);
String storedPassword = null;
if (hasIdentity) {
// check if key has no passphrase
var ok = rad.isPassphraseCorrect("", radHome);
if (ok) {
projectSettings.savePassphrase(radDetails.nodeId, "");
rad.auth("", "", radHome, radPath);
return new ProcessOutput(0);
}
// the key has a non-empty passphrase, check if we have saved a password
storedPassword = projectSettings.getPassword(radDetails.nodeId);
}
var hasStoredPassword = storedPassword != null;
var showDialog = ((!hasIdentity || !isIdentityUnlocked) && !hasStoredPassword);
IdentityDialog.IdentityDialogData dialogData = null;
if (showDialog) {
var authService = project.getService(AuthService.class);
var title = !hasIdentity ? RadicleBundle.message("newIdentity") :
RadicleBundle.message("unlockIdentity");
dialogData = authService.showIdentityDialog(title, hasIdentity, dialog);
final var hasStoredPassword = storedPassword != null;
boolean hasCorrectStoredPassword = hasStoredPassword;
if (hasIdentity && hasStoredPassword) {
hasCorrectStoredPassword = rad.isPassphraseCorrect(storedPassword, radHome);
}
if (!isIdentityUnlocked && hasIdentity && hasStoredPassword) {
var authOutput = rad.auth(storedPassword, "", radHome, radPath);
var success = RadAuth.validateOutput(authOutput);
if (!RadAction.isSuccess(success)) {
projectSettings.savePassphrase(radDetails.nodeId, null);
}
return success;
var showDialog = !hasIdentity || !hasStoredPassword || !hasCorrectStoredPassword;
if (!showDialog) {
// attempt to unlock identity (add key to ssh-agent)
rad.auth(storedPassword, "", radHome, radPath);
return new ProcessOutput(0);
}
if (dialogData != null) {
var authOutput = rad.auth(dialogData.passphrase(), dialogData.alias(), radHome, radPath);
var success = RadAuth.validateOutput(authOutput);
if (RadAction.isSuccess(success)) {
output = rad.self(radHome, radPath);
lines = output.getStdoutLines(true);
radDetails = new RadDetails(lines);
projectSettings.savePassphrase(radDetails.nodeId, dialogData.passphrase());
}
return success;
var authService = project.getService(AuthService.class);
var title = !hasIdentity ? RadicleBundle.message("newIdentity") : RadicleBundle.message("unlockIdentity");
final var dialogData = authService.showIdentityDialog(title, hasIdentity, dialog);
if (hasIdentity) {
// we already had an identity, so check the passphrase again
hasCorrectStoredPassword = rad.isPassphraseCorrect(dialogData.passphrase(), radHome);
if (!hasCorrectStoredPassword) {
return new ProcessOutput(-1);
}
}
var newOutput = new ProcessOutput(showDialog ? -1 : 0);
newOutput.appendStderr(RadicleBundle.message("unableToUnlock"));
return newOutput;
// either create the new identity, or attempt to unlock the existing one
rad.auth(dialogData.passphrase(), dialogData.alias(), radHome, radPath);
var output = rad.self(radHome, radPath);
lines = output.getStdoutLines(true);
radDetails = new RadDetails(lines);
projectSettings.savePassphrase(radDetails.nodeId, dialogData.passphrase());
return new ProcessOutput(0);
}

public ProcessOutput perform(CountDownLatch latch, String radHome, String radPath, IdentityDialog dialog) {
Expand Down Expand Up @@ -292,6 +301,18 @@ public static boolean isSuccess(ProcessOutput out) {
return !out.isTimeout() && !out.isCancelled() && out.getExitCode() == 0;
}

public static ProcessOutput validateAuthOutput(ProcessOutput output) {
/* rad auth return success exit code (0) and a failed msg if the password is wrong */
var isSuccess = RadAction.isSuccess(output) && !output.getStdout().contains("failed") &&
!output.getStdout().contains("Nothing to do, ssh-agent is not running.");
var pr = new ProcessOutput(isSuccess ? 0 : -1);
/* Write from stdOut to stdErr in order to appear the message in the notification */
var stdOut = output.getStdout();
var errorMessage = !Strings.isNullOrEmpty(stdOut) ? stdOut : RadicleBundle.message("radCliError");
pr.appendStderr(errorMessage);
return pr;
}

public static class ConfigureRadCliNotificationAction extends NotificationAction {
final Project project;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.google.common.base.Charsets;
import com.google.common.base.Strings;
import com.intellij.execution.CommandLineUtil;
import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.execution.util.ExecUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.FileUtil;
import network.radicle.jetbrains.radiclejetbrainsplugin.config.RadicleProjectSettingsHandler;
Expand Down Expand Up @@ -104,7 +104,7 @@ private String getExportRadPassphrase() {
if (Strings.isNullOrEmpty(password)) {
return "";
}
return "export RAD_PASSPHRASE=" + ExecUtil.escapeUnixShellArgument(password);
return "export RAD_PASSPHRASE=" + CommandLineUtil.posixQuote(password);
}

private String getRandomScriptName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,18 @@ private void unlockOrCreateIdentity() {
var radHome = getPathFromTextField(radHomeField);
var radPath = getPathFromTextField(radPathField);
var radSelf = new RadSelf(radHome, radPath, myProject);
var output = radSelf.perform(radHome, radPath, dialog);
final var output = radSelf.perform(radHome, radPath, dialog);
var lines = output.getStdoutLines(true);
radDetails = new RadDetails(lines);
logger.debug("got rad details: {}", radDetails.did);
var cli = myProject.getService(RadicleCliService.class);
if (cli != null) {
cli.resetIdentity();
}
myLatch.countDown();
if (!RadAction.isSuccess(output)) {
ApplicationManager.getApplication().invokeLater(() -> this.msgLabel.setText(RadicleBundle.message("unableToUnlock")), ModalityState.any());
return;
}
// check if rad home is non-default
if (Strings.isNullOrEmpty(autoDetectRadHome.detected)) {
autoDetectRadHome.detect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private void updateCommitFileComponents(LinkLabel<?> fromLabel, JLabel toLabel)
ApplicationManager.getApplication().executeOnPooledThread(() -> {
//Start loading indicator
progressStripe.startLoading();
//Get project information from httpd
//Get project information
projectInfo = getProjectInfo(mySelectedRepo);
if (projectInfo == null) {
return;
Expand Down Expand Up @@ -305,7 +305,8 @@ private void createPatch() {
RadicleBundle.message("findRemoteErrorDesc"));
return;
}
ApplicationManager.getApplication().executeOnPooledThread(() -> {
// execute in background via rad, so that the jobs get assigned a progress indicator and ij stops throwing useless errors
radicleProjectService.executeInBackground(() -> {
newPatchButton.setEnabled(false);
loadingPanel.setVisible(true);
var cli = project.getService(RadicleCliService.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.intellij.collaboration.ui.codereview.commits.CommitsBrowserComponentBuilder;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.vcs.VcsException;
Expand Down Expand Up @@ -98,7 +99,7 @@ public JComponent createCommitComponent() {
}

public void updateFileAndCommitComponents(GitRepository repo, String revNumber, String mainBranchHead, int retriesAttempt) {
ApplicationManager.getApplication().executeOnPooledThread(() -> {
radicleProjectService.executeInBackground(() -> {
try {
var changes = calculatePatchCommits(repo, mainBranchHead, revNumber);
calculatePatchChanges(repo, mainBranchHead, revNumber);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.base.Strings;
import com.intellij.execution.util.ExecUtil;
import com.intellij.execution.CommandLineUtil;
import com.intellij.execution.wsl.WSLDistribution;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.util.ui.EDT;
import jnr.ffi.LibraryLoader;
import network.radicle.jetbrains.radiclejetbrainsplugin.RadicleBundle;
import network.radicle.jetbrains.radiclejetbrainsplugin.actions.rad.RadAction;
import network.radicle.jetbrains.radiclejetbrainsplugin.models.Embed;
import network.radicle.jetbrains.radiclejetbrainsplugin.models.RadDiscussion;
Expand Down Expand Up @@ -66,6 +67,10 @@ public JRadResponse editIssueTitleDescription(String repoId, String issueId, Str
var resp = RadicleCliService.MAPPER.readValue(res, JRadResponse.class);
if (resp == null || !resp.ok) {
logger.warn("error response from native service: " + resp);
if (resp != null) {
RadAction.showErrorNotification(project, RadicleBundle.message("radNativeError"),
RadicleBundle.message("radNativeErrorText") + "\n" + resp.msg());
}
if (resp == null) {
resp = new JRadResponse(false, "no resp");
}
Expand All @@ -89,6 +94,10 @@ public Map<String, String> getEmbeds(String repoId, List<String> oids) {
var resp = RadicleCliService.MAPPER.readValue(res, JRadResponse.class);
if (resp == null || !resp.ok) {
logger.warn("error response from native service: " + resp);
if (resp != null) {
RadAction.showErrorNotification(project, RadicleBundle.message("radNativeError"),
RadicleBundle.message("radNativeErrorText") + "\n" + resp.msg());
}
return null;
}
var tree = RadicleCliService.MAPPER.readTree(res);
Expand Down Expand Up @@ -197,6 +206,10 @@ public boolean createPatchComment(
var resp = RadicleCliService.MAPPER.readValue(res, JRadResponse.class);
if (resp == null || !resp.ok) {
logger.warn("Error creating patch comment:" + resp);
if (resp != null) {
RadAction.showErrorNotification(project, RadicleBundle.message("radNativeError"),
RadicleBundle.message("radNativeErrorText") + "\n" + resp.msg());
}
return false;
}
return true;
Expand Down Expand Up @@ -225,6 +238,10 @@ public boolean editPatchComment(
var resp = RadicleCliService.MAPPER.readValue(res, JRadResponse.class);
if (resp == null || !resp.ok) {
logger.warn("Error creating patch comment:" + resp);
if (resp != null) {
RadAction.showErrorNotification(project, RadicleBundle.message("radNativeError"),
RadicleBundle.message("radNativeErrorText") + "\n" + resp.msg());
}
return false;
}
return true;
Expand Down Expand Up @@ -252,6 +269,10 @@ public boolean deletePatchComment(String repoId, String patchId, String revision
var resp = RadicleCliService.MAPPER.readValue(res, JRadResponse.class);
if (resp == null || !resp.ok) {
logger.warn("Error deleting patch comment:" + resp);
if (resp != null) {
RadAction.showErrorNotification(project, RadicleBundle.message("radNativeError"),
RadicleBundle.message("radNativeErrorText") + "\n" + resp.msg());
}
return false;
}
return true;
Expand Down Expand Up @@ -279,6 +300,10 @@ public boolean editIssueComment(
var resp = RadicleCliService.MAPPER.readValue(res, JRadResponse.class);
if (resp == null || !resp.ok) {
logger.warn("Error editing issue comment:" + resp);
if (resp != null) {
RadAction.showErrorNotification(project, RadicleBundle.message("radNativeError"),
RadicleBundle.message("radNativeErrorText") + "\n" + resp.msg());
}
return false;
}
return true;
Expand Down Expand Up @@ -307,6 +332,10 @@ public boolean patchCommentReact(
var resp = RadicleCliService.MAPPER.readValue(res, JRadResponse.class);
if (resp == null || !resp.ok) {
logger.warn("Error adding patch comment reaction:" + resp);
if (resp != null) {
RadAction.showErrorNotification(project, RadicleBundle.message("radNativeError"),
RadicleBundle.message("radNativeErrorText") + "\n" + resp.msg());
}
return false;
}
return true;
Expand Down Expand Up @@ -334,6 +363,10 @@ public boolean issueCommentReact(
var resp = RadicleCliService.MAPPER.readValue(res, JRadResponse.class);
if (resp == null || !resp.ok) {
logger.warn("Error adding issue comment reaction:" + resp);
if (resp != null) {
RadAction.showErrorNotification(project, RadicleBundle.message("radNativeError"),
RadicleBundle.message("radNativeErrorText") + "\n" + resp.msg());
}
return false;
}
return true;
Expand Down Expand Up @@ -503,7 +536,7 @@ public String issueCommentReact(String input) {
}

public String execute(String method, String input) {
var out = rad.executeCommandFromFile(wslBin, null, List.of(method, ExecUtil.escapeUnixShellArgument(input)));
var out = rad.executeCommandFromFile(wslBin, null, List.of(method, CommandLineUtil.posixQuote(input)));
if (!RadAction.isSuccess(out)) {
return "{\"ok\": false, \"msg\": \"error executing command with exit code:" + out.getExitCode() + " \"}";
}
Expand Down
Loading

0 comments on commit 85d9c35

Please sign in to comment.