forked from Consensys/teku
-
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.
Create the key at
—p2p-private-key-file
path if none (Consensys#7875)
- Loading branch information
1 parent
b71a58e
commit 821da85
Showing
9 changed files
with
191 additions
and
37 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
84 changes: 84 additions & 0 deletions
84
...p/src/main/java/tech/pegasys/teku/networking/p2p/network/config/FilePrivateKeySource.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,84 @@ | ||
/* | ||
* Copyright Consensys Software Inc., 2024 | ||
* | ||
* Licensed 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 tech.pegasys.teku.networking.p2p.network.config; | ||
|
||
import static tech.pegasys.teku.infrastructure.logging.StatusLogger.STATUS_LOG; | ||
|
||
import io.libp2p.core.crypto.KeyKt; | ||
import io.libp2p.core.crypto.KeyType; | ||
import io.libp2p.core.crypto.PrivKey; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.Objects; | ||
import org.apache.tuweni.bytes.Bytes; | ||
|
||
public class FilePrivateKeySource implements PrivateKeySource { | ||
private final String fileName; | ||
|
||
public FilePrivateKeySource(String fileName) { | ||
this.fileName = fileName; | ||
} | ||
|
||
@Override | ||
public Bytes getOrGeneratePrivateKeyBytes() { | ||
try { | ||
File file = new File(fileName); | ||
if (!file.createNewFile()) { | ||
return getPrivateKeyBytesFromFile(); | ||
} | ||
final PrivKey privKey = KeyKt.generateKeyPair(KeyType.SECP256K1).component1(); | ||
final Bytes privateKeyBytes = Bytes.wrap(KeyKt.marshalPrivateKey(privKey)); | ||
Files.writeString(file.toPath(), privateKeyBytes.toHexString()); | ||
STATUS_LOG.usingGeneratedP2pPrivateKey(fileName, true); | ||
return privateKeyBytes; | ||
|
||
} catch (IOException e) { | ||
throw new IllegalArgumentException( | ||
"Not able to create or retrieve p2p private key file - " + fileName); | ||
} | ||
} | ||
|
||
private Bytes getPrivateKeyBytesFromFile() { | ||
try { | ||
final Bytes privateKeyBytes = Bytes.fromHexString(Files.readString(Paths.get(fileName))); | ||
STATUS_LOG.usingGeneratedP2pPrivateKey(fileName, false); | ||
return privateKeyBytes; | ||
} catch (IOException e) { | ||
throw new RuntimeException("p2p private key file not found - " + fileName); | ||
} | ||
} | ||
|
||
public String getFileName() { | ||
return fileName; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
FilePrivateKeySource that = (FilePrivateKeySource) o; | ||
return Objects.equals(fileName, that.fileName); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(fileName); | ||
} | ||
} |
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
20 changes: 20 additions & 0 deletions
20
...g/p2p/src/main/java/tech/pegasys/teku/networking/p2p/network/config/PrivateKeySource.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,20 @@ | ||
/* | ||
* Copyright Consensys Software Inc., 2024 | ||
* | ||
* Licensed 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 tech.pegasys.teku.networking.p2p.network.config; | ||
|
||
import org.apache.tuweni.bytes.Bytes; | ||
|
||
public interface PrivateKeySource { | ||
Bytes getOrGeneratePrivateKeyBytes(); | ||
} |
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
65 changes: 65 additions & 0 deletions
65
...c/test/java/tech/pegasys/teku/networking/p2p/network/config/FilePrivateKeySourceTest.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 @@ | ||
/* | ||
* Copyright Consensys Software Inc., 2024 | ||
* | ||
* Licensed 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 tech.pegasys.teku.networking.p2p.network.config; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import org.apache.tuweni.bytes.Bytes; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
import tech.pegasys.teku.network.p2p.jvmlibp2p.PrivateKeyGenerator; | ||
|
||
class FilePrivateKeySourceTest { | ||
|
||
@Test | ||
void shouldCreateKeyAndSaveToFile(@TempDir Path tempDir) throws IOException { | ||
final Path file = tempDir.resolve("file.txt"); | ||
final PrivateKeySource privateKeySource = new FilePrivateKeySource(file.toString()); | ||
final Bytes generatedBytes = privateKeySource.getOrGeneratePrivateKeyBytes(); | ||
final Bytes savedBytes = Bytes.fromHexString(Files.readString(file)); | ||
|
||
assertThat(generatedBytes).isEqualTo(savedBytes); | ||
} | ||
|
||
@Test | ||
void shouldGetKeyFromSavedFile(@TempDir Path tempDir) throws IOException { | ||
final Path file = tempDir.resolve("file.txt"); | ||
final Bytes privateKey = Bytes.wrap(PrivateKeyGenerator.generate().bytes()); | ||
Files.writeString(file, privateKey.toHexString()); | ||
final PrivateKeySource privateKeySource = new FilePrivateKeySource(file.toString()); | ||
|
||
assertThat(privateKeySource.getOrGeneratePrivateKeyBytes()).isEqualTo(privateKey); | ||
} | ||
|
||
@Test | ||
void shouldThrowExceptionIfInvalidFileName(@TempDir Path tempDir) { | ||
final PrivateKeySource privateKeySource = | ||
new FilePrivateKeySource(tempDir + "/invalid file name!!\0"); | ||
assertThatThrownBy(privateKeySource::getOrGeneratePrivateKeyBytes) | ||
.isInstanceOf(IllegalArgumentException.class) | ||
.hasMessageContaining("Not able to create or retrieve p2p private key file -"); | ||
} | ||
|
||
@Test | ||
void shouldThrowExceptionIfProvideDirectory(@TempDir Path tempDir) { | ||
final PrivateKeySource privateKeySource = new FilePrivateKeySource(tempDir.toString()); | ||
assertThatThrownBy(privateKeySource::getOrGeneratePrivateKeyBytes) | ||
.isInstanceOf(RuntimeException.class) | ||
.hasMessageContaining("p2p private key file not found -"); | ||
} | ||
} |
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