-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finish the APIs related with ssh-key
We finish the APIs related with ssh-key, including the following APIs: * Upload a ssh-key * Delete a ssh-key * Get ssh-keys with pagination * Update a ssh-key We use spring transaction to ensure the atomicity of the operations. See #32.
- Loading branch information
1 parent
38ef416
commit 0633edb
Showing
28 changed files
with
732 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
CREATE TABLE public.t_ssh_key ( | ||
id bigint NOT NULL, | ||
user_id bigint NOT NULL, | ||
name character varying(255) NOT NULL, | ||
public_key character varying(4096) NOT NULL, | ||
gmt_created timestamp without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL, | ||
gmt_updated timestamp without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL, | ||
gmt_deleted timestamp without time zone | ||
); | ||
|
||
COMMENT ON TABLE public.t_ssh_key IS 'Table for storing ssh public key.'; | ||
COMMENT ON COLUMN public.t_ssh_key.id IS 'Primary key of the ssh_key table.'; | ||
COMMENT ON COLUMN public.t_ssh_key.user_id IS 'ID of the user who owns the ssh key.'; | ||
COMMENT ON COLUMN public.t_ssh_key.name IS 'Name of the ssh key.'; | ||
COMMENT ON COLUMN public.t_ssh_key.public_key IS 'Public key of the ssh key.'; | ||
COMMENT ON COLUMN public.t_ssh_key.gmt_created IS 'Timestamp when the ssh_key record was created.'; | ||
COMMENT ON COLUMN public.t_ssh_key.gmt_updated IS 'Timestamp when the ssh_key record was last updated.'; | ||
COMMENT ON COLUMN public.t_ssh_key.gmt_deleted IS 'Timestamp when the ssh_key record was deleted. | ||
If set to NULL, it indicates that the ssh_key record has not been deleted.'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package edu.cmipt.gcs.constant; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class GitConstant { | ||
public static String GIT_USER_NAME; | ||
|
||
public static String GIT_HOME_DIRECTORY; | ||
|
||
public static String GIT_REPOSITORY_DIRECTORY; | ||
|
||
public static String GIT_REPOSITORY_SUFFIX; | ||
|
||
public static String GIT_SERVER_DOMAIN; | ||
|
||
public static final String SSH_KEY_PREFIX = "no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty "; | ||
|
||
@Value("${git.user.name}") | ||
public void setGIT_USER_NAME(String gitUserName) { | ||
GitConstant.GIT_USER_NAME = gitUserName; | ||
} | ||
|
||
@Value("${git.home.directory}") | ||
public void setGIT_HOME_DIRECTORY(String gitHomeDirectory) { | ||
GitConstant.GIT_HOME_DIRECTORY = gitHomeDirectory; | ||
} | ||
|
||
@Value("${git.repository.directory}") | ||
public void setGIT_REPOSITORY_DIRECTORY(String gitRepositoryDirectory) { | ||
GitConstant.GIT_REPOSITORY_DIRECTORY = gitRepositoryDirectory; | ||
} | ||
|
||
@Value("${git.repository.suffix}") | ||
public void setGIT_REPOSITORY_SUFFIX(String gitRepositorySuffix) { | ||
GitConstant.GIT_REPOSITORY_SUFFIX = gitRepositorySuffix; | ||
} | ||
|
||
@Value("${git.server.domain}") | ||
public void setGIT_SERVER_DOMAIN(String gitServerDomain) { | ||
GitConstant.GIT_SERVER_DOMAIN = gitServerDomain; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
180 changes: 180 additions & 0 deletions
180
src/main/java/edu/cmipt/gcs/controller/SshKeyController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
package edu.cmipt.gcs.controller; | ||
|
||
import edu.cmipt.gcs.constant.ApiPathConstant; | ||
import edu.cmipt.gcs.constant.HeaderParameter; | ||
import edu.cmipt.gcs.enumeration.ErrorCodeEnum; | ||
import edu.cmipt.gcs.exception.GenericException; | ||
import edu.cmipt.gcs.pojo.error.ErrorVO; | ||
import edu.cmipt.gcs.pojo.ssh.SshKeyDTO; | ||
import edu.cmipt.gcs.pojo.ssh.SshKeyPO; | ||
import edu.cmipt.gcs.pojo.ssh.SshKeyVO; | ||
import edu.cmipt.gcs.service.SshKeyService; | ||
import edu.cmipt.gcs.util.JwtUtil; | ||
import edu.cmipt.gcs.validation.group.CreateGroup; | ||
import edu.cmipt.gcs.validation.group.UpdateGroup; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.Parameter; | ||
import io.swagger.v3.oas.annotations.Parameters; | ||
import io.swagger.v3.oas.annotations.enums.ParameterIn; | ||
import io.swagger.v3.oas.annotations.media.Content; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestHeader; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | ||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | ||
|
||
@RestController | ||
@Tag(name = "SSH", description = "SSH APIs") | ||
public class SshKeyController { | ||
private static final Logger logger = LoggerFactory.getLogger(SshKeyController.class); | ||
|
||
@Autowired private SshKeyService sshKeyService; | ||
|
||
@PostMapping(ApiPathConstant.SSH_KEY_UPLOAD_SSH_KEY_API_PATH) | ||
@Operation( | ||
summary = "Upload SSH key", | ||
description = "Upload SSH key with the given information", | ||
tags = {"SSH", "Post Method"}) | ||
@Parameters({ | ||
@Parameter( | ||
name = HeaderParameter.ACCESS_TOKEN, | ||
description = "Access token", | ||
required = true, | ||
in = ParameterIn.HEADER, | ||
schema = @Schema(implementation = String.class))}) | ||
@ApiResponses({ | ||
@ApiResponse(responseCode = "200", description = "SSH key uploaded successfully"), | ||
@ApiResponse( | ||
responseCode = "400", | ||
description = "SSH key upload failed", | ||
content = @Content(schema = @Schema(implementation = ErrorVO.class))), | ||
@ApiResponse(responseCode = "500", description = "Internal server error") | ||
}) | ||
public void uploadSshKey(@Validated(CreateGroup.class) @RequestBody SshKeyDTO sshKeyDTO, | ||
@RequestHeader(HeaderParameter.ACCESS_TOKEN) String accessToken){ | ||
if (!sshKeyService.save(new SshKeyPO(sshKeyDTO))) { | ||
throw new GenericException(ErrorCodeEnum.SSH_KEY_UPLOAD_FAILED, sshKeyDTO); | ||
} | ||
} | ||
|
||
@DeleteMapping(ApiPathConstant.SSH_KEY_DELETE_SSH_KEY_API_PATH) | ||
@Operation( | ||
summary = "Delete SSH key", | ||
description = "Delete SSH key with the given information", | ||
tags = {"SSH", "Delete Method"}) | ||
@Parameters({ | ||
@Parameter( | ||
name = HeaderParameter.ACCESS_TOKEN, | ||
description = "Access token", | ||
required = true, | ||
in = ParameterIn.HEADER, | ||
schema = @Schema(implementation = String.class)), | ||
@Parameter( | ||
name = "id", | ||
description = "SSH key ID", | ||
required = true, | ||
in = ParameterIn.QUERY, | ||
schema = @Schema(implementation = Long.class))}) | ||
@ApiResponse(responseCode = "200", description = "SSH key deleted successfully") | ||
public void deleteSshKey(@RequestHeader(HeaderParameter.ACCESS_TOKEN) String accessToken, | ||
@RequestParam("id") Long id) { | ||
var res = sshKeyService.getById(id); | ||
if (res == null) { | ||
throw new GenericException(ErrorCodeEnum.SSH_KEY_NOT_FOUND, id); | ||
} | ||
String idInToken = JwtUtil.getId(accessToken); | ||
String idInRes = res.getUserId().toString(); | ||
if (!idInRes.equals(idInToken)) { | ||
logger.info("User[{}] tried to get SSH key of user[{}]", idInToken, idInRes); | ||
throw new GenericException(ErrorCodeEnum.ACCESS_DENIED); | ||
} | ||
if (!sshKeyService.removeById(id)) { | ||
throw new GenericException(ErrorCodeEnum.SSH_KEY_DELETE_FAILED, id); | ||
} | ||
} | ||
|
||
@PostMapping(ApiPathConstant.SSH_KEY_UPDATE_SSH_KEY_API_PATH) | ||
@Operation( | ||
summary = "Update SSH key", | ||
description = "Update SSH key with the given information", | ||
tags = {"SSH", "Post Method"}) | ||
@Parameters({ | ||
@Parameter( | ||
name = HeaderParameter.ACCESS_TOKEN, | ||
description = "Access token", | ||
required = true, | ||
in = ParameterIn.HEADER, | ||
schema = @Schema(implementation = String.class))}) | ||
@ApiResponses({ | ||
@ApiResponse(responseCode = "200", description = "SSH key updated successfully"), | ||
@ApiResponse( | ||
responseCode = "400", | ||
description = "SSH key update failed", | ||
content = @Content(schema = @Schema(implementation = ErrorVO.class)))}) | ||
public ResponseEntity<SshKeyVO> updateSshKey( | ||
@Validated(UpdateGroup.class) @RequestBody SshKeyDTO sshKeyDTO | ||
) { | ||
if (!sshKeyService.updateById(new SshKeyPO(sshKeyDTO))) { | ||
throw new GenericException(ErrorCodeEnum.SSH_KEY_UPDATE_FAILED, sshKeyDTO); | ||
} | ||
return ResponseEntity.ok().body(new SshKeyVO(sshKeyService.getById(Long.valueOf(sshKeyDTO.id())))); | ||
} | ||
|
||
@GetMapping(ApiPathConstant.SSH_KEY_PAGE_SSH_KEY_API_PATH) | ||
@Operation( | ||
summary = "Page SSH key", | ||
description = "Page SSH key with the given information", | ||
tags = {"SSH", "Get Method"}) | ||
@Parameters({ | ||
@Parameter( | ||
name = HeaderParameter.ACCESS_TOKEN, | ||
description = "Access token", | ||
required = true, | ||
in = ParameterIn.HEADER, | ||
schema = @Schema(implementation = String.class)), | ||
@Parameter( | ||
name = "id", | ||
description = "User ID", | ||
required = true, | ||
in = ParameterIn.QUERY, | ||
schema = @Schema(implementation = Long.class)), | ||
@Parameter( | ||
name = "page", | ||
description = "Page number", | ||
example = "1", | ||
required = true, | ||
in = ParameterIn.QUERY, | ||
schema = @Schema(implementation = Integer.class)), | ||
@Parameter( | ||
name = "size", | ||
description = "Page size", | ||
example = "10", | ||
required = true, | ||
in = ParameterIn.QUERY, | ||
schema = @Schema(implementation = Integer.class))}) | ||
@ApiResponse(responseCode = "200", description = "SSH key paged successfully") | ||
public List<SshKeyVO> pageSshKey(@RequestParam("id") Long userId, | ||
@RequestParam("page") Integer page, @RequestParam("size") Integer size) { | ||
QueryWrapper<SshKeyPO> wrapper = new QueryWrapper<>(); | ||
wrapper.eq("user_id", userId); | ||
return sshKeyService.list(new Page<>(page, size), wrapper).stream().map(SshKeyVO::new).collect(Collectors.toList()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package edu.cmipt.gcs.dao; | ||
|
||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; | ||
|
||
import edu.cmipt.gcs.pojo.ssh.SshKeyPO; | ||
|
||
public interface SshKeyMapper extends BaseMapper<SshKeyPO> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.