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

Use temporary self signed certificates for JSON-RPC HTTP TLS unit testing #311

Merged
merged 16 commits into from
Jan 29, 2020
Merged

Use temporary self signed certificates for JSON-RPC HTTP TLS unit testing #311

merged 16 commits into from
Jan 29, 2020

Conversation

usmansaleem
Copy link
Member

@usmansaleem usmansaleem commented Jan 18, 2020

PR description

-- Generate and use temporary self signed certificates in PKCS12 format with SAN extensions for dns and IP to be used by TLS unit/integration tests
-- Remove pre-generated key stores and trust stores used by TLS tests

Signed-off-by: Usman Saleem usman@usmans.info

 -- Generate self signed certificates in PKCS12 format with SAN extensions for testing
 -- Remove pre-generated key stores and trust stores used by TLS tests

Signed-off-by: Usman Saleem <usman@usmans.info>
Signed-off-by: Usman Saleem <usman@usmans.info>
Signed-off-by: Usman Saleem <usman@usmans.info>
Signed-off-by: Usman Saleem <usman@usmans.info>
@usmansaleem usmansaleem changed the title SelfSignedPfxStore in testSupport for JSON-RPC HTTP TLS testing Use temporary self signed certificates for JSON-RPC HTTP TLS unit testing Jan 19, 2020
@usmansaleem usmansaleem marked this pull request as ready for review January 19, 2020 00:48
Signed-off-by: Usman Saleem <usman@usmans.info>
gradle/versions.gradle Outdated Show resolved Hide resolved
 -- required by Tuweni TLS

Signed-off-by: Usman Saleem <usman@usmans.info>
Signed-off-by: Usman Saleem <usman@usmans.info>
…Jar needs to be exported.

Signed-off-by: Usman Saleem <usman@usmans.info>
Signed-off-by: Usman Saleem <usman@usmans.info>
@usmansaleem usmansaleem requested review from rain-on and CjHare January 22, 2020 03:38
Signed-off-by: Usman Saleem <usman@usmans.info>
Copy link
Contributor

@CjHare CjHare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was the motivation for locating this self signing code under ethereum/api rather then say crypto?


@Before
public void initServer() throws Exception {
selfSignedPfxStore = SelfSignedPfxStore.create();
selfSignedPfxStoreForHttpClient = SelfSignedPfxStore.create();
selfSignedPfxStoreForHttpClientNotTrustedByServer = SelfSignedPfxStore.create();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems odd, the same method of creation gives a PfxStore, PfxStoreForHttpClient and PfxStoreForHttpClientNotTrustedByServer.

If there are three different stores, then perhaps separate helper methods to create each of them and their corresponding state (e.g. that makes one trusted and another not trusted), might be an idea?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The utility class generate both keystore and truststore containing self-sligned certificate. The keystore is used by server/json-rpc and truststore is used by okhttp/client vice-versa is also applicable for testing client-auth. The keystore used by okhttp client, its truststore is used by server. Hence there is a need of having three different instances of self-signed certs. I can wrap them in a method for the sake of reaability, but all it will do is return instances which will again store in three different variables.

Signed-off-by: Usman Saleem <usman@usmans.info>
@usmansaleem
Copy link
Member Author

What was the motivation for locating this self signing code under ethereum/api rather then say crypto?

@CjHare The test code (and the unit under testing) which required self signed certificates was in ethereum/api. I don't mind moving it under crypto/testSupport (I think it make more sense there)

 -- Renaming class to clear intent
 -- Introducing builder in TlsHttpClient test utility
 -- cleaning unit tests

Signed-off-by: Usman Saleem <usman@usmans.info>
Signed-off-by: Usman Saleem <usman@usmans.info>
@usmansaleem usmansaleem requested a review from CjHare January 28, 2020 12:20
…r to construct OkHttpClient

Signed-off-by: Usman Saleem <usman@usmans.info>
Signed-off-by: Usman Saleem <usman@usmans.info>
final String fingerprint = TLS.certificateHexFingerprint(certificate);
final String commonName = extractCommonName(certificate);
final String knownClientsLine = String.format("%s %s", commonName, fingerprint);
final Path temporaryKnownClientsFile = Files.createTempFile("testKnownClients", ".txt");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'd remove "test" - you never know if someone wants to make this a general capability in an app/library.
Having said that - having this create a tempFile, when you don't know the use of this function is kinda dangerous.
But am wondering if these two functions should effeictvely exist on the SelfSignedP12Certificate - such taht someone can say:
cert.getFingerPrint();
cert.getCommonName();
writeFile(fingerprint, commonName)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've refactored it.

private static final NatService natService = new NatService(Optional.empty());

private final SelfSignedP12Certificate besuCertificate = SelfSignedP12Certificate.create();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: is besuCertificate correct here, or is this the certificate presented by the client?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

besuCertificate contains certificate keystore used by Besu. It also exposes trustStore that can be used by OkHttp client library to trust Besu.

import okhttp3.OkHttpClient;

public final class TlsOkHttpClientBuilder {
private SelfSignedP12Certificate besuCertificate;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whats the difference between the besuCertificate and the okHttpCertificate?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case of this builder class, we need to trust Besu certificate, okhttpcertificate is optional and required if besu enables client-auth ... OkHttpClient set its keystore exposed by okhttpcertificate.

Signed-off-by: Usman Saleem <usman@usmans.info>
@usmansaleem usmansaleem merged commit 5b1ee14 into hyperledger:master Jan 29, 2020
@usmansaleem usmansaleem deleted the rpc_tls_tests_certs branch January 29, 2020 02:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants