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

Remove sun.java dependency #5980

Merged
merged 3 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
import org.wso2.carbon.utils.CarbonUtils;
import org.wso2.carbon.utils.NetworkUtils;
import org.wso2.carbon.utils.multitenancy.MultitenantUtils;
import sun.security.provider.X509Factory;

import java.io.ByteArrayInputStream;
import java.io.File;
Expand Down Expand Up @@ -1368,8 +1367,8 @@ public static String convertCertificateToPEM(Certificate certificate) throws Cer

byte[] encodedCertificate = org.apache.commons.codec.binary.Base64.encodeBase64(certificate.getEncoded());

String encodedPEM = String.format("%s\n%s\n%s", X509Factory.BEGIN_CERT, new String(encodedCertificate),
X509Factory.END_CERT);
String encodedPEM = String.format("%s\n%s\n%s", PEM_BEGIN_CERTFICATE, new String(encodedCertificate),
hwupathum marked this conversation as resolved.
Show resolved Hide resolved
PEM_END_CERTIFICATE);

return encodedPEM;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,17 @@
import org.wso2.carbon.utils.ConfigurationContextService;
import org.wso2.carbon.utils.NetworkUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.SignatureException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -82,6 +90,10 @@
@Listeners(MockitoTestNGListener.class)
public class IdentityUtilTest {

private static final String PRIMARY_KEY_STORE = "wso2carbon.jks";
private static final String PRIMARY_KEY_STORE_PASSWORD = "wso2carbon";
private static final String PRIMARY_KEY_STORE_ALIAS = "wso2carbon";

@Mock
private IdentityConfigParser mockConfigParser;
@Mock
Expand All @@ -105,6 +117,8 @@ public class IdentityUtilTest {
@Mock
private HttpServletRequest mockRequest;

private KeyStore primaryKeyStore;

MockedStatic<CarbonUtils> carbonUtils;
MockedStatic<ServerConfiguration> serverConfiguration;
MockedStatic<NetworkUtils> networkUtils;
Expand Down Expand Up @@ -139,6 +153,8 @@ public void setUp() throws Exception {

System.setProperty(IdentityConstants.CarbonPlaceholders.CARBON_PORT_HTTP_PROPERTY, "9763");
System.setProperty(IdentityConstants.CarbonPlaceholders.CARBON_PORT_HTTPS_PROPERTY, "9443");

primaryKeyStore = getKeyStoreFromFile(PRIMARY_KEY_STORE, PRIMARY_KEY_STORE_PASSWORD);
}

@AfterMethod
Expand All @@ -157,6 +173,17 @@ public void tearDown() throws Exception {
identityTenantUtil.close();
}

@Test(description = "Test converting a certificate to PEM format")
public void convertCertificateToPEM() throws CertificateException, KeyStoreException, IOException {

Certificate validCertificate = primaryKeyStore.getCertificate(PRIMARY_KEY_STORE_ALIAS);
Path pemPath = Paths.get(System.getProperty("user.dir"), "src", "test", "resources",
"repository", "resources", "security", "certificate.pem");
String expectedPEM = String.join("\n", Files.readAllLines(pemPath));

assertEquals(IdentityUtil.convertCertificateToPEM(validCertificate), expectedPEM);
}

@DataProvider
public Object[][] getIdentityErrorMsgTestData() {
return new Object[][]{
Expand Down Expand Up @@ -898,4 +925,14 @@ private Object getPrivateStaticField(Class<?> clazz, String fieldName)
field.setAccessible(true);
return field.get(null);
}

private KeyStore getKeyStoreFromFile(String keystoreName, String password) throws Exception {

Path tenantKeystorePath = Paths.get(System.getProperty("user.dir"), "src", "test", "resources", "repository", "resources", "security", keystoreName);
FileInputStream file = new FileInputStream(tenantKeystorePath.toString());
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
keystore.load(file, password.toCharArray());
return keystore;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-----BEGIN CERTIFICATE-----
MIICNTCCAZ6gAwIBAgIES343gjANBgkqhkiG9w0BAQUFADBVMQswCQYDVQQGEwJVUzELMAkGA1UECAwCQ0ExFjAUBgNVBAcMDU1vdW50YWluIFZpZXcxDTALBgNVBAoMBFdTTzIxEjAQBgNVBAMMCWxvY2FsaG9zdDAeFw0xMDAyMTkwNzAyMjZaFw0zNTAyMTMwNzAyMjZaMFUxCzAJBgNVBAYTAlVTMQswCQYDVQQIDAJDQTEWMBQGA1UEBwwNTW91bnRhaW4gVmlldzENMAsGA1UECgwEV1NPMjESMBAGA1UEAwwJbG9jYWxob3N0MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCUp/oV1vWc8/TkQSiAvTousMzOM4asB2iltr2QKozni5aVFu818MpOLZIr8LMnTzWllJvvaA5RAAdpbECb+48FjbBe0hseUdN5HpwvnH/DW8ZccGvk53I6Orq7hLCv1ZHtuOCokghz/ATrhyPq+QktMfXnRS4HrKGJTzxaCcU7OQIDAQABoxIwEDAOBgNVHQ8BAf8EBAMCBPAwDQYJKoZIhvcNAQEFBQADgYEAW5wPR7cr1LAdq+IrR44iQlRG5ITCZXY9hI0PygLP2rHANh+PYfTmxbuOnykNGyhM6FjFLbW2uZHQTY1jMrPprjOrmyK5sjJRO4d1DeGHT/YnIjs9JogRKv4XHECwLtIVdAbIdWHEtVZJyMSktcyysFcvuhPQK8Qc/E/Wq8uHSCo=
-----END CERTIFICATE-----
Loading