-
Notifications
You must be signed in to change notification settings - Fork 0
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 #235 from MeeTeamNumdle/feat/#234_ssh-con
[FEAT] 로컬 spring에서 rds 연결 가능하도록 구현
- Loading branch information
Showing
4 changed files
with
128 additions
and
0 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
36 changes: 36 additions & 0 deletions
36
src/main/java/synk/meeteam/infra/rds/SshDataSourceConfig.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,36 @@ | ||
package synk.meeteam.infra.rds; | ||
|
||
import javax.sql.DataSource; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; | ||
import org.springframework.boot.jdbc.DataSourceBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Primary; | ||
import org.springframework.context.annotation.Profile; | ||
|
||
@Slf4j | ||
@Profile("stg") | ||
@Configuration | ||
@RequiredArgsConstructor | ||
public class SshDataSourceConfig { | ||
|
||
private final SshTunnelingInitializer initializer; | ||
|
||
@Bean("dataSource") | ||
@Primary | ||
public DataSource dataSource(DataSourceProperties properties) { | ||
|
||
Integer forwardedPort = initializer.buildSshConnection(); // ssh 연결 및 터널링 설정 | ||
String url = properties.getUrl().replace("[forwardedPort]", Integer.toString(forwardedPort)); | ||
log.info(url); | ||
return DataSourceBuilder.create() | ||
.url(url) | ||
.username(properties.getUsername()) | ||
.password(properties.getPassword()) | ||
.driverClassName(properties.getDriverClassName()) | ||
.build(); | ||
} | ||
|
||
} |
82 changes: 82 additions & 0 deletions
82
src/main/java/synk/meeteam/infra/rds/SshTunnelingInitializer.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,82 @@ | ||
package synk.meeteam.infra.rds; | ||
|
||
import static java.lang.System.exit; | ||
|
||
import com.jcraft.jsch.JSch; | ||
import com.jcraft.jsch.Session; | ||
import jakarta.annotation.PreDestroy; | ||
import jakarta.validation.constraints.NotNull; | ||
import java.util.Properties; | ||
import lombok.Setter; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.validation.annotation.Validated; | ||
|
||
@Slf4j | ||
@Profile("stg") | ||
@Component | ||
@ConfigurationProperties(prefix = "ssh") | ||
@Validated | ||
@Setter | ||
public class SshTunnelingInitializer { | ||
|
||
@NotNull | ||
private String remoteJumpHost; | ||
@NotNull | ||
private String user; | ||
@NotNull | ||
private int sshPort; | ||
@NotNull | ||
private String privateKey; | ||
@NotNull | ||
private String databaseUrl; | ||
@NotNull | ||
private int databasePort; | ||
|
||
private Session session; | ||
|
||
@PreDestroy | ||
public void closeSSH() { | ||
if (session.isConnected()) | ||
session.disconnect(); | ||
} | ||
|
||
public Integer buildSshConnection() { | ||
|
||
Integer forwardedPort = null; | ||
|
||
try { | ||
log.info("{}@{}:{}:{} with privateKey",user, remoteJumpHost, sshPort, databasePort); | ||
|
||
log.info("start ssh tunneling.."); | ||
JSch jSch = new JSch(); | ||
|
||
log.info("creating ssh session"); | ||
jSch.addIdentity(privateKey); // 개인키 | ||
session = jSch.getSession(user, remoteJumpHost, sshPort); // 세션 설정 | ||
Properties config = new Properties(); | ||
config.put("StrictHostKeyChecking", "no"); | ||
session.setConfig(config); | ||
log.info("complete creating ssh session"); | ||
|
||
log.info("start connecting ssh connection"); | ||
session.connect(); // ssh 연결 | ||
log.info("success connecting ssh connection "); | ||
|
||
// 로컬pc의 남는 포트 하나와 원격 접속한 pc의 db포트 연결 | ||
log.info("start forwarding"); | ||
forwardedPort = session.setPortForwardingL(0, databaseUrl, databasePort); | ||
log.info("successfully connected to database"); | ||
|
||
} catch (Exception e){ | ||
log.error("fail to make ssh tunneling"); | ||
this.closeSSH(); | ||
e.printStackTrace(); | ||
exit(1); | ||
} | ||
|
||
return forwardedPort; | ||
} | ||
} |
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