Skip to content

Commit 6cee00f

Browse files
anmolnarbbeaudreault
authored andcommitted
HBASE-27346 Autodetect key/truststore file type from file extension (apache#4757)
Signed-off-by: Duo Zhang <[email protected]> Signed-off-by: Bryan Beaudreault <[email protected]>
1 parent 45eecf2 commit 6cee00f

21 files changed

+1647
-126
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.hadoop.hbase.io.crypto.tls;
19+
20+
/**
21+
* Implementation of {@link FileKeyStoreLoader} that loads from BCKFS files.
22+
* <p/>
23+
* This file has been copied from the Apache ZooKeeper project.
24+
* @see <a href=
25+
* "https://github.com/apache/zookeeper/blob/c74658d398cdc1d207aa296cb6e20de00faec03e/zookeeper-server/src/main/java/org/apache/zookeeper/common/BCFKSFileLoader.java">Base
26+
* revision</a>
27+
*/
28+
final class BCFKSFileLoader extends StandardTypeFileKeyStoreLoader {
29+
private BCFKSFileLoader(String keyStorePath, String trustStorePath, char[] keyStorePassword,
30+
char[] trustStorePassword) {
31+
super(keyStorePath, trustStorePath, keyStorePassword, trustStorePassword,
32+
SupportedStandardKeyFormat.BCFKS);
33+
}
34+
35+
static class Builder extends FileKeyStoreLoader.Builder<BCFKSFileLoader> {
36+
@Override
37+
BCFKSFileLoader build() {
38+
return new BCFKSFileLoader(keyStorePath, trustStorePath, keyStorePassword,
39+
trustStorePassword);
40+
}
41+
}
42+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.hadoop.hbase.io.crypto.tls;
19+
20+
import java.util.Objects;
21+
22+
/**
23+
* Base class for instances of {@link KeyStoreLoader} which load the key/trust stores from files on
24+
* a filesystem.
25+
* <p/>
26+
* This file has been copied from the Apache ZooKeeper project.
27+
* @see <a href=
28+
* "https://github.com/apache/zookeeper/blob/c74658d398cdc1d207aa296cb6e20de00faec03e/zookeeper-server/src/main/java/org/apache/zookeeper/common/FileKeyStoreLoader.java">Base
29+
* revision</a>
30+
*/
31+
abstract class FileKeyStoreLoader implements KeyStoreLoader {
32+
final String keyStorePath;
33+
final String trustStorePath;
34+
final char[] keyStorePassword;
35+
final char[] trustStorePassword;
36+
37+
FileKeyStoreLoader(String keyStorePath, String trustStorePath, char[] keyStorePassword,
38+
char[] trustStorePassword) {
39+
this.keyStorePath = keyStorePath;
40+
this.trustStorePath = trustStorePath;
41+
this.keyStorePassword = keyStorePassword;
42+
this.trustStorePassword = trustStorePassword;
43+
}
44+
45+
/**
46+
* Base class for builder pattern used by subclasses.
47+
* @param <T> the subtype of FileKeyStoreLoader created by the Builder.
48+
*/
49+
static abstract class Builder<T extends FileKeyStoreLoader> {
50+
String keyStorePath;
51+
String trustStorePath;
52+
char[] keyStorePassword;
53+
char[] trustStorePassword;
54+
55+
Builder() {
56+
}
57+
58+
Builder<T> setKeyStorePath(String keyStorePath) {
59+
this.keyStorePath = Objects.requireNonNull(keyStorePath);
60+
return this;
61+
}
62+
63+
Builder<T> setTrustStorePath(String trustStorePath) {
64+
this.trustStorePath = Objects.requireNonNull(trustStorePath);
65+
return this;
66+
}
67+
68+
Builder<T> setKeyStorePassword(char[] keyStorePassword) {
69+
this.keyStorePassword = Objects.requireNonNull(keyStorePassword);
70+
return this;
71+
}
72+
73+
Builder<T> setTrustStorePassword(char[] trustStorePassword) {
74+
this.trustStorePassword = Objects.requireNonNull(trustStorePassword);
75+
return this;
76+
}
77+
78+
abstract T build();
79+
}
80+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.hadoop.hbase.io.crypto.tls;
19+
20+
import java.util.Objects;
21+
22+
/**
23+
* This file has been copied from the Apache ZooKeeper project.
24+
* @see <a href=
25+
* "https://github.com/apache/zookeeper/blob/c74658d398cdc1d207aa296cb6e20de00faec03e/zookeeper-server/src/main/java/org/apache/zookeeper/common/FileKeyStoreLoaderBuilderProvider.java">Base
26+
* revision</a>
27+
*/
28+
final class FileKeyStoreLoaderBuilderProvider {
29+
/**
30+
* Returns a {@link FileKeyStoreLoader.Builder} that can build a loader which loads keys and certs
31+
* from files of the given {@link KeyStoreFileType}.
32+
* @param type the file type to load keys/certs from.
33+
* @return a new Builder.
34+
*/
35+
static FileKeyStoreLoader.Builder<? extends FileKeyStoreLoader>
36+
getBuilderForKeyStoreFileType(KeyStoreFileType type) {
37+
switch (Objects.requireNonNull(type)) {
38+
case JKS:
39+
return new JKSFileLoader.Builder();
40+
case PEM:
41+
return new PEMFileLoader.Builder();
42+
case PKCS12:
43+
return new PKCS12FileLoader.Builder();
44+
case BCFKS:
45+
return new BCFKSFileLoader.Builder();
46+
default:
47+
throw new AssertionError("Unexpected StoreFileType: " + type.name());
48+
}
49+
}
50+
51+
private FileKeyStoreLoaderBuilderProvider() {
52+
// disabled
53+
}
54+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.hadoop.hbase.io.crypto.tls;
19+
20+
/**
21+
* Implementation of {@link FileKeyStoreLoader} that loads from JKS files.
22+
* <p/>
23+
* This file has been copied from the Apache ZooKeeper project.
24+
* @see <a href=
25+
* "https://github.com/apache/zookeeper/blob/c74658d398cdc1d207aa296cb6e20de00faec03e/zookeeper-server/src/main/java/org/apache/zookeeper/common/JKSFileLoader.java">Base
26+
* revision</a>
27+
*/
28+
final class JKSFileLoader extends StandardTypeFileKeyStoreLoader {
29+
private JKSFileLoader(String keyStorePath, String trustStorePath, char[] keyStorePassword,
30+
char[] trustStorePassword) {
31+
super(keyStorePath, trustStorePath, keyStorePassword, trustStorePassword,
32+
SupportedStandardKeyFormat.JKS);
33+
}
34+
35+
static class Builder extends FileKeyStoreLoader.Builder<JKSFileLoader> {
36+
@Override
37+
JKSFileLoader build() {
38+
return new JKSFileLoader(keyStorePath, trustStorePath, keyStorePassword, trustStorePassword);
39+
}
40+
}
41+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.hadoop.hbase.io.crypto.tls;
19+
20+
import java.io.IOException;
21+
import java.security.GeneralSecurityException;
22+
import java.security.KeyStore;
23+
24+
/**
25+
* An interface for an object that can load key stores or trust stores.
26+
* <p/>
27+
* This file has been copied from the Apache ZooKeeper project.
28+
* @see <a href=
29+
* "https://github.com/apache/zookeeper/blob/c74658d398cdc1d207aa296cb6e20de00faec03e/zookeeper-server/src/main/java/org/apache/zookeeper/common/KeyStoreLoader.java">Base
30+
* revision</a>
31+
*/
32+
interface KeyStoreLoader {
33+
/**
34+
* Loads a KeyStore which contains at least one private key and the associated X509 cert chain.
35+
* @return a new KeyStore
36+
* @throws IOException if loading the key store fails due to an IO error, such as
37+
* "file not found".
38+
* @throws GeneralSecurityException if loading the key store fails due to a security error, such
39+
* as "unsupported crypto algorithm".
40+
*/
41+
KeyStore loadKeyStore() throws IOException, GeneralSecurityException;
42+
43+
/**
44+
* Loads a KeyStore which contains at least one X509 cert chain for a trusted Certificate
45+
* Authority (CA).
46+
* @return a new KeyStore
47+
* @throws IOException if loading the trust store fails due to an IO error, such as
48+
* "file not found".
49+
* @throws GeneralSecurityException if loading the trust store fails due to a security error, such
50+
* as "unsupported crypto algorithm".
51+
*/
52+
KeyStore loadTrustStore() throws IOException, GeneralSecurityException;
53+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.hadoop.hbase.io.crypto.tls;
19+
20+
import java.io.File;
21+
import java.io.IOException;
22+
import java.security.GeneralSecurityException;
23+
import java.security.KeyStore;
24+
25+
/**
26+
* Implementation of {@link FileKeyStoreLoader} that loads from PEM files.
27+
* <p/>
28+
* This file has been copied from the Apache ZooKeeper project.
29+
* @see <a href=
30+
* "https://github.com/apache/zookeeper/blob/c74658d398cdc1d207aa296cb6e20de00faec03e/zookeeper-server/src/main/java/org/apache/zookeeper/common/PEMFileLoader.java">Base
31+
* revision</a>
32+
*/
33+
final class PEMFileLoader extends FileKeyStoreLoader {
34+
private PEMFileLoader(String keyStorePath, String trustStorePath, char[] keyStorePassword,
35+
char[] trustStorePassword) {
36+
super(keyStorePath, trustStorePath, keyStorePassword, trustStorePassword);
37+
}
38+
39+
@Override
40+
public KeyStore loadKeyStore() throws IOException, GeneralSecurityException {
41+
File file = new File(keyStorePath);
42+
return PemReader.loadKeyStore(file, file, keyStorePassword);
43+
}
44+
45+
@Override
46+
public KeyStore loadTrustStore() throws IOException, GeneralSecurityException {
47+
return PemReader.loadTrustStore(new File(trustStorePath));
48+
}
49+
50+
static class Builder extends FileKeyStoreLoader.Builder<PEMFileLoader> {
51+
@Override
52+
PEMFileLoader build() {
53+
return new PEMFileLoader(keyStorePath, trustStorePath, keyStorePassword, trustStorePassword);
54+
}
55+
}
56+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.hadoop.hbase.io.crypto.tls;
19+
20+
/**
21+
* Implementation of {@link FileKeyStoreLoader} that loads from PKCS12 files.
22+
* <p/>
23+
* This file has been copied from the Apache ZooKeeper project.
24+
* @see <a href=
25+
* "https://github.com/apache/zookeeper/blob/c74658d398cdc1d207aa296cb6e20de00faec03e/zookeeper-server/src/main/java/org/apache/zookeeper/common/PKCS12FileLoader.java">Base
26+
* revision</a>
27+
*/
28+
final class PKCS12FileLoader extends StandardTypeFileKeyStoreLoader {
29+
private PKCS12FileLoader(String keyStorePath, String trustStorePath, char[] keyStorePassword,
30+
char[] trustStorePassword) {
31+
super(keyStorePath, trustStorePath, keyStorePassword, trustStorePassword,
32+
SupportedStandardKeyFormat.PKCS12);
33+
}
34+
35+
static class Builder extends FileKeyStoreLoader.Builder<PKCS12FileLoader> {
36+
@Override
37+
PKCS12FileLoader build() {
38+
return new PKCS12FileLoader(keyStorePath, trustStorePath, keyStorePassword,
39+
trustStorePassword);
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)