From 12b41095a7a9ea865148234a4df8f62fc759f4ca Mon Sep 17 00:00:00 2001 From: bhashinee Date: Mon, 11 Dec 2023 17:24:41 +0530 Subject: [PATCH] Address review suggestions --- ballerina/tests/private_public_key_test.bal | 48 +++++++++---------- .../stdlib/crypto/nativeimpl/Decode.java | 3 +- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/ballerina/tests/private_public_key_test.bal b/ballerina/tests/private_public_key_test.bal index e664be1f..c0ed0a93 100644 --- a/ballerina/tests/private_public_key_test.bal +++ b/ballerina/tests/private_public_key_test.bal @@ -23,7 +23,7 @@ isolated function testParseEncryptedPrivateKeyFromP12() returns Error? { password: "ballerina" }; PrivateKey result = check decodeRsaPrivateKeyFromKeyStore(keyStore, "ballerina", "ballerina"); - test:assertEquals(result["algorithm"], "RSA"); + test:assertEquals(result.algorithm, "RSA"); } @test:Config {} @@ -85,13 +85,13 @@ isolated function testReadPrivateKeyFromP12WithInvalidKeyPassword() { @test:Config {} isolated function testParsePrivateKeyFromKeyFile() returns Error? { PrivateKey result = check decodeRsaPrivateKeyFromKeyFile(PRIVATE_KEY_PATH); - test:assertEquals(result["algorithm"], "RSA"); + test:assertEquals(result.algorithm, "RSA"); } @test:Config {} isolated function testParseEncryptedPrivateKeyFromKeyFile() returns Error? { PrivateKey result = check decodeRsaPrivateKeyFromKeyFile(ENCRYPTED_PRIVATE_KEY_PATH, "ballerina"); - test:assertEquals(result["algorithm"], "RSA"); + test:assertEquals(result.algorithm, "RSA"); } @test:Config {} @@ -117,7 +117,7 @@ isolated function testParseEncryptedPrivateKeyFromKeyFileWithNoPassword() { @test:Config {} isolated function testParseEncryptedPrivateKeyFromKeyPairFile() returns Error? { PrivateKey result = check decodeRsaPrivateKeyFromKeyFile(ENCRYPTED_KEY_PAIR_PATH, "ballerina"); - test:assertEquals(result["algorithm"], "RSA"); + test:assertEquals(result.algorithm, "RSA"); } @test:Config {} @@ -143,13 +143,13 @@ isolated function testParseEncryptedPrivateKeyFromKeyPairFileWithNoPassword() { @test:Config {} isolated function testParsePrivateKeyFromKeyPairFile() returns Error? { PrivateKey result = check decodeRsaPrivateKeyFromKeyFile(KEY_PAIR_PATH); - test:assertEquals(result["algorithm"], "RSA"); + test:assertEquals(result.algorithm, "RSA"); } @test:Config {} isolated function testParseEcPrivateKeyFromKeyFile() returns Error? { PrivateKey result = check decodeEcPrivateKeyFromKeyFile(EC_PRIVATE_KEY_PATH); - test:assertEquals(result["algorithm"], "ECDSA"); + test:assertEquals(result.algorithm, "ECDSA"); } @test:Config {} @@ -189,13 +189,13 @@ isolated function testParsePublicKeyFromP12() returns Error? { password: "ballerina" }; PublicKey publicKey = check decodeRsaPublicKeyFromTrustStore(trustStore, "ballerina"); - test:assertEquals(publicKey["algorithm"], "RSA"); - Certificate certificate = publicKey["certificate"]; + test:assertEquals(publicKey.algorithm, "RSA"); + Certificate certificate = publicKey.certificate; - string serial = (certificate["serial"]).toString(); - string issuer = certificate["issuer"]; - string subject = certificate["subject"]; - string signingAlgorithm = certificate["signingAlgorithm"]; + string serial = (certificate.serial).toString(); + string issuer = certificate.issuer; + string subject = certificate.subject; + string signingAlgorithm = certificate.signingAlgorithm; test:assertEquals(serial, "2097012467"); test:assertEquals(issuer, "CN=localhost,OU=WSO2,O=WSO2,L=Mountain View,ST=CA,C=US"); @@ -248,13 +248,13 @@ isolated function testReadPublicKeyFromP12WithInvalidAlias() { @test:Config {} isolated function testParsePublicKeyFromX509CertFile() returns Error? { PublicKey publicKey = check decodeRsaPublicKeyFromCertFile(X509_PUBLIC_CERT_PATH); - test:assertEquals(publicKey["algorithm"], "RSA"); - Certificate certificate = publicKey["certificate"]; + test:assertEquals(publicKey.algorithm, "RSA"); + Certificate certificate = publicKey.certificate; - string serial = (certificate["serial"]).toString(); - string issuer = certificate["issuer"]; - string subject = certificate["subject"]; - string signingAlgorithm = certificate["signingAlgorithm"]; + string serial = (certificate.serial).toString(); + string issuer = certificate.issuer; + string subject = certificate.subject; + string signingAlgorithm = certificate.signingAlgorithm; test:assertEquals(serial, "2097012467"); test:assertEquals(issuer, "CN=localhost,OU=WSO2,O=WSO2,L=Mountain View,ST=CA,C=US"); @@ -265,13 +265,13 @@ isolated function testParsePublicKeyFromX509CertFile() returns Error? { @test:Config {} isolated function testParseEcPublicKeyFromX509CertFile() returns Error? { PublicKey publicKey = check decodeEcPublicKeyFromCertFile(EC_CERT_PATH); - test:assertEquals(publicKey["algorithm"], "EC"); - Certificate certificate = publicKey["certificate"]; + test:assertEquals(publicKey.algorithm, "EC"); + Certificate certificate = publicKey.certificate; - string serial = (certificate["serial"]).toString(); - string issuer = certificate["issuer"]; - string subject = certificate["subject"]; - string signingAlgorithm = certificate["signingAlgorithm"]; + string serial = (certificate.serial).toString(); + string issuer = certificate.issuer; + string subject = certificate.subject; + string signingAlgorithm = certificate.signingAlgorithm; test:assertEquals(serial, "813081972327485475"); test:assertEquals(issuer, "CN=sigstore-intermediate,O=sigstore.dev"); diff --git a/native/src/main/java/io/ballerina/stdlib/crypto/nativeimpl/Decode.java b/native/src/main/java/io/ballerina/stdlib/crypto/nativeimpl/Decode.java index a4557b32..41d4dcc2 100644 --- a/native/src/main/java/io/ballerina/stdlib/crypto/nativeimpl/Decode.java +++ b/native/src/main/java/io/ballerina/stdlib/crypto/nativeimpl/Decode.java @@ -166,8 +166,7 @@ private static Object getPrivateKey(BString keyFilePath, Object keyPassword) { return CryptoUtils.createError("Failed to parse private key information from: " + keyFilePath.getValue()); } - PrivateKey privateKey = converter.getPrivateKey(privateKeyInfo); - return privateKey; + return converter.getPrivateKey(privateKeyInfo); } catch (FileNotFoundException e) { return CryptoUtils.createError("Key file not found at: " + privateKeyFile.getAbsoluteFile()); } catch (PKCSException | IOException e) {