diff --git a/src/main/java/com/eactive/apim/portal/spring/DatabaseSessionVerifier.java b/src/main/java/com/eactive/apim/portal/spring/DatabaseSessionVerifier.java new file mode 100644 index 0000000..34bfc6b --- /dev/null +++ b/src/main/java/com/eactive/apim/portal/spring/DatabaseSessionVerifier.java @@ -0,0 +1,97 @@ +package com.eactive.apim.portal.spring; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.SmartInitializingSingleton; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; + +import javax.sql.DataSource; +import java.sql.*; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.*; + +@Configuration +@Profile("db_check") +@Slf4j +public class DatabaseSessionVerifier implements SmartInitializingSingleton { + private final DataSource emsDataSource; + + + public DatabaseSessionVerifier(DataSource emsDataSource) { + this.emsDataSource = emsDataSource; + } + + + @Override + public void afterSingletonsInstantiated() { + log.info("DB 검증 작업 시작 - {}", now()); + log.info("---------------------------------------------------"); + + log.info("1. 커넥션 확인"); + try (Connection conn = emsDataSource.getConnection()) { + try (PreparedStatement statement = conn.prepareStatement("select 1 from dual")) { + ResultSet rs = statement.executeQuery(); + if (rs.next()) { + log.info("DB 커넥션 확인됨"); + } + } + + log.info("\n2. Table 권한 확인 - {}", now()); + Map> grantTables = this.printTablePrivileges(conn); + + + log.info("\n3. PTL_PROPERTY 테이블 확인 - {}", now()); + if (!grantTables.containsKey("PTL_PROPERTY")) { + log.info("PTL_PROPERTY 테이블 확인 안됨. ROLE 재로딩 시도"); + + try (Statement stmt = conn.createStatement()) { + stmt.execute("SET ROLE_RL_EMS_ALL"); + } + + this.printTablePrivileges(conn); + + throw new InterruptedException("DB 검증 실패로 강제 종료"); + } else { + log.info("PTL_PROPERTY 테이블 확인 됨."); + } + + log.info("검증 작업 종료 - {}", now()); + } catch (Exception e) { + log.error("DB 세션 검증 실패", e); + throw new RuntimeException(e); + } + } + + + private LinkedHashMap> printTablePrivileges(Connection conn) throws SQLException { + LinkedHashMap> tables = new LinkedHashMap<>(); + + try (PreparedStatement stmt = conn.prepareStatement("SELECT * FROM ROLE_TAB_PRIVS WHERE role = ? ORDER BY table_name ASC")) { + stmt.setString(1, "RL_EMS_ALL"); + ResultSet rs = stmt.executeQuery(); + while(rs.next()) { + Set privileges; + if (tables.containsKey(rs.getString("table_name"))) { + privileges = tables.get(rs.getString("table_name")); + } else { + privileges = new LinkedHashSet<>(); + tables.put(rs.getString("table_name").toUpperCase(), privileges); + } + + privileges.add(rs.getString("privilege")); + } + + log.info("| Table | Privileges |"); + log.info("|---|---|"); + tables.forEach((key, value) -> log.info(String.format("| %-20s | %-36s |", key, value))); + } + + return tables; + } + + + private String now() { + return LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME); + } +} diff --git a/src/main/resources/logback-debug.xml b/src/main/resources/logback-debug.xml index abfa1c3..9edde75 100644 --- a/src/main/resources/logback-debug.xml +++ b/src/main/resources/logback-debug.xml @@ -35,6 +35,17 @@ + + ${LOG_PATH}/debug-report.log + + ${LOG_PATH}/backup/debug-report.%d{yyyy-MM-dd}.log + 7 + + + %msg%n + + + @@ -43,6 +54,7 @@ + @@ -53,4 +65,7 @@ + + + \ No newline at end of file diff --git a/src/main/resources/logback-rinjae.xml b/src/main/resources/logback-rinjae.xml index 99f8815..1bd0831 100644 --- a/src/main/resources/logback-rinjae.xml +++ b/src/main/resources/logback-rinjae.xml @@ -27,6 +27,17 @@ + + ${LOG_PATH}/report.log + + ${LOG_PATH}/backup/report.%d{yyyy-MM-dd}.log + 7 + + + %msg%n + + + @@ -45,5 +56,8 @@ + + + \ No newline at end of file