Commit 3531f256 authored by Denis's avatar Denis Committed by Denis Ligin

Загрузка шаблонов из гитлаба

parent 5233a7cb
Pipeline #364 canceled with stage
...@@ -29,8 +29,10 @@ services: ...@@ -29,8 +29,10 @@ services:
- SPRING_DATASOURCE_URL=jdbc:postgresql://postgres:5432/template_db - SPRING_DATASOURCE_URL=jdbc:postgresql://postgres:5432/template_db
- KEYCLOAK_URI=https://idp.applatform.qaztech.gov.kz - KEYCLOAK_URI=https://idp.applatform.qaztech.gov.kz
- KEYCLOAK_CLIENT_SECRET=1NYLMNlWXpPDW3QKeZ4VjlY76DuzNtaB - KEYCLOAK_CLIENT_SECRET=1NYLMNlWXpPDW3QKeZ4VjlY76DuzNtaB
- GITLAB_BASE_PATH=http://gitlab.lan.arta.kz
- GITLAB_TOKEN=zif-LhgdzuLtpNW7uxYs
networks: networks:
printform: printform:
external: true external: true
name: printform name: printform
\ No newline at end of file
package kz.project.printedFormsService.config; package kz.project.printedFormsService.config;
import kz.project.printedFormsService.exception.ValidationException;
import lombok.experimental.UtilityClass; import lombok.experimental.UtilityClass;
import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
...@@ -45,4 +46,18 @@ public class SecurityContextUtils { ...@@ -45,4 +46,18 @@ public class SecurityContextUtils {
) )
); );
} }
public static String getGitlabProjectBranchFromRole() throws ValidationException {
return SecurityContextHolder
.getContext()
.getAuthentication()
.getAuthorities()
.stream()
.map(GrantedAuthority::getAuthority)
.filter(role -> role.contains("gitlab"))
.map(role -> role.replace("ROLE_gitlab_", ""))
.findFirst()
.orElseThrow(() -> new ValidationException("Не указана роль для доступа к gitlab"));
}
} }
...@@ -26,15 +26,19 @@ public class JwtAuthConverter implements Converter<Jwt, AbstractAuthenticationTo ...@@ -26,15 +26,19 @@ public class JwtAuthConverter implements Converter<Jwt, AbstractAuthenticationTo
private Collection<GrantedAuthority> extractAuthorities(Jwt jwt) { private Collection<GrantedAuthority> extractAuthorities(Jwt jwt) {
if (jwt.getClaim("resource_access") != null) { if (jwt.getClaim("resource_access") != null) {
Map<String, Map<String,Object>> resourceAccess = jwt.getClaim("resource_access"); Map<String, Map<String, Object>> resourceAccess = jwt.getClaim("resource_access");
if (resourceAccess.containsKey("print_form")){ if (resourceAccess.containsKey("print_form")) {
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
List<String> roles = mapper.convertValue(resourceAccess.get("print_form").get("roles"), new TypeReference<>() { List<String> roles = mapper.convertValue(resourceAccess.get("print_form").get("roles"), new TypeReference<>() {
}); });
Set<GrantedAuthority> authorities = new HashSet<>(); Set<GrantedAuthority> authorities = new HashSet<>();
for (String role : roles) { for (String role : roles) {
authorities.add(new SimpleGrantedAuthority("ROLE_" + role.substring(role.lastIndexOf('_')+1))); if (role.contains("gitlab")) {
authorities.add(new SimpleGrantedAuthority("ROLE_" + role));
} else {
authorities.add(new SimpleGrantedAuthority("ROLE_" + role.substring(role.lastIndexOf('_') + 1)));
}
} }
return authorities; return authorities;
......
...@@ -4,9 +4,9 @@ import org.springframework.web.multipart.MultipartFile; ...@@ -4,9 +4,9 @@ import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
public interface GitlabClient { public interface GitlabClient {
Mono<byte[]> downloadFileFromGitLab(String projectName, String filePath); Mono<byte[]> downloadFileFromGitLab(String projectName, String filePath, String branch);
Mono<String> downloadFileContentAsString(String projectName, String filePath); Mono<String> downloadFileContentAsString(String projectName, String filePath, String branch);
MultipartFile downloadFileAsMultipartFile(String projectName, String filePath); MultipartFile downloadFileAsMultipartFile(String projectName, String filePath, String branch);
} }
...@@ -24,12 +24,12 @@ public class GitlabClientImpl implements GitlabClient { ...@@ -24,12 +24,12 @@ public class GitlabClientImpl implements GitlabClient {
//http://gitlab.lan.arta.kz/d.ermakov/templates/-/raw/master/dto.txt?inline=false //http://gitlab.lan.arta.kz/d.ermakov/templates/-/raw/master/dto.txt?inline=false
@Override @Override
public Mono<byte[]> downloadFileFromGitLab(String projectName, String filePath) { public Mono<byte[]> downloadFileFromGitLab(String projectName, String filePath, String branch) {
String urlPath = String.format("/api/v4/projects/%s/repository/files/%s/raw", projectName, filePath); String urlPath = String.format("/api/v4/projects/%s/repository/files/%s/raw", projectName, filePath);
return webClient.get() return webClient.get()
.uri(uriBuilder -> uriBuilder .uri(uriBuilder -> uriBuilder
.path(urlPath) .path(urlPath)
.queryParam("ref", "master") // or the branch you want to fetch from .queryParam("ref", branch) // or the branch you want to fetch from
.build()) .build())
.retrieve() .retrieve()
.bodyToMono(byte[].class) .bodyToMono(byte[].class)
...@@ -37,12 +37,12 @@ public class GitlabClientImpl implements GitlabClient { ...@@ -37,12 +37,12 @@ public class GitlabClientImpl implements GitlabClient {
} }
@Override @Override
public Mono<String> downloadFileContentAsString(String projectName, String filePath) { public Mono<String> downloadFileContentAsString(String projectName, String filePath, String branch) {
String urlPath = String.format("/api/v4/projects/%s/repository/files/%s/raw", projectName, filePath); String urlPath = String.format("/api/v4/projects/%s/repository/files/%s/raw", projectName, filePath);
return webClient.get() return webClient.get()
.uri(uriBuilder -> uriBuilder .uri(uriBuilder -> uriBuilder
.path(urlPath) .path(urlPath)
.queryParam("ref", "master") // or the branch you want to fetch from .queryParam("ref", branch) // or the branch you want to fetch from
.build()) .build())
.retrieve() .retrieve()
.bodyToMono(String.class) .bodyToMono(String.class)
...@@ -50,12 +50,12 @@ public class GitlabClientImpl implements GitlabClient { ...@@ -50,12 +50,12 @@ public class GitlabClientImpl implements GitlabClient {
} }
@Override @Override
public MultipartFile downloadFileAsMultipartFile(String projectName, String filePath) { public MultipartFile downloadFileAsMultipartFile(String projectName, String filePath, String branch) {
String urlPath = String.format("/api/v4/projects/%s/repository/files/%s/raw", projectName, filePath); String urlPath = String.format("/api/v4/projects/%s/repository/files/%s/raw", projectName, filePath);
return webClient.get() return webClient.get()
.uri(uriBuilder -> uriBuilder .uri(uriBuilder -> uriBuilder
.path(urlPath) .path(urlPath)
.queryParam("ref", "master") // or the branch you want to fetch from .queryParam("ref", branch) // or the branch you want to fetch from
.build()) .build())
.retrieve() .retrieve()
.bodyToMono(byte[].class) .bodyToMono(byte[].class)
......
package kz.project.printedFormsService.service.impl; package kz.project.printedFormsService.service.impl;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import kz.project.printedFormsService.config.SecurityContextUtils;
import kz.project.printedFormsService.data.dto.GitlabUploadRequest; import kz.project.printedFormsService.data.dto.GitlabUploadRequest;
import kz.project.printedFormsService.data.dto.TemplateDto; import kz.project.printedFormsService.data.dto.TemplateDto;
import kz.project.printedFormsService.data.dto.TemplateResponseDto; import kz.project.printedFormsService.data.dto.TemplateResponseDto;
...@@ -28,16 +29,19 @@ public class GitlabTemplateServiceImpl implements kz.project.printedFormsService ...@@ -28,16 +29,19 @@ public class GitlabTemplateServiceImpl implements kz.project.printedFormsService
@SneakyThrows @SneakyThrows
@Override @Override
public TemplateDto saveFromGitlab(GitlabUploadRequest gitlabUploadRequest) { public TemplateDto saveFromGitlab(GitlabUploadRequest gitlabUploadRequest) {
String gitlabBranch = SecurityContextUtils.getGitlabProjectBranchFromRole();
List<MultipartFile> filesToSave = new ArrayList<>(); List<MultipartFile> filesToSave = new ArrayList<>();
filesToSave.add(gitlabClient.downloadFileAsMultipartFile( filesToSave.add(gitlabClient.downloadFileAsMultipartFile(
gitlabUploadRequest.getRepository(), gitlabUploadRequest.getRepository(),
gitlabUploadRequest.getFileDataPath() gitlabUploadRequest.getFileDataPath(),
gitlabBranch
)); ));
String dtoString = gitlabClient.downloadFileContentAsString( String dtoString = gitlabClient.downloadFileContentAsString(
gitlabUploadRequest.getRepository(), gitlabUploadRequest.getRepository(),
gitlabUploadRequest.getFileDtoPath() gitlabUploadRequest.getFileDtoPath(),
gitlabBranch
) )
.blockOptional() .blockOptional()
.orElseThrow(); .orElseThrow();
...@@ -48,7 +52,8 @@ public class GitlabTemplateServiceImpl implements kz.project.printedFormsService ...@@ -48,7 +52,8 @@ public class GitlabTemplateServiceImpl implements kz.project.printedFormsService
filesToSave.add( filesToSave.add(
gitlabClient.downloadFileAsMultipartFile( gitlabClient.downloadFileAsMultipartFile(
gitlabUploadRequest.getRepository(), gitlabUploadRequest.getRepository(),
gitlabUploadRequest.getFileHeaderPath() gitlabUploadRequest.getFileHeaderPath(),
gitlabBranch
) )
); );
} }
...@@ -59,16 +64,19 @@ public class GitlabTemplateServiceImpl implements kz.project.printedFormsService ...@@ -59,16 +64,19 @@ public class GitlabTemplateServiceImpl implements kz.project.printedFormsService
@SneakyThrows @SneakyThrows
@Override @Override
public TemplateResponseDto updateFromGitlab(GitlabUploadRequest gitlabUploadRequest) { public TemplateResponseDto updateFromGitlab(GitlabUploadRequest gitlabUploadRequest) {
String gitlabBranch = SecurityContextUtils.getGitlabProjectBranchFromRole();
List<MultipartFile> filesToSave = new ArrayList<>(); List<MultipartFile> filesToSave = new ArrayList<>();
filesToSave.add(gitlabClient.downloadFileAsMultipartFile( filesToSave.add(gitlabClient.downloadFileAsMultipartFile(
gitlabUploadRequest.getRepository(), gitlabUploadRequest.getRepository(),
gitlabUploadRequest.getFileDataPath() gitlabUploadRequest.getFileDataPath(),
gitlabBranch
)); ));
String dtoString = gitlabClient.downloadFileContentAsString( String dtoString = gitlabClient.downloadFileContentAsString(
gitlabUploadRequest.getRepository(), gitlabUploadRequest.getRepository(),
gitlabUploadRequest.getFileDtoPath() gitlabUploadRequest.getFileDtoPath(),
gitlabBranch
) )
.blockOptional() .blockOptional()
.orElseThrow(); .orElseThrow();
...@@ -79,7 +87,8 @@ public class GitlabTemplateServiceImpl implements kz.project.printedFormsService ...@@ -79,7 +87,8 @@ public class GitlabTemplateServiceImpl implements kz.project.printedFormsService
filesToSave.add( filesToSave.add(
gitlabClient.downloadFileAsMultipartFile( gitlabClient.downloadFileAsMultipartFile(
gitlabUploadRequest.getRepository(), gitlabUploadRequest.getRepository(),
gitlabUploadRequest.getFileHeaderPath() gitlabUploadRequest.getFileHeaderPath(),
gitlabBranch
) )
); );
} }
......
...@@ -74,5 +74,5 @@ management: ...@@ -74,5 +74,5 @@ management:
enabled: false enabled: false
gitlab: gitlab:
base-url: http://gitlab.lan.arta.kz base-url: ${GITLAB_BASE_PATH:http://gitlab.lan.arta.kz}
token: zif-LhgdzuLtpNW7uxYs token: ${GITLAB_TOKEN:zif-LhgdzuLtpNW7uxYs}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment