|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.hudi.cli.commands; |
| 20 | + |
| 21 | +import org.apache.hadoop.conf.Configuration; |
| 22 | +import org.apache.hadoop.security.UserGroupInformation; |
| 23 | +import org.springframework.shell.standard.ShellComponent; |
| 24 | +import org.springframework.shell.standard.ShellMethod; |
| 25 | +import org.springframework.shell.standard.ShellOption; |
| 26 | + |
| 27 | +import java.io.IOException; |
| 28 | + |
| 29 | +/** |
| 30 | + * CLI command to perform Kerberos authentication. |
| 31 | + */ |
| 32 | +@ShellComponent |
| 33 | +public class KerberosAuthenticationCommand { |
| 34 | + |
| 35 | + @ShellMethod(key = "kerberos kinit", value = "Perform Kerberos authentication") |
| 36 | + public String performKerberosAuthentication( |
| 37 | + @ShellOption(value = "--krb5conf", help = "Path to krb5.conf", defaultValue = "/etc/krb5.conf") String krb5ConfPath, |
| 38 | + @ShellOption(value = "--principal", help = "Kerberos principal") String principal, |
| 39 | + @ShellOption(value = "--keytab", help = "Path to keytab") String keytabPath) throws IOException { |
| 40 | + |
| 41 | + System.out.println("Perform Kerberos authentication"); |
| 42 | + System.out.println("Parameters:"); |
| 43 | + System.out.println("--krb5conf: " + krb5ConfPath); |
| 44 | + System.out.println("--principal: " + principal); |
| 45 | + System.out.println("--keytab: " + keytabPath); |
| 46 | + |
| 47 | + System.setProperty("java.security.krb5.conf", krb5ConfPath); |
| 48 | + Configuration conf = new Configuration(); |
| 49 | + conf.set("hadoop.security.authentication", "kerberos"); |
| 50 | + conf.set("keytab.file", keytabPath); |
| 51 | + conf.set("kerberos.principal", principal); |
| 52 | + UserGroupInformation.setConfiguration(conf); |
| 53 | + UserGroupInformation.loginUserFromKeytab(principal, keytabPath); |
| 54 | + |
| 55 | + System.out.println("Kerberos current user: " + UserGroupInformation.getCurrentUser()); |
| 56 | + System.out.println("Kerberos login user: " + UserGroupInformation.getLoginUser()); |
| 57 | + |
| 58 | + return "Kerberos authentication success"; |
| 59 | + } |
| 60 | +} |
0 commit comments