|
20 | 20 |
|
21 | 21 | package org.apache.seatunnel.engine.imap.storage.file.config;
|
22 | 22 |
|
| 23 | +import org.apache.seatunnel.engine.imap.storage.api.exception.IMapStorageException; |
| 24 | + |
| 25 | +import org.apache.commons.lang3.StringUtils; |
23 | 26 | import org.apache.hadoop.conf.Configuration;
|
| 27 | +import org.apache.hadoop.fs.Path; |
| 28 | +import org.apache.hadoop.security.UserGroupInformation; |
24 | 29 |
|
| 30 | +import java.io.IOException; |
25 | 31 | import java.util.Map;
|
26 | 32 |
|
27 |
| -import static org.apache.hadoop.fs.CommonConfigurationKeysPublic.FS_DEFAULT_NAME_DEFAULT; |
28 | 33 | import static org.apache.hadoop.fs.FileSystem.FS_DEFAULT_NAME_KEY;
|
29 | 34 |
|
30 | 35 | public class HdfsConfiguration extends AbstractConfiguration {
|
31 | 36 |
|
| 37 | + /** hdfs uri is required */ |
| 38 | + private static final String HDFS_DEF_FS_NAME = "fs.defaultFS"; |
| 39 | + /** hdfs kerberos principal( is optional) */ |
| 40 | + private static final String KERBEROS_PRINCIPAL = "kerberosPrincipal"; |
| 41 | + |
| 42 | + private static final String KERBEROS_KEYTAB_FILE_PATH = "kerberosKeytabFilePath"; |
| 43 | + private static final String HADOOP_SECURITY_AUTHENTICATION_KEY = |
| 44 | + "hadoop.security.authentication"; |
| 45 | + |
| 46 | + private static final String KERBEROS_KEY = "kerberos"; |
| 47 | + |
| 48 | + /** ******** Hdfs constants ************* */ |
| 49 | + private static final String HDFS_IMPL = "org.apache.hadoop.hdfs.DistributedFileSystem"; |
| 50 | + |
| 51 | + private static final String HDFS_IMPL_KEY = "fs.hdfs.impl"; |
| 52 | + |
| 53 | + private static final String HDFS_SITE_PATH = "hdfs_site_path"; |
| 54 | + |
| 55 | + private static final String SEATUNNEL_HADOOP_PREFIX = "seatunnel.hadoop."; |
| 56 | + |
32 | 57 | @Override
|
33 |
| - public Configuration buildConfiguration(Map<String, Object> config) { |
| 58 | + public Configuration buildConfiguration(Map<String, String> config) { |
34 | 59 | Configuration hadoopConf = new Configuration();
|
35 |
| - hadoopConf.set( |
36 |
| - FS_DEFAULT_NAME_KEY, |
37 |
| - String.valueOf(config.getOrDefault(FS_DEFAULT_NAME_KEY, FS_DEFAULT_NAME_DEFAULT))); |
| 60 | + if (config.containsKey(HDFS_DEF_FS_NAME)) { |
| 61 | + hadoopConf.set(HDFS_DEF_FS_NAME, config.get(HDFS_DEF_FS_NAME)); |
| 62 | + } |
| 63 | + hadoopConf.set(HDFS_IMPL_KEY, HDFS_IMPL); |
| 64 | + hadoopConf.set(FS_DEFAULT_NAME_KEY, config.get(FS_DEFAULT_NAME_KEY)); |
| 65 | + if (config.containsKey(KERBEROS_PRINCIPAL) |
| 66 | + && config.containsKey(KERBEROS_KEYTAB_FILE_PATH)) { |
| 67 | + String kerberosPrincipal = config.get(KERBEROS_PRINCIPAL); |
| 68 | + String kerberosKeytabFilePath = config.get(KERBEROS_KEYTAB_FILE_PATH); |
| 69 | + if (StringUtils.isNotBlank(kerberosPrincipal) |
| 70 | + && StringUtils.isNotBlank(kerberosKeytabFilePath)) { |
| 71 | + hadoopConf.set(HADOOP_SECURITY_AUTHENTICATION_KEY, KERBEROS_KEY); |
| 72 | + authenticateKerberos(kerberosPrincipal, kerberosKeytabFilePath, hadoopConf); |
| 73 | + } |
| 74 | + } |
| 75 | + if (config.containsKey(HDFS_SITE_PATH)) { |
| 76 | + hadoopConf.addResource(new Path(config.get(HDFS_SITE_PATH))); |
| 77 | + } |
| 78 | + // support other hdfs optional config keys |
| 79 | + config.entrySet().stream() |
| 80 | + .filter(entry -> entry.getKey().startsWith(SEATUNNEL_HADOOP_PREFIX)) |
| 81 | + .forEach( |
| 82 | + entry -> { |
| 83 | + String key = entry.getKey().replace(SEATUNNEL_HADOOP_PREFIX, ""); |
| 84 | + String value = entry.getValue(); |
| 85 | + hadoopConf.set(key, value); |
| 86 | + }); |
| 87 | + |
38 | 88 | return hadoopConf;
|
39 | 89 | }
|
| 90 | + |
| 91 | + /** |
| 92 | + * Authenticate kerberos |
| 93 | + * |
| 94 | + * @param kerberosPrincipal kerberos principal |
| 95 | + * @param kerberosKeytabFilePath kerberos keytab file path |
| 96 | + * @param hdfsConf hdfs configuration |
| 97 | + * @throws IMapStorageException authentication exception |
| 98 | + */ |
| 99 | + private void authenticateKerberos( |
| 100 | + String kerberosPrincipal, String kerberosKeytabFilePath, Configuration hdfsConf) |
| 101 | + throws IMapStorageException { |
| 102 | + UserGroupInformation.setConfiguration(hdfsConf); |
| 103 | + try { |
| 104 | + UserGroupInformation.loginUserFromKeytab(kerberosPrincipal, kerberosKeytabFilePath); |
| 105 | + } catch (IOException e) { |
| 106 | + throw new IMapStorageException( |
| 107 | + "Failed to login user from keytab : " |
| 108 | + + kerberosKeytabFilePath |
| 109 | + + " and kerberos principal : " |
| 110 | + + kerberosPrincipal, |
| 111 | + e); |
| 112 | + } |
| 113 | + } |
40 | 114 | }
|
0 commit comments