2015-10-13 08:19:45 +08:00
|
|
|
# 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.
|
|
|
|
|
2020-02-08 23:46:48 +08:00
|
|
|
import logging
|
2015-10-13 08:19:45 +08:00
|
|
|
import os
|
|
|
|
import subprocess
|
2016-09-01 00:14:59 +08:00
|
|
|
from tempfile import mkdtemp
|
|
|
|
from shutil import rmtree
|
2015-11-04 13:25:15 +08:00
|
|
|
from ducktape.template import TemplateRenderer
|
|
|
|
from kafkatest.services.security.minikdc import MiniKdc
|
2019-06-28 01:10:43 +08:00
|
|
|
from kafkatest.services.security.listener_security_config import ListenerSecurityConfig
|
2016-04-05 09:49:29 +08:00
|
|
|
import itertools
|
2015-10-13 08:19:45 +08:00
|
|
|
|
2016-12-12 10:43:23 +08:00
|
|
|
|
2016-09-01 00:14:59 +08:00
|
|
|
class SslStores(object):
|
2020-02-08 23:46:48 +08:00
|
|
|
def __init__(self, local_scratch_dir, logger=None):
|
|
|
|
self.logger = logger
|
2016-12-12 10:43:23 +08:00
|
|
|
self.ca_crt_path = os.path.join(local_scratch_dir, "test.ca.crt")
|
|
|
|
self.ca_jks_path = os.path.join(local_scratch_dir, "test.ca.jks")
|
2016-09-01 00:14:59 +08:00
|
|
|
self.ca_passwd = "test-ca-passwd"
|
2015-10-13 08:19:45 +08:00
|
|
|
|
2016-12-12 10:43:23 +08:00
|
|
|
self.truststore_path = os.path.join(local_scratch_dir, "test.truststore.jks")
|
2016-09-01 00:14:59 +08:00
|
|
|
self.truststore_passwd = "test-ts-passwd"
|
|
|
|
self.keystore_passwd = "test-ks-passwd"
|
2020-02-08 23:46:48 +08:00
|
|
|
# Zookeeper TLS (as of v3.5.6) does not support a key password different than the keystore password
|
|
|
|
self.key_passwd = self.keystore_passwd
|
2016-09-02 01:05:36 +08:00
|
|
|
# Allow upto one hour of clock skew between host and VMs
|
|
|
|
self.startdate = "-1H"
|
2016-12-12 10:43:23 +08:00
|
|
|
|
|
|
|
for file in [self.ca_crt_path, self.ca_jks_path, self.truststore_path]:
|
|
|
|
if os.path.exists(file):
|
|
|
|
os.remove(file)
|
2016-09-01 00:14:59 +08:00
|
|
|
|
|
|
|
def generate_ca(self):
|
2015-10-13 08:19:45 +08:00
|
|
|
"""
|
2016-09-01 00:14:59 +08:00
|
|
|
Generate CA private key and certificate.
|
2015-10-13 08:19:45 +08:00
|
|
|
"""
|
|
|
|
|
2020-02-17 17:49:35 +08:00
|
|
|
self.runcmd("keytool -genkeypair -alias ca -keyalg RSA -keysize 2048 -keystore %s -storetype JKS -storepass %s -keypass %s -dname CN=SystemTestCA -startdate %s --ext bc=ca:true" % (self.ca_jks_path, self.ca_passwd, self.ca_passwd, self.startdate))
|
2016-09-01 00:14:59 +08:00
|
|
|
self.runcmd("keytool -export -alias ca -keystore %s -storepass %s -storetype JKS -rfc -file %s" % (self.ca_jks_path, self.ca_passwd, self.ca_crt_path))
|
|
|
|
|
|
|
|
def generate_truststore(self):
|
|
|
|
"""
|
|
|
|
Generate JKS truststore containing CA certificate.
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.runcmd("keytool -importcert -alias ca -file %s -keystore %s -storepass %s -storetype JKS -noprompt" % (self.ca_crt_path, self.truststore_path, self.truststore_passwd))
|
|
|
|
|
|
|
|
def generate_and_copy_keystore(self, node):
|
|
|
|
"""
|
|
|
|
Generate JKS keystore with certificate signed by the test CA.
|
|
|
|
The generated certificate has the node's hostname as a DNS SubjectAlternativeName.
|
|
|
|
"""
|
|
|
|
|
|
|
|
ks_dir = mkdtemp(dir="/tmp")
|
|
|
|
ks_path = os.path.join(ks_dir, "test.keystore.jks")
|
|
|
|
csr_path = os.path.join(ks_dir, "test.kafka.csr")
|
|
|
|
crt_path = os.path.join(ks_dir, "test.kafka.crt")
|
|
|
|
|
2017-09-08 09:29:03 +08:00
|
|
|
self.runcmd("keytool -genkeypair -alias kafka -keyalg RSA -keysize 2048 -keystore %s -storepass %s -storetype JKS -keypass %s -dname CN=systemtest -ext SAN=DNS:%s -startdate %s" % (ks_path, self.keystore_passwd, self.key_passwd, self.hostname(node), self.startdate))
|
|
|
|
self.runcmd("keytool -certreq -keystore %s -storepass %s -storetype JKS -keypass %s -alias kafka -file %s" % (ks_path, self.keystore_passwd, self.key_passwd, csr_path))
|
|
|
|
self.runcmd("keytool -gencert -keystore %s -storepass %s -storetype JKS -alias ca -infile %s -outfile %s -dname CN=systemtest -ext SAN=DNS:%s -startdate %s" % (self.ca_jks_path, self.ca_passwd, csr_path, crt_path, self.hostname(node), self.startdate))
|
|
|
|
self.runcmd("keytool -importcert -keystore %s -storepass %s -storetype JKS -alias ca -file %s -noprompt" % (ks_path, self.keystore_passwd, self.ca_crt_path))
|
|
|
|
self.runcmd("keytool -importcert -keystore %s -storepass %s -storetype JKS -keypass %s -alias kafka -file %s -noprompt" % (ks_path, self.keystore_passwd, self.key_passwd, crt_path))
|
2016-12-12 10:43:23 +08:00
|
|
|
node.account.copy_to(ks_path, SecurityConfig.KEYSTORE_PATH)
|
2020-02-08 23:46:48 +08:00
|
|
|
|
|
|
|
# also generate ZooKeeper client TLS config file for mutual authentication use case
|
|
|
|
str = """zookeeper.clientCnxnSocket=org.apache.zookeeper.ClientCnxnSocketNetty
|
|
|
|
zookeeper.ssl.client.enable=true
|
|
|
|
zookeeper.ssl.truststore.location=%s
|
|
|
|
zookeeper.ssl.truststore.password=%s
|
|
|
|
zookeeper.ssl.keystore.location=%s
|
|
|
|
zookeeper.ssl.keystore.password=%s
|
|
|
|
""" % (SecurityConfig.TRUSTSTORE_PATH, self.truststore_passwd, SecurityConfig.KEYSTORE_PATH, self.keystore_passwd)
|
|
|
|
node.account.create_file(SecurityConfig.ZK_CLIENT_MUTUAL_AUTH_CONFIG_PATH, str)
|
|
|
|
|
2016-09-01 00:14:59 +08:00
|
|
|
rmtree(ks_dir)
|
|
|
|
|
|
|
|
def hostname(self, node):
|
|
|
|
""" Hostname which may be overridden for testing validation failures
|
|
|
|
"""
|
|
|
|
return node.account.hostname
|
|
|
|
|
|
|
|
def runcmd(self, cmd):
|
2020-02-08 23:46:48 +08:00
|
|
|
if self.logger:
|
|
|
|
self.logger.log(logging.DEBUG, cmd)
|
2015-10-13 08:19:45 +08:00
|
|
|
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
2016-12-12 10:43:23 +08:00
|
|
|
stdout, stderr = proc.communicate()
|
|
|
|
|
2015-10-13 08:19:45 +08:00
|
|
|
if proc.returncode != 0:
|
2016-12-12 10:43:23 +08:00
|
|
|
raise RuntimeError("Command '%s' returned non-zero exit status %d: %s" % (cmd, proc.returncode, stdout))
|
2015-10-13 08:19:45 +08:00
|
|
|
|
|
|
|
|
2015-11-04 13:25:15 +08:00
|
|
|
class SecurityConfig(TemplateRenderer):
|
2015-10-13 08:19:45 +08:00
|
|
|
|
|
|
|
PLAINTEXT = 'PLAINTEXT'
|
|
|
|
SSL = 'SSL'
|
2015-11-04 13:25:15 +08:00
|
|
|
SASL_PLAINTEXT = 'SASL_PLAINTEXT'
|
|
|
|
SASL_SSL = 'SASL_SSL'
|
|
|
|
SASL_MECHANISM_GSSAPI = 'GSSAPI'
|
|
|
|
SASL_MECHANISM_PLAIN = 'PLAIN'
|
2017-01-17 20:55:07 +08:00
|
|
|
SASL_MECHANISM_SCRAM_SHA_256 = 'SCRAM-SHA-256'
|
|
|
|
SASL_MECHANISM_SCRAM_SHA_512 = 'SCRAM-SHA-512'
|
|
|
|
SCRAM_CLIENT_USER = "kafka-client"
|
|
|
|
SCRAM_CLIENT_PASSWORD = "client-secret"
|
|
|
|
SCRAM_BROKER_USER = "kafka-broker"
|
|
|
|
SCRAM_BROKER_PASSWORD = "broker-secret"
|
2015-11-04 13:25:15 +08:00
|
|
|
CONFIG_DIR = "/mnt/security"
|
|
|
|
KEYSTORE_PATH = "/mnt/security/test.keystore.jks"
|
|
|
|
TRUSTSTORE_PATH = "/mnt/security/test.truststore.jks"
|
2020-02-08 23:46:48 +08:00
|
|
|
ZK_CLIENT_MUTUAL_AUTH_CONFIG_PATH = "/mnt/security/zk_client_mutual_auth_config.properties"
|
2015-11-04 13:25:15 +08:00
|
|
|
JAAS_CONF_PATH = "/mnt/security/jaas.conf"
|
|
|
|
KRB5CONF_PATH = "/mnt/security/krb5.conf"
|
|
|
|
KEYTAB_PATH = "/mnt/security/keytab"
|
2015-10-13 08:19:45 +08:00
|
|
|
|
2016-12-12 10:43:23 +08:00
|
|
|
# This is initialized only when the first instance of SecurityConfig is created
|
|
|
|
ssl_stores = None
|
2015-10-13 08:19:45 +08:00
|
|
|
|
2016-12-12 10:43:23 +08:00
|
|
|
def __init__(self, context, security_protocol=None, interbroker_security_protocol=None,
|
2016-04-30 00:41:12 +08:00
|
|
|
client_sasl_mechanism=SASL_MECHANISM_GSSAPI, interbroker_sasl_mechanism=SASL_MECHANISM_GSSAPI,
|
2020-02-08 23:46:48 +08:00
|
|
|
zk_sasl=False, zk_tls=False, template_props="", static_jaas_conf=True, jaas_override_variables=None,
|
2019-06-28 01:10:43 +08:00
|
|
|
listener_security_config=ListenerSecurityConfig()):
|
2015-10-13 08:19:45 +08:00
|
|
|
"""
|
|
|
|
Initialize the security properties for the node and copy
|
|
|
|
keystore and truststore to the remote node if the transport protocol
|
|
|
|
is SSL. If security_protocol is None, the protocol specified in the
|
|
|
|
template properties file is used. If no protocol is specified in the
|
|
|
|
template properties either, PLAINTEXT is used as default.
|
|
|
|
"""
|
|
|
|
|
2016-12-12 10:43:23 +08:00
|
|
|
self.context = context
|
|
|
|
if not SecurityConfig.ssl_stores:
|
|
|
|
# This generates keystore/trustore files in a local scratch directory which gets
|
|
|
|
# automatically destroyed after the test is run
|
|
|
|
# Creating within the scratch directory allows us to run tests in parallel without fear of collision
|
2020-02-08 23:46:48 +08:00
|
|
|
SecurityConfig.ssl_stores = SslStores(context.local_scratch_dir, context.logger)
|
2016-12-12 10:43:23 +08:00
|
|
|
SecurityConfig.ssl_stores.generate_ca()
|
|
|
|
SecurityConfig.ssl_stores.generate_truststore()
|
|
|
|
|
2015-10-13 08:19:45 +08:00
|
|
|
if security_protocol is None:
|
|
|
|
security_protocol = self.get_property('security.protocol', template_props)
|
|
|
|
if security_protocol is None:
|
|
|
|
security_protocol = SecurityConfig.PLAINTEXT
|
2015-11-04 13:25:15 +08:00
|
|
|
elif security_protocol not in [SecurityConfig.PLAINTEXT, SecurityConfig.SSL, SecurityConfig.SASL_PLAINTEXT, SecurityConfig.SASL_SSL]:
|
2015-10-13 08:19:45 +08:00
|
|
|
raise Exception("Invalid security.protocol in template properties: " + security_protocol)
|
|
|
|
|
2015-11-04 13:25:15 +08:00
|
|
|
if interbroker_security_protocol is None:
|
|
|
|
interbroker_security_protocol = security_protocol
|
|
|
|
self.interbroker_security_protocol = interbroker_security_protocol
|
2015-12-04 09:47:44 +08:00
|
|
|
self.has_sasl = self.is_sasl(security_protocol) or self.is_sasl(interbroker_security_protocol) or zk_sasl
|
2020-02-08 23:46:48 +08:00
|
|
|
self.has_ssl = self.is_ssl(security_protocol) or self.is_ssl(interbroker_security_protocol) or zk_tls
|
2015-12-04 09:47:44 +08:00
|
|
|
self.zk_sasl = zk_sasl
|
2020-02-08 23:46:48 +08:00
|
|
|
self.zk_tls = zk_tls
|
2017-01-18 02:42:55 +08:00
|
|
|
self.static_jaas_conf = static_jaas_conf
|
2019-06-28 01:10:43 +08:00
|
|
|
self.listener_security_config = listener_security_config
|
2015-10-13 08:19:45 +08:00
|
|
|
self.properties = {
|
|
|
|
'security.protocol' : security_protocol,
|
|
|
|
'ssl.keystore.location' : SecurityConfig.KEYSTORE_PATH,
|
2016-09-01 00:14:59 +08:00
|
|
|
'ssl.keystore.password' : SecurityConfig.ssl_stores.keystore_passwd,
|
|
|
|
'ssl.key.password' : SecurityConfig.ssl_stores.key_passwd,
|
2015-10-13 08:19:45 +08:00
|
|
|
'ssl.truststore.location' : SecurityConfig.TRUSTSTORE_PATH,
|
2016-09-01 00:14:59 +08:00
|
|
|
'ssl.truststore.password' : SecurityConfig.ssl_stores.truststore_passwd,
|
|
|
|
'ssl.endpoint.identification.algorithm' : 'HTTPS',
|
2016-04-30 00:41:12 +08:00
|
|
|
'sasl.mechanism' : client_sasl_mechanism,
|
|
|
|
'sasl.mechanism.inter.broker.protocol' : interbroker_sasl_mechanism,
|
2015-11-04 13:25:15 +08:00
|
|
|
'sasl.kerberos.service.name' : 'kafka'
|
2015-10-13 08:19:45 +08:00
|
|
|
}
|
2019-06-28 01:10:43 +08:00
|
|
|
self.properties.update(self.listener_security_config.client_listener_overrides)
|
2018-08-24 05:55:40 +08:00
|
|
|
self.jaas_override_variables = jaas_override_variables or {}
|
2015-11-04 13:25:15 +08:00
|
|
|
|
2018-08-24 05:55:40 +08:00
|
|
|
def client_config(self, template_props="", node=None, jaas_override_variables=None):
|
2017-01-18 02:42:55 +08:00
|
|
|
# If node is not specified, use static jaas config which will be created later.
|
|
|
|
# Otherwise use static JAAS configuration files with SASL_SSL and sasl.jaas.config
|
|
|
|
# property with SASL_PLAINTEXT so that both code paths are tested by existing tests.
|
|
|
|
# Note that this is an artibtrary choice and it is possible to run all tests with
|
|
|
|
# either static or dynamic jaas config files if required.
|
|
|
|
static_jaas_conf = node is None or (self.has_sasl and self.has_ssl)
|
2018-08-24 05:55:40 +08:00
|
|
|
return SecurityConfig(self.context, self.security_protocol,
|
|
|
|
client_sasl_mechanism=self.client_sasl_mechanism,
|
|
|
|
template_props=template_props,
|
|
|
|
static_jaas_conf=static_jaas_conf,
|
2019-06-28 01:10:43 +08:00
|
|
|
jaas_override_variables=jaas_override_variables,
|
|
|
|
listener_security_config=self.listener_security_config)
|
2015-11-04 13:25:15 +08:00
|
|
|
|
2017-02-25 01:24:07 +08:00
|
|
|
def enable_security_protocol(self, security_protocol):
|
|
|
|
self.has_sasl = self.has_sasl or self.is_sasl(security_protocol)
|
|
|
|
self.has_ssl = self.has_ssl or self.is_ssl(security_protocol)
|
|
|
|
|
2016-07-01 12:16:13 +08:00
|
|
|
def setup_ssl(self, node):
|
|
|
|
node.account.ssh("mkdir -p %s" % SecurityConfig.CONFIG_DIR, allow_fail=False)
|
2016-12-12 10:43:23 +08:00
|
|
|
node.account.copy_to(SecurityConfig.ssl_stores.truststore_path, SecurityConfig.TRUSTSTORE_PATH)
|
2016-09-01 00:14:59 +08:00
|
|
|
SecurityConfig.ssl_stores.generate_and_copy_keystore(node)
|
2016-07-01 12:16:13 +08:00
|
|
|
|
|
|
|
def setup_sasl(self, node):
|
|
|
|
node.account.ssh("mkdir -p %s" % SecurityConfig.CONFIG_DIR, allow_fail=False)
|
|
|
|
jaas_conf_file = "jaas.conf"
|
|
|
|
java_version = node.account.ssh_capture("java -version")
|
2018-08-24 05:55:40 +08:00
|
|
|
|
2019-06-28 01:10:43 +08:00
|
|
|
jaas_conf = None
|
|
|
|
if 'sasl.jaas.config' not in self.properties:
|
|
|
|
jaas_conf = self.render_jaas_config(
|
|
|
|
jaas_conf_file,
|
|
|
|
{
|
|
|
|
'node': node,
|
|
|
|
'is_ibm_jdk': any('IBM' in line for line in java_version),
|
|
|
|
'SecurityConfig': SecurityConfig,
|
|
|
|
'client_sasl_mechanism': self.client_sasl_mechanism,
|
|
|
|
'enabled_sasl_mechanisms': self.enabled_sasl_mechanisms
|
|
|
|
}
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
jaas_conf = self.properties['sasl.jaas.config']
|
2018-08-24 05:55:40 +08:00
|
|
|
|
2017-01-18 02:42:55 +08:00
|
|
|
if self.static_jaas_conf:
|
|
|
|
node.account.create_file(SecurityConfig.JAAS_CONF_PATH, jaas_conf)
|
2019-06-28 01:10:43 +08:00
|
|
|
elif 'sasl.jaas.config' not in self.properties:
|
2017-01-18 02:42:55 +08:00
|
|
|
self.properties['sasl.jaas.config'] = jaas_conf.replace("\n", " \\\n")
|
2016-07-01 12:16:13 +08:00
|
|
|
if self.has_sasl_kerberos:
|
2016-12-12 10:43:23 +08:00
|
|
|
node.account.copy_to(MiniKdc.LOCAL_KEYTAB_FILE, SecurityConfig.KEYTAB_PATH)
|
|
|
|
node.account.copy_to(MiniKdc.LOCAL_KRB5CONF_FILE, SecurityConfig.KRB5CONF_PATH)
|
2016-07-01 12:16:13 +08:00
|
|
|
|
2018-08-24 05:55:40 +08:00
|
|
|
def render_jaas_config(self, jaas_conf_file, config_variables):
|
|
|
|
"""
|
|
|
|
Renders the JAAS config file contents
|
|
|
|
|
|
|
|
:param jaas_conf_file: name of the JAAS config template file
|
|
|
|
:param config_variables: dict of variables used in the template
|
|
|
|
:return: the rendered template string
|
|
|
|
"""
|
|
|
|
variables = config_variables.copy()
|
|
|
|
variables.update(self.jaas_override_variables) # override variables
|
|
|
|
return self.render(jaas_conf_file, **variables)
|
|
|
|
|
2015-10-13 08:19:45 +08:00
|
|
|
def setup_node(self, node):
|
2015-11-04 13:25:15 +08:00
|
|
|
if self.has_ssl:
|
2016-07-01 12:16:13 +08:00
|
|
|
self.setup_ssl(node)
|
2015-10-13 08:19:45 +08:00
|
|
|
|
2015-11-04 13:25:15 +08:00
|
|
|
if self.has_sasl:
|
2016-07-01 12:16:13 +08:00
|
|
|
self.setup_sasl(node)
|
2015-11-04 13:25:15 +08:00
|
|
|
|
2017-01-17 20:55:07 +08:00
|
|
|
def setup_credentials(self, node, path, zk_connect, broker):
|
|
|
|
if broker:
|
|
|
|
self.maybe_create_scram_credentials(node, zk_connect, path, self.interbroker_sasl_mechanism,
|
|
|
|
SecurityConfig.SCRAM_BROKER_USER, SecurityConfig.SCRAM_BROKER_PASSWORD)
|
|
|
|
else:
|
|
|
|
self.maybe_create_scram_credentials(node, zk_connect, path, self.client_sasl_mechanism,
|
|
|
|
SecurityConfig.SCRAM_CLIENT_USER, SecurityConfig.SCRAM_CLIENT_PASSWORD)
|
|
|
|
|
|
|
|
def maybe_create_scram_credentials(self, node, zk_connect, path, mechanism, user_name, password):
|
|
|
|
if self.has_sasl and self.is_sasl_scram(mechanism):
|
|
|
|
cmd = "%s --zookeeper %s --entity-name %s --entity-type users --alter --add-config %s=[password=%s]" % \
|
|
|
|
(path.script("kafka-configs.sh", node), zk_connect,
|
|
|
|
user_name, mechanism, password)
|
|
|
|
node.account.ssh(cmd)
|
|
|
|
|
2015-10-13 08:19:45 +08:00
|
|
|
def clean_node(self, node):
|
2015-11-04 13:25:15 +08:00
|
|
|
if self.security_protocol != SecurityConfig.PLAINTEXT:
|
|
|
|
node.account.ssh("rm -rf %s" % SecurityConfig.CONFIG_DIR, allow_fail=False)
|
2015-10-13 08:19:45 +08:00
|
|
|
|
|
|
|
def get_property(self, prop_name, template_props=""):
|
|
|
|
"""
|
|
|
|
Get property value from the string representation of
|
|
|
|
a properties file.
|
|
|
|
"""
|
|
|
|
value = None
|
|
|
|
for line in template_props.split("\n"):
|
|
|
|
items = line.split("=")
|
|
|
|
if len(items) == 2 and items[0].strip() == prop_name:
|
|
|
|
value = str(items[1].strip())
|
|
|
|
return value
|
|
|
|
|
2015-11-04 13:25:15 +08:00
|
|
|
def is_ssl(self, security_protocol):
|
|
|
|
return security_protocol == SecurityConfig.SSL or security_protocol == SecurityConfig.SASL_SSL
|
|
|
|
|
|
|
|
def is_sasl(self, security_protocol):
|
|
|
|
return security_protocol == SecurityConfig.SASL_PLAINTEXT or security_protocol == SecurityConfig.SASL_SSL
|
|
|
|
|
2017-01-17 20:55:07 +08:00
|
|
|
def is_sasl_scram(self, sasl_mechanism):
|
|
|
|
return sasl_mechanism == SecurityConfig.SASL_MECHANISM_SCRAM_SHA_256 or sasl_mechanism == SecurityConfig.SASL_MECHANISM_SCRAM_SHA_512
|
|
|
|
|
2015-10-13 08:19:45 +08:00
|
|
|
@property
|
|
|
|
def security_protocol(self):
|
|
|
|
return self.properties['security.protocol']
|
|
|
|
|
2015-11-04 13:25:15 +08:00
|
|
|
@property
|
2016-04-30 00:41:12 +08:00
|
|
|
def client_sasl_mechanism(self):
|
2015-11-04 13:25:15 +08:00
|
|
|
return self.properties['sasl.mechanism']
|
|
|
|
|
2016-04-30 00:41:12 +08:00
|
|
|
@property
|
|
|
|
def interbroker_sasl_mechanism(self):
|
|
|
|
return self.properties['sasl.mechanism.inter.broker.protocol']
|
|
|
|
|
|
|
|
@property
|
|
|
|
def enabled_sasl_mechanisms(self):
|
|
|
|
return set([self.client_sasl_mechanism, self.interbroker_sasl_mechanism])
|
|
|
|
|
2015-11-04 13:25:15 +08:00
|
|
|
@property
|
|
|
|
def has_sasl_kerberos(self):
|
2016-04-30 00:41:12 +08:00
|
|
|
return self.has_sasl and (SecurityConfig.SASL_MECHANISM_GSSAPI in self.enabled_sasl_mechanisms)
|
2015-11-04 13:25:15 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def kafka_opts(self):
|
|
|
|
if self.has_sasl:
|
2017-01-18 02:42:55 +08:00
|
|
|
if self.static_jaas_conf:
|
|
|
|
return "\"-Djava.security.auth.login.config=%s -Djava.security.krb5.conf=%s\"" % (SecurityConfig.JAAS_CONF_PATH, SecurityConfig.KRB5CONF_PATH)
|
|
|
|
else:
|
|
|
|
return "\"-Djava.security.krb5.conf=%s\"" % SecurityConfig.KRB5CONF_PATH
|
2015-11-04 13:25:15 +08:00
|
|
|
else:
|
|
|
|
return ""
|
|
|
|
|
2016-04-05 09:49:29 +08:00
|
|
|
def props(self, prefix=''):
|
2015-10-13 08:19:45 +08:00
|
|
|
"""
|
2016-04-05 09:49:29 +08:00
|
|
|
Return properties as string with line separators, optionally with a prefix.
|
2015-10-13 08:19:45 +08:00
|
|
|
This is used to append security config properties to
|
|
|
|
a properties file.
|
2016-04-05 09:49:29 +08:00
|
|
|
:param prefix: prefix to add to each property
|
|
|
|
:return: a string containing line-separated properties
|
2015-10-13 08:19:45 +08:00
|
|
|
"""
|
2016-04-05 09:49:29 +08:00
|
|
|
if self.security_protocol == SecurityConfig.PLAINTEXT:
|
|
|
|
return ""
|
2017-01-18 02:42:55 +08:00
|
|
|
if self.has_sasl and not self.static_jaas_conf and 'sasl.jaas.config' not in self.properties:
|
|
|
|
raise Exception("JAAS configuration property has not yet been initialized")
|
2016-04-05 09:49:29 +08:00
|
|
|
config_lines = (prefix + key + "=" + value for key, value in self.properties.iteritems())
|
|
|
|
# Extra blank lines ensure this can be appended/prepended safely
|
|
|
|
return "\n".join(itertools.chain([""], config_lines, [""]))
|
2015-10-13 08:19:45 +08:00
|
|
|
|
2016-04-05 09:49:29 +08:00
|
|
|
def __str__(self):
|
|
|
|
"""
|
|
|
|
Return properties as a string with line separators.
|
|
|
|
"""
|
|
|
|
return self.props()
|