From d674eaba57cdd92b8588bdaa63b80fd2f36b5d5d Mon Sep 17 00:00:00 2001 From: liwenhe1993 <32166572+liwenhe1993@users.noreply.github.com> Date: Sun, 16 Feb 2020 09:52:47 +0800 Subject: [PATCH] Support DS to create user and group in windows environment (#1953) * Support DS to create user and group in windows environment * Add unit test --- .../common/utils/OSUtils.java | 84 +++++++++++++++++-- .../common/utils/OSUtilsTest.java | 23 +++-- 2 files changed, 93 insertions(+), 14 deletions(-) diff --git a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/OSUtils.java b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/OSUtils.java index c8d33e3ef2..acfca77c8c 100644 --- a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/OSUtils.java +++ b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/OSUtils.java @@ -40,6 +40,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.regex.Pattern; /** * os utils @@ -138,7 +139,7 @@ public class OSUtils { if (isMacOS()) { return getUserListFromMac(); } else if (isWindows()) { - // do something + return getUserListFromWindows(); } else { return getUserListFromLinux(); } @@ -185,6 +186,47 @@ public class OSUtils { return Collections.emptyList(); } + /** + * get user list from windows + * @return user list + * @throws IOException + */ + private static List getUserListFromWindows() throws IOException { + String result = exeCmd("net user"); + String[] lines = result.split("\n"); + + int startPos = 0; + int endPos = lines.length - 2; + for (int i = 0; i < lines.length; i++) { + if (lines[i].isEmpty()) { + continue; + } + + int count = 0; + if (lines[i].charAt(0) == '-') { + for (int j = 0; j < lines[i].length(); j++) { + if (lines[i].charAt(i) == '-') { + count++; + } + } + } + + if (count == lines[i].length()) { + startPos = i + 1; + break; + } + } + + List users = new ArrayList<>(); + while (startPos <= endPos) { + Pattern pattern = Pattern.compile("\\s+"); + users.addAll(Arrays.asList(pattern.split(lines[startPos]))); + startPos++; + } + + return users; + } + /** * create user * @param userName user name @@ -200,7 +242,7 @@ public class OSUtils { if (isMacOS()) { createMacUser(userName, userGroup); } else if (isWindows()) { - // do something + createWindowsUser(userName, userGroup); } else { createLinuxUser(userName, userGroup); } @@ -243,16 +285,46 @@ public class OSUtils { OSUtils.exeCmd(appendGroupCmd); } + /** + * create windows user + * @param userName user name + * @param userGroup user group + * @throws IOException in case of an I/O error + */ + private static void createWindowsUser(String userName, String userGroup) throws IOException { + logger.info("create windows os user : {}", userName); + String userCreateCmd = String.format("net user \"%s\" /add", userName); + String appendGroupCmd = String.format("net localgroup \"%s\" \"%s\" /add", userGroup, userName); + + logger.info("execute create user command : {}", userCreateCmd); + OSUtils.exeCmd(userCreateCmd); + + logger.info("execute append user to group : {}", appendGroupCmd); + OSUtils.exeCmd(appendGroupCmd); + } + /** * get system group information * @return system group info * @throws IOException errors */ public static String getGroup() throws IOException { - String result = exeCmd("groups"); - if (StringUtils.isNotEmpty(result)) { - String[] groupInfo = result.split(" "); - return groupInfo[0]; + if (isWindows()) { + String currentProcUserName = System.getProperty("user.name"); + String result = exeCmd(String.format("net user \"%s\"", currentProcUserName)); + String line = result.split("\n")[22]; + String group = Pattern.compile("\\s+").split(line)[1]; + if (group.charAt(0) == '*') { + return group.substring(1); + } else { + return group; + } + } else { + String result = exeCmd("groups"); + if (StringUtils.isNotEmpty(result)) { + String[] groupInfo = result.split(" "); + return groupInfo[0]; + } } return null; diff --git a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/OSUtilsTest.java b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/OSUtilsTest.java index 3d51aa830f..7106804aaf 100644 --- a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/OSUtilsTest.java +++ b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/OSUtilsTest.java @@ -36,9 +36,9 @@ public class OSUtilsTest { Assert.assertNotEquals("System user list should not be empty", userList.size(), 0); logger.info("OS user list : {}", userList.toString()); } + @Test public void testOSMetric(){ - double availablePhysicalMemorySize = OSUtils.availablePhysicalMemorySize(); Assert.assertTrue(availablePhysicalMemorySize > 0.0f); double totalMemorySize = OSUtils.totalMemorySize(); @@ -50,17 +50,23 @@ public class OSUtilsTest { double cpuUsage = OSUtils.cpuUsage(); Assert.assertTrue(cpuUsage > 0.0f); } + @Test public void getGroup() { - if(OSUtils.isMacOS() || !OSUtils.isWindows()){ - try { - String group = OSUtils.getGroup(); - Assert.assertNotNull(group); - } catch (IOException e) { - Assert.fail("get group failed " + e.getMessage()); - } + try { + String group = OSUtils.getGroup(); + Assert.assertNotNull(group); + } catch (IOException e) { + Assert.fail("get group failed " + e.getMessage()); } } + + @Test + public void createUser() { + boolean result = OSUtils.createUser("test123"); + Assert.assertTrue(result); + } + @Test public void exeCmd() { if(OSUtils.isMacOS() || !OSUtils.isWindows()){ @@ -113,4 +119,5 @@ public class OSUtilsTest { Assert.assertFalse(resource); } + }