-
Notifications
You must be signed in to change notification settings - Fork 438
RATIS-2095. Extract common logic of ratis-shell to RaftUtils for reuse #1098
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
Merged
+145
−77
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ca41aca
RATIS-2095. Move common logic of ratis-shell to RaftUtils so that Ozo…
cfd4cf3
fix import
eae5ae0
fix license, findbug of dereference, line length
19abc56
fix checkstyle
17d2e43
pass path of certificate as command option to ratis shell
a3b4463
remove securityUtil class, revert changes from subcommands of Abstrac…
d4cd161
move ratis client with TlsConf to a different PR
b943e2a
Improve code readability and maintainability
91a9d4c
removed unused method AbstractRatisCommand.run, fix checkstyle.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
ratis-shell/src/main/java/org/apache/ratis/shell/cli/SecurityUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.ratis.shell.cli; | ||
|
|
||
| import javax.net.ssl.TrustManager; | ||
| import javax.net.ssl.TrustManagerFactory; | ||
| import javax.net.ssl.X509TrustManager; | ||
| import java.io.InputStream; | ||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Paths; | ||
| import java.security.KeyStore; | ||
| import java.security.cert.CertificateException; | ||
| import java.security.cert.CertificateFactory; | ||
| import java.security.cert.X509Certificate; | ||
| import java.util.Arrays; | ||
|
|
||
| public final class SecurityUtils { | ||
| private SecurityUtils() { | ||
| // prevent instantiation | ||
| } | ||
|
|
||
| public static KeyStore getTrustStore(String crtPath) | ||
| throws Exception { | ||
| X509Certificate[] certificate = getCertificate(crtPath); | ||
|
|
||
| // build trustStore | ||
| KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); | ||
| trustStore.load(null, null); | ||
|
|
||
| for (X509Certificate cert: certificate) { | ||
| trustStore.setCertificateEntry(cert.getSerialNumber().toString(), cert); | ||
| } | ||
| return trustStore; | ||
| } | ||
|
|
||
| public static X509TrustManager getTrustManager(KeyStore keyStore) throws Exception{ | ||
| TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance( | ||
| TrustManagerFactory.getDefaultAlgorithm()); | ||
| trustManagerFactory.init(keyStore); | ||
| TrustManager[] trustManagers = trustManagerFactory.getTrustManagers(); | ||
| if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) { | ||
| throw new IllegalStateException("Unexpected default trust managers:" | ||
| + Arrays.toString(trustManagers)); | ||
| } | ||
| return (X509TrustManager) trustManagers[0]; | ||
| } | ||
|
|
||
| static X509Certificate[] getCertificate(String certPath) | ||
| throws CertificateException, IOException { | ||
| // Read certificates | ||
| X509Certificate[] certificate = new X509Certificate[1]; | ||
| CertificateFactory fact = CertificateFactory.getInstance("X.509"); | ||
| try (InputStream is = Files.newInputStream(Paths.get(certPath))) { | ||
| certificate[0] = (X509Certificate) fact.generateCertificate(is); | ||
| } | ||
| return certificate; | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.