|
| 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 com.dtstack.flink.sql.util; |
| 20 | + |
| 21 | +import org.apache.commons.io.FileUtils; |
| 22 | + |
| 23 | +import java.io.File; |
| 24 | +import java.io.IOException; |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.Map; |
| 27 | + |
| 28 | +/** |
| 29 | + * Utility methods for helping with security tasks. |
| 30 | + * Date: 2019/12/28 |
| 31 | + * Company: www.dtstack.com |
| 32 | + * @author maqi |
| 33 | + */ |
| 34 | +public class AuthUtil { |
| 35 | + |
| 36 | + public static String creatJaasFile(String prefix, String suffix, JAASConfig jaasConfig) throws IOException { |
| 37 | + File krbConf = new File(System.getProperty("user.dir")); |
| 38 | + File temp = File.createTempFile(prefix, suffix, krbConf); |
| 39 | + temp.deleteOnExit(); |
| 40 | + FileUtils.writeStringToFile(temp, jaasConfig.toString()); |
| 41 | + return temp.getAbsolutePath(); |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + public static class JAASConfig { |
| 46 | + private String entryName; |
| 47 | + private String loginModule; |
| 48 | + private String loginModuleFlag; |
| 49 | + private Map<String, String> loginModuleOptions; |
| 50 | + |
| 51 | + public JAASConfig(String entryName, String loginModule, String loginModuleFlag, Map<String, String> loginModuleOptions) { |
| 52 | + this.entryName = entryName; |
| 53 | + this.loginModule = loginModule; |
| 54 | + this.loginModuleFlag = loginModuleFlag; |
| 55 | + this.loginModuleOptions = loginModuleOptions; |
| 56 | + } |
| 57 | + |
| 58 | + public static Builder builder() { |
| 59 | + return new Builder(); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public String toString() { |
| 64 | + StringBuilder stringBuilder = new StringBuilder(entryName).append(" {\n\t") |
| 65 | + .append(loginModule).append(" ").append(loginModuleFlag).append("\n\t"); |
| 66 | + String[] keys = loginModuleOptions.keySet().toArray(new String[loginModuleOptions.size()]); |
| 67 | + for (int i = 0; i < keys.length; i++) { |
| 68 | + stringBuilder.append(keys[i]).append("=").append(loginModuleOptions.get(keys[i])); |
| 69 | + if (i != keys.length - 1) { |
| 70 | + stringBuilder.append("\n\t"); |
| 71 | + } else { |
| 72 | + stringBuilder.append(";\n"); |
| 73 | + } |
| 74 | + |
| 75 | + } |
| 76 | + stringBuilder.append("\n").append("};"); |
| 77 | + return stringBuilder.toString(); |
| 78 | + } |
| 79 | + |
| 80 | + public static class Builder { |
| 81 | + private String entryName; |
| 82 | + private String loginModule; |
| 83 | + private String loginModuleFlag; |
| 84 | + private Map<String, String> loginModuleOptions; |
| 85 | + |
| 86 | + public Builder setEntryName(String entryName) { |
| 87 | + this.entryName = entryName; |
| 88 | + return this; |
| 89 | + } |
| 90 | + |
| 91 | + public Builder setLoginModule(String loginModule) { |
| 92 | + this.loginModule = loginModule; |
| 93 | + return this; |
| 94 | + } |
| 95 | + |
| 96 | + public Builder setLoginModuleFlag(String loginModuleFlag) { |
| 97 | + this.loginModuleFlag = loginModuleFlag; |
| 98 | + return this; |
| 99 | + } |
| 100 | + |
| 101 | + public Builder setLoginModuleOptions(Map<String, String> loginModuleOptions) { |
| 102 | + this.loginModuleOptions = loginModuleOptions; |
| 103 | + return this; |
| 104 | + } |
| 105 | + |
| 106 | + public JAASConfig build() { |
| 107 | + return new JAASConfig( |
| 108 | + entryName, loginModule, loginModuleFlag, loginModuleOptions); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments