Skip to content

Commit

Permalink
refactor(core.keystore): Extracted rest.keystore from core.keystore b…
Browse files Browse the repository at this point in the history
…undle (#5341)

* Extracted rest.keystore from core.keystore bundle

Signed-off-by: pierantoniomerlino <[email protected]>

* Refactored KeystoreRemoteService

Signed-off-by: pierantoniomerlino <[email protected]>

* Added missing files

Signed-off-by: pierantoniomerlino <[email protected]>

---------

Signed-off-by: pierantoniomerlino <[email protected]>
  • Loading branch information
pierantoniomerlino authored Aug 30, 2024
1 parent 837b1e3 commit 86fbc13
Show file tree
Hide file tree
Showing 40 changed files with 793 additions and 158 deletions.
1 change: 1 addition & 0 deletions kura/distrib/config/kura.build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ org.eclipse.kura.rest.security.provider.version=2.0.0-SNAPSHOT
org.eclipse.kura.rest.identity.provider.version=2.0.0-SNAPSHOT
org.eclipse.kura.rest.service.listing.provider.version=2.0.0-SNAPSHOT
org.eclipse.kura.rest.system.provider.version=2.0.0-SNAPSHOT
org.eclipse.kura.rest.keystore.provider.version=1.0.0-SNAPSHOT
org.eclipse.kura.request.handler.jaxrs.version=2.0.0-SNAPSHOT
org.eclipse.kura.rest.wire.provider.version=2.0.0-SNAPSHOT
org.eclipse.kura.wire.ai.component.provider.version=2.0.0-SNAPSHOT
Expand Down
7 changes: 7 additions & 0 deletions kura/distrib/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,11 @@
<artifactId>org.eclipse.kura.log.filesystem.provider</artifactId>
<version>${org.eclipse.kura.log.filesystem.provider.version}</version>
</artifactItem>
<artifactItem>
<groupId>org.eclipse.kura</groupId>
<artifactId>org.eclipse.kura.rest.keystore.provider</artifactId>
<version>${org.eclipse.kura.rest.keystore.provider.version}</version>
</artifactItem>
<artifactItem>
<groupId>org.eclipse.kura</groupId>
<artifactId>org.eclipse.kura.rest.cloudconnection.provider</artifactId>
Expand Down Expand Up @@ -823,6 +828,7 @@
<move file="target/plugins/org.eclipse.kura.network.threat.manager.jar" tofile="target/plugins/org.eclipse.kura.network.threat.manager_${org.eclipse.kura.network.threat.manager.version}.jar" />
<move file="target/plugins/org.eclipse.kura.core.keystore.jar" tofile="target/plugins/org.eclipse.kura.core.keystore_${org.eclipse.kura.core.keystore.version}.jar" />
<move file="target/plugins/org.eclipse.kura.log.filesystem.provider.jar" tofile="target/plugins/org.eclipse.kura.log.filesystem.provider_${org.eclipse.kura.log.filesystem.provider.version}.jar" />
<move file="target/plugins/org.eclipse.kura.rest.keystore.provider.jar" tofile="target/plugins/org.eclipse.kura.rest.keystore.provider_${org.eclipse.kura.rest.keystore.provider.version}.jar" />
<move file="target/plugins/org.eclipse.kura.rest.cloudconnection.provider.jar" tofile="target/plugins/org.eclipse.kura.rest.cloudconnection.provider_${org.eclipse.kura.rest.cloudconnection.provider.version}.jar" />
<move file="target/plugins/org.eclipse.kura.rest.configuration.provider.jar" tofile="target/plugins/org.eclipse.kura.rest.configuration.provider_${org.eclipse.kura.rest.configuration.provider.version}.jar" />
<move file="target/plugins/org.eclipse.kura.rest.network.configuration.provider.jar" tofile="target/plugins/org.eclipse.kura.rest.network.configuration.provider_${org.eclipse.kura.rest.network.configuration.provider.version}.jar" />
Expand Down Expand Up @@ -2080,6 +2086,7 @@
<copy file="${project.build.directory}/plugins/org.eclipse.kura.rest.service.listing.provider_${org.eclipse.kura.rest.service.listing.provider.version}.jar" todir="${project.build.directory}/staging/target-definition/equinox_3.16.0/repository/plugins" />
<copy file="${project.build.directory}/plugins/org.eclipse.kura.rest.system.provider_${org.eclipse.kura.rest.system.provider.version}.jar" todir="${project.build.directory}/staging/target-definition/equinox_3.16.0/repository/plugins" />
<copy file="${project.build.directory}/plugins/org.eclipse.kura.rest.asset.provider_${org.eclipse.kura.rest.asset.provider.version}.jar" todir="${project.build.directory}/staging/target-definition/equinox_3.16.0/repository/plugins" />
<copy file="${project.build.directory}/plugins/org.eclipse.kura.rest.keystore.provider_${org.eclipse.kura.rest.keystore.provider.version}.jar" todir="${project.build.directory}/staging/target-definition/equinox_3.16.0/repository/plugins" />
<!-- Add Artemis bundles-->
<copy file="${project.build.directory}/plugins/org.eclipse.kura.broker.artemis.core_${org.eclipse.kura.broker.artemis.core.version}.jar" todir="${project.build.directory}/staging/target-definition/equinox_3.16.0/repository/plugins" />
<copy file="${project.build.directory}/plugins/org.eclipse.kura.broker.artemis.simple.mqtt_${org.eclipse.kura.broker.artemis.simple.mqtt.version}.jar" todir="${project.build.directory}/staging/target-definition/equinox_3.16.0/repository/plugins" />
Expand Down
5 changes: 5 additions & 0 deletions kura/distrib/src/main/ant/build_equinox_distrib.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1255,6 +1255,8 @@ fi]]>
value=", reference:file:${kura.install.dir}/${kura.symlink}/${plugins.folder}/org.eclipse.kura.rest.wire.provider_${org.eclipse.kura.rest.wire.provider.version}.jar@4:start" />
<entry key="osgi.bundles" operation="+"
value=", reference:file:${kura.install.dir}/${kura.symlink}/${plugins.folder}/org.eclipse.kura.request.handler.jaxrs_${org.eclipse.kura.request.handler.jaxrs.version}.jar@4" />
<entry key="osgi.bundles" operation="+"
value=", reference:file:${kura.install.dir}/${kura.symlink}/${plugins.folder}/org.eclipse.kura.rest.keystore.provider_${org.eclipse.kura.rest.keystore.provider.version}.jar@4:start" />
</propertyfile>
</target>

Expand Down Expand Up @@ -1378,6 +1380,9 @@ fi]]>
<zipfileset
file="${project.build.directory}/plugins/org.eclipse.kura.request.handler.jaxrs_${org.eclipse.kura.request.handler.jaxrs.version}.jar"
prefix="${build.output.name}/${plugins.folder}" />
<zipfileset
file="${project.build.directory}/plugins/org.eclipse.kura.rest.keystore.provider_${org.eclipse.kura.rest.keystore.provider.version}.jar"
prefix="${build.output.name}/${plugins.folder}" />
</zip>
</target>

Expand Down
11 changes: 0 additions & 11 deletions kura/org.eclipse.kura.core.keystore/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ Bundle-Version: 2.0.0.qualifier
Bundle-Vendor: Eclipse Kura
Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.8))"
Import-Package: com.eclipsesource.json;version="0.9.5",
com.google.gson;version="2.7.0",
com.google.gson.annotations;version="2.7.0",
javax.annotation.security;version="1.2.0",
javax.ws.rs;version="2.0.1",
javax.ws.rs.core;version="2.0.1",
org.bouncycastle.asn1;version="1.78.1",
org.bouncycastle.asn1.cms;version="1.78.1",
org.bouncycastle.asn1.pkcs;version="1.78.1",
Expand All @@ -34,14 +29,8 @@ Import-Package: com.eclipsesource.json;version="0.9.5",
org.bouncycastle.util.io.pem;version="1.78.1",
org.eclipse.kura;version="[1.6,2.0)",
org.eclipse.kura.certificate;version="[2.1,3.0)",
org.eclipse.kura.cloudconnection.message;version="[1.0,2.0)",
org.eclipse.kura.cloudconnection.request;version="[1.0,2.0)",
org.eclipse.kura.configuration;version="[1.2,2.0)",
org.eclipse.kura.crypto;version="[1.1,2.0)",
org.eclipse.kura.marshalling;version="[1.0,2.0)",
org.eclipse.kura.message;version="[1.4,2.0)",
org.eclipse.kura.request.handler.jaxrs;version="[1.0,2.0)",
org.eclipse.kura.rest.utils;version="[1.0,2.0)",
org.eclipse.kura.security.keystore;version="[1.1,1.2)",
org.eclipse.kura.system;version="[1.5,2.0)",
org.eclipse.kura.util.configuration;version="[1.0,2.0)",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*******************************************************************************
* Copyright (c) 2024 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech
*
*******************************************************************************/
package org.eclipse.kura.core.keystore.util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.KeyStore.PrivateKeyEntry;
import java.security.KeyStore.TrustedCertificateEntry;
import java.security.PrivateKey;
import java.security.Security;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Collection;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;

public class KeystoreUtils {

private KeystoreUtils() {
// Empty constructor
}

public static PrivateKeyEntry createPrivateKey(String privateKey, String publicKey)
throws IOException, GeneralSecurityException {
// Works with RSA and DSA. EC is not supported since the certificate is encoded
// with ECDSA while the corresponding private key with EC.
// This cause an error when the PrivateKeyEntry is generated.
Certificate[] certs = parsePublicCertificates(publicKey);

Security.addProvider(new BouncyCastleProvider());
PEMParser pemParser = new PEMParser(new StringReader(privateKey));
Object object = pemParser.readObject();
pemParser.close();
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");
PrivateKey privkey = null;
if (object instanceof org.bouncycastle.asn1.pkcs.PrivateKeyInfo) {
privkey = converter.getPrivateKey((org.bouncycastle.asn1.pkcs.PrivateKeyInfo) object);
} else if (object instanceof org.bouncycastle.openssl.PEMKeyPair) {
privkey = converter.getKeyPair((org.bouncycastle.openssl.PEMKeyPair) object).getPrivate();
} else {
throw new IOException("PrivateKey not recognized.");
}
return new PrivateKeyEntry(privkey, certs);
}

public static X509Certificate[] parsePublicCertificates(String certificates) throws CertificateException {
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
ByteArrayInputStream is = new ByteArrayInputStream(certificates.getBytes(StandardCharsets.UTF_8));

final Collection<? extends Certificate> decodedCertificates = certFactory.generateCertificates(is);

final ArrayList<X509Certificate> result = new ArrayList<>();

for (final Certificate cert : decodedCertificates) {
if (!(cert instanceof X509Certificate)) {
throw new CertificateException("Provided certificate is not a X509Certificate");
}

result.add((X509Certificate) cert);
}

return result.toArray(new X509Certificate[result.size()]);
}

public static TrustedCertificateEntry createCertificateEntry(String certificate) throws CertificateException {
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
ByteArrayInputStream is = new ByteArrayInputStream(certificate.getBytes(StandardCharsets.UTF_8));
X509Certificate cert = (X509Certificate) certFactory.generateCertificate(is);
return new TrustedCertificateEntry(cert);
}

}
35 changes: 35 additions & 0 deletions kura/org.eclipse.kura.rest.keystore.provider/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: org.eclipse.kura.rest.keystore.provider
Bundle-SymbolicName: org.eclipse.kura.rest.keystore.provider;singleton:=true
Bundle-Version: 1.0.0.qualifier
Import-Package: com.google.gson;version="2.7.0",
com.google.gson.annotations;version="2.7.0",
javax.annotation.security;version="1.2.0",
javax.ws.rs;version="2.0.1",
javax.ws.rs.core;version="2.0.1",
org.bouncycastle.asn1.pkcs;version="1.78.1",
org.bouncycastle.jce.provider;version="1.78.1",
org.bouncycastle.openssl;version="1.78.1",
org.bouncycastle.openssl.jcajce;version="1.78.1",
org.eclipse.kura;version="[1.7,2.0)",
org.eclipse.kura.cloudconnection.message;version="[1.0,2.0)",
org.eclipse.kura.cloudconnection.request;version="[1.0,2.0)",
org.eclipse.kura.core.keystore.util;version="[1.1,2.0)",
org.eclipse.kura.marshalling;version="[1.0,2.0)",
org.eclipse.kura.message;version="[1.4,2.0)",
org.eclipse.kura.request.handler.jaxrs;version="[1.1,2.0)",
org.eclipse.kura.rest.utils;version="[1.0,2.0)",
org.eclipse.kura.security.keystore;version="[1.1,1.2)",
org.eclipse.kura.util.service;version="[1.0,2.0)",
org.osgi.framework;version="1.10.0",
org.osgi.service.component;version="1.2.0",
org.osgi.service.useradmin;version="1.1.0",
org.osgi.util.tracker;version="1.5.2",
org.slf4j;version="1.7.36"
Bundle-Vendor: Eclipse Kura
Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.8))"
Bundle-ClassPath: .
Bundle-ActivationPolicy: lazy
Service-Component: OSGI-INF/*.xml

Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
Eurotech
-->
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="activate" deactivate="deactivate" enabled="true" immediate="true" name="org.eclipse.kura.core.keystore.rest.provider.KeystoreRestServiceV1">
<implementation class="org.eclipse.kura.core.keystore.rest.provider.KeystoreRestServiceV1"/>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="activate" deactivate="deactivate" enabled="true" immediate="true" name="org.eclipse.kura.internal.rest.keystore.provider.KeystoreRestServiceV1">
<implementation class="org.eclipse.kura.internal.rest.keystore.provider.KeystoreRestServiceV1"/>

<property name="service.pid" value="org.eclipse.kura.core.keystore.rest.provider.KeystoreRestServiceV1"/>
<property name="service.pid" value="org.eclipse.kura.internal.rest.keystore.provider.KeystoreRestServiceV1"/>
<reference bind="setUserAdmin" cardinality="1..1" interface="org.osgi.service.useradmin.UserAdmin" name="UserAdmin" policy="static"/>
<service>
<provide interface="org.eclipse.kura.core.keystore.rest.provider.KeystoreRestService"/>
<provide interface="org.eclipse.kura.internal.rest.keystore.provider.KeystoreRestService"/>
</service>
</scr:component>
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
Eurotech
-->
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="activate" deactivate="deactivate" enabled="true" immediate="true" name="org.eclipse.kura.core.keystore.rest.provider.KeystoreRestServiceV2">
<implementation class="org.eclipse.kura.core.keystore.rest.provider.KeystoreRestServiceV2"/>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="activate" deactivate="deactivate" enabled="true" immediate="true" name="org.eclipse.kura.internal.rest.keystore.provider.KeystoreRestServiceV2">
<implementation class="org.eclipse.kura.internal.rest.keystore.provider.KeystoreRestServiceV2"/>

<property name="service.pid" value="org.eclipse.kura.core.keystore.rest.provider.KeystoreRestServiceV2"/>
<property name="service.pid" value="org.eclipse.kura.internal.rest.keystore.provider.KeystoreRestServiceV2"/>
<reference bind="setUserAdmin" cardinality="1..1" interface="org.osgi.service.useradmin.UserAdmin" name="UserAdmin" policy="static"/>
<service>
<provide interface="org.eclipse.kura.core.keystore.rest.provider.KeystoreRestService"/>
<provide interface="org.eclipse.kura.internal.rest.keystore.provider.KeystoreRestService"/>
</service>
</scr:component>
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
Eurotech
-->
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="activate" deactivate="deactivate" enabled="true" immediate="true" name="org.eclipse.kura.core.keystore.request.handler.KeystoreRequestHandlerV1">
<implementation class="org.eclipse.kura.core.keystore.request.handler.KeystoreServiceRequestHandlerV1"/>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="activate" deactivate="deactivate" enabled="true" immediate="true" name="org.eclipse.kura.internal.rest.keystore.request.handler.KeystoreRequestHandlerV1">
<implementation class="org.eclipse.kura.internal.rest.keystore.request.handler.KeystoreServiceRequestHandlerV1"/>

<property name="service.pid" value="org.eclipse.kura.core.keystore.request.handler.KeystoreServiceRequestHandlerV1"/>
<property name="service.pid" value="org.eclipse.kura.internal.rest.keystore.request.handler.KeystoreServiceRequestHandlerV1"/>
<reference bind="setRequestHandlerRegistry" cardinality="0..n" interface="org.eclipse.kura.cloudconnection.request.RequestHandlerRegistry" name="RequestHandlerRegistry" policy="dynamic" unbind="unsetRequestHandlerRegistry"/>
</scr:component>
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
Eurotech
-->
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="activate" deactivate="deactivate" enabled="true" immediate="true" name="org.eclipse.kura.core.keystore.request.handler.KeystoreRequestHandlerV2">
<implementation class="org.eclipse.kura.core.keystore.request.handler.KeystoreServiceRequestHandlerV2"/>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="activate" deactivate="deactivate" enabled="true" immediate="true" name="org.eclipse.kura.internal.rest.keystore.request.handler.KeystoreRequestHandlerV2">
<implementation class="org.eclipse.kura.internal.rest.keystore.request.handler.KeystoreServiceRequestHandlerV2"/>

<property name="service.pid" value="org.eclipse.kura.core.keystore.request.handler.KeystoreServiceRequestHandlerV2"/>
<property name="service.pid" value="org.eclipse.kura.internal.rest.keystore.request.handler.KeystoreServiceRequestHandlerV2"/>
<reference bind="setRequestHandlerRegistry" cardinality="0..n" interface="org.eclipse.kura.cloudconnection.request.RequestHandlerRegistry" name="RequestHandlerRegistry" policy="dynamic" unbind="unsetRequestHandlerRegistry"/>
</scr:component>
36 changes: 36 additions & 0 deletions kura/org.eclipse.kura.rest.keystore.provider/about.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>About</title>
</head>
<body lang="EN-US">
<h2>About This Content</h2>

<p>November 30, 2017</p>
<h3>License</h3>

<p>
The Eclipse Foundation makes available all content in this plug-in
(&quot;Content&quot;). Unless otherwise indicated below, the Content
is provided to you under the terms and conditions of the Eclipse
Public License Version 2.0 (&quot;EPL&quot;). A copy of the EPL is
available at <a href="http://www.eclipse.org/legal/epl-2.0">http://www.eclipse.org/legal/epl-2.0</a>.
For purposes of the EPL, &quot;Program&quot; will mean the Content.
</p>

<p>
If you did not receive this Content directly from the Eclipse
Foundation, the Content is being redistributed by another party
(&quot;Redistributor&quot;) and different terms and conditions may
apply to your use of any object code in the Content. Check the
Redistributor's license that was provided with the Content. If no such
license exists, contact the Redistributor. Unless otherwise indicated
below, the terms and conditions of the EPL still apply to any source
code in the Content and such source code may be obtained at <a
href="http://www.eclipse.org/">http://www.eclipse.org</a>.
</p>

</body>
</html>
Loading

0 comments on commit 86fbc13

Please sign in to comment.