-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from nhnacademy-be6-5ritang/feature/config
feat: Secure Key Manager 추가
- Loading branch information
Showing
14 changed files
with
426 additions
and
34 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
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
18 changes: 18 additions & 0 deletions
18
src/main/java/com/nhnacademy/bookstoreaccount/global/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,18 @@ | ||
package com.nhnacademy.bookstoreaccount.global.config; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
import org.springframework.boot.web.client.RestTemplateBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.converter.StringHttpMessageConverter; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Configuration | ||
public class RestTemplateConfig { | ||
|
||
@Bean | ||
public RestTemplate restTemplate() { | ||
return new RestTemplate(); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...ount/global/config/LogNCrashAppender.java → ...ccount/global/util/LogNCrashAppender.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
24 changes: 24 additions & 0 deletions
24
src/main/java/com/nhnacademy/bookstoreaccount/keymanager/dto/KeyResponse.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,24 @@ | ||
package com.nhnacademy.bookstoreaccount.keymanager.dto; | ||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
public class KeyResponse { | ||
private Header header; | ||
private Body body; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public static class Body { | ||
private String secret; | ||
} | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public static class Header { | ||
private Integer resultCode; | ||
private String resultMessage; | ||
private boolean isSuccessful; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/nhnacademy/bookstoreaccount/keymanager/property/RedisProperty.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,16 @@ | ||
package com.nhnacademy.bookstoreaccount.keymanager.property; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@ConfigurationProperties("oritang.redis") | ||
public class RedisProperty { | ||
private String host; | ||
private String port; | ||
private String password; | ||
private String database; | ||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/com/nhnacademy/bookstoreaccount/keymanager/service/KeyManagerService.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,65 @@ | ||
package com.nhnacademy.bookstoreaccount.keymanager.service; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import com.nhnacademy.bookstoreaccount.keymanager.dto.KeyResponse; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
/** | ||
* @author 이경헌 | ||
* KeyManager는 인증서를 사용하여 클라우드에서 기밀 데이터를 가져오는 클래스입니다. | ||
*/ | ||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class KeyManagerService { | ||
private final RestTemplate restTemplate; | ||
|
||
/** | ||
* 인증서를 사용하여 클라우드에서 기밀 데이터를 가져오는 메서드 | ||
* | ||
* @param keyId 조회를 원하는 데이터의 Key value | ||
* @return key value 에 해당하는 데이터 | ||
*/ | ||
public String getSecret(String keyId) { | ||
try { | ||
// HTTP 요청을 위한 헤더 설정 | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setContentType(MediaType.APPLICATION_JSON); | ||
headers.setAccept(List.of(MediaType.APPLICATION_JSON)); | ||
headers.set("X-TC-AUTHENTICATION-ID", "3bTA7VD3xkZzLXPnt31X"); | ||
headers.set("X-TC-AUTHENTICATION-SECRET", "WuXXhpYwgdSoE3mY"); | ||
|
||
// URI 생성 | ||
String url = | ||
"https://api-keymanager.nhncloudservice.com/keymanager/v1.2/appkey/2SxwmBzUfnqJaA2A/secrets/" + keyId; | ||
|
||
// HttpEntity를 사용하여 헤더 포함 | ||
HttpEntity<String> entity = new HttpEntity<>(headers); | ||
|
||
// 데이터 요청 및 반환 | ||
ResponseEntity<KeyResponse> responseEntity = restTemplate.exchange(url, HttpMethod.GET, entity, | ||
KeyResponse.class); | ||
KeyResponse responseBody = responseEntity.getBody(); | ||
if (responseBody != null) { | ||
return responseBody.getBody().getSecret(); | ||
} else { | ||
log.error("응답 본문이 비어있습니다."); | ||
return null; | ||
} | ||
} catch (Exception e) { | ||
log.error("키매니저 에러: {}", e.getMessage()); | ||
return null; | ||
} | ||
} | ||
} |
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
13 changes: 0 additions & 13 deletions
13
src/test/java/com/nhnacademy/bookstoreaccount/BookStoreAccountApplicationTests.java
This file was deleted.
Oops, something went wrong.
64 changes: 64 additions & 0 deletions
64
src/test/java/com/nhnacademy/bookstoreaccount/global/LogNCrashAppenderTest.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,64 @@ | ||
package com.nhnacademy.bookstoreaccount.global; | ||
|
||
import static org.mockito.Mockito.*; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import com.nhnacademy.bookstoreaccount.global.util.LogNCrashAppender; | ||
|
||
import ch.qos.logback.classic.LoggerContext; | ||
import ch.qos.logback.classic.spi.ILoggingEvent; | ||
|
||
class LogNCrashAppenderTest { | ||
|
||
@Mock | ||
private RestTemplate restTemplate; | ||
|
||
@InjectMocks | ||
private LogNCrashAppender logNCrashAppender; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
|
||
// Create a LoggerContext | ||
LoggerContext loggerContext = new LoggerContext(); | ||
logNCrashAppender.setContext(loggerContext); | ||
logNCrashAppender.start(); | ||
} | ||
|
||
@Test | ||
void testAppend() { | ||
ILoggingEvent loggingEvent = mock(ILoggingEvent.class); | ||
|
||
when(loggingEvent.getFormattedMessage()).thenReturn("Test log message"); | ||
when(loggingEvent.getLevel()).thenReturn(ch.qos.logback.classic.Level.INFO); | ||
|
||
Map<String, Object> logData = new HashMap<>(); | ||
logData.put("projectName", "Xyx7DoyszcG66ULx"); | ||
logData.put("projectVersion", "1.0.0"); | ||
logData.put("logVersion", "v2"); | ||
logData.put("body", loggingEvent.getFormattedMessage()); | ||
logData.put("logSource", "http"); | ||
logData.put("logType", "log"); | ||
logData.put("platform", "5ritang-Gateway"); | ||
logData.put("host", "192.168.0.75"); | ||
logData.put("logLevel", loggingEvent.getLevel().toString()); | ||
|
||
when(restTemplate.postForEntity("https://api-logncrash.nhncloudservice.com/v2/log", logData, String.class)).thenReturn(new ResponseEntity<>(HttpStatus.OK)); | ||
|
||
logNCrashAppender.doAppend(loggingEvent); | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.