Kafka版本升级,开启kerberos问题

版本间的对比如下:

1、kafka-0.10.x等老版本LoginManager.java中采用LoginType

public static final LoginManager acquireLoginManager(LoginType loginType, boolean hasKerberos, Map configs) throws IOException, LoginException {

        synchronized (LoginManager.class) {

            LoginManager loginManager = CACHED_INSTANCES.get(loginType);

            if (loginManager == null) {

                loginManager = new LoginManager(loginType, hasKerberos, configs);

                CACHED_INSTANCES.put(loginType, loginManager);

            }

            return loginManager.acquire();

        }

    }

2、kafka-1.0.0等新版本采用JaasContext jaasContext

public static LoginManager acquireLoginManager(JaasContext jaasContext, boolean hasKerberos,
                                                   Map configs) throws IOException, LoginException {
        synchronized (LoginManager.class) {
            // SASL_JAAS_CONFIG is only supported by clients
            LoginManager loginManager;
            Password jaasConfigValue = (Password) configs.get(SaslConfigs.SASL_JAAS_CONFIG);
            if (jaasContext.type() == JaasContext.Type.CLIENT && jaasConfigValue != null) {
                loginManager = DYNAMIC_INSTANCES.get(jaasConfigValue);
                if (loginManager == null) {
                    loginManager = new LoginManager(jaasContext, hasKerberos, configs, jaasConfigValue);
                    DYNAMIC_INSTANCES.put(jaasConfigValue, loginManager);
                }
            } else {
                loginManager = STATIC_INSTANCES.get(jaasContext.name());
                if (loginManager == null) {
                    loginManager = new LoginManager(jaasContext, hasKerberos, configs, jaasConfigValue);
                    STATIC_INSTANCES.put(jaasContext.name(), loginManager);
                }
            }
            return loginManager.acquire();
        }
    }

因版本间发生变化,因此Ranger对应的kafka-plugin插件代码需要修改,RangerKafkaAuthorizer.java中

@Override
    public void configure(Map configs) {
        RangerBasePlugin me = rangerPlugin;
        if (me == null) {
            synchronized(RangerKafkaAuthorizer.class) {
                me = rangerPlugin;
                if (me == null) {
                    try {
                        // Possible to override JAAS configuration which is used by Ranger, otherwise
                        // SASL_PLAINTEXT is used, which force Kafka to use 'sasl_plaintext.KafkaServer',
                        // if it's not defined, then it reverts to 'KafkaServer' configuration.
                        final Object jaasContext = configs.get("ranger.jaas.context");
                        final String listenerName = (jaasContext instanceof String
                                && StringUtils.isNotEmpty((String) jaasContext)) ? (String) jaasContext
                                        : SecurityProtocol.SASL_PLAINTEXT.name();
                        JaasContext context = JaasContext.load(Type.SERVER, new ListenerName(listenerName), configs);
                        LoginManager loginManager = LoginManager.acquireLoginManager(context, true, configs);
                        Subject subject = loginManager.subject();
                        UserGroupInformation ugi = MiscUtil
                                .createUGIFromSubject(subject);
                        if (ugi != null) {
                            MiscUtil.setUGILoginUser(ugi, subject);
                        }
                        logger.info("LoginUser=" + MiscUtil.getUGILoginUser());
                    } catch (Throwable t) {
                        logger.error("Error getting principal.", t);
                    }
                    me = rangerPlugin = new RangerBasePlugin("kafka", "kafka");
                }
            }
        }
        logger.info("Calling plugin.init()");
        rangerPlugin.init();
        RangerDefaultAuditHandler auditHandler = new RangerDefaultAuditHandler();
        rangerPlugin.setResultProcessor(auditHandler);
    }

你可能感兴趣的:(Kafka版本升级,开启kerberos问题)