Ranger源码修改(1) -- ranger plugin无法

2021-08-09  本文已影响0人  _Kantin

背景介绍

{"httpStatusCode":401,"statusCode":401,"msgDesc":"Authentication Failed"}

问题处理

//新增了是否使用安全模式的标志位(false表示不关闭认证)
    private boolean   useSecureModeForPolicyDownload = false;
//新增一般认证方法
    protected boolean shouldUseSecureMode(UserGroupInformation user) {
        return useSecureModeForPolicyDownload && user != null && 
                UserGroupInformation.isSecurityEnabled();
    }
//将此类中全部关于鉴权的进行修改 
    boolean isSecureMode = user != null && UserGroupInformation.isSecurityEnabled();
//改为
    boolean isSecureMode = shouldUseSecureMode(user);
package com.xxxxxx.kantlin.kerberos;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        params();
    }
    public static void params() {
        //client principal!最好和service principal的一致
        String user = "xxxxxx/hive2@<realm>";
        //client的keytab文件,没有的话先kinit一下
        String keytab = "C:\\\\Users\\\\xxxxx\\\\Desktop\\\\keytab\\\\hive2.keytab";
        //kerberos krb5文件(ps:一般机器上都有,找不到的话执行命令:find / -name krb5.conf)
        String krb5Location = "C:\\Users\\xxxxx\\Desktop\\keytab\\krb5.conf";
        try {
            //此处的true为将kerberos设置为debug模式,将打印更多详细日志
            RequestKerberosUrlUtils restTest = new RequestKerberosUrlUtils(user, keytab, krb5Location, true);
            //此处为开启了kerberos http认证的ranger admin 为例,url中带secure表示运行在高安全模式
            //特别重要!!这里请求的hostname一定要和principal的FQDN(hive2)一样,并配置好相关的hosts,千万不能用ip(切记kerberos都是域名)
            String url_liststatus = "http://hive2:6080/service/plugins/secure/policies/download/hive2";
            HttpResponse response = restTest.callRestUrl(url_liststatus, user);
            InputStream is = response.getEntity().getContent();
            System.out.println("Status code " + response.getStatusLine().getStatusCode());
            System.out.println("message is :" + Arrays.deepToString(response.getAllHeaders()));
            System.out.println("string:\n" + new String(IOUtils.toByteArray(is), StandardCharsets.UTF_8));
        } catch (Exception exp) {
            exp.printStackTrace();
        }
    }
}

package com.xxxxxx.kantlin.kerberos;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthSchemeProvider;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.AuthSchemes;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.config.Lookup;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.impl.auth.SPNegoSchemeFactory;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.security.auth.Subject;
import javax.security.auth.kerberos.KerberosPrincipal;
import javax.security.auth.login.AppConfigurationEntry;
import javax.security.auth.login.Configuration;
import javax.security.auth.login.LoginContext;
import java.io.IOException;
import java.security.Principal;
import java.security.PrivilegedAction;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public class RequestKerberosUrlUtils {
    private String principal;
    private String keyTabLocation;

    public RequestKerberosUrlUtils() {
    }

    public RequestKerberosUrlUtils(String principal, String keyTabLocation) {
        super();
        this.principal = principal;
        this.keyTabLocation = keyTabLocation;
    }

    public RequestKerberosUrlUtils(String principal, String keyTabLocation, boolean isDebug) {
        this(principal, keyTabLocation);
        if (isDebug) {
            System.setProperty("sun.security.spnego.debug", "true");
            System.setProperty("sun.security.krb5.debug", "true");
        }
    }

    public RequestKerberosUrlUtils(String principal, String keyTabLocation, String krb5Location, boolean isDebug) {
        this(principal, keyTabLocation, isDebug);
        System.setProperty("java.security.krb5.conf", krb5Location);
    }

    private static HttpClient buildSpengoHttpClient() {
        HttpClientBuilder builder = HttpClientBuilder.create();
        Lookup<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create().
                register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory(true)).build();
        builder.setDefaultAuthSchemeRegistry(authSchemeRegistry);
        BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(new AuthScope(null, -1, null), new Credentials() {
            @Override
            public Principal getUserPrincipal() {
                return null;
            }
            @Override
            public String getPassword() {
                return null;
            }
        });
        builder.setDefaultCredentialsProvider(credentialsProvider);
        CloseableHttpClient httpClient = builder.build();
        return httpClient;
    }

    public HttpResponse callRestUrl(final String url, final String userId) {
        System.out.println(String.format("Calling KerberosHttpClient %s %s %s", this.principal, this.keyTabLocation, url));
        Configuration config = new Configuration() {
            @SuppressWarnings("serial")
            @Override
            public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
                return new AppConfigurationEntry[]{new AppConfigurationEntry("com.sun.security.auth.module.Krb5LoginModule",
                        AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, new HashMap<String, Object>() {
                    {
                        //Krb5 in GSS API needs to be refreshed so it does not throw the error
                        //Specified version of key is not available
                        put("useTicketCache", "false");
                        put("useKeyTab", "true");
                        put("keyTab", keyTabLocation);
                        put("refreshKrb5Config", "true");
                        put("principal", principal);
                        put("storeKey", "true");
                        put("doNotPrompt", "true");
                        put("isInitiator", "true");
                        put("debug", "true");
                    }
                })};
            }
        };
        Set<Principal> princ = new HashSet<Principal>(1);
        princ.add(new KerberosPrincipal(userId));
        Subject sub = new Subject(false, princ, new HashSet<Object>(), new HashSet<Object>());
        try {
            //指定认证模块为Krb5Login
            LoginContext lc = new LoginContext("Krb5Login", sub, null, config);
            //请求kdc进行client身份认证,如果能通过的话,则可以从TGT获取ticker作为后面二次访问时Authorization: Negotiate的基础
            lc.login();
           //无报错则表示client身份认证通过,此时可以在Subject对象中看到已经获取了kerberos的ticker
            Subject serviceSubject = lc.getSubject();
            return Subject.doAs(serviceSubject, new PrivilegedAction<HttpResponse>() {
                HttpResponse httpResponse = null;
                @Override
                public HttpResponse run() {
                    try {
                        HttpUriRequest request = new HttpGet(url);
                        //根据刚刚获取的kerberos的ticker构建Spengo请求,会经历一次401后再带上Negotiate二次请求
                        HttpClient spnegoHttpClient = buildSpengoHttpClient();
                       //返回的为二次响应后的结果
                        httpResponse = spnegoHttpClient.execute(request);
                        return httpResponse;
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    }
                    return httpResponse;
                }
            });
        } catch (Exception le) {
            le.printStackTrace();
        }
        return null;
    }
}
上一篇下一篇

猜你喜欢

热点阅读