Skip to content

Commit 577e033

Browse files
vivekratnavelxiaoyuyao
authored andcommitted
HDDS-1946. CertificateClient should not persist keys/certs to ozone.m… (#1311)
(cherry picked from commit b1eee8b)
1 parent 708f031 commit 577e033

File tree

15 files changed

+241
-193
lines changed

15 files changed

+241
-193
lines changed

hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/XceiverClientGrpc.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
*/
7171
public class XceiverClientGrpc extends XceiverClientSpi {
7272
static final Logger LOG = LoggerFactory.getLogger(XceiverClientGrpc.class);
73+
private static final String COMPONENT = "dn";
7374
private final Pipeline pipeline;
7475
private final Configuration config;
7576
private Map<UUID, XceiverClientProtocolServiceStub> asyncStubs;
@@ -150,9 +151,9 @@ private void connectToDatanode(DatanodeDetails dn, String encodedToken)
150151
.intercept(new ClientCredentialInterceptor(userName, encodedToken),
151152
new GrpcClientInterceptor());
152153
if (secConfig.isGrpcTlsEnabled()) {
153-
File trustCertCollectionFile = secConfig.getTrustStoreFile();
154-
File privateKeyFile = secConfig.getClientPrivateKeyFile();
155-
File clientCertChainFile = secConfig.getClientCertChainFile();
154+
File trustCertCollectionFile = secConfig.getTrustStoreFile(COMPONENT);
155+
File privateKeyFile = secConfig.getClientPrivateKeyFile(COMPONENT);
156+
File clientCertChainFile = secConfig.getClientCertChainFile(COMPONENT);
156157

157158
SslContextBuilder sslContextBuilder = GrpcSslContexts.forClient();
158159
if (trustCertCollectionFile != null) {

hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/security/x509/SecurityConfig.java

Lines changed: 68 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.apache.hadoop.hdds.security.x509;
2121

2222
import com.google.common.base.Preconditions;
23+
import org.apache.commons.lang3.StringUtils;
2324
import org.apache.hadoop.conf.Configuration;
2425
import org.apache.hadoop.ozone.OzoneConfigKeys;
2526
import org.apache.ratis.thirdparty.io.netty.handler.ssl.SslProvider;
@@ -246,23 +247,12 @@ public String getPrivateKeyFileName() {
246247
return privateKeyFileName;
247248
}
248249

249-
/**
250-
* Returns the File path to where keys are stored.
251-
*
252-
* @return path Key location.
253-
*/
254-
public Path getKeyLocation() {
255-
Preconditions.checkNotNull(this.metadatDir, "Metadata directory can't be"
256-
+ " null. Please check configs.");
257-
return Paths.get(metadatDir, keyDir);
258-
}
259-
260250
/**
261251
* Returns the File path to where keys are stored with an additional component
262252
* name inserted in between.
263253
*
264254
* @param component - Component Name - String.
265-
* @return Path location.
255+
* @return Path Key location.
266256
*/
267257
public Path getKeyLocation(String component) {
268258
Preconditions.checkNotNull(this.metadatDir, "Metadata directory can't be"
@@ -271,18 +261,8 @@ public Path getKeyLocation(String component) {
271261
}
272262

273263
/**
274-
* Returns the File path to where keys are stored.
275-
*
276-
* @return path Key location.
277-
*/
278-
public Path getCertificateLocation() {
279-
Preconditions.checkNotNull(this.metadatDir, "Metadata directory can't be"
280-
+ " null. Please check configs.");
281-
return Paths.get(metadatDir, certificateDir);
282-
}
283-
284-
/**
285-
* Returns the File path to where keys are stored with an addition component
264+
* Returns the File path to where certificates are stored with an addition
265+
* component
286266
* name inserted in between.
287267
*
288268
* @param component - Component Name - String.
@@ -379,31 +359,75 @@ public boolean isGrpcMutualTlsRequired() {
379359
return this.grpcMutualTlsRequired;
380360
}
381361

362+
/**
363+
* Returns the TLS-enabled gRPC client private key file(Only needed for mutual
364+
* authentication) for the given component.
365+
* @param component name of the component.
366+
* @return the TLS-enabled gRPC client private key file.
367+
*/
368+
public File getClientPrivateKeyFile(String component) {
369+
return Paths.get(getKeyLocation(component).toString(),
370+
"client." + privateKeyFileName).toFile();
371+
}
372+
382373
/**
383374
* Returns the TLS-enabled gRPC client private key file(Only needed for mutual
384375
* authentication).
385376
* @return the TLS-enabled gRPC client private key file.
386377
*/
387378
public File getClientPrivateKeyFile() {
388-
return Paths.get(getKeyLocation().toString(),
389-
"client." + privateKeyFileName).toFile();
379+
return getClientPrivateKeyFile(StringUtils.EMPTY);
380+
}
381+
382+
/**
383+
* Returns the TLS-enabled gRPC server private key file for the given
384+
* component.
385+
* @param component name of the component.
386+
* @return the TLS-enabled gRPC server private key file.
387+
*/
388+
public File getServerPrivateKeyFile(String component) {
389+
return Paths.get(getKeyLocation(component).toString(),
390+
"server." + privateKeyFileName).toFile();
390391
}
391392

392393
/**
393394
* Returns the TLS-enabled gRPC server private key file.
394395
* @return the TLS-enabled gRPC server private key file.
395396
*/
396397
public File getServerPrivateKeyFile() {
397-
return Paths.get(getKeyLocation().toString(),
398-
"server." + privateKeyFileName).toFile();
398+
return getServerPrivateKeyFile(StringUtils.EMPTY);
399+
}
400+
401+
/**
402+
* Get the trusted CA certificate file for the given component. (CA
403+
* certificate)
404+
* @param component name of the component.
405+
* @return the trusted CA certificate.
406+
*/
407+
public File getTrustStoreFile(String component) {
408+
return Paths.get(getKeyLocation(component).toString(),
409+
trustStoreFileName).
410+
toFile();
399411
}
400412

401413
/**
402414
* Get the trusted CA certificate file. (CA certificate)
403415
* @return the trusted CA certificate.
404416
*/
405417
public File getTrustStoreFile() {
406-
return Paths.get(getKeyLocation().toString(), trustStoreFileName).
418+
return getTrustStoreFile(StringUtils.EMPTY);
419+
}
420+
421+
/**
422+
* Get the TLS-enabled gRPC Client certificate chain file for the given
423+
* component (only needed for
424+
* mutual authentication).
425+
* @param component name of the component.
426+
* @return the TLS-enabled gRPC Server certificate chain file.
427+
*/
428+
public File getClientCertChainFile(String component) {
429+
return Paths.get(getKeyLocation(component).toString(),
430+
clientCertChainFileName).
407431
toFile();
408432
}
409433

@@ -413,7 +437,18 @@ public File getTrustStoreFile() {
413437
* @return the TLS-enabled gRPC Server certificate chain file.
414438
*/
415439
public File getClientCertChainFile() {
416-
return Paths.get(getKeyLocation().toString(), clientCertChainFileName).
440+
return getClientCertChainFile(StringUtils.EMPTY);
441+
}
442+
443+
/**
444+
* Get the TLS-enabled gRPC Server certificate chain file for the given
445+
* component.
446+
* @param component name of the component.
447+
* @return the TLS-enabled gRPC Server certificate chain file.
448+
*/
449+
public File getServerCertChainFile(String component) {
450+
return Paths.get(getKeyLocation(component).toString(),
451+
serverCertChainFileName).
417452
toFile();
418453
}
419454

@@ -422,8 +457,7 @@ public File getClientCertChainFile() {
422457
* @return the TLS-enabled gRPC Server certificate chain file.
423458
*/
424459
public File getServerCertChainFile() {
425-
return Paths.get(getKeyLocation().toString(), serverCertChainFileName).
426-
toFile();
460+
return getServerCertChainFile(StringUtils.EMPTY);
427461
}
428462

429463
/**
@@ -437,7 +471,7 @@ public SslProvider getGrpcSslProvider() {
437471

438472
/**
439473
* Return true if using test certificates with authority as localhost.
440-
* This should be used only for unit test where certifiates are generated
474+
* This should be used only for unit test where certificates are generated
441475
* by openssl with localhost as DN and should never use for production as it
442476
* will bypass the hostname/ip matching verification.
443477
* @return true if using test certificates.
@@ -464,7 +498,7 @@ private Provider initSecurityProvider(String providerName) {
464498

465499
/**
466500
* Returns max date for which S3 tokens will be valid.
467-
* */
501+
*/
468502
public long getS3TokenMaxDate() {
469503
return getConfiguration().getTimeDuration(
470504
OzoneConfigKeys.OZONE_S3_TOKEN_MAX_LIFETIME_KEY,

hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/security/x509/certificate/client/DNCertificateClient.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,24 @@
2525
import org.slf4j.LoggerFactory;
2626

2727
import org.apache.hadoop.hdds.security.x509.SecurityConfig;
28+
2829
/**
2930
* Certificate client for DataNodes.
3031
*/
3132
public class DNCertificateClient extends DefaultCertificateClient {
3233

3334
private static final Logger LOG =
3435
LoggerFactory.getLogger(DNCertificateClient.class);
36+
37+
public static final String COMPONENT_NAME = "dn";
38+
3539
public DNCertificateClient(SecurityConfig securityConfig,
3640
String certSerialId) {
37-
super(securityConfig, LOG, certSerialId);
41+
super(securityConfig, LOG, certSerialId, COMPONENT_NAME);
3842
}
3943

4044
public DNCertificateClient(SecurityConfig securityConfig) {
41-
super(securityConfig, LOG, null);
45+
super(securityConfig, LOG, null, COMPONENT_NAME);
4246
}
4347

4448
/**

hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/security/x509/certificate/client/DefaultCertificateClient.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,18 @@ public abstract class DefaultCertificateClient implements CertificateClient {
8989
private X509Certificate x509Certificate;
9090
private Map<String, X509Certificate> certificateMap;
9191
private String certSerialId;
92+
private String component;
9293

9394

9495
DefaultCertificateClient(SecurityConfig securityConfig, Logger log,
95-
String certSerialId) {
96+
String certSerialId, String component) {
9697
Objects.requireNonNull(securityConfig);
9798
this.securityConfig = securityConfig;
98-
keyCodec = new KeyCodec(securityConfig);
99+
keyCodec = new KeyCodec(securityConfig, component);
99100
this.logger = log;
100101
this.certificateMap = new ConcurrentHashMap<>();
101102
this.certSerialId = certSerialId;
103+
this.component = component;
102104

103105
loadAllCertificates();
104106
}
@@ -108,15 +110,15 @@ public abstract class DefaultCertificateClient implements CertificateClient {
108110
* */
109111
private void loadAllCertificates() {
110112
// See if certs directory exists in file system.
111-
Path certPath = securityConfig.getCertificateLocation();
113+
Path certPath = securityConfig.getCertificateLocation(component);
112114
if (Files.exists(certPath) && Files.isDirectory(certPath)) {
113115
getLogger().info("Loading certificate from location:{}.",
114116
certPath);
115117
File[] certFiles = certPath.toFile().listFiles();
116118

117119
if (certFiles != null) {
118120
CertificateCodec certificateCodec =
119-
new CertificateCodec(securityConfig);
121+
new CertificateCodec(securityConfig, component);
120122
for (File file : certFiles) {
121123
if (file.isFile()) {
122124
try {
@@ -158,7 +160,7 @@ public PrivateKey getPrivateKey() {
158160
return privateKey;
159161
}
160162

161-
Path keyPath = securityConfig.getKeyLocation();
163+
Path keyPath = securityConfig.getKeyLocation(component);
162164
if (OzoneSecurityUtil.checkIfFileExist(keyPath,
163165
securityConfig.getPrivateKeyFileName())) {
164166
try {
@@ -182,7 +184,7 @@ public PublicKey getPublicKey() {
182184
return publicKey;
183185
}
184186

185-
Path keyPath = securityConfig.getKeyLocation();
187+
Path keyPath = securityConfig.getKeyLocation(component);
186188
if (OzoneSecurityUtil.checkIfFileExist(keyPath,
187189
securityConfig.getPublicKeyFileName())) {
188190
try {
@@ -477,9 +479,10 @@ public void storeCertificate(String pemEncodedCert, boolean force)
477479
@Override
478480
public void storeCertificate(String pemEncodedCert, boolean force,
479481
boolean caCert) throws CertificateException {
480-
CertificateCodec certificateCodec = new CertificateCodec(securityConfig);
482+
CertificateCodec certificateCodec = new CertificateCodec(securityConfig,
483+
component);
481484
try {
482-
Path basePath = securityConfig.getCertificateLocation();
485+
Path basePath = securityConfig.getCertificateLocation(component);
483486

484487
X509Certificate cert =
485488
CertificateCodec.getX509Certificate(pemEncodedCert);
@@ -738,7 +741,7 @@ protected boolean validateKeyPair(PublicKey pubKey)
738741
* location.
739742
* */
740743
protected void bootstrapClientKeys() throws CertificateException {
741-
Path keyPath = securityConfig.getKeyLocation();
744+
Path keyPath = securityConfig.getKeyLocation(component);
742745
if (Files.notExists(keyPath)) {
743746
try {
744747
Files.createDirectories(keyPath);

hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/security/x509/certificate/client/OMCertificateClient.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,15 @@ public class OMCertificateClient extends DefaultCertificateClient {
3939
private static final Logger LOG =
4040
LoggerFactory.getLogger(OMCertificateClient.class);
4141

42+
public static final String COMPONENT_NAME = "om";
43+
4244
public OMCertificateClient(SecurityConfig securityConfig,
4345
String certSerialId) {
44-
super(securityConfig, LOG, certSerialId);
46+
super(securityConfig, LOG, certSerialId, COMPONENT_NAME);
4547
}
4648

4749
public OMCertificateClient(SecurityConfig securityConfig) {
48-
super(securityConfig, LOG, null);
50+
super(securityConfig, LOG, null, COMPONENT_NAME);
4951
}
5052

5153
protected InitResponse handleCase(InitCase init) throws

hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/security/x509/certificate/utils/CertificateCodec.java

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919

2020
package org.apache.hadoop.hdds.security.x509.certificate.utils;
2121

22-
import com.google.common.base.Preconditions;
2322
import org.apache.commons.io.IOUtils;
24-
import org.apache.hadoop.conf.Configuration;
2523
import org.apache.hadoop.hdds.security.exception.SCMSecurityException;
2624
import org.apache.hadoop.hdds.security.x509.SecurityConfig;
2725
import org.bouncycastle.cert.X509CertificateHolder;
@@ -70,7 +68,7 @@ public class CertificateCodec {
7068
Stream.of(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE)
7169
.collect(Collectors.toSet());
7270
/**
73-
* Creates an CertificateCodec.
71+
* Creates a CertificateCodec with component name.
7472
*
7573
* @param config - Security Config.
7674
* @param component - Component String.
@@ -80,27 +78,6 @@ public CertificateCodec(SecurityConfig config, String component) {
8078
this.location = securityConfig.getCertificateLocation(component);
8179
}
8280

83-
/**
84-
* Creates an CertificateCodec.
85-
*
86-
* @param config - Security Config.
87-
*/
88-
public CertificateCodec(SecurityConfig config) {
89-
this.securityConfig = config;
90-
this.location = securityConfig.getCertificateLocation();
91-
}
92-
93-
/**
94-
* Creates an CertificateCodec.
95-
*
96-
* @param configuration - Configuration
97-
*/
98-
public CertificateCodec(Configuration configuration) {
99-
Preconditions.checkNotNull(configuration, "Config cannot be null");
100-
this.securityConfig = new SecurityConfig(configuration);
101-
this.location = securityConfig.getCertificateLocation();
102-
}
103-
10481
/**
10582
* Returns a X509 Certificate from the Certificate Holder.
10683
*

0 commit comments

Comments
 (0)