Skip to content

Commit

Permalink
feat: fix for security files
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeongh00 committed Jul 22, 2024
1 parent ba5913b commit 329f55f
Show file tree
Hide file tree
Showing 10 changed files with 218 additions and 3 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Backend CD # actions 이름

on:
push:
branches: [ feat/market-info ]
branches: [ feat/store-info ]

jobs:
deploy:
Expand All @@ -17,6 +17,13 @@ jobs:
- name: 저장소 Checkout
uses: actions/checkout@v3

- name: make application-s3 yml file
run: |
cd ./common-module/src/main/resources
touch ./application-s3.yml
echo "${{ secrets.APPLICATION_S3 }}" > ./application-s3.yml
shell: bash

- name: Gradlew 권한 부여
run: chmod +x ./gradlew

Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ out/

### VS Code ###
.vscode/

common-module/src/main/resources/application-s3.yml
4 changes: 2 additions & 2 deletions api-module/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ spring:
config:
activate:
on-profile: local
import: application-local.yml, application-redis.yml, application-oauth.yml
import: application-local.yml, application-redis.yml, application-oauth.yml, application-s3.yml

---
spring:
config:
activate:
on-profile: prod
import: application-prod.yml, application-redis.yml, application-oauth.yml
import: application-prod.yml, application-redis.yml, application-oauth.yml, application-s3.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.likelion.commonmodule.image.config;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@RequiredArgsConstructor
public class AwsS3Config {

@Value("${cloud.aws.credentials.access-key}")
private String accessKey;

@Value("${cloud.aws.credentials.secret-key}")
private String secretKey;

@Value("${cloud.aws.region.static}")
private String region;

@Bean
public AmazonS3 generateS3client() {
AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
return AmazonS3ClientBuilder.standard()
.withRegion(region)
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
.build();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.likelion.commonmodule.image.exception;

import com.likelion.commonmodule.exception.common.BaseErrorCode;

public class FileDeleteException extends ImageException {
public FileDeleteException(BaseErrorCode errorCode) {
super(errorCode);
}

public FileDeleteException(BaseErrorCode errorCode, Throwable cause) {
super(errorCode, cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.likelion.commonmodule.image.exception;

import com.likelion.commonmodule.exception.common.BaseErrorCode;

public class FileExtensionException extends ImageException {
public FileExtensionException(BaseErrorCode errorCode) {
super(errorCode);
}

public FileExtensionException(BaseErrorCode errorCode, Throwable cause) {
super(errorCode, cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.likelion.commonmodule.image.exception;

import com.likelion.commonmodule.exception.common.BaseErrorCode;

public class FileUploadException extends ImageException {
public FileUploadException(BaseErrorCode errorCode) {
super(errorCode);
}

public FileUploadException(BaseErrorCode errorCode, Throwable cause) {
super(errorCode, cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.likelion.commonmodule.image.exception;

import com.likelion.commonmodule.exception.common.ApiResponse;
import com.likelion.commonmodule.exception.common.BaseErrorCode;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;

@Getter
@AllArgsConstructor
public enum ImageErrorCode implements BaseErrorCode {

FILE_UPLOAD_FAIL(HttpStatus.INTERNAL_SERVER_ERROR, "3000", "파일 업로드에 실패했습니다."),
FILE_DELETE_FAIL(HttpStatus.INTERNAL_SERVER_ERROR, "3000", "파일 삭제에 실패했습니다."),
WRONG_FILE_FORMAT(HttpStatus.INTERNAL_SERVER_ERROR, "3000", "파일 타입이 올바르지 않습니다."),
;

private final HttpStatus httpStatus;
private final String code;
private final String message;


@Override
public ApiResponse<Void> getErrorResponse() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.likelion.commonmodule.image.exception;

import com.likelion.commonmodule.exception.common.BaseErrorCode;
import lombok.Getter;

@Getter
public class ImageException extends RuntimeException {

private final BaseErrorCode errorCode;

private final Throwable cause;

public ImageException(BaseErrorCode errorCode) {
this.errorCode = errorCode;
this.cause = null;
}

public ImageException(BaseErrorCode errorCode, Throwable cause) {
this.errorCode = errorCode;
this.cause = cause;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.likelion.commonmodule.image.service;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.likelion.commonmodule.image.exception.FileDeleteException;
import com.likelion.commonmodule.image.exception.FileExtensionException;
import com.likelion.commonmodule.image.exception.FileUploadException;
import com.likelion.commonmodule.image.exception.ImageErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;
import java.util.UUID;

@Service
@RequiredArgsConstructor
public class AwsS3Service {

private final AmazonS3 amazonS3;
@Value("${cloud.aws.s3.bucket}")
private String bucketName;

/**
* file upload
*/
public String uploadFile(MultipartFile multipartFile) {

if (Objects.isNull(multipartFile)) return null;
if (multipartFile.isEmpty()) return null;

String fileName = createFileName(multipartFile.getOriginalFilename());

ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentType(multipartFile.getContentType());

try (InputStream inputStream = multipartFile.getInputStream()) {
amazonS3.putObject(new PutObjectRequest(bucketName, fileName, inputStream, objectMetadata)
.withCannedAcl(CannedAccessControlList.PublicRead));
} catch (IOException e) {
throw new FileUploadException(ImageErrorCode.FILE_UPLOAD_FAIL);
}

return amazonS3.getUrl(bucketName, fileName).toString();
}

/**
* 파일 삭제 메서드
*/
public void deleteFile(String fileUrl) {
if (fileUrl == null) return;
try {
amazonS3.deleteObject(bucketName, fileUrl);
} catch (AmazonServiceException e) {
throw new FileDeleteException(ImageErrorCode.FILE_DELETE_FAIL);
}
}

/**
* 파일 업로드 시에 파일명을 난수화하는 메서드
*/
private String createFileName(String fileName) {
return UUID.randomUUID().toString().concat(getFileExtension(fileName));
}

/**
* 파일 확장자 가져오는 메서드
*/
private String getFileExtension(String fileName) {
try {
return fileName.substring(fileName.lastIndexOf("."));
} catch (StringIndexOutOfBoundsException e) {
throw new FileExtensionException(ImageErrorCode.WRONG_FILE_FORMAT);
}
}

}

0 comments on commit 329f55f

Please sign in to comment.