Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a PCLI sub command to sign services stream files #6309

Merged
merged 25 commits into from May 5, 2023
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
4d85663
Add a PCLI sub command to sign services stream files
iwsimon Apr 28, 2023
d02ded8
fixed typo
iwsimon Apr 28, 2023
bcc815d
added unit test to record stream pcli command and account balance com…
povolev15 Apr 28, 2023
47d696a
spotless
iwsimon Apr 28, 2023
dcb4236
added record stream gz support and updated unit tests.
iwsimon May 1, 2023
9a1df9c
Added missing resources
iwsimon May 1, 2023
fdb55fe
added unit tests, updated with comments.
iwsimon May 1, 2023
753b2d9
added unit tests
iwsimon May 1, 2023
c055ee3
updated unit tests
iwsimon May 1, 2023
0e63c71
updated unit tests
iwsimon May 1, 2023
3da20ad
added unit tests
iwsimon May 2, 2023
24483f5
Merge branch 'develop' into 5710-record-stream-sign
iwsimon May 2, 2023
270645a
added unit tests, fixed sonar bugs.
iwsimon May 2, 2023
455b696
added unit tests
iwsimon May 2, 2023
6d32212
updated unit test and added additional check
iwsimon May 2, 2023
17d3d5e
added more unit tests, updated with reviews.
iwsimon May 4, 2023
c2c1514
added more unit tests, updated with reviews.
iwsimon May 5, 2023
1b18317
removed debug line and spotless
iwsimon May 5, 2023
440a10e
updated unit tests and key files
iwsimon May 5, 2023
ed8a5fd
updated test name
iwsimon May 5, 2023
95a98cf
removed commented line.
iwsimon May 5, 2023
cf42746
keep bytes output for debug if something went wrong.
iwsimon May 5, 2023
cb6b37d
updated with reviews.
iwsimon May 5, 2023
3c9f3bc
spotless
iwsimon May 5, 2023
cb5b0c9
Merge branch 'develop' into 5710-record-stream-sign
iwsimon May 5, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion hedera-node/cli-clients/build.gradle.kts
Expand Up @@ -17,7 +17,6 @@
plugins {
id("com.hedera.hashgraph.conventions")
id("com.hedera.hashgraph.shadow-jar")
id("org.gradle.java-test-fixtures")
}

description = "Hedera Services Command-Line Clients"
Expand Down
Expand Up @@ -74,15 +74,19 @@ public static boolean signAccountBalanceFile(

return true;
} catch (final SignatureException | IOException e) {
System.err.println("Failed to sign file " + streamFileToSign.getFileName() + ". Exception: " + e);
System.err.printf(
"signAccountBalanceFile :: Failed to sign file [%s] with exception : [%s]%n", streamFileToSign, e);
return false;
} catch (final InvalidKeyException | NoSuchAlgorithmException | NoSuchProviderException e) {
System.err.println("Irrecoverable error encountered: " + e);
throw new RuntimeException("Irrecoverable error encountered", e);
System.err.printf(
"signAccountBalanceFile :: Irrecoverable error encountered when signing [%s] with exception : [%s]%n",
streamFileToSign, e);
throw new RuntimeException("signAccountBalanceFile :: Irrecoverable error encountered", e);
}
}

private static void generateSigBalanceFile(final File filePath, final byte[] signature, final byte[] fileHash) {
private static void generateSigBalanceFile(final File filePath, final byte[] signature, final byte[] fileHash)
throws IOException {
try (final BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(filePath))) {
output.write(TYPE_FILE_HASH);
output.write(fileHash);
Expand All @@ -91,8 +95,10 @@ private static void generateSigBalanceFile(final File filePath, final byte[] sig
output.write(signature);
output.flush();
} catch (final IOException e) {
System.err.println("generateSigBalanceFile :: Fail to generate signature file for " + filePath
+ " with exception :" + e);
System.err.printf(
"generateSigBalanceFile :: Failed to generate signature file [%s] with exception : [%s]%n",
david-bakin-sl marked this conversation as resolved.
Show resolved Hide resolved
filePath.getAbsolutePath(), e);
david-bakin-sl marked this conversation as resolved.
Show resolved Hide resolved
throw e;
}
}
}
@@ -0,0 +1,33 @@
/*
* Copyright (C) 2020-2023 Hedera Hashgraph, LLC
*
* 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 com.hedera.services.cli.sign;

/**
* An exception that is thrown if invalid protobuf version is found.
*
*/
public class InvalidProtobufVersionException extends Exception {

/**
* Constructs a new exception with the specified detail message.
*
* @param errMessage, the detail error message.
*/
public InvalidProtobufVersionException(String errMessage) {
super(errMessage);
}
}
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2016-2023 Hedera Hashgraph, LLC
*
* 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 com.hedera.services.cli.sign;

import com.swirlds.cli.PlatformCli;
import com.swirlds.cli.utility.AbstractCommand;
import com.swirlds.cli.utility.SubcommandOf;
import picocli.CommandLine;

/**
* A collection of operations on record stream.
*/
@CommandLine.Command(
david-bakin-sl marked this conversation as resolved.
Show resolved Hide resolved
name = "record-stream",
mixinStandardHelpOptions = true,
description = "Operations on record stream files.")
@SubcommandOf(PlatformCli.class)
public final class RecordStreamCommand extends AbstractCommand {

private RecordStreamCommand() {}
}
@@ -0,0 +1,64 @@
/*
* Copyright (C) 2023 Hedera Hashgraph, LLC
*
* 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 com.hedera.services.cli.sign;

import com.google.common.annotations.VisibleForTesting;
import com.swirlds.cli.utility.SubcommandOf;
import com.swirlds.platform.cli.SignCommand;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.nio.file.Path;
import java.security.KeyPair;
import picocli.CommandLine;
import picocli.CommandLine.Option;

/**
* A subcommand of the {@link SignCommand}, for signing record stream files
*/
@CommandLine.Command(name = "sign", mixinStandardHelpOptions = true, description = "Sign record stream files")
@SubcommandOf(RecordStreamCommand.class)
public final class RecordStreamSignCommand extends SignCommand {
@Option(
names = {"-hv", "--hapiVersion"},
arity = "1",
description =
"Hapi protobuf version. This is the hapi-version value in hedera-services/settings.gradle.kts. Mandatory!")
String hapiVersion;
/**
* {@inheritDoc}
*/
@Override
public boolean generateSignatureFile(
@NonNull Path signatureFileDestination, @NonNull Path fileToSign, @NonNull KeyPair keyPair) {

return RecordStreamSigningUtils.signRecordStreamFile(
signatureFileDestination, fileToSign, keyPair, hapiVersion);
}

/**
* {@inheritDoc}
*/
@Override
public boolean isFileSupported(@NonNull final Path path) {
return RecordStreamType.getInstance().isStreamFile(path.toFile())
|| RecordStreamType.getInstance().isGzFile(path.toFile().getName());
}

@VisibleForTesting
public void setHapiVersion(String hapiVersion) {
david-bakin-sl marked this conversation as resolved.
Show resolved Hide resolved
this.hapiVersion = hapiVersion;
}
}