-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[collector] bugfix sshd cannot use private key to connect (#1084)
Co-authored-by: Musk.Chen <[email protected]> Co-authored-by: tomsun28 <[email protected]>
- Loading branch information
1 parent
c982a17
commit b66febc
Showing
16 changed files
with
132 additions
and
151 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
67 changes: 0 additions & 67 deletions
67
collector/src/main/java/org/dromara/hertzbeat/collector/util/KeyPairUtil.java
This file was deleted.
Oops, something went wrong.
48 changes: 48 additions & 0 deletions
48
collector/src/main/java/org/dromara/hertzbeat/collector/util/PrivateKeyUtils.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,48 @@ | ||
package org.dromara.hertzbeat.collector.util; | ||
|
||
import lombok.experimental.UtilityClass; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.Objects; | ||
|
||
/** | ||
* 将私钥写入~/.ssh | ||
* | ||
* @author <a href="mailto:[email protected]">gcdd1993</a> | ||
* Created by gcdd1993 on 2023/7/9 | ||
*/ | ||
@Slf4j | ||
@UtilityClass | ||
public class PrivateKeyUtils { | ||
|
||
private static final String KEY_PATH = System.getProperty("user.home") + "/.ssh"; | ||
|
||
/** | ||
* write private key to ~/.ssh, filename is ~/.ssh/id_rsa_${host} | ||
* | ||
* @param host host | ||
* @param keyStr key string | ||
* @return key file path | ||
* @throws IOException if ~/.ssh is not exist and create dir error | ||
*/ | ||
public static String writePrivateKey(String host, String keyStr) throws IOException { | ||
var sshPath = Paths.get(KEY_PATH); | ||
if (!Files.exists(sshPath)) { | ||
Files.createDirectories(sshPath); | ||
} | ||
var keyPath = Paths.get(KEY_PATH, "id_rsa_" + host); | ||
if (!Files.exists(keyPath)) { | ||
Files.writeString(keyPath, keyStr); | ||
} else { | ||
var oldKey = Files.readString(keyPath); | ||
if (!Objects.equals(oldKey, keyStr)) { | ||
Files.writeString(keyPath, keyStr); | ||
} | ||
} | ||
return keyPath.toString(); | ||
} | ||
|
||
} |
71 changes: 0 additions & 71 deletions
71
collector/src/test/java/org/dromara/hertzbeat/collector/util/KeyPairUtilTest.java
This file was deleted.
Oops, something went wrong.
61 changes: 61 additions & 0 deletions
61
collector/src/test/java/org/dromara/hertzbeat/collector/util/PrivateKeyUtilsTest.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,61 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.dromara.hertzbeat.collector.util; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">gcdd1993</a> | ||
* Created by gcdd1993 on 2023/7/9 | ||
*/ | ||
class PrivateKeyUtilsTest { | ||
|
||
@DisplayName("write key to ~/.ssh") | ||
@Test | ||
void writePrivateKey() throws IOException { | ||
var key = "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+eUcQEm+NZppnDBJavdpP48ZZM/t5v/2fQ5ytbYqk0KEzIGu0dP8g\n" + | ||
"jfB76JbMvStvTfB+TrXsfhGyA3oJrEcG+3IUshsRU2sohT1ScY27z2VMLgilnWvF\n" + | ||
"7t49sQm9uB/yn669n8LIciHxDItOpvqgKdG3AoGBAO2NxA6PtZ+4jAIz/19bsbc7\n" + | ||
"zDIqaovrKe8tMMglXg/ZE0e0aLvdvqRkRAKU1Z51Ob5lLuDwEYoyWZCgk1gL90Vp\n" + | ||
"wpT+P3zlcyCBo39IWMDB8C8IydRbF/GbaaNtoKds92m+qWwwUd87XCf+3M0wvvI6\n" + | ||
"75TW1PLEbyOgFz8Khh8hAoGBAJbDc87Ul9sCAtp2Ip2hvWk2coPR8vfADz9C8cn5\n" + | ||
"/BShBOcVfipSt2b1n8GCP/TnFU4XgBVeiSkA9+4Rg6AzMzejdY1+JvWvfqCnRVM/\n" + | ||
"GkOnMzZb17tyZi+ck8OKC/IcHkAyUYFWL0GWQSOojvBsPQxt+0V8aEIwsHjNSSha\n" + | ||
"nyNpAoGAd0XqdByRxbWgg5ZsvM0tvrpMITpEZsGMG9VeQPGl0wsQvC2zw5QGLvz/\n" + | ||
"57YhofOOr0M3yElcFA9Imvek5CYZsyL8eIWGZyadfRiYvGOUyvDDO3BYRG4DmhyF\n" + | ||
"KVk3URjEuOCC29ORvZ/7HaCO9iuEbvAA/mrAtd7KdCA+3PzfEOw=\n" + | ||
"-----END RSA PRIVATE KEY-----"; | ||
PrivateKeyUtils.writePrivateKey("127.0.0.1", key); | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.