application.yml 에는 암호화 키, 각종 서비스의 access key 등 탈취 시 치명적인 데이터들이 보관되어 있다.
따라서 이를 암호화된 암호문 상태로 관리하고,
사용시에는 자동으로 복호화를 하기 위해 Jasypt라는 라이브러리를 사용하려고 한다.
라이브러리 설치
build.gradle
// jasypt
implementation 'com.github.ulisesbocchio:jasypt-spring-boot-starter:3.0.5'
config 설정
JasyptConfig.java
@Configuration
@Profile({"dev"}) // config를 적용시킬 profile
public class JasyptConfig {
@Value("${jasypt.encryptor.password}")
private String password;
@Bean("jasyptEncryptor")
public StringEncryptor stringEncryptor() {
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword(password);
config.setAlgorithm("PBEWithMD5AndDES");
config.setKeyObtentionIterations("1000");
config.setPoolSize("1");
config.setProviderName("SunJCE");
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
config.setIvGeneratorClassName("org.jasypt.iv.NoIvGenerator");
config.setStringOutputType("base64");
encryptor.setConfig(config);
return encryptor;
}
}
이 때 password는 build 시 환경변수로 입력받도록 설정해준다.
application.yml
jasypt:
encryptor:
bean: jasyptEncryptor
password: ${ENCRYPTION_PASSWORD}
만약 개발 환경에서 default로 password를 적용하고 싶다면
password: ${ENCRYPTION_PASSWORD:dragon123}
위와 같이 작성할 수 있다.
암호화
다음으로 application.yml에 암호화시킬 값들을 암호화시켜 넣어야 하는데
Jasypt Encryption and Decryption Online
Jasypt online free tool for encryption and decryption.This tool supports one way and two way password encryptor using Jasypt as well as matching encrypted password using Jasypt.
www.devglan.com
위 사이트에서 이를 진행할 수 있다.
위와 같이 평문을 집어 넣고 two way지정 후 우리가 사용할 password를 집어넣어 encrypt를 누르면 암호화된 문자열을 반환해준다.
적용
application.yml
aes:
key: ENC(hBoxPL+XJ5yxNbnGo8Xa38eIvS396f1Zt7eYjph+m98IWWbBiPrZxA==)
위에서 반환받은 암호화된 문자열을 application.yml에 ENC()로 집어넣어주면 끝
빌드
프로젝트를 빌드 할 때 환경변수로 password를 주입해주면 된다