-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge PR(#9) feature/keymanager - Secret Key Manager
- Loading branch information
Showing
12 changed files
with
443 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,3 +31,9 @@ build/ | |
|
||
### VS Code ### | ||
.vscode/ | ||
|
||
### Key Files ### | ||
*.p12 | ||
|
||
### yaml ### | ||
application-local.yml |
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
52 changes: 52 additions & 0 deletions
52
src/main/java/com/t3t/authenticationapi/config/RestTemplateConfig.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,52 @@ | ||
package com.t3t.authenticationapi.config; | ||
|
||
import com.t3t.authenticationapi.keymanager.properties.SecretKeyManagerProperties; | ||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory; | ||
import org.apache.http.impl.client.HttpClients; | ||
import org.apache.http.ssl.SSLContextBuilder; | ||
import org.springframework.boot.web.client.RestTemplateBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.io.IOException; | ||
import java.security.*; | ||
import java.security.cert.CertificateException; | ||
import java.time.Duration; | ||
|
||
|
||
@Configuration | ||
public class RestTemplateConfig { | ||
|
||
/** | ||
* Secret Key Manager 인증서를 사용하여 요청을 보내기 위한 RestTemplate 빈 등록 | ||
* @author woody35545(구건모) | ||
*/ | ||
@Bean | ||
@Profile("!local") | ||
public RestTemplate sslRestTemplate(SecretKeyManagerProperties secretKeyManagerProperties) | ||
throws KeyStoreException, IOException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException, CertificateException { | ||
|
||
KeyStore keyStore = KeyStore.getInstance(secretKeyManagerProperties.getCertKeyType()); | ||
|
||
keyStore.load(secretKeyManagerProperties.getCertKey().getInputStream(), | ||
secretKeyManagerProperties.getPassword().toCharArray()); | ||
|
||
RestTemplate sslRestTemplate = new RestTemplateBuilder() | ||
.setConnectTimeout(Duration.ofSeconds(5)) | ||
.setConnectTimeout(Duration.ofSeconds(5)) | ||
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) | ||
.defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE) | ||
.build(); | ||
|
||
sslRestTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(HttpClients.custom() | ||
.setSSLSocketFactory(new SSLConnectionSocketFactory(SSLContextBuilder.create() | ||
.loadKeyMaterial(keyStore, secretKeyManagerProperties.getPassword().toCharArray()).build())).build())); | ||
|
||
return sslRestTemplate; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...n/java/com/t3t/authenticationapi/exception/SecretKeyManagerApiRequestFailedException.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,10 @@ | ||
package com.t3t.authenticationapi.exception; | ||
|
||
/** | ||
* Secret Key Manager API 요청이 실패한 경우 발생하는 예외 | ||
*/ | ||
public class SecretKeyManagerApiRequestFailedException extends RuntimeException{ | ||
public SecretKeyManagerApiRequestFailedException(String message) { | ||
super(message); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...in/java/com/t3t/authenticationapi/keymanager/model/response/SecretKeyManagerResponse.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,25 @@ | ||
package com.t3t.authenticationapi.keymanager.model.response; | ||
|
||
import lombok.Getter; | ||
|
||
/** | ||
* Secret Key Manager API의 응답 형식을 정의한 클래스 | ||
* @author woody35545(구건모) | ||
*/ | ||
@Getter | ||
public class SecretKeyManagerResponse { | ||
private SecretKeyManagerResponseHeaderPartDto header; | ||
private SecretKeyManagerResponseBodyPartDto body; | ||
|
||
@Getter | ||
public static class SecretKeyManagerResponseHeaderPartDto { | ||
private int resultCode; | ||
private String resultMessage; | ||
private String isSuccessful; | ||
} | ||
|
||
@Getter | ||
public static class SecretKeyManagerResponseBodyPartDto { | ||
private String secret; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...main/java/com/t3t/authenticationapi/keymanager/properties/SecretKeyManagerProperties.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,25 @@ | ||
package com.t3t.authenticationapi.keymanager.properties; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.core.io.Resource; | ||
|
||
/** | ||
* Secret Key Manager 에서 사용될 속성을 저장하는 프로퍼티 클래스 | ||
* @author woody35545(구건모) | ||
*/ | ||
@Getter | ||
@Setter | ||
@Profile("!local") | ||
@ConfigurationProperties(prefix = "t3t.secret-key-manager") | ||
public class SecretKeyManagerProperties { | ||
private String appKey; | ||
private String password; | ||
private String certKeyType; | ||
private String certKeyPath; | ||
@Value("${t3t.secretKeyManager.certKeyPath}") | ||
private Resource certKey; | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/t3t/authenticationapi/keymanager/properties/SecretKeyProperties.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,34 @@ | ||
package com.t3t.authenticationapi.keymanager.properties; | ||
|
||
import lombok.Getter; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* Secret Key Manager 에 등록된 기밀 데이터의 key id를 저장하는 프로퍼티 클래스 | ||
* @author woody35545(구건모) | ||
*/ | ||
@Profile("!local") | ||
@Getter | ||
@Component | ||
public class SecretKeyProperties { | ||
@Value("${t3t.secretKeyManager.secrets.databaseServerIpAddress.keyId}") | ||
private String databaseIpAddressKeyId; | ||
@Value("${t3t.secretKeyManager.secrets.databaseServerPort.keyId}") | ||
private String databasePortKeyId; | ||
@Value("${t3t.secretKeyManager.secrets.databaseServerUsername.keyId}") | ||
private String databaseNameKeyId; | ||
@Value("${t3t.secretKeyManager.secrets.databaseName.keyId}") | ||
private String databaseUsernameKeyId; | ||
@Value("${t3t.secretKeyManager.secrets.databaseServerPassword.keyId}") | ||
private String databasePasswordKeyId; | ||
@Value("${t3t.secretKeyManager.secrets.jwtSecretKey.keyId}") | ||
private String jwtSecretKeyId; | ||
@Value("${t3t.secretKeyManager.secrets.redisServerIpAddress.keyId}") | ||
private String redisIpAddressKeyId; | ||
@Value("${t3t.secretKeyManager.secrets.redisServerPort.keyId}") | ||
private String redisPortKeyId; | ||
@Value("${t3t.secretKeyManager.secrets.redisServerPassword.keyId}") | ||
private String redisPasswordKeyId; | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/com/t3t/authenticationapi/keymanager/service/SecretKeyManagerService.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,59 @@ | ||
package com.t3t.authenticationapi.keymanager.service; | ||
|
||
import com.t3t.authenticationapi.exception.SecretKeyManagerApiRequestFailedException; | ||
import com.t3t.authenticationapi.keymanager.model.response.SecretKeyManagerResponse; | ||
import com.t3t.authenticationapi.keymanager.properties.SecretKeyManagerProperties; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.core.ParameterizedTypeReference; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
/** | ||
* Secret Key Manager 에 등록된 Secret 값을 가져오기 위한 서비스 클래스 | ||
* | ||
* @author woody35545(구건모) | ||
*/ | ||
@Profile("!local") | ||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class SecretKeyManagerService { | ||
private final RestTemplate sslRestTemplate; | ||
private final SecretKeyManagerProperties secretKeyManagerProperties; | ||
|
||
private static final ParameterizedTypeReference<SecretKeyManagerResponse> secretKeyManagerResponseTypeReference | ||
= new ParameterizedTypeReference<SecretKeyManagerResponse>() { | ||
}; | ||
|
||
/** | ||
* Secret Key Manager 에서 Secret 값 조회 | ||
* | ||
* @param keyId 조회할 Key ID(Secret Key Manager 에 등록된 기밀 데이터의 Key ID) | ||
* @return Secret Key Manager 에서 조회한 Secret 값을 String 형태로 반환 | ||
* @author woody35545(구건모) | ||
*/ | ||
public String getSecretValue(String keyId) { | ||
|
||
HttpEntity<SecretKeyManagerResponse> response = | ||
sslRestTemplate.exchange("https://api-keymanager.nhncloudservice.com/keymanager/v1.0/appkey/{appKey}/secrets/{keyId}", | ||
HttpMethod.GET, null, SecretKeyManagerResponse.class, | ||
secretKeyManagerProperties.getAppKey(), keyId); | ||
|
||
SecretKeyManagerResponse responseBody = response.getBody(); | ||
|
||
if (responseBody == null) { | ||
throw new SecretKeyManagerApiRequestFailedException("Response body is null."); | ||
} | ||
|
||
if (responseBody.getHeader() == null || responseBody.getBody() == null || !responseBody.getHeader().getIsSuccessful().equals("true") || responseBody.getBody().getSecret() == null) { | ||
log.error("Secret Key Manager API response: {}", responseBody); | ||
throw new SecretKeyManagerApiRequestFailedException(new StringBuilder().append("Fail to request Secret Key Manager API (Key ID:").append(keyId).append(")").toString()); | ||
} | ||
|
||
return responseBody.getBody().getSecret(); | ||
} | ||
} |
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.