diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/artifacts/base/ArtifactServiceCE.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/artifacts/base/ArtifactServiceCE.java index c8d37afd9a02..36a5493611df 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/artifacts/base/ArtifactServiceCE.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/artifacts/base/ArtifactServiceCE.java @@ -4,6 +4,7 @@ import com.appsmith.server.constants.ArtifactType; import com.appsmith.server.domains.Artifact; import com.appsmith.server.domains.GitAuth; +import com.appsmith.server.dtos.GitAuthDTO; import reactor.core.publisher.Mono; public interface ArtifactServiceCE { @@ -21,4 +22,6 @@ public interface ArtifactServiceCE { * @return public key which will be used by user to copy to relevant platform */ Mono createOrUpdateSshKeyPair(ArtifactType artifactType, String branchedArtifactId, String keyType); + + Mono getSshKey(ArtifactType artifactType, String branchedArtifactId); } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/artifacts/base/ArtifactServiceCEImpl.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/artifacts/base/ArtifactServiceCEImpl.java index 079fb6bb773d..d08a1fddbef1 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/artifacts/base/ArtifactServiceCEImpl.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/artifacts/base/ArtifactServiceCEImpl.java @@ -4,12 +4,15 @@ import com.appsmith.server.artifacts.base.artifactbased.ArtifactBasedService; import com.appsmith.server.artifacts.permissions.ArtifactPermission; import com.appsmith.server.constants.ArtifactType; +import com.appsmith.server.constants.Assets; import com.appsmith.server.constants.FieldName; import com.appsmith.server.domains.Application; import com.appsmith.server.domains.ApplicationMode; import com.appsmith.server.domains.Artifact; import com.appsmith.server.domains.GitArtifactMetadata; import com.appsmith.server.domains.GitAuth; +import com.appsmith.server.dtos.GitAuthDTO; +import com.appsmith.server.dtos.GitDeployKeyDTO; import com.appsmith.server.exceptions.AppsmithError; import com.appsmith.server.exceptions.AppsmithException; import com.appsmith.server.helpers.GitDeployKeyGenerator; @@ -19,6 +22,7 @@ import org.springframework.util.StringUtils; import reactor.core.publisher.Mono; +import java.util.List; import java.util.Map; @Slf4j @@ -113,4 +117,59 @@ public Mono createOrUpdateSshKeyPair( }) .thenReturn(gitAuth); } + + /** + * Method to get the SSH public key + * + * @param branchedArtifactId artifact for which the SSH key is requested + * @return public SSH key + */ + @Override + public Mono getSshKey(ArtifactType artifactType, String branchedArtifactId) { + ArtifactBasedService artifactBasedService = getArtifactBasedService(artifactType); + ArtifactPermission artifactPermission = artifactBasedService.getPermissionService(); + return artifactBasedService + .findById(branchedArtifactId, artifactPermission.getEditPermission()) + .switchIfEmpty(Mono.error(new AppsmithException( + AppsmithError.ACL_NO_RESOURCE_FOUND, FieldName.ARTIFACT_ID, branchedArtifactId))) + .flatMap(artifact -> { + GitArtifactMetadata gitData = artifact.getGitArtifactMetadata(); + List gitDeployKeyDTOList = GitDeployKeyGenerator.getSupportedProtocols(); + if (gitData == null) { + return Mono.error(new AppsmithException( + AppsmithError.INVALID_GIT_CONFIGURATION, + "Can't find valid SSH key. Please configure the artifact with git")); + } + // Check if the artifact is base artifact + if (branchedArtifactId.equals(gitData.getDefaultArtifactId())) { + gitData.getGitAuth().setDocUrl(Assets.GIT_DEPLOY_KEY_DOC_URL); + GitAuthDTO gitAuthDTO = new GitAuthDTO(); + gitAuthDTO.setPublicKey(gitData.getGitAuth().getPublicKey()); + gitAuthDTO.setPrivateKey(gitData.getGitAuth().getPrivateKey()); + gitAuthDTO.setDocUrl(gitData.getGitAuth().getDocUrl()); + gitAuthDTO.setGitSupportedSSHKeyType(gitDeployKeyDTOList); + return Mono.just(gitAuthDTO); + } + + if (gitData.getDefaultArtifactId() == null) { + throw new AppsmithException( + AppsmithError.INVALID_GIT_CONFIGURATION, + "Can't find root artifact. Please configure the artifact with git"); + } + + return artifactBasedService + .findById(gitData.getDefaultArtifactId(), artifactPermission.getEditPermission()) + .map(baseArtifact -> { + GitAuthDTO gitAuthDTO = new GitAuthDTO(); + GitAuth gitAuth = + baseArtifact.getGitArtifactMetadata().getGitAuth(); + gitAuth.setDocUrl(Assets.GIT_DEPLOY_KEY_DOC_URL); + gitAuthDTO.setPublicKey(gitAuth.getPublicKey()); + gitAuthDTO.setPrivateKey(gitAuth.getPrivateKey()); + gitAuthDTO.setDocUrl(gitAuth.getDocUrl()); + gitAuthDTO.setGitSupportedSSHKeyType(gitDeployKeyDTOList); + return gitAuthDTO; + }); + }); + } } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/git/controllers/GitApplicationController.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/git/controllers/GitApplicationController.java index ccff87e094cf..3e55a8552492 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/git/controllers/GitApplicationController.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/git/controllers/GitApplicationController.java @@ -1,9 +1,9 @@ package com.appsmith.server.git.controllers; +import com.appsmith.server.artifacts.base.ArtifactService; import com.appsmith.server.constants.Url; import com.appsmith.server.git.autocommit.AutoCommitService; import com.appsmith.server.git.central.CentralGitService; -import com.appsmith.server.git.utils.GitProfileUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -14,7 +14,7 @@ public class GitApplicationController extends GitApplicationControllerCE { public GitApplicationController( - CentralGitService centralGitService, GitProfileUtils gitProfileUtils, AutoCommitService autoCommitService) { - super(centralGitService, gitProfileUtils, autoCommitService); + CentralGitService centralGitService, AutoCommitService autoCommitService, ArtifactService artifactService) { + super(centralGitService, autoCommitService, artifactService); } } diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/git/controllers/GitApplicationControllerCE.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/git/controllers/GitApplicationControllerCE.java index 578ee90cb97a..a631781b9bae 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/git/controllers/GitApplicationControllerCE.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/git/controllers/GitApplicationControllerCE.java @@ -6,13 +6,16 @@ import com.appsmith.external.git.constants.ce.RefType; import com.appsmith.external.views.Views; import com.appsmith.git.dto.CommitDTO; +import com.appsmith.server.artifacts.base.ArtifactService; import com.appsmith.server.constants.ArtifactType; import com.appsmith.server.constants.FieldName; import com.appsmith.server.constants.Url; import com.appsmith.server.domains.Artifact; import com.appsmith.server.domains.GitArtifactMetadata; +import com.appsmith.server.domains.GitAuth; import com.appsmith.server.dtos.AutoCommitResponseDTO; import com.appsmith.server.dtos.BranchProtectionRequestDTO; +import com.appsmith.server.dtos.GitAuthDTO; import com.appsmith.server.dtos.GitConnectDTO; import com.appsmith.server.dtos.GitMergeDTO; import com.appsmith.server.dtos.GitPullDTO; @@ -20,7 +23,6 @@ import com.appsmith.server.git.autocommit.AutoCommitService; import com.appsmith.server.git.central.CentralGitService; import com.appsmith.server.git.central.GitType; -import com.appsmith.server.git.utils.GitProfileUtils; import com.fasterxml.jackson.annotation.JsonView; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; @@ -48,8 +50,8 @@ public class GitApplicationControllerCE { protected final CentralGitService centralGitService; - protected final GitProfileUtils gitProfileUtils; protected final AutoCommitService autoCommitService; + protected final ArtifactService artifactService; protected static final ArtifactType ARTIFACT_TYPE = ArtifactType.APPLICATION; protected static final GitType GIT_TYPE = GitType.FILE_SYSTEM; @@ -96,7 +98,7 @@ public Mono> createReference( referencedApplicationId, srcBranch); return centralGitService - .createReference(referencedApplicationId, ArtifactType.APPLICATION, gitRefDTO, GIT_TYPE) + .createReference(referencedApplicationId, ARTIFACT_TYPE, gitRefDTO, GIT_TYPE) .map(result -> new ResponseDTO<>(HttpStatus.CREATED.value(), result, null)); } @@ -254,4 +256,21 @@ public Mono>> getReferences( branchedApplicationId, ARTIFACT_TYPE, BooleanUtils.isTrue(pruneBranches), GIT_TYPE) .map(result -> new ResponseDTO<>(HttpStatus.OK.value(), result, null)); } + + @JsonView(Views.Public.class) + @GetMapping("/{branchedApplicationId}/ssh-keypair") + public Mono> getSSHKey(@PathVariable String branchedApplicationId) { + return artifactService + .getSshKey(ARTIFACT_TYPE, branchedApplicationId) + .map(created -> new ResponseDTO<>(HttpStatus.CREATED.value(), created, null)); + } + + @JsonView(Views.Public.class) + @PostMapping("/{branchedApplicationId}/ssh-keypair") + public Mono> generateSSHKeyPair( + @PathVariable String branchedApplicationId, @RequestParam(required = false) String keyType) { + return artifactService + .createOrUpdateSshKeyPair(ARTIFACT_TYPE, branchedApplicationId, keyType) + .map(created -> new ResponseDTO<>(HttpStatus.CREATED.value(), created, null)); + } }