├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── pom.xml └── src ├── main ├── java │ ├── com │ │ └── m11n │ │ │ └── jdbc │ │ │ └── ssh │ │ │ ├── SshConfiguration.java │ │ │ ├── SshDriver.java │ │ │ └── SshTunnel.java │ └── overview.ad └── resources │ ├── META-INF │ └── services │ │ └── java.sql.Driver │ └── PROPERTIES ├── site ├── asciidoc │ ├── configuration.adoc │ ├── contribute.adoc │ └── index.adoc ├── resources │ ├── css │ │ └── site.css │ └── images │ │ └── bg.png └── site.xml └── test ├── java └── com │ └── m11n │ └── jdbc │ └── ssh │ ├── DerbySshDriverTest.java │ ├── H2SshDriverTest.java │ ├── JdbcSshDriverTest.java │ └── util │ ├── BogusPasswordAuthenticator.java │ ├── Slf4jDerbyBridge.java │ ├── Slf4jOutputStream.java │ └── TestCachingPublicKeyAuthenticator.java └── resources ├── logback.xml └── ssh.properties /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | pom.xml.tag 3 | pom.xml.releaseBackup 4 | pom.xml.versionsBackup 5 | pom.xml.next 6 | release.config 7 | atlassian-ide-plugin.xml 8 | .idea 9 | *.iml 10 | build.sh 11 | release.sh 12 | trace.db 13 | derby.log -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | 1.0.10 2 | 3 | - bug fix: avoid wrong local port increment (@emac) 4 | - Update dependencies 5 | 6 | 1.0.9 7 | 8 | - pass original connect properties to real driver and 1 bug fix (@emac) 9 | 10 | 1.0.8 11 | 12 | - Osgi compatible package structure 13 | - Constistent naming of classes (should be final now) 14 | 15 | 1.0.7 16 | 17 | - release promoted to Maven central 18 | - fixed GPG signing for releases 19 | 20 | 1.0.6 21 | 22 | - project configuration improvements 23 | 24 | 1.0.5 25 | 26 | - project configuration improvements 27 | 28 | 1.0.4 29 | 30 | - project configuration improvements 31 | 32 | 1.0.3 33 | 34 | - check for valid configuration properties 35 | - refactored all packages to "com.m11n.jdbc" 36 | - project configuration improvements 37 | 38 | 1.0.2 39 | 40 | - passwordless logins 41 | - Derby logging over Slf4j 42 | - GPG signing for Maven Central 43 | - use Asciidoclet 44 | - minor improvements and bugfixes 45 | 46 | 1.0.1 47 | 48 | - Initial release 49 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # jdbc-ssh 2 | 3 | JDBC driver over SSH tunnel. ![Maven Central](https://img.shields.io/maven-central/v/com.github.monkeysintown/jdbc-ssh.svg) 4 | 5 | 6 | ## Build 7 | 8 | If you just want to compile the project without running the tests: 9 | 10 | ``` 11 | mvn -DskipTests clean install 12 | ``` 13 | 14 | If you want to run the tests (Derby and H2 in server mode): 15 | 16 | ``` 17 | mvn -Djdbc.ssh.username="xxx" -Djdbc.ssh.password="xxx" clean install 18 | ``` 19 | 20 | NOTE: 21 | 22 | If your SSH server is not running on the default port 22 and/or localhost then you can change those parameters: 23 | 24 | ``` 25 | mvn -Djdbc.ssh.username="xxx" -Djdbc.ssh.password="xxx" -Djdbc.ssh.host="192.168.0.1" -Djdbc.ssh.port="2222" clean install 26 | ``` 27 | 28 | At the moment a locally running SSH server is needed for the tests. The embedded SSH server in the unit tests is not yet 29 | ready (authentication works, but port forwarding fails at the moment). 30 | 31 | ## Maven dependencies 32 | 33 | You can find the latest releases here: 34 | 35 | [ ![Download](https://api.bintray.com/packages/cheetah/monkeysintown/jdbc-ssh/images/download.svg) ](https://bintray.com/cheetah/monkeysintown/jdbc-ssh/_latestVersion) 36 | 37 | ... or setup your Maven dependencies: 38 | 39 | ```xml 40 | 41 | com.github.monkeysintown 42 | jdbc-ssh 43 | 1.0.9 44 | 45 | ``` 46 | 47 | ... and configure Bintray's JCenter repository in your pom.xml: 48 | 49 | ```xml 50 | ... 51 | 52 | 53 | 54 | false 55 | 56 | central 57 | bintray 58 | http://jcenter.bintray.com 59 | 60 | 61 | ... 62 | ``` 63 | 64 | Get automatic notifications about new releases here: 65 | 66 | [ ![Get automatic notifications about new "jdbc-ssh" versions](https://www.bintray.com/docs/images/bintray_badge_color.png) ](https://bintray.com/cheetah/monkeysintown/jdbc-ssh/view?source=watch) 67 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | com.github.monkeysintown 6 | jdbc-ssh 7 | 1.0.11-SNAPSHOT 8 | 9 | jar 10 | JDBC SSH 11 | JDBC over SSH 12 | https://monkeysintown.github.io/jdbc-ssh 13 | 14 | 15 | 16 | Apache 2.0 17 | http://www.apache.org/licenses/LICENSE-2.0.txt 18 | repo 19 | 20 | 21 | 22 | 23 | Monkeys in Town Lda. 24 | https://github.com/monkeysintown 25 | 26 | 27 | 28 | scm:git:https://github.com/monkeysintown/jdbc-ssh.git 29 | scm:git:https://github.com/monkeysintown/jdbc-ssh.git 30 | scm:git:https://github.com/monkeysintown/jdbc-ssh.git 31 | 32 | 33 | 34 | 35 | vidakovic 36 | Aleksandar Vidakovic 37 | 38 | https://github.com/vidakovic 39 | Monkeys in Town Lda. 40 | https://github.com/monkeysintown 41 | 42 | developer 43 | 44 | 0 45 | 46 | 47 | 48 | 49 | 3.2.3 50 | 51 | 52 | 53 | 1.7.12 54 | 1.1.3 55 | 0.1.53 56 | 1.1.3 57 | UTF-8 58 | UTF-8 59 | 1.7 60 | github 61 | 62 | 63 | 64 | 65 | bintray 66 | https://api.bintray.com/maven/cheetah/monkeysintown/jdbc-ssh 67 | 68 | 69 | 70 | 71 | 72 | rubygems-release 73 | http://rubygems-proxy.torquebox.org/releases 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | com.jcraft 82 | jsch 83 | ${jsch.version} 84 | 85 | 86 | com.jcraft 87 | jzlib 88 | ${jzlib.version} 89 | 90 | 91 | 92 | 93 | org.slf4j 94 | slf4j-api 95 | ${slf4j.version} 96 | 97 | 98 | ch.qos.logback 99 | logback-core 100 | ${logback.version} 101 | true 102 | 103 | 104 | ch.qos.logback 105 | logback-classic 106 | ${logback.version} 107 | true 108 | 109 | 110 | org.slf4j 111 | slf4j-api 112 | 113 | 114 | 115 | 116 | 117 | mysql 118 | mysql-connector-java 119 | 5.1.34 120 | test 121 | 122 | 123 | 124 | com.h2database 125 | h2 126 | 1.4.183 127 | test 128 | 129 | 130 | 131 | org.apache.derby 132 | derby 133 | 10.11.1.1 134 | test 135 | 136 | 137 | 138 | org.apache.derby 139 | derbynet 140 | 10.11.1.1 141 | test 142 | 143 | 144 | 145 | org.apache.derby 146 | derbyclient 147 | 10.11.1.1 148 | test 149 | 150 | 151 | 152 | org.apache.sshd 153 | sshd-core 154 | 0.14.0 155 | test 156 | 157 | 158 | org.slf4j 159 | slf4j-api 160 | 161 | 162 | 163 | 164 | 165 | org.bouncycastle 166 | bcpg-jdk15on 167 | 1.52 168 | test 169 | 170 | 171 | org.bouncycastle 172 | bcpkix-jdk15on 173 | 1.52 174 | test 175 | 176 | 177 | 178 | junit 179 | junit 180 | 4.12 181 | test 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | src/main/resources 191 | true 192 | 193 | 194 | 195 | 196 | 197 | 198 | org.apache.maven.plugins 199 | maven-source-plugin 200 | 2.2.1 201 | 202 | 203 | attach-sources 204 | 205 | jar-no-fork 206 | 207 | 208 | 209 | 210 | 211 | 212 | org.apache.maven.plugins 213 | maven-compiler-plugin 214 | 3.2 215 | 216 | ${java.version} 217 | ${java.version} 218 | 219 | 220 | 221 | 222 | org.apache.maven.plugins 223 | maven-deploy-plugin 224 | 2.8.2 225 | 226 | 227 | 228 | org.apache.maven.plugins 229 | maven-project-info-reports-plugin 230 | 2.7 231 | 232 | 233 | 234 | org.asciidoctor 235 | asciidoctor-maven-plugin 236 | 1.5.2 237 | 238 | ${project.build.directory}/generated-docs/asciidoc 239 | ${project.build.directory}/generated-docs/asciidoc 240 | index.ad 241 | ${project.build.directory}/gems-provided 242 | 243 | 244 | true 245 | true 246 | UTF-8 247 | images 248 | 249 | 250 | true 251 | left 252 | font 253 | true 254 | 255 | - 256 | true 257 | ${project.url} 258 | ${project.build.sourceDirectory} 259 | 260 | coderay 261 | 262 | 263 | 264 | 265 | asciidoc-to-docbook 266 | generate-resources 267 | 268 | process-asciidoc 269 | 270 | 271 | ${project.build.directory}/generated-docs/docbook/en-US 272 | docbook5 273 | book 274 | slim 275 | true 276 | false 277 | 278 | 279 | 280 | asciidoc-to-html5 281 | generate-resources 282 | 283 | process-asciidoc 284 | 285 | 286 | ${project.build.directory}/generated-docs/html5 287 | html5 288 | 289 | 290 | 291 | asciidoc-to-pdf 292 | generate-resources 293 | 294 | process-asciidoc 295 | 296 | 297 | ${project.build.directory}/gems 298 | 299 | asciidoctor-pdf 300 | 301 | pdf 302 | 303 | 304 | 305 | 306 | 307 | org.asciidoctor 308 | asciidoctorj 309 | 1.5.2 310 | 311 | 312 | org.jruby 313 | jruby-complete 314 | 1.7.18 315 | 316 | 317 | 318 | 319 | 320 | com.github.github 321 | site-maven-plugin 322 | 0.10 323 | 324 | Creating site for ${project.version} 325 | 326 | 327 | 328 | 329 | site 330 | 331 | site 332 | 333 | 334 | 335 | 336 | 337 | org.apache.maven.plugins 338 | maven-site-plugin 339 | 3.4 340 | 341 | 342 | lt.velykis.maven.skins 343 | reflow-velocity-tools 344 | 1.1.1 345 | 346 | 347 | 348 | org.apache.velocity 349 | velocity 350 | 1.7 351 | 352 | 353 | org.asciidoctor 354 | asciidoctor-maven-plugin 355 | 1.5.2 356 | 357 | 358 | 359 | 360 | external.atlassian.jgitflow 361 | jgitflow-maven-plugin 362 | 1.0-m4.3 363 | 364 | ${github.username} 365 | ${github.password} 366 | true 367 | true 368 | true 369 | false 370 | false 371 | false 372 | false 373 | true 374 | 375 | 376 | 377 | 378 | org.apache.maven.plugins 379 | maven-javadoc-plugin 380 | 2.10.1 381 | 382 | 383 | attach-javadocs 384 | 385 | test-jar 386 | jar 387 | 388 | 389 | 390 | 391 | ${java.version} 392 | org.asciidoctor.Asciidoclet 393 | 394 | org.asciidoctor 395 | asciidoclet 396 | 1.5.1 397 | 398 | src/main/java/overview.ad 399 | 400 | --base-dir ${project.basedir} 401 | --attribute "name=${project.name}" 402 | --attribute "version=${project.version}" 403 | --attribute "title-link=${project.url}[${project.name} ${project.version}]" 404 | 405 | true 406 | 407 | 408 | 409 | 410 | org.owasp 411 | dependency-check-maven 412 | 1.2.8 413 | 414 | 415 | 416 | check 417 | 418 | 419 | 420 | 421 | 422 | 423 | org.apache.maven.plugins 424 | maven-gpg-plugin 425 | 1.5 426 | 427 | 428 | sign-artifacts 429 | verify 430 | 431 | sign 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | org.apache.maven.plugins 442 | maven-compiler-plugin 443 | 444 | 445 | 446 | external.atlassian.jgitflow 447 | jgitflow-maven-plugin 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | org.owasp 456 | dependency-check-maven 457 | 458 | 459 | 460 | aggregate 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | release-profile 471 | 472 | 473 | performRelease 474 | true 475 | 476 | 477 | 478 | 479 | 480 | org.apache.maven.plugins 481 | maven-javadoc-plugin 482 | 483 | 484 | 485 | org.apache.maven.plugins 486 | maven-gpg-plugin 487 | 488 | 489 | 490 | com.github.github 491 | site-maven-plugin 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | -------------------------------------------------------------------------------- /src/main/java/com/m11n/jdbc/ssh/SshConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.m11n.jdbc.ssh; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | 6 | import java.io.*; 7 | import java.util.*; 8 | 9 | /** 10 | * = JdbcSshConfiguration 11 | * 12 | * A think wrapper around `java.util.Properties`. 13 | * 14 | * @author https://github.com/vidakovic[Aleksandar Vidakovic] 15 | */ 16 | public class SshConfiguration { 17 | private static final Logger logger = LoggerFactory.getLogger(SshConfiguration.class); 18 | 19 | public static final String CONFIG = "jdbc.ssh.config"; 20 | public static final String DRIVER_PREFIX = "jdbc:ssh:"; 21 | public static final String CONFIG_HOST = "jdbc.ssh.host"; 22 | public static final String CONFIG_PORT = "jdbc.ssh.port"; 23 | public static final String CONFIG_USERNAME = "jdbc.ssh.username"; 24 | public static final String CONFIG_PASSWORD = "jdbc.ssh.password"; 25 | public static final String CONFIG_KEY_PRIVATE = "jdbc.ssh.key.private"; 26 | public static final String CONFIG_KEY_PUBLIC = "jdbc.ssh.key.public"; 27 | public static final String CONFIG_PASSPHRASE = "jdbc.ssh.passphrase"; 28 | public static final String CONFIG_KNOWN_HOSTS = "jdbc.ssh.known.hosts"; 29 | public static final String CONFIG_HOST_REMOTE = "jdbc.ssh.host.remote"; 30 | public static final String CONFIG_PORT_REMOTE = "jdbc.ssh.port.remote"; 31 | public static final String CONFIG_PORT_AUTO = "jdbc.ssh.port.auto"; 32 | 33 | private Properties config; 34 | 35 | private final Set allowed = new HashSet<>(); 36 | 37 | public SshConfiguration() { 38 | importAllowedPropertyNames(); 39 | 40 | config = new Properties(); 41 | 42 | try { 43 | InputStream is; 44 | 45 | String path = System.getProperty(CONFIG)==null ? "ssh.properties" : System.getProperty(CONFIG); 46 | 47 | File f = new File(path); 48 | 49 | if(f.exists()) { 50 | is = new FileInputStream(f); 51 | } else { 52 | is = SshConfiguration.class.getClassLoader().getResourceAsStream(path); 53 | } 54 | 55 | if(is!=null) { 56 | config.load(is); 57 | } 58 | 59 | check(config); 60 | 61 | config = setDefaults(config); 62 | } catch(Exception e) { 63 | logger.warn(e.toString(), e); 64 | } 65 | } 66 | 67 | public SshConfiguration(Properties c) { 68 | this(); 69 | 70 | // NOTE: assume same username and password for SSH connection and database if not set explicitly 71 | if(c.getProperty("user")!=null && config.getProperty(CONFIG_USERNAME) == null) { 72 | config.setProperty(CONFIG_USERNAME, c.getProperty("user")); 73 | } 74 | if(c.getProperty("password")!=null && config.getProperty(CONFIG_PASSWORD) == null) { 75 | config.setProperty(CONFIG_PASSWORD, c.getProperty("password")); 76 | } 77 | 78 | for(String key : allowed) { 79 | if(c.getProperty(key)!=null && !"".equals(c.getProperty(key).trim())) { 80 | config.setProperty(key, c.getProperty(key)); 81 | } 82 | } 83 | 84 | if(logger.isDebugEnabled()) { 85 | logger.debug("Configuration: {}", config); 86 | } 87 | } 88 | 89 | private void importAllowedPropertyNames() { 90 | try { 91 | allowed.clear(); 92 | 93 | BufferedReader reader = new BufferedReader(new InputStreamReader(SshConfiguration.class.getClassLoader().getResourceAsStream("PROPERTIES"))); 94 | 95 | String line; 96 | 97 | while((line=reader.readLine())!=null) { 98 | allowed.add(line); 99 | } 100 | } catch(Exception e) { 101 | throw new RuntimeException(e); 102 | } 103 | } 104 | 105 | private Properties setDefaults(Properties c) { 106 | if(c.getProperty("Compression")==null) { 107 | c.put("Compression", getSystemPropertyOrDefault("Compression", "no")); 108 | } 109 | if(c.getProperty("ConnectionAttempts") == null) { 110 | c.put("ConnectionAttempts", getSystemPropertyOrDefault("ConnectionAttempts", "2")); 111 | } 112 | if(c.getProperty("StrictHostKeyChecking")==null) { 113 | c.put("StrictHostKeyChecking", getSystemPropertyOrDefault("StrictHostKeyChecking", "no")); 114 | } 115 | if(c.getProperty(CONFIG_HOST)==null) { 116 | c.put(CONFIG_HOST, getSystemPropertyOrDefault(CONFIG_HOST, "localhost")); 117 | } 118 | if(c.getProperty(CONFIG_PORT)==null) { 119 | c.put(CONFIG_PORT, getSystemPropertyOrDefault(CONFIG_PORT, "22")); 120 | } 121 | if(c.getProperty(CONFIG_PORT_AUTO)==null) { 122 | c.put(CONFIG_PORT_AUTO, getSystemPropertyOrDefault(CONFIG_PORT_AUTO, "20000")); 123 | } 124 | if(c.getProperty(CONFIG_USERNAME)==null) { 125 | c.put(CONFIG_USERNAME, System.getProperty(CONFIG_USERNAME)); 126 | } 127 | if(c.getProperty(CONFIG_PASSWORD)==null) { 128 | c.put(CONFIG_PASSWORD, System.getProperty(CONFIG_PASSWORD)); 129 | } 130 | if(c.getProperty(CONFIG_KEY_PRIVATE)==null) { 131 | c.put(CONFIG_KEY_PRIVATE, getSystemPropertyOrDefault(CONFIG_KEY_PRIVATE, "")); 132 | } 133 | if(c.getProperty(CONFIG_KEY_PUBLIC)==null) { 134 | c.put(CONFIG_KEY_PUBLIC, getSystemPropertyOrDefault(CONFIG_KEY_PUBLIC, c.getProperty(CONFIG_KEY_PRIVATE) + ".pub")); 135 | } 136 | if(c.getProperty(CONFIG_PASSPHRASE)==null) { 137 | c.put(CONFIG_PASSPHRASE, getSystemPropertyOrDefault(CONFIG_PASSPHRASE, "")); 138 | } 139 | if(c.getProperty(CONFIG_KNOWN_HOSTS)==null) { 140 | c.put(CONFIG_KNOWN_HOSTS, getSystemPropertyOrDefault(CONFIG_KNOWN_HOSTS, "~/.ssh/known_hosts")); 141 | } 142 | 143 | return c; 144 | } 145 | 146 | private void check(Properties c) { 147 | Enumeration keys = c.keys(); 148 | while(keys.hasMoreElements()) { 149 | String key = keys.nextElement().toString(); 150 | if(!allowed.contains(key)) { 151 | logger.warn("Skipping property: {}", key); 152 | c.remove(key); 153 | } 154 | } 155 | } 156 | 157 | private String getSystemPropertyOrDefault(String name, String defaultValue) { 158 | return System.getProperty(name)==null ? defaultValue : System.getProperty(name); 159 | } 160 | 161 | public Properties getProperties() { 162 | return config; 163 | } 164 | 165 | public String getProperty(String name) { 166 | return config.getProperty(name); 167 | } 168 | } 169 | -------------------------------------------------------------------------------- /src/main/java/com/m11n/jdbc/ssh/SshDriver.java: -------------------------------------------------------------------------------- 1 | package com.m11n.jdbc.ssh; 2 | 3 | import java.net.MalformedURLException; 4 | import java.net.URL; 5 | import java.sql.*; 6 | import java.util.Enumeration; 7 | import java.util.Properties; 8 | import java.util.logging.Logger; 9 | 10 | import static com.m11n.jdbc.ssh.SshConfiguration.*; 11 | 12 | public class SshDriver implements Driver { 13 | private static final int VERSION_MAJOR = 1; 14 | private static final int VERSION_MINOR = 0; 15 | 16 | private SshTunnel tunnel; 17 | 18 | public SshDriver() throws SQLException { 19 | } 20 | 21 | static 22 | { 23 | try 24 | { 25 | java.sql.DriverManager.registerDriver(new SshDriver()); 26 | } 27 | catch (SQLException e) 28 | { 29 | e.printStackTrace(); 30 | } 31 | } 32 | 33 | @Override 34 | public boolean acceptsURL(String url) throws SQLException { 35 | return (url != null && url.startsWith(DRIVER_PREFIX)); 36 | } 37 | 38 | private String extractUrl(String url) { 39 | return url.startsWith(DRIVER_PREFIX) ? url.replace("ssh:", "") : url; 40 | } 41 | 42 | @Override 43 | public Connection connect(String url, Properties info) throws SQLException { 44 | if (url == null) { 45 | throw new SQLException("URL is required"); 46 | } 47 | 48 | if( !acceptsURL(url) ) { 49 | return null; 50 | } 51 | 52 | SshConfiguration config = configure(url, info); 53 | 54 | tunnel = new SshTunnel(config); 55 | tunnel.start(); 56 | 57 | // TODO: check if this is enough for most common drivers 58 | String realUrl = extractUrl(url).replaceFirst(":\\d+", ":" + tunnel.getLocalPort().toString()); 59 | realUrl = realUrl.replaceFirst("://(\\d+\\.\\d+\\.\\d+\\.\\d+|[a-zA-Z0-9_\\-\\.]*)", "://localhost"); 60 | Driver driver = findDriver(realUrl); 61 | 62 | return driver.connect(realUrl, info); 63 | } 64 | 65 | private SshConfiguration configure(String url, Properties info) throws SQLException { 66 | SshConfiguration config; 67 | 68 | try { 69 | URL u = toURL(url); 70 | 71 | Properties properties = new Properties(); 72 | 73 | if(info!=null && !info.isEmpty()) { 74 | properties.putAll(info); 75 | } 76 | 77 | if(u.getQuery()!=null) { 78 | String[] parts = u.getQuery().split("&"); 79 | 80 | for(String part : parts) { 81 | String[] pair = part.split("="); 82 | if(pair!=null && pair.length==2) { 83 | properties.setProperty(pair[0], pair[1]); 84 | } 85 | } 86 | } 87 | 88 | properties.setProperty(CONFIG_HOST_REMOTE, u.getHost()); 89 | properties.setProperty(CONFIG_PORT_REMOTE, u.getPort()+""); 90 | 91 | config = new SshConfiguration(properties); 92 | } catch (Exception e) { 93 | throw new SQLException(e); 94 | } 95 | 96 | return config; 97 | } 98 | 99 | private URL toURL(String url) throws MalformedURLException { 100 | // NOTE: trick to parse multipart scheme of most common JDBC URLs 101 | String sanitizedString = null; 102 | int schemeEndOffset = url.indexOf("://"); 103 | if (-1 == schemeEndOffset) { 104 | // couldn't find one? try our best here. 105 | sanitizedString = "http://" + url; 106 | } else { 107 | sanitizedString = "http" + url.substring(schemeEndOffset); 108 | } 109 | 110 | return new URL(sanitizedString); 111 | } 112 | 113 | private Driver findDriver(String url) throws SQLException { 114 | Driver realDriver = null; 115 | 116 | for(Enumeration drivers = DriverManager.getDrivers(); drivers.hasMoreElements();) { 117 | try { 118 | Driver driver = drivers.nextElement(); 119 | 120 | if (driver.acceptsURL(url)) { 121 | realDriver = driver; 122 | break; 123 | } 124 | } catch (SQLException e) { 125 | } 126 | } 127 | 128 | if( realDriver == null ) { 129 | throw new SQLException("Unable to find a driver that accepts " + url); 130 | } 131 | 132 | return realDriver; 133 | } 134 | 135 | @Override 136 | public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException { 137 | return findDriver(url).getPropertyInfo(url, info); 138 | } 139 | 140 | @Override 141 | public int getMajorVersion() { 142 | return VERSION_MAJOR; 143 | } 144 | 145 | @Override 146 | public int getMinorVersion() { 147 | return VERSION_MINOR; 148 | } 149 | 150 | @Override 151 | public boolean jdbcCompliant() { 152 | return true; 153 | } 154 | 155 | @Override 156 | public Logger getParentLogger() throws SQLFeatureNotSupportedException { 157 | throw new SQLFeatureNotSupportedException("Feature not supported"); 158 | } 159 | } 160 | -------------------------------------------------------------------------------- /src/main/java/com/m11n/jdbc/ssh/SshTunnel.java: -------------------------------------------------------------------------------- 1 | package com.m11n.jdbc.ssh; 2 | 3 | import com.jcraft.jsch.Channel; 4 | import com.jcraft.jsch.JSch; 5 | import com.jcraft.jsch.Session; 6 | import org.slf4j.Logger; 7 | import org.slf4j.LoggerFactory; 8 | 9 | import java.net.InetSocketAddress; 10 | import java.net.Socket; 11 | import java.util.concurrent.atomic.AtomicInteger; 12 | 13 | import static com.m11n.jdbc.ssh.SshConfiguration.*; 14 | 15 | public class SshTunnel { 16 | private static final Logger logger = LoggerFactory.getLogger(SshTunnel.class); 17 | 18 | private SshConfiguration config; 19 | 20 | private Session session; 21 | 22 | private AtomicInteger localPort; 23 | 24 | public SshTunnel(SshConfiguration config) { 25 | this.config = config; 26 | 27 | localPort = new AtomicInteger(Integer.valueOf(config.getProperty(CONFIG_PORT_AUTO))); 28 | 29 | logger.info("Automatic local port assignment starts at: {}", localPort.get()); 30 | 31 | Runtime.getRuntime().addShutdownHook(new Thread() { 32 | @Override 33 | public void run() { 34 | logger.info("Shutting down tunnel..."); 35 | SshTunnel.this.stop(); 36 | } 37 | }); 38 | } 39 | 40 | public void start() { 41 | int assignedPort = 0; 42 | 43 | try { 44 | JSch jsch = new JSch(); 45 | 46 | String username = config.getProperty(CONFIG_USERNAME); 47 | String password = config.getProperty(CONFIG_PASSWORD); 48 | String keyPrivate = config.getProperty(CONFIG_KEY_PRIVATE); 49 | String keyPublic = config.getProperty(CONFIG_KEY_PUBLIC); 50 | String passphrase = config.getProperty(CONFIG_PASSPHRASE); 51 | String knownHosts = config.getProperty(CONFIG_KNOWN_HOSTS); 52 | String host = config.getProperty(CONFIG_HOST); 53 | Integer port = Integer.valueOf(config.getProperty(CONFIG_PORT)); 54 | 55 | assert host!=null; 56 | assert port!=null; 57 | 58 | boolean useKey = (keyPrivate!=null && !"".equals(keyPrivate.trim())); 59 | 60 | session = jsch.getSession(username, host, port); 61 | 62 | jsch.setKnownHosts(knownHosts); 63 | 64 | if(useKey) { 65 | if(passphrase==null || "".equals(passphrase.trim())) { 66 | jsch.addIdentity(keyPrivate, keyPublic); 67 | } else { 68 | jsch.addIdentity(keyPrivate, keyPublic, passphrase.getBytes()); 69 | } 70 | } else { 71 | session.setPassword(password); 72 | } 73 | 74 | session.setConfig(config.getProperties()); 75 | session.setDaemonThread(true); 76 | 77 | // Connect 78 | session.connect(); 79 | 80 | Channel channel = session.openChannel("shell"); 81 | channel.connect(); 82 | 83 | String forwardHost = config.getProperty(CONFIG_HOST_REMOTE); 84 | Integer remotePort = Integer.valueOf(config.getProperty(CONFIG_PORT_REMOTE)); 85 | 86 | int nextPort = localPort.incrementAndGet(); 87 | 88 | // NOTE: scan max next 10 ports 89 | for(int i=0; i<10; i++) { 90 | if(isPortOpen("127.0.0.1", nextPort)) { 91 | break; 92 | } 93 | 94 | nextPort = localPort.incrementAndGet(); 95 | } 96 | 97 | assignedPort = session.setPortForwardingL(nextPort, forwardHost, remotePort); 98 | 99 | if(logger.isDebugEnabled()) { 100 | logger.debug("Server version: {}", session.getServerVersion()); 101 | logger.debug("Client version: {}", session.getClientVersion()); 102 | logger.debug("Host : {}", session.getHost()); 103 | logger.debug("Port : {}", session.getPort()); 104 | logger.debug("Forwarding : {}", session.getPortForwardingL()); 105 | logger.debug("Connected : {}", session.isConnected()); 106 | logger.debug("Private key : {}", useKey); 107 | } 108 | } catch (Exception e) { 109 | logger.error(e.toString(), e); 110 | } 111 | 112 | if (assignedPort == 0) { 113 | throw new RuntimeException("Port forwarding failed !"); 114 | } 115 | } 116 | 117 | public void stop() { 118 | if(session!=null) { 119 | session.disconnect(); 120 | 121 | if(logger.isDebugEnabled()) { 122 | logger.debug("Disconnected."); 123 | } 124 | } 125 | } 126 | 127 | public Integer getLocalPort() { 128 | return localPort.get(); 129 | } 130 | 131 | public boolean isPortOpen(String ip, int port) { 132 | try { 133 | Socket socket = new Socket(); 134 | socket.connect(new InetSocketAddress(ip, port), 1000); 135 | socket.close(); 136 | return false; 137 | } catch (Exception ex) { 138 | return true; 139 | } 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /src/main/java/overview.ad: -------------------------------------------------------------------------------- 1 | = Overview 2 | :linkattrs: 3 | 4 | JDBC driver over SSH tunnel. 5 | 6 | == Build 7 | 8 | If you just want to compile the project without running the tests: 9 | 10 | [source] 11 | ---- 12 | mvn -DskipTests clean install 13 | ---- 14 | 15 | If you want to run the tests (Derby and H2 in server mode): 16 | 17 | [source] 18 | ---- 19 | mvn -Djdbc.ssh.username="xxx" -Djdbc.ssh.password="xxx" clean install 20 | ---- 21 | 22 | NOTE: If your SSH server is not running on the default port 22 and/or localhost then you can change those paramaters. 23 | 24 | [source] 25 | ---- 26 | mvn -Djdbc.ssh.username="xxx" -Djdbc.ssh.password="xxx" -Djdbc.ssh.host="192.168.0.1" -Djdbc.ssh.port="2222" clean install 27 | ---- 28 | 29 | At the moment a locally running SSH server is needed for the tests. The embedded SSH server in the unit tests is not yet 30 | ready (authentication works, but port forwarding fails at the moment). 31 | 32 | == Maven dependencies 33 | 34 | You can find the latest releases here: 35 | 36 | https://bintray.com/cheetah/monkeysintown/jdbc-ssh/_latestVersion[ image:https://api.bintray.com/packages/cheetah/monkeysintown/jdbc-ssh/images/download.svg[Download], window="_blank" ] 37 | 38 | … or setup your Maven dependencies: 39 | 40 | [source,xml,subs="specialcharacters,attributes,callouts"] 41 | ---- 42 | 43 | com.github.monkeysintown 44 | jdbc-ssh 45 | {version} 46 | 47 | ---- 48 | 49 | … and configure Bintray's JCenter repository in your pom.xml: 50 | 51 | [source,xml] 52 | ---- 53 | ... 54 | 55 | 56 | 57 | false 58 | 59 | central 60 | bintray 61 | http://jcenter.bintray.com 62 | 63 | 64 | ... 65 | ---- 66 | 67 | Get automatic notifications about new releases here: 68 | 69 | https://bintray.com/cheetah/monkeysintown/jdbc-ssh/view?source=watch[ image:https://www.bintray.com/docs/images/bintray_badge_color.png[Get automatic notifications about new "jdbc-ssh" versions], window="_blank" ] -------------------------------------------------------------------------------- /src/main/resources/META-INF/services/java.sql.Driver: -------------------------------------------------------------------------------- 1 | com.m11n.jdbc.ssh.SshDriver -------------------------------------------------------------------------------- /src/main/resources/PROPERTIES: -------------------------------------------------------------------------------- 1 | jdbc.ssh.config 2 | jdbc.ssh.host 3 | jdbc.ssh.port 4 | jdbc.ssh.username 5 | jdbc.ssh.password 6 | jdbc.ssh.key.private 7 | jdbc.ssh.key.public 8 | jdbc.ssh.passphrase 9 | jdbc.ssh.known.hosts 10 | jdbc.ssh.host.remote 11 | jdbc.ssh.port.remote 12 | jdbc.ssh.port.auto 13 | Compression 14 | ConnectionAttempts 15 | StrictHostKeyChecking 16 | Host 17 | Match 18 | AddressFamily 19 | BatchMode 20 | BindAddress 21 | CanonicalDomains 22 | CanonicalizeFallbackLocal 23 | CanonicalizeHostname 24 | CanonicalizeMaxDots 25 | CanonicalizePermittedCNAMEs 26 | ChallengeResponseAuthentication 27 | CheckHostIP 28 | Cipher 29 | Ciphers 30 | ClearAllForwardings 31 | CompressionLevel 32 | ConnectTimeout 33 | ControlMaster 34 | ControlPath 35 | ControlPersist 36 | DynamicForward 37 | EnableSSHKeysign 38 | EscapeChar 39 | ExitOnForwardFailure 40 | FingerprintHash 41 | ForwardAgent 42 | ForwardX11 43 | ForwardX11Timeout 44 | ForwardX11Trusted 45 | GatewayPorts 46 | GlobalKnownHostsFile 47 | GSSAPIAuthentication 48 | GSSAPIDelegateCredentials 49 | HashKnownHosts 50 | HostbasedAuthentication 51 | HostKeyAlgorithms 52 | HostKeyAlias 53 | HostName 54 | IdentitiesOnly 55 | IdentityFile 56 | IgnoreUnknown 57 | IPQoS 58 | KbdInteractiveAuthentication 59 | KbdInteractiveDevices 60 | KexAlgorithms 61 | LocalCommand 62 | LocalForward 63 | LogLevel 64 | MACs 65 | NoHostAuthenticationForLocalhost 66 | NumberOfPasswordPrompts 67 | PasswordAuthentication 68 | PermitLocalCommand 69 | PKCS11Provider 70 | Port 71 | PreferredAuthentications 72 | Protocol 73 | ProxyCommand 74 | ProxyUseFdpass 75 | PubkeyAuthentication 76 | RekeyLimit 77 | RemoteForward 78 | RequestTTY 79 | RevokedHostKeys 80 | RhostsRSAAuthentication 81 | RSAAuthentication 82 | SendEnv 83 | ServerAliveCountMax 84 | ServerAliveInterval 85 | StreamLocalBindMask 86 | StreamLocalBindUnlink 87 | StrictHostKeyChecking 88 | TCPKeepAlive 89 | Tunnel 90 | TunnelDevice 91 | UsePrivilegedPort 92 | User 93 | UserKnownHostsFile 94 | VerifyHostKeyDNS 95 | VisualHostKey 96 | XAuthLocation 97 | -------------------------------------------------------------------------------- /src/site/asciidoc/configuration.adoc: -------------------------------------------------------------------------------- 1 | = Configuration 2 | 3 | TBD -------------------------------------------------------------------------------- /src/site/asciidoc/contribute.adoc: -------------------------------------------------------------------------------- 1 | = Contribute (Asciidoc) 2 | 3 | JDBC over SSH… 4 | 5 | == Issues (bug and feature tracker) 6 | 7 | Please report any bugs found, feature requests or other issues on 8 | https://github.com/monkeysintown/jdbc-ssh/issues/[JDBC SSH GitHub tracker]. 9 | 10 | When creating a new issue, try following http://github.com/necolas/issue-guidelines/#readme[necolas's guidelines]. 11 | 12 | == Fork, patch and contribute code 13 | 14 | Feel free to fork Reflow skin's https://github.com/monkeysintown/jdbc-ssh[Git repository at GitHub] for your own use and 15 | updates. 16 | 17 | Contribute your fixes and new features back to the main codebase using 18 | http://help.github.com/articles/using-pull-requests[GitHub pull requests]. 19 | 20 | == Support 21 | 22 | Let me know if you are using JDBC over SSH in your project. 23 | 24 | https://github.com/vidakovic[Contact me] if you have questions about the usage, or just like to say something about it. 25 | 26 | == Acknowledgements 27 | 28 | TBD 29 | 30 | == Copyright and license 31 | 32 | Copyright 2015 Monkeys in Town Lda. 33 | 34 | Licensed under the Apache License, Version 2.0 (the "License"); 35 | you may not use this work except in compliance with the License. 36 | You may obtain a copy of the License in the LICENSE file, or at: 37 | 38 | http://www.apache.org/licenses/LICENSE-2.0[http://www.apache.org/licenses/LICENSE-2.0] 39 | 40 | Unless required by applicable law or agreed to in writing, software 41 | distributed under the License is distributed on an "AS IS" BASIS, 42 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 43 | See the License for the specific language governing permissions and 44 | limitations under the License. -------------------------------------------------------------------------------- /src/site/asciidoc/index.adoc: -------------------------------------------------------------------------------- 1 | == Overview 2 | :linkattrs: 3 | :version: 1.0.8 4 | 5 | JDBC driver over SSH tunnel. ![Maven Central](https://img.shields.io/maven-central/v/com.github.monkeysintown/jdbc-ssh.svg) 6 | 7 | == Build 8 | 9 | If you just want to compile the project without running the tests: 10 | 11 | [source] 12 | ---- 13 | mvn -DskipTests clean install 14 | ---- 15 | 16 | If you want to run the tests (Derby and H2 in server mode): 17 | 18 | [source] 19 | ---- 20 | mvn -Djdbc.ssh.username="xxx" -Djdbc.ssh.password="xxx" clean install 21 | ---- 22 | 23 | NOTE: If your SSH server is not running on the default port 22 and/or localhost then you can change those paramaters. 24 | 25 | [source] 26 | ---- 27 | mvn -Djdbc.ssh.username="xxx" -Djdbc.ssh.password="xxx" -Djdbc.ssh.host="192.168.0.1" -Djdbc.ssh.port="2222" clean install 28 | ---- 29 | 30 | At the moment a locally running SSH server is needed for the tests. The embedded SSH server in the unit tests is not yet 31 | ready (authentication works, but port forwarding fails at the moment). 32 | 33 | == Get it now 34 | 35 | You can find the latest releases here: 36 | 37 | https://bintray.com/cheetah/monkeysintown/jdbc-ssh/_latestVersion[ image:https://api.bintray.com/packages/cheetah/monkeysintown/jdbc-ssh/images/download.svg[Download], window="_blank" ] 38 | 39 | … or setup your Maven dependencies: 40 | 41 | [source,xml,subs="specialcharacters,attributes,callouts"] 42 | ---- 43 | 44 | com.github.monkeysintown 45 | jdbc-ssh 46 | {version} 47 | 48 | ---- 49 | 50 | … and configure Bintray's JCenter repository in your pom.xml: 51 | 52 | [source,xml] 53 | ---- 54 | ... 55 | 56 | 57 | 58 | false 59 | 60 | central 61 | bintray 62 | http://jcenter.bintray.com 63 | 64 | 65 | ... 66 | ---- 67 | 68 | Get automatic notifications about new releases here: 69 | 70 | https://bintray.com/cheetah/monkeysintown/jdbc-ssh/view?source=watch[ image:https://www.bintray.com/docs/images/bintray_badge_color.png[Get automatic notifications about new jdbc-ssh versions] ] 71 | 72 | == Configure 73 | 74 | TBD 75 | 76 | === About 77 | 78 | The code is https://github.com/monkeysintown/jdbc-ssh[open source] and licensed under http://www.apache.org/licenses/LICENSE-2.0[Apache license]. 79 | 80 | link:contribute.html[Contribute] by reporting issues, suggesting new features, or forking the 81 | Git repository on GitHub. Pull requests welcome! -------------------------------------------------------------------------------- /src/site/resources/css/site.css: -------------------------------------------------------------------------------- 1 | .color-highlight { 2 | color: #225E9B; 3 | } 4 | body { 5 | background-image:url(../images/bg.png); 6 | } 7 | body[class*="page-themes-bootswatch"] { 8 | background-image: none; 9 | } 10 | body[class*="page-themes-bootswatch"] .color-highlight { 11 | color: inherit; 12 | } 13 | .thumbnail-row { 14 | margin-top: 10px; 15 | } 16 | /*h1, */ 17 | h2 { 18 | margin-top: 30px; 19 | } 20 | h3, h4, h5, h6 { 21 | margin-top: 20px; 22 | } 23 | .carousel-caption a { 24 | color: white; 25 | } 26 | /* A bit of workaround for links being not clickable due to :before element in headings */ 27 | .project-reflow-parent.page-index h2[id]:before { 28 | height: 30px; 29 | margin-top: -30px; 30 | } 31 | .project-reflow-parent.page-index .sidebar { 32 | margin-top: 10px; 33 | } 34 | .project-reflow-parent.page-index .sidebar { 35 | margin-top: 10px; 36 | } 37 | .project-reflow-parent.page-index .sidebar h3 { 38 | margin-top: 10px; 39 | } 40 | @media (min-width: 980px) { 41 | .page-themes-bootswatch-readable .navbar.affix { 42 | top: 60px; 43 | } 44 | .page-themes-bootswatch-cerulean .navbar.affix { 45 | top: 50px; 46 | } 47 | .page-themes-bootswatch-spruce .navbar.affix { 48 | top: 55px; 49 | } 50 | } -------------------------------------------------------------------------------- /src/site/resources/images/bg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/monkeysintown/jdbc-ssh/3833d284b85155780e2b8993bfe5cc88ea9df07d/src/site/resources/images/bg.png -------------------------------------------------------------------------------- /src/site/site.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | JDBC over SSH 10 | ]]> 11 | 12 | https://github.com/monkeysintown/jdbc-ssh 13 | 14 | 15 | 16 | 17 | UA- 18 | 19 | 20 | lt.velykis.maven.skins 21 | reflow-maven-skin 22 | 1.1.1 23 | 24 | 25 | 26 | false 27 | true 28 | default 29 | true 30 | github 31 | https://github.com/monkeysintown/jdbc-ssh/ 32 | 33 | 34 | JDBC over SSH 36 | ]]> 37 | 38 | https://github.com/monkeysintown/jdbc-ssh 39 | 40 | JDBC over SSH 41 | %2$s | %1$s 42 | top 43 | 6 44 | Documentation|Tools|Contribute 45 | 46 | Main|Tools 47 | Download|Contribute 48 | Documentation 49 | reports 50 | 51 | 52 | JDBC over SSH is a wrapper for all JDBC compliant drivers that use any form of TCP communication. 54 | ]]> 55 | 56 | 57 | 58 | 59 | JDBC over SSH 60 | 61 | false 62 | 63 | false 64 | false 65 | 66 | 69 | 2 70 | 71 | 72 | 73 | 74 | 75 | false 76 | 77 | 78 | 79 | false 80 | 81 | 82 | false 83 | false 84 | 85 | 86 | false 87 | false 88 | 89 | 90 | 91 | false 92 | 93 | 94 | false 95 | 96 | 97 | 4 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | -------------------------------------------------------------------------------- /src/test/java/com/m11n/jdbc/ssh/DerbySshDriverTest.java: -------------------------------------------------------------------------------- 1 | package com.m11n.jdbc.ssh; 2 | 3 | import com.m11n.jdbc.ssh.util.Slf4jDerbyBridge; 4 | import com.m11n.jdbc.ssh.util.Slf4jOutputStream; 5 | import org.apache.derby.drda.NetworkServerControl; 6 | import org.junit.AfterClass; 7 | import org.junit.Before; 8 | import org.junit.BeforeClass; 9 | import org.slf4j.Logger; 10 | import org.slf4j.LoggerFactory; 11 | 12 | import java.io.PrintWriter; 13 | import java.net.InetAddress; 14 | 15 | public class DerbySshDriverTest extends JdbcSshDriverTest { 16 | private static final Logger logger = LoggerFactory.getLogger(DerbySshDriverTest.class); 17 | 18 | private static NetworkServerControl dbServerDerby; 19 | 20 | @BeforeClass 21 | public static void init() throws Exception { 22 | setUpDerby(); 23 | setUpSshd(); 24 | } 25 | 26 | @AfterClass 27 | public static void cleanup() throws Exception { 28 | dbServerDerby.shutdown(); 29 | shutDownSshd(); 30 | } 31 | 32 | private static void setUpDerby() throws Exception { 33 | System.setProperty("derby.drda.startNetworkServer", "true"); 34 | System.setProperty("jdbc.ssh.port.auto", "30000"); 35 | 36 | Slf4jDerbyBridge.setLogger(logger); 37 | System.setProperty("derby.stream.error.method", Slf4jDerbyBridge.class.getName() + ".bridge"); 38 | 39 | if(logger.isTraceEnabled()) { 40 | // see here for more options: http://wiki.apache.org/db-derby/DebugPropertiesTmpl 41 | System.setProperty("derby.drda.logConnections", "true"); 42 | System.setProperty("derby.language.logStatementText", "true"); 43 | System.setProperty("derby.language.logQueryPlan", "true"); 44 | System.setProperty("derby.locks.deadlockTrace", "true"); 45 | } 46 | 47 | dbServerDerby = new NetworkServerControl(InetAddress.getByName("localhost"), 1527); 48 | dbServerDerby.start(new PrintWriter(new Slf4jOutputStream(logger), true)); 49 | 50 | for (int i = 0; i < 10; ++i) { 51 | try { 52 | logger.info("Attempting to ping..."); 53 | dbServerDerby.ping(); 54 | break; 55 | } catch (Exception e) { 56 | logger.warn(e.getMessage()); 57 | } 58 | Thread.sleep(10); 59 | } 60 | } 61 | 62 | @Before 63 | public void setUp() throws Exception { 64 | sshUrl = System.getProperty("url")!=null ? System.getProperty("url") : "jdbc:ssh:derby://127.0.0.1:1527/target/test;create=true"; 65 | realUrl = System.getProperty("realUrl")!=null ? System.getProperty("realUrl") : "jdbc:derby://127.0.0.1:1527/target/test;create=true"; 66 | 67 | logger.info("JDBC URL (SSH) : {}", sshUrl); 68 | logger.info("JDBC URL (real): {}", realUrl); 69 | 70 | sql = "CREATE TABLE TEST_SSH(ID INT PRIMARY KEY, NAME VARCHAR(255))"; 71 | 72 | logger.info("JDBC Runtime Info:\n{}", dbServerDerby.getRuntimeInfo()); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/test/java/com/m11n/jdbc/ssh/H2SshDriverTest.java: -------------------------------------------------------------------------------- 1 | package com.m11n.jdbc.ssh; 2 | 3 | import org.h2.tools.Server; 4 | import org.junit.AfterClass; 5 | import org.junit.Before; 6 | import org.junit.BeforeClass; 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | 10 | public class H2SshDriverTest extends JdbcSshDriverTest { 11 | private static final Logger logger = LoggerFactory.getLogger(H2SshDriverTest.class); 12 | 13 | private static Server dbServerH2; 14 | 15 | @BeforeClass 16 | public static void init() throws Exception { 17 | setUpH2(); 18 | setUpSshd(); 19 | } 20 | 21 | @AfterClass 22 | public static void cleanup() throws Exception { 23 | dbServerH2.shutdown(); 24 | shutDownSshd(); 25 | } 26 | 27 | @Before 28 | public void setUp() throws Exception { 29 | sshUrl = System.getProperty("url")!=null ? System.getProperty("url") : "jdbc:ssh:h2:" + dbServerH2.getURL() + "/test"; 30 | realUrl = System.getProperty("realUrl")!=null ? System.getProperty("realUrl") : "jdbc:h2:" + dbServerH2.getURL() + "/test"; 31 | 32 | sql = "CREATE TABLE IF NOT EXISTS TEST_SSH(ID INT PRIMARY KEY, NAME VARCHAR(255));"; 33 | 34 | logger.info("JDBC URL (SSH) : {}", sshUrl); 35 | logger.info("JDBC URL (real): {}", realUrl); 36 | } 37 | 38 | private static void setUpH2() throws Exception { 39 | System.setProperty("h2.baseDir", "./target/h2"); 40 | System.setProperty("jdbc.ssh.port.auto", "20000"); 41 | 42 | dbServerH2 = Server.createTcpServer("-tcpPort" , "8092" , "-tcpAllowOthers", "-tcpDaemon").start(); 43 | 44 | logger.info("Database server status: u = {} - s = {} ({})", dbServerH2.getURL(), dbServerH2.getStatus(), dbServerH2.isRunning(true)); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/test/java/com/m11n/jdbc/ssh/JdbcSshDriverTest.java: -------------------------------------------------------------------------------- 1 | package com.m11n.jdbc.ssh; 2 | 3 | import com.m11n.jdbc.ssh.util.BogusPasswordAuthenticator; 4 | import com.m11n.jdbc.ssh.util.TestCachingPublicKeyAuthenticator; 5 | import org.apache.sshd.SshServer; 6 | import org.apache.sshd.server.Command; 7 | import org.apache.sshd.server.CommandFactory; 8 | import org.apache.sshd.server.command.UnknownCommand; 9 | import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider; 10 | import org.junit.Test; 11 | import org.slf4j.Logger; 12 | import org.slf4j.LoggerFactory; 13 | 14 | import java.sql.*; 15 | import java.util.Enumeration; 16 | import java.util.Properties; 17 | 18 | import static com.m11n.jdbc.ssh.SshConfiguration.CONFIG_HOST; 19 | import static com.m11n.jdbc.ssh.SshConfiguration.CONFIG_PORT; 20 | 21 | import static org.junit.Assert.*; 22 | 23 | public abstract class JdbcSshDriverTest { 24 | private static final Logger logger = LoggerFactory.getLogger(JdbcSshDriverTest.class); 25 | 26 | protected String sshUrl; 27 | 28 | protected String realUrl; 29 | 30 | protected static SshServer sshd; 31 | 32 | protected String sql; 33 | 34 | protected static void setUpSshd() throws Exception { 35 | if(sshd==null && "true".equals(System.getProperty("sshd"))) { 36 | Properties p = new Properties(); 37 | p.load(SshDriver.class.getClassLoader().getResourceAsStream("ssh.properties")); 38 | 39 | sshd = SshServer.setUpDefaultServer(); 40 | sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("target/hostkey.rsa", "RSA")); 41 | sshd.setPasswordAuthenticator(new BogusPasswordAuthenticator()); 42 | sshd.setPublickeyAuthenticator(new TestCachingPublicKeyAuthenticator()); 43 | sshd.setCommandFactory(new CommandFactory() { 44 | public Command createCommand(String command) { 45 | return new UnknownCommand(command); 46 | } 47 | }); 48 | sshd.setHost(p.getProperty(CONFIG_HOST)); 49 | sshd.setPort(Integer.valueOf(p.getProperty(CONFIG_PORT))); 50 | sshd.start(); 51 | } 52 | } 53 | 54 | protected static void shutDownSshd() throws Exception { 55 | if(sshd!=null && !sshd.isClosed()) { 56 | sshd.stop(); 57 | } 58 | } 59 | 60 | @Test 61 | public void testDriverRegistration() throws SQLException { 62 | boolean found = false; 63 | 64 | for(Enumeration drivers = DriverManager.getDrivers(); drivers.hasMoreElements();) { 65 | Driver driver = drivers.nextElement(); 66 | 67 | if(driver.getClass().equals(SshDriver.class)) { 68 | found = true; 69 | break; 70 | } 71 | } 72 | 73 | assertTrue(found); 74 | } 75 | 76 | @Test 77 | public void testSshDriver() throws Exception { 78 | Connection connection = DriverManager.getConnection(sshUrl); 79 | 80 | logger.info("Info: {}", connection.getClientInfo()); 81 | 82 | DatabaseMetaData metadata = connection.getMetaData(); 83 | 84 | // Get all the tables and views 85 | String[] tableType = {"TABLE"}; 86 | java.sql.ResultSet tables = metadata.getTables(null, null, "%", tableType); 87 | 88 | assertNotNull(tables); 89 | 90 | String tableName; 91 | while (tables.next()) { 92 | tableName = tables.getString(3); 93 | 94 | logger.info("Table: {}", tableName); 95 | } 96 | } 97 | 98 | @Test 99 | public void testRealDriver() throws Exception { 100 | Connection connection = DriverManager.getConnection(realUrl); 101 | 102 | Statement s = connection.createStatement(); 103 | s.execute(sql); 104 | 105 | DatabaseMetaData metadata = connection.getMetaData(); 106 | 107 | // Get all the tables and views 108 | String[] tableType = {"TABLE"}; 109 | java.sql.ResultSet tables = metadata.getTables(null, null, "%", tableType); 110 | 111 | assertNotNull(tables); 112 | 113 | String tableName; 114 | while (tables.next()) { 115 | tableName = tables.getString(3); 116 | 117 | logger.info("Table: {}", tableName); 118 | } 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /src/test/java/com/m11n/jdbc/ssh/util/BogusPasswordAuthenticator.java: -------------------------------------------------------------------------------- 1 | package com.m11n.jdbc.ssh.util; 2 | 3 | import org.apache.sshd.server.PasswordAuthenticator; 4 | import org.apache.sshd.server.session.ServerSession; 5 | 6 | public class BogusPasswordAuthenticator implements PasswordAuthenticator { 7 | public boolean authenticate(String username, String password, ServerSession session) { 8 | return true; 9 | } 10 | } -------------------------------------------------------------------------------- /src/test/java/com/m11n/jdbc/ssh/util/Slf4jDerbyBridge.java: -------------------------------------------------------------------------------- 1 | package com.m11n.jdbc.ssh.util; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | 6 | import java.io.Writer; 7 | 8 | /** 9 | * Funnels Derby log outputs into an SLF4J logger. This current implementation logs 10 | * all errors at the `ERROR` threshold. 11 | */ 12 | public class Slf4jDerbyBridge { 13 | private static Logger logger = LoggerFactory.getLogger(Slf4jDerbyBridge.class); 14 | 15 | private Slf4jDerbyBridge() 16 | { 17 | } 18 | 19 | /** 20 | * A basic adapter that funnels Derby's logs through an SLF4J logger. 21 | */ 22 | public static final class LoggingWriter extends Writer 23 | { 24 | @Override 25 | public void write(final char[] buf, final int off, final int len) 26 | { 27 | // skip empty lines. 28 | if (len > 1) 29 | { 30 | String message = new String(buf, off, len); 31 | 32 | logger.trace(message); 33 | } 34 | } 35 | 36 | @Override 37 | public void flush() 38 | { 39 | // noop. 40 | } 41 | 42 | @Override 43 | public void close() 44 | { 45 | // noop. 46 | } 47 | } 48 | 49 | public static void setLogger(Logger log) { 50 | logger = log; 51 | } 52 | 53 | public static Writer bridge() 54 | { 55 | return new LoggingWriter(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/test/java/com/m11n/jdbc/ssh/util/Slf4jOutputStream.java: -------------------------------------------------------------------------------- 1 | package com.m11n.jdbc.ssh.util; 2 | 3 | import org.slf4j.Logger; 4 | 5 | import java.io.IOException; 6 | import java.io.OutputStream; 7 | 8 | /** 9 | * == An OutputStream that flushes out to a Category. 10 | * 11 | * NOTE: no data is written out to the Category until the stream is flushed or closed. 12 | * 13 | * Example: 14 | * [source,java] 15 | * -- 16 | * // make sure everything sent to System.err is logged 17 | * System.setErr(new PrintStream(new LoggingOutputStream(Logger.getRootCategory()), true)); 18 | * 19 | * // make sure everything sent to System.out is also logged 20 | * System.setOut(new PrintStream(new LoggingOutputStream(Logger.getRootCategory()), true)); 21 | * -- 22 | * 23 | * @author https://github.com/vidakovic[Aleksandar Vidakovic] 24 | */ 25 | public class Slf4jOutputStream extends OutputStream { 26 | /** 27 | * Used to maintain the contract of #close()}. 28 | */ 29 | private boolean hasBeenClosed = false; 30 | 31 | /** 32 | * The internal buffer where data is stored. 33 | */ 34 | private byte[] buf; 35 | 36 | /** 37 | * The number of valid bytes in the buffer. This value is always 38 | * in the range 0 through buf.length; elements 39 | * buf[0] through buf[count-1] contain valid 40 | * byte data. 41 | */ 42 | private int count; 43 | 44 | /** 45 | * Remembers the size of the buffer for speed. 46 | */ 47 | private int bufLength; 48 | 49 | /** 50 | * The default number of bytes in the buffer. =2048 51 | */ 52 | public static final int DEFAULT_BUFFER_LENGTH = 2048; 53 | 54 | 55 | /** 56 | * The category to write to. 57 | */ 58 | private Logger logger; 59 | 60 | /** 61 | * Creates the LoggingOutputStream to flush to the given Category. 62 | * 63 | * @param log the Logger to write to 64 | * @throws IllegalArgumentException if cat == null or priority == null 65 | */ 66 | public Slf4jOutputStream(Logger log) throws IllegalArgumentException { 67 | if (log == null) { 68 | throw new IllegalArgumentException("cat == null"); 69 | } 70 | 71 | this.logger = log; 72 | this.bufLength = DEFAULT_BUFFER_LENGTH; 73 | this.buf = new byte[DEFAULT_BUFFER_LENGTH]; 74 | this.count = 0; 75 | } 76 | 77 | 78 | /** 79 | * Closes this output stream and releases any system resources 80 | * associated with this stream. The general contract of 81 | * `close` 82 | * is that it closes the output stream. A closed stream cannot 83 | * perform 84 | * output operations and cannot be reopened. 85 | */ 86 | public void close() { 87 | flush(); 88 | hasBeenClosed = true; 89 | } 90 | 91 | 92 | /** 93 | * Writes the specified byte to this output stream. The general 94 | * contract for `write` is that one byte is written 95 | * to the output stream. The byte to be written is the eight 96 | * low-order bits of the argument `b`. The 24 97 | * high-order bits of `b` are ignored. 98 | * 99 | * @param b the `byte` to write 100 | * @throws java.io.IOException if an I/O error occurs. In particular, an `IOException` may be 101 | * thrown if the output stream has been closed. 102 | */ 103 | public void write(final int b) throws IOException { 104 | if (hasBeenClosed) { 105 | throw new IOException("The stream has been closed."); 106 | } 107 | 108 | // would this be writing past the buffer? 109 | 110 | if (count == bufLength) { 111 | // grow the buffer 112 | final int newBufLength = bufLength + DEFAULT_BUFFER_LENGTH; 113 | final byte[] newBuf = new byte[newBufLength]; 114 | 115 | System.arraycopy(buf, 0, newBuf, 0, bufLength); 116 | buf = newBuf; 117 | 118 | bufLength = newBufLength; 119 | } 120 | 121 | buf[count] = (byte) b; 122 | 123 | count++; 124 | } 125 | 126 | 127 | /** 128 | * Flushes this output stream and forces any buffered output bytes 129 | * to be written out. The general contract of `flush` is 130 | * that calling it is an indication that, if any bytes previously 131 | * written have been buffered by the implementation of the output 132 | * stream, such bytes should immediately be written to their 133 | * intended destination. 134 | */ 135 | public void flush() { 136 | 137 | if (count == 0) { 138 | return; 139 | } 140 | 141 | // don't print out blank lines; flushing from PrintStream puts 142 | 143 | // For linux system 144 | 145 | if (count == 1 && ((char) buf[0]) == '\n') { 146 | reset(); 147 | return; 148 | } 149 | 150 | // For mac system 151 | 152 | if (count == 1 && ((char) buf[0]) == '\r') { 153 | reset(); 154 | return; 155 | } 156 | 157 | // On windows system 158 | 159 | if (count == 2 && (char) buf[0] == '\r' && (char) buf[1] == '\n') { 160 | reset(); 161 | return; 162 | } 163 | 164 | final byte[] theBytes = new byte[count]; 165 | System.arraycopy(buf, 0, theBytes, 0, count); 166 | String message = new String(theBytes); 167 | 168 | if(logger.isTraceEnabled()) { 169 | logger.trace(message); 170 | } else if(logger.isDebugEnabled()) { 171 | logger.debug(message); 172 | } else if(logger.isInfoEnabled()) { 173 | logger.info(message); 174 | } else if(logger.isWarnEnabled()) { 175 | logger.warn(message); 176 | } else if(logger.isErrorEnabled()) { 177 | logger.error(message); 178 | } else { 179 | logger.info(message); 180 | } 181 | 182 | reset(); 183 | 184 | } 185 | 186 | private void reset() { 187 | // not resetting the buffer -- assuming that if it grew then it will likely grow similarly again 188 | count = 0; 189 | } 190 | } 191 | -------------------------------------------------------------------------------- /src/test/java/com/m11n/jdbc/ssh/util/TestCachingPublicKeyAuthenticator.java: -------------------------------------------------------------------------------- 1 | package com.m11n.jdbc.ssh.util; 2 | 3 | import org.apache.sshd.common.KeyPairProvider; 4 | import org.apache.sshd.server.PublickeyAuthenticator; 5 | import org.apache.sshd.server.auth.CachingPublicKeyAuthenticator; 6 | import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider; 7 | import org.apache.sshd.server.session.ServerSession; 8 | 9 | import java.security.KeyPair; 10 | import java.security.PublicKey; 11 | import java.util.Map; 12 | 13 | public class TestCachingPublicKeyAuthenticator extends CachingPublicKeyAuthenticator { 14 | private KeyPairProvider keyProvider = new SimpleGeneratorHostKeyProvider("target/hostkey.rsa", "RSA"); 15 | private KeyPair pairRsa = keyProvider.loadKey(KeyPairProvider.SSH_RSA); 16 | 17 | public TestCachingPublicKeyAuthenticator() { 18 | super(new PublickeyAuthenticator() { 19 | @Override 20 | public boolean authenticate(String s, PublicKey publicKey, ServerSession serverSession) { 21 | return true; 22 | } 23 | }); 24 | } 25 | public Map> getCache() { 26 | return cache; 27 | } 28 | public KeyPairProvider getKeyProvider() { 29 | return keyProvider; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/test/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | true 5 | 6 | %d{HH:mm:ss.SSS} [%thread] %highlight(%-5level) %boldYellow(%logger{36}) - %msg%n 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /src/test/resources/ssh.properties: -------------------------------------------------------------------------------- 1 | #jdbc.ssh.host=localhost 2 | #jdbc.ssh.port=22 3 | #jdbc.ssh.username= 4 | #jdbc.ssh.password= 5 | #jdbc.ssh.key.private=~/.ssh/id_rsa 6 | #jdbc.ssh.key.public=~/.ssh/id_rsa.pub 7 | #jdbc.ssh.passphrase= 8 | #jdbc.ssh.known.hosts=~/.ssh/known_hosts 9 | 10 | #jdbc.ssh.port.auto=20000 11 | 12 | # see here for all options: http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man5/ssh_config.5?query=ssh_config&sec=5 13 | 14 | Compression=yes 15 | #ConnectionAttempts=2 16 | #StrictHostKeyChecking=no 17 | 18 | #Host 19 | #Match 20 | #AddressFamily 21 | #BatchMode 22 | #BindAddress 23 | #CanonicalDomains 24 | #CanonicalizeFallbackLocal 25 | #CanonicalizeHostname 26 | #CanonicalizeMaxDots 27 | #CanonicalizePermittedCNAMEs 28 | #ChallengeResponseAuthentication 29 | #CheckHostIP 30 | #Cipher 31 | #Ciphers 32 | #ClearAllForwardings 33 | #CompressionLevel 34 | #ConnectTimeout 35 | #ControlMaster 36 | #ControlPath 37 | #ControlPersist 38 | #DynamicForward 39 | #EnableSSHKeysign 40 | #EscapeChar 41 | #ExitOnForwardFailure 42 | #FingerprintHash 43 | #ForwardAgent 44 | #ForwardX11 45 | #ForwardX11Timeout 46 | #ForwardX11Trusted 47 | #GatewayPorts 48 | #GlobalKnownHostsFile 49 | #GSSAPIAuthentication 50 | #GSSAPIDelegateCredentials 51 | #HashKnownHosts 52 | #HostbasedAuthentication 53 | #HostKeyAlgorithms 54 | #HostKeyAlias 55 | #HostName 56 | #IdentitiesOnly 57 | #IdentityFile 58 | #IgnoreUnknown 59 | #IPQoS 60 | #KbdInteractiveAuthentication 61 | #KbdInteractiveDevices 62 | #KexAlgorithms 63 | #LocalCommand 64 | #LocalForward 65 | #LogLevel 66 | #MACs 67 | #NoHostAuthenticationForLocalhost 68 | #NumberOfPasswordPrompts 69 | #PasswordAuthentication 70 | #PermitLocalCommand 71 | #PKCS11Provider 72 | #Port 73 | #PreferredAuthentications 74 | #Protocol 75 | #ProxyCommand 76 | #ProxyUseFdpass 77 | #PubkeyAuthentication 78 | #RekeyLimit 79 | #RemoteForward 80 | #RequestTTY 81 | #RevokedHostKeys 82 | #RhostsRSAAuthentication 83 | #RSAAuthentication 84 | #SendEnv 85 | #ServerAliveCountMax 86 | #ServerAliveInterval 87 | #StreamLocalBindMask 88 | #StreamLocalBindUnlink 89 | #StrictHostKeyChecking 90 | #TCPKeepAlive 91 | #Tunnel 92 | #TunnelDevice 93 | #UsePrivilegedPort 94 | #User 95 | #UserKnownHostsFile 96 | #VerifyHostKeyDNS 97 | #VisualHostKey 98 | #XAuthLocation 99 | --------------------------------------------------------------------------------