NIFI-12265 Fixed OpenPGP Hexadecimal Key Formatting with leading 0

- Replaced Long.toHexString() with String.format() using pattern with leading 0

This closes #7926

Signed-off-by: David Handermann <exceptionfactory@apache.org>
This commit is contained in:
nathluu 2023-10-24 14:38:01 +07:00 committed by exceptionfactory
parent 90498a352d
commit 8e301cfdef
No known key found for this signature in database
GPG Key ID: 29B6A52D2AAE8DBA
11 changed files with 18 additions and 13 deletions

View File

@ -38,7 +38,7 @@ import org.apache.nifi.processor.util.StandardValidators;
import org.apache.nifi.processors.pgp.attributes.DecryptionStrategy;
import org.apache.nifi.processors.pgp.exception.PGPDecryptionException;
import org.apache.nifi.processors.pgp.exception.PGPProcessException;
import org.apache.nifi.processors.pgp.io.KeyIdentifierConverter;
import org.apache.nifi.pgp.service.api.KeyIdentifierConverter;
import org.apache.nifi.stream.io.StreamUtils;
import org.apache.nifi.util.StringUtils;

View File

@ -39,7 +39,7 @@ import org.apache.nifi.processors.pgp.attributes.HashAlgorithm;
import org.apache.nifi.processors.pgp.attributes.SigningStrategy;
import org.apache.nifi.processors.pgp.exception.PGPProcessException;
import org.apache.nifi.processors.pgp.io.EncodingStreamCallback;
import org.apache.nifi.processors.pgp.io.KeyIdentifierConverter;
import org.apache.nifi.pgp.service.api.KeyIdentifierConverter;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPLiteralDataGenerator;
import org.bouncycastle.openpgp.PGPOnePassSignature;

View File

@ -31,7 +31,7 @@ import org.apache.nifi.processor.ProcessSession;
import org.apache.nifi.processor.Relationship;
import org.apache.nifi.processor.io.StreamCallback;
import org.apache.nifi.processors.pgp.exception.PGPProcessException;
import org.apache.nifi.processors.pgp.io.KeyIdentifierConverter;
import org.apache.nifi.pgp.service.api.KeyIdentifierConverter;
import org.apache.nifi.stream.io.StreamUtils;
import org.bouncycastle.openpgp.PGPCompressedData;
import org.bouncycastle.openpgp.PGPException;

View File

@ -21,7 +21,7 @@ import org.apache.nifi.pgp.util.PGPOperationUtils;
import org.apache.nifi.processors.pgp.attributes.DecryptionStrategy;
import org.apache.nifi.processors.pgp.attributes.FileEncoding;
import org.apache.nifi.processors.pgp.attributes.SymmetricKeyAlgorithm;
import org.apache.nifi.processors.pgp.io.KeyIdentifierConverter;
import org.apache.nifi.pgp.service.api.KeyIdentifierConverter;
import org.apache.nifi.reporting.InitializationException;
import org.apache.nifi.stream.io.StreamUtils;
import org.apache.nifi.util.MockFlowFile;

View File

@ -21,7 +21,7 @@ import org.apache.nifi.pgp.util.PGPSecretKeyGenerator;
import org.apache.nifi.processors.pgp.attributes.FileEncoding;
import org.apache.nifi.processors.pgp.attributes.HashAlgorithm;
import org.apache.nifi.processors.pgp.attributes.SigningStrategy;
import org.apache.nifi.processors.pgp.io.KeyIdentifierConverter;
import org.apache.nifi.pgp.service.api.KeyIdentifierConverter;
import org.apache.nifi.reporting.InitializationException;
import org.apache.nifi.util.LogMessage;
import org.apache.nifi.util.MockFlowFile;

View File

@ -20,7 +20,7 @@ import org.apache.nifi.pgp.service.api.PGPPublicKeyService;
import org.apache.nifi.pgp.util.PGPFileUtils;
import org.apache.nifi.pgp.util.PGPSecretKeyGenerator;
import org.apache.nifi.pgp.util.PGPOperationUtils;
import org.apache.nifi.processors.pgp.io.KeyIdentifierConverter;
import org.apache.nifi.pgp.service.api.KeyIdentifierConverter;
import org.apache.nifi.reporting.InitializationException;
import org.apache.nifi.util.LogMessage;
import org.apache.nifi.util.MockFlowFile;

View File

@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.nifi.processors.pgp.io;
package org.apache.nifi.pgp.service.api;
import java.math.BigInteger;
@ -24,6 +24,8 @@ import java.math.BigInteger;
public class KeyIdentifierConverter {
private static final int HEXADECIMAL_RADIX = 16;
private static final String KEY_ID_FORMAT = "%016X";
/**
* Format numeric key identifier as uppercase hexadecimal string
*
@ -31,7 +33,7 @@ public class KeyIdentifierConverter {
* @return Uppercase hexadecimal string
*/
public static String format(final long keyId) {
return Long.toHexString(keyId).toUpperCase();
return String.format(KEY_ID_FORMAT, keyId);
}
/**

View File

@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.nifi.processors.pgp.io;
package org.apache.nifi.pgp.service.api;
import org.junit.jupiter.api.Test;

View File

@ -27,6 +27,7 @@ import org.apache.nifi.context.PropertyContext;
import org.apache.nifi.controller.AbstractControllerService;
import org.apache.nifi.controller.ConfigurationContext;
import org.apache.nifi.expression.ExpressionLanguageScope;
import org.apache.nifi.pgp.service.api.KeyIdentifierConverter;
import org.apache.nifi.pgp.service.api.PGPPrivateKeyService;
import org.apache.nifi.pgp.service.standard.exception.PGPConfigurationException;
import org.apache.nifi.processor.util.StandardValidators;
@ -142,7 +143,7 @@ public class StandardPGPPrivateKeyService extends AbstractControllerService impl
*/
@Override
public Optional<PGPPrivateKey> findPrivateKey(final long keyIdentifier) {
getLogger().debug("Find Private Key [{}]", Long.toHexString(keyIdentifier).toUpperCase());
getLogger().debug("Find Private Key [{}]", KeyIdentifierConverter.format(keyIdentifier));
return Optional.ofNullable(privateKeys.get(keyIdentifier));
}
@ -256,7 +257,7 @@ public class StandardPGPPrivateKeyService extends AbstractControllerService impl
for (final PGPSecretKeyRing keyRing : keyRings) {
for (final PGPSecretKey secretKey : keyRing) {
final long keyId = secretKey.getKeyID();
final String keyIdentifier = Long.toHexString(keyId).toUpperCase();
final String keyIdentifier = KeyIdentifierConverter.format(keyId);
try {
final PGPPrivateKey privateKey = secretKey.extractPrivateKey(keyDecryptor);
extractedPrivateKeys.add(privateKey);

View File

@ -27,6 +27,7 @@ import org.apache.nifi.context.PropertyContext;
import org.apache.nifi.controller.AbstractControllerService;
import org.apache.nifi.controller.ConfigurationContext;
import org.apache.nifi.expression.ExpressionLanguageScope;
import org.apache.nifi.pgp.service.api.KeyIdentifierConverter;
import org.apache.nifi.pgp.service.api.PGPPublicKeyService;
import org.apache.nifi.pgp.service.standard.exception.PGPConfigurationException;
import org.apache.nifi.processor.util.StandardValidators;
@ -186,7 +187,7 @@ public class StandardPGPPublicKeyService extends AbstractControllerService imple
private boolean isPublicKeyMatched(final PGPPublicKey publicKey, final String search) {
boolean matched = false;
final String keyId = Long.toHexString(publicKey.getKeyID()).toUpperCase();
final String keyId = KeyIdentifierConverter.format(publicKey.getKeyID());
if (keyId.equals(search)) {
matched = true;
} else {

View File

@ -16,6 +16,7 @@
*/
package org.apche.nifi.pgp.service.standard;
import org.apache.nifi.pgp.service.api.KeyIdentifierConverter;
import org.apache.nifi.pgp.service.standard.StandardPGPPublicKeyService;
import org.apache.nifi.pgp.util.PGPFileUtils;
import org.apache.nifi.pgp.util.PGPSecretKeyGenerator;
@ -129,7 +130,7 @@ public class StandardPGPPublicKeyServiceTest {
private void assertPublicKeyFound(final PGPSecretKey secretKey) {
final long keyIdentifier = secretKey.getKeyID();
final String publicKeySearch = Long.toHexString(keyIdentifier).toUpperCase();
final String publicKeySearch = KeyIdentifierConverter.format(keyIdentifier);
final Optional<PGPPublicKey> optionalPublicKey = service.findPublicKey(publicKeySearch);
assertTrue(optionalPublicKey.isPresent());
final PGPPublicKey publicKey = optionalPublicKey.get();