jasypt 로 프로퍼티 암호화하기

wisdom·2022년 9월 18일
0

백엔드 개발자라면?

목록 보기
34/42

jasypt

Jasypt is a java library which allows the developer to add basic encryption capabilities to his/her projects with minimum effort, and without the need of having deep knowledge on how cryptography works.

  • 프로젝트에 대한 기본 암호화 기능을 추가할 수 있도록 하는 자바 라이브러리
  • 프로젝트 내에 작성된 설정 정보를 암호화하여 노출시키지 않는 목적으로 사용

의존성 추가 - Gradle

compileOnly 'com.github.ulisesbocchio:jasypt-spring-boot:3.0.4'

설정파일에 추가

jasypt:  
  encryptor:  
    bean: jasyptStringEncryptor
    property:  
	  prefix: ENC(  
	  suffix: )

Config 클래스 작성

JasyptConfig.java

  
import org.jasypt.encryption.StringEncryptor;  
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;  
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;  
import org.springframework.context.annotation.Bean;  

  
@EnableEncryptableProperties  
@Configuration
public class JasyptConfig {  

    @Bean("jasyptStringEncryptor")  
    public StringEncryptor stringEncryptor() {  
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();  
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();  
        config.setPassword("testpassword"); //암호화에 사용할 키 -> 중요  
        config.setAlgorithm("PBEWithMD5AndDES"); 
        config.setKeyObtentionIterations("1000");  
        config.setPoolSize("1");  
        config.setProviderName("SunJCE");  
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");  
        config.setStringOutputType("base64");  
        encryptor.setConfig(config);  
        return encryptor;  
    }  
}
  • 설정 클래스를 하나 만들고 위와 같이 작성해서 jasyptStringEncryptor 빈을 만들어 준다.
  • 그런데 위 password 키 값을 숨겨야 한다. 저렇게 노출되면 암호화하는 의미가 없다. 왜냐하면 해당 키값만 알면 암호값을 다시 복호화시킬 수 있기 때문이다.
  • 찾아보니 vm Options, github secrets, aws parameter store 등 다양한 방법으로 키값을 숨길 수 있다.

암호화

encrypt key 값을 숨기는 방법

  1. VM Options 사용

build.gradle

tasks.named('test') {  
    useJUnitPlatform()  
    systemProperty "jasypt.encryptor.password", System.getProperties().get("jasypt.encryptor.password")  
}
@Value("${jasypt.encryptor.password}")  
private String encryptKey;  
  
@Bean("jasyptStringEncryptor")  
public StringEncryptor stringEncryptor(){  
    PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();  
    SimpleStringPBEConfig config = new SimpleStringPBEConfig();  
    config.setPassword(encryptKey);
  1. github repository secrets 이용
  • secrets 는 깃허브 Actions 설정에서 전역으로 JASYPT_PASSWORD 라는 키에 숨겨야할 키값을 넣어주고 ${} 형식으로 불러올 수 있다.
jobs:
 build:
  runs-on: ubuntu-latest
  env:
	JASYPT_PASSWORD: ${secrets.JASYPT_PASSWORD}
  1. AWS Parameter Store
  • 만약 프로젝트를 aws ec2에 등록한다면 aws parameter store 를 통해 직접 변수 값을 등록하고 이를 스프링 클라우드와 연동해서 사용할 수 있다고도 한다.

암호화된 프로퍼티 파일 일부

  • application-oauth.yaml
spring:
  security:
    oauth2:
      client:
      ...
        registration:
          google:
            client-id: ENC(3IyFtfCmoSJx/Ox0eDl3fPgjtdaoA/juAFz5WLSld+y1MJXqG9ZqX4g5SPLqOSqht2ISckgiuIyeQ0JEYKJSpYs1uAFMxR5h9xmTjVwSl/tt/FuiB9dCIg==)
            client-secret: ENC(7ce2kygTwrk//wZcLo7IZoNRas46P4DW4gDrgFqmCDLx8dOBL4K74Ot8TkQMzRZk)
profile
문제를 정의하고, 문제를 해결하는

0개의 댓글