-
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.
feat: #8
SecretKeyManagerService
- 기밀 데이터 조회 기능
- Loading branch information
1 parent
82645fa
commit 376d63d
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
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(); | ||
} | ||
} |