diff --git a/src/main/java/bio/terra/cli/command/resource/addref/GitRepo.java b/src/main/java/bio/terra/cli/command/resource/addref/GitRepo.java index 157e20ee5..66383e53d 100644 --- a/src/main/java/bio/terra/cli/command/resource/addref/GitRepo.java +++ b/src/main/java/bio/terra/cli/command/resource/addref/GitRepo.java @@ -4,9 +4,15 @@ import bio.terra.cli.command.shared.options.Format; import bio.terra.cli.command.shared.options.ReferencedResourceCreation; import bio.terra.cli.command.shared.options.WorkspaceOverride; +import bio.terra.cli.exception.SystemException; +import bio.terra.cli.exception.UserActionableException; import bio.terra.cli.serialization.userfacing.input.AddGitRepoParams; import bio.terra.cli.serialization.userfacing.input.CreateResourceParams; import bio.terra.cli.serialization.userfacing.resource.UFGitRepo; +import bio.terra.cli.service.ExternalCredentialsManagerService; +import bio.terra.externalcreds.model.SshKeyPairType; +import org.springframework.http.HttpStatus; +import org.springframework.web.client.HttpStatusCodeException; import picocli.CommandLine; /** This class corresponds to the fourth-level "terra resource add-ref git-repo" command. */ @@ -46,5 +52,17 @@ protected void execute() { bio.terra.cli.businessobject.resource.GitRepo addedResource = bio.terra.cli.businessobject.resource.GitRepo.addReferenced(createParams.build()); formatOption.printReturnValue(new UFGitRepo(addedResource), GitRepo::printText); + ExternalCredentialsManagerService ecmService = ExternalCredentialsManagerService.fromContext(); + try { + ecmService.getSshKeyPair(SshKeyPairType.GITHUB); + } catch (SystemException e) { + if (e.getCause() instanceof HttpStatusCodeException + && ((HttpStatusCodeException) e.getCause()).getStatusCode() == HttpStatus.NOT_FOUND) { + throw new UserActionableException( + "You do not have a Terra ssh key, cloning the git repo in the GCP notebook will" + + " fail. Please run `terra user ssh-key generate` and store the output (public key) in" + + " your GitHub account https://github.com/settings/keys."); + } + } } } diff --git a/src/main/java/bio/terra/cli/command/user/sshkey/Generate.java b/src/main/java/bio/terra/cli/command/user/sshkey/Generate.java index dddcac6a3..ab107fe3f 100644 --- a/src/main/java/bio/terra/cli/command/user/sshkey/Generate.java +++ b/src/main/java/bio/terra/cli/command/user/sshkey/Generate.java @@ -30,8 +30,7 @@ protected void execute() { confirmationPrompt.confirmOrThrow( "Generating a new Terra SSH key will replace the old Terra SSH key if it exists. " + "You must associate the new SSH public key with your GitHub account using " - + "https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent#adding-your-ssh-key-to-the-ssh-agent. " - + "Are you sure you want to proceed (y/N)?", + + "https://github.com/settings/keys. Are you sure you want to proceed (y/N)?", "Generating new SSH key is aborted"); var ecmService = ExternalCredentialsManagerService.fromContext(); var sshKeyPair = ecmService.generateSshKeyPair(SshKeyPairType.GITHUB); diff --git a/src/test/java/unit/GitRepoReferenced.java b/src/test/java/unit/GitRepoReferenced.java index 4d4a61f37..77755a765 100644 --- a/src/test/java/unit/GitRepoReferenced.java +++ b/src/test/java/unit/GitRepoReferenced.java @@ -48,6 +48,9 @@ static List listGitRepoResourcesWithName(String resourceName) void listDescribeReflectAdd() throws IOException { workspaceCreator.login(); + // Generate ssh key to avoid prompt about ssh key + TestCommand.runCommandExpectSuccess("user", "ssh-key", "generate", "--quiet"); + // `terra workspace set --id=$id` TestCommand.runCommandExpectSuccess("workspace", "set", "--id=" + getUserFacingId()); @@ -103,13 +106,8 @@ void resolve() throws IOException { // `terra resource add-ref git-repo --name=$name --repo-url=$repoUrl String name = "resolve"; - TestCommand.runAndParseCommandExpectSuccess( - UFGitRepo.class, - "resource", - "add-ref", - "git-repo", - "--name=" + name, - "--repo-url=" + GIT_REPO_SSH_URL); + TestCommand.runCommandExpectSuccess( + "resource", "add-ref", "git-repo", "--name=" + name, "--repo-url=" + GIT_REPO_SSH_URL); // `terra resource resolve --name=$name --format=json` JSONObject resolved = @@ -130,13 +128,8 @@ void listReflectsDelete() throws IOException { // `terra resource add-ref git-repo --name=$name --repo-url=$repoUrl String name = "listReflectsDelete"; - TestCommand.runAndParseCommandExpectSuccess( - UFGitRepo.class, - "resource", - "add-ref", - "git-repo", - "--name=" + name, - "--repo-url=" + GIT_REPO_SSH_URL); + TestCommand.runCommandExpectSuccess( + "resource", "add-ref", "git-repo", "--name=" + name, "--repo-url=" + GIT_REPO_SSH_URL); // `terra resource delete --name=$name --format=json` TestCommand.runCommandExpectSuccess("resource", "delete", "--name=" + name, "--quiet"); @@ -206,8 +199,7 @@ void updateIndividualProperties() throws IOException { // `terra resource add-ref git-repo --name=$name --repo-url=$repoUrl --description=$description` String name = "updateIndividualProperties"; String description = "updateDescription"; - TestCommand.runAndParseCommandExpectSuccess( - UFGitRepo.class, + TestCommand.runCommandExpectSuccess( "resource", "add-ref", "git-repo", @@ -286,8 +278,7 @@ void updateMultipleOrNoProperties() throws IOException { // --repo-url=$gitUrl String name = "updateMultipleOrNoProperties"; String description = "updateDescription"; - TestCommand.runAndParseCommandExpectSuccess( - UFGitRepo.class, + TestCommand.runCommandExpectSuccess( "resource", "add-ref", "git-repo",