jyothsnakonisa commented on code in PR #2372: URL: https://github.com/apache/cassandra/pull/2372#discussion_r1230162320
########## src/java/org/apache/cassandra/auth/MutualTlsInternodeAuthenticator.java: ########## @@ -0,0 +1,186 @@ +/* + * 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.cassandra.auth; + +import java.io.InputStream; +import java.net.InetAddress; +import java.nio.file.Files; +import java.nio.file.Paths; // checkstyle: permit this import +import java.security.KeyStore; +import java.security.cert.Certificate; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Enumeration; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +import com.google.common.annotations.VisibleForTesting; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.cassandra.config.Config; +import org.apache.cassandra.config.DatabaseDescriptor; +import org.apache.cassandra.config.EncryptionOptions; +import org.apache.cassandra.config.ParameterizedClass; +import org.apache.cassandra.exceptions.AuthenticationException; +import org.apache.cassandra.exceptions.ConfigurationException; +import org.apache.cassandra.utils.NoSpamLogger; + +/* + * Performs mTLS authentication for internode connections by extracting identities from outbound certificates + * configured for the cassandra instance. + * + * If the certificate identity is same as the identity present in the outbound keystore, the user is authorized + * Otherwise, the connection will fail. A bounce is not required when a new node is added or removed. + * + */ +public class MutualTlsInternodeAuthenticator implements IInternodeAuthenticator +{ + private static final String VALIDATOR_CLASS_NAME = "validator_class_name"; + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + private final NoSpamLogger noSpamLogger = NoSpamLogger.getLogger(logger, 30L, TimeUnit.SECONDS); + private final MutualTlsCertificateValidator certificateValidator; + private final List<String> validIdentities; + + public MutualTlsInternodeAuthenticator(Map<String, String> parameters) + { + String certificateValidatorClassName = parameters.get(VALIDATOR_CLASS_NAME); + if (StringUtils.isEmpty(certificateValidatorClassName)) + { + String message = "internode_authenticator.parameters.validator_class_name is not set"; + logger.error(message); + throw new ConfigurationException(message); + } + + certificateValidator = ParameterizedClass.newInstance(new ParameterizedClass(certificateValidatorClassName), + Arrays.asList("", AuthConfig.class.getPackage().getName())); + Config config = DatabaseDescriptor.getRawConfig(); + checkInternodeMtlsConfigurationIsValid(config); + validIdentities = getIdentitiesFromKeyStore(config.server_encryption_options.outbound_keystore, + config.server_encryption_options.outbound_keystore_password, + config.server_encryption_options.store_type); + + if (!validIdentities.isEmpty()) + { + logger.info("Initializing internode authenticator with identities {}", validIdentities); + } + else + { + String message = String.format("No identity was extracted from the outbound keystore '%s'", config.server_encryption_options.outbound_keystore); + logger.info(message); + throw new ConfigurationException(message); + } + } + + @Override + public boolean authenticate(InetAddress remoteAddress, int remotePort) + { + throw new UnsupportedOperationException("mTLS Authenticator only supports certificate based authenticate method"); + } + + @Override + public boolean authenticate(InetAddress remoteAddress, int remotePort, Certificate[] certificates, InternodeConnectionDirection connectionType) + { + return authenticateInternodeWithMtls(remoteAddress, remotePort, certificates, connectionType); + } + + + @Override + public void validateConfiguration() throws ConfigurationException + { + + } + + protected boolean authenticateInternodeWithMtls(InetAddress remoteAddress, int remotePort, Certificate[] certificates, + IInternodeAuthenticator.InternodeConnectionDirection connectionType) + { + if (connectionType == IInternodeAuthenticator.InternodeConnectionDirection.INBOUND) + { + String identity = certificateValidator.identity(certificates); + if (!certificateValidator.isValidCertificate(certificates)) + { + noSpamLogger.error("Not a valid outbound certificate {}", identity); Review Comment: We are authenticating for inbound connection and validating an outbound certificate. So I think the log message should have outbound certificate. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

