Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Issue 915: don't create full path on useradd

  • Loading branch information...
commit 40bd08ee6bd18648b4cc4b4df14e2a9fb6e7240e 1 parent 47a2e32
Adrian Cole authored
2  compute/src/test/java/org/jclouds/compute/callables/RunScriptOnNodeUsingSshTest.java
@@ -126,7 +126,7 @@ public void testUserAddAsRoot() {
126 126 expect(sshClient.getUsername()).andReturn("tester");
127 127 expect(sshClient.getHostAddress()).andReturn("somewhere.example.com");
128 128 expect(
129   - sshClient.exec("sudo sh <<'RUN_SCRIPT_AS_ROOT_SSH'\n" + "mkdir -p /home/users/testuser\n"
  129 + sshClient.exec("sudo sh <<'RUN_SCRIPT_AS_ROOT_SSH'\n" + "mkdir -p /home/users\n"
130 130 + "useradd -s /bin/bash -m -d /home/users/testuser testuser\n"
131 131 + "chown -R testuser /home/users/testuser\n" + "RUN_SCRIPT_AS_ROOT_SSH\n")).andReturn(
132 132 new ExecResponse("done", null, 0));
2  compute/src/test/resources/initscript_with_java.sh
@@ -182,7 +182,7 @@ END_OF_JCLOUDS_SCRIPT
182 182 %wheel ALL = (ALL) NOPASSWD:ALL
183 183 END_OF_JCLOUDS_FILE
184 184 chmod 0440 /etc/sudoers
185   - mkdir -p /home/users/defaultAdminUsername
  185 + mkdir -p /home/users
186 186 groupadd -f wheel
187 187 useradd -s /bin/bash -g wheel -m -d /home/users/defaultAdminUsername -p 'crypt(randompassword)' defaultAdminUsername
188 188 mkdir -p /home/users/defaultAdminUsername/.ssh
2  compute/src/test/resources/initscript_with_jboss.sh
@@ -182,7 +182,7 @@ END_OF_JCLOUDS_SCRIPT
182 182 %wheel ALL = (ALL) NOPASSWD:ALL
183 183 END_OF_JCLOUDS_FILE
184 184 chmod 0440 /etc/sudoers
185   - mkdir -p /home/users/web
  185 + mkdir -p /home/users
186 186 groupadd -f wheel
187 187 useradd -s /bin/bash -g wheel -m -d /home/users/web -p 'crypt(randompassword)' web
188 188 mkdir -p /home/users/web/.ssh
2  compute/src/test/resources/runscript_adminUpdate.sh
@@ -89,7 +89,7 @@ END_OF_JCLOUDS_SCRIPT
89 89 %wheel ALL = (ALL) NOPASSWD:ALL
90 90 END_OF_JCLOUDS_FILE
91 91 chmod 0440 /etc/sudoers
92   - mkdir -p /over/ridden/foo
  92 + mkdir -p /over/ridden
93 93 groupadd -f wheel
94 94 useradd -s /bin/bash -g wheel -m -d /over/ridden/foo -p 'crypt(randompassword)' foo
95 95 mkdir -p /over/ridden/foo/.ssh
37 scriptbuilder/src/main/java/org/jclouds/scriptbuilder/statements/login/UserAdd.java
@@ -18,6 +18,7 @@
18 18 */
19 19 package org.jclouds.scriptbuilder.statements.login;
20 20
  21 +import static com.google.common.base.Objects.equal;
21 22 import static com.google.common.base.Preconditions.checkNotNull;
22 23
23 24 import java.util.List;
@@ -36,6 +37,7 @@
36 37 import com.google.common.annotations.VisibleForTesting;
37 38 import com.google.common.base.Function;
38 39 import com.google.common.base.Joiner;
  40 +import com.google.common.base.Objects;
39 41 import com.google.common.base.Throwables;
40 42 import com.google.common.collect.ImmutableList;
41 43 import com.google.common.collect.ImmutableMap;
@@ -178,10 +180,10 @@ public String render(OsFamily family) {
178 180 checkNotNull(family, "family");
179 181 if (family == OsFamily.WINDOWS)
180 182 throw new UnsupportedOperationException("windows not yet implemented");
181   - String homeDir = (home != null) ? home : (defaultHome + "{fs}" + login);
  183 + String homeDir = (home != null) ? home : (defaultHome + '/' + login);
182 184 ImmutableList.Builder<Statement> statements = ImmutableList.builder();
183 185 // useradd cannot create the default homedir
184   - statements.add(Statements.exec("{md} " + homeDir));
  186 + statements.add(Statements.exec("{md} " + homeDir.substring(0, homeDir.lastIndexOf('/'))));
185 187
186 188 ImmutableMap.Builder<String, String> userAddOptions = ImmutableMap.builder();
187 189 userAddOptions.put("-s", shell);
@@ -222,27 +224,22 @@ public String render(OsFamily family) {
222 224 }
223 225
224 226 @Override
  227 + public boolean equals(Object o) {
  228 + if (this == o)
  229 + return true;
  230 + if (o == null || getClass() != o.getClass())
  231 + return false;
  232 + UserAdd that = UserAdd.class.cast(o);
  233 + return equal(this.login, that.login);
  234 + }
  235 +
  236 + @Override
225 237 public int hashCode() {
226   - final int prime = 31;
227   - int result = 1;
228   - result = prime * result + ((login == null) ? 0 : login.hashCode());
229   - return result;
  238 + return Objects.hashCode(login);
230 239 }
231 240
232 241 @Override
233   - public boolean equals(Object obj) {
234   - if (this == obj)
235   - return true;
236   - if (obj == null)
237   - return false;
238   - if (getClass() != obj.getClass())
239   - return false;
240   - UserAdd other = (UserAdd) obj;
241   - if (login == null) {
242   - if (other.login != null)
243   - return false;
244   - } else if (!login.equals(other.login))
245   - return false;
246   - return true;
  242 + public String toString() {
  243 + return Objects.toStringHelper("").add("login", login).toString();
247 244 }
248 245 }
18 scriptbuilder/src/test/java/org/jclouds/scriptbuilder/statements/login/UserAddTest.java
@@ -33,51 +33,51 @@
33 33
34 34 public void testUNIX() {
35 35 assertEquals(UserAdd.builder().login("me").build().render(OsFamily.UNIX),
36   - "mkdir -p /home/users/me\nuseradd -s /bin/bash -m -d /home/users/me me\nchown -R me /home/users/me\n");
  36 + "mkdir -p /home/users\nuseradd -s /bin/bash -m -d /home/users/me me\nchown -R me /home/users/me\n");
37 37 }
38 38
39 39 public void testWithBaseUNIX() {
40 40 assertEquals(UserAdd.builder().login("me").defaultHome("/export/home").build().render(OsFamily.UNIX),
41   - "mkdir -p /export/home/me\nuseradd -s /bin/bash -m -d /export/home/me me\nchown -R me /export/home/me\n");
  41 + "mkdir -p /export/home\nuseradd -s /bin/bash -m -d /export/home/me me\nchown -R me /export/home/me\n");
42 42 }
43 43
44 44 public void testWithGroupUNIX() {
45 45 assertEquals(UserAdd.builder().login("me").group("wheel").build().render(OsFamily.UNIX),
46   - "mkdir -p /home/users/me\ngroupadd -f wheel\nuseradd -s /bin/bash -g wheel -m -d /home/users/me me\nchown -R me /home/users/me\n");
  46 + "mkdir -p /home/users\ngroupadd -f wheel\nuseradd -s /bin/bash -g wheel -m -d /home/users/me me\nchown -R me /home/users/me\n");
47 47 }
48 48
49 49 public void testWithGroupsUNIX() {
50 50 assertEquals(UserAdd.builder().login("me").groups(ImmutableList.of("wheel", "candy")).build().render(
51 51 OsFamily.UNIX),
52   - "mkdir -p /home/users/me\ngroupadd -f wheel\ngroupadd -f candy\nuseradd -s /bin/bash -g wheel -G candy -m -d /home/users/me me\nchown -R me /home/users/me\n");
  52 + "mkdir -p /home/users\ngroupadd -f wheel\ngroupadd -f candy\nuseradd -s /bin/bash -g wheel -G candy -m -d /home/users/me me\nchown -R me /home/users/me\n");
53 53 }
54 54
55 55 public void testWithPasswordUNIX() {
56 56 String userAdd = UserAdd.builder().login("me").password("foo").group("wheel").build().render(OsFamily.UNIX);
57   - assert userAdd.startsWith("mkdir -p /home/users/me\ngroupadd -f wheel\nuseradd -s /bin/bash -g wheel -m -d /home/users/me -p '$6$") : userAdd;
  57 + assert userAdd.startsWith("mkdir -p /home/users\ngroupadd -f wheel\nuseradd -s /bin/bash -g wheel -m -d /home/users/me -p '$6$") : userAdd;
58 58 assert userAdd.endsWith("' me\nchown -R me /home/users/me\n") : userAdd;
59 59 }
60 60
61 61 public void testWithSshAuthorizedKeyUNIX() {
62 62 assertEquals(
63 63 UserAdd.builder().login("me").authorizeRSAPublicKey("rsapublickey").build().render(OsFamily.UNIX),
64   - "mkdir -p /home/users/me\nuseradd -s /bin/bash -m -d /home/users/me me\nmkdir -p /home/users/me/.ssh\ncat >> /home/users/me/.ssh/authorized_keys <<-'END_OF_JCLOUDS_FILE'\n\trsapublickey\nEND_OF_JCLOUDS_FILE\nchmod 600 /home/users/me/.ssh/authorized_keys\nchown -R me /home/users/me\n");
  64 + "mkdir -p /home/users\nuseradd -s /bin/bash -m -d /home/users/me me\nmkdir -p /home/users/me/.ssh\ncat >> /home/users/me/.ssh/authorized_keys <<-'END_OF_JCLOUDS_FILE'\n\trsapublickey\nEND_OF_JCLOUDS_FILE\nchmod 600 /home/users/me/.ssh/authorized_keys\nchown -R me /home/users/me\n");
65 65 }
66 66
67 67 public void testWithSshInstalledKeyUNIX() {
68 68 assertEquals(
69 69 UserAdd.builder().login("me").installRSAPrivateKey("rsaprivate").build().render(OsFamily.UNIX),
70   - "mkdir -p /home/users/me\nuseradd -s /bin/bash -m -d /home/users/me me\nmkdir -p /home/users/me/.ssh\nrm /home/users/me/.ssh/id_rsa\ncat >> /home/users/me/.ssh/id_rsa <<-'END_OF_JCLOUDS_FILE'\n\trsaprivate\nEND_OF_JCLOUDS_FILE\nchmod 600 /home/users/me/.ssh/id_rsa\nchown -R me /home/users/me\n");
  70 + "mkdir -p /home/users\nuseradd -s /bin/bash -m -d /home/users/me me\nmkdir -p /home/users/me/.ssh\nrm /home/users/me/.ssh/id_rsa\ncat >> /home/users/me/.ssh/id_rsa <<-'END_OF_JCLOUDS_FILE'\n\trsaprivate\nEND_OF_JCLOUDS_FILE\nchmod 600 /home/users/me/.ssh/id_rsa\nchown -R me /home/users/me\n");
71 71 }
72 72
73 73 public void testWithHomeUNIX() {
74 74 assertEquals(UserAdd.builder().login("me").home("/myhome/myme").build().render(
75 75 OsFamily.UNIX),
76   - "mkdir -p /myhome/myme\nuseradd -s /bin/bash -m -d /myhome/myme me\nchown -R me /myhome/myme\n");
  76 + "mkdir -p /myhome\nuseradd -s /bin/bash -m -d /myhome/myme me\nchown -R me /myhome/myme\n");
77 77
78 78 assertEquals(UserAdd.builder().login("me").home("/myhome/myme").defaultHome("/ignoreddefault").build().render(
79 79 OsFamily.UNIX),
80   - "mkdir -p /myhome/myme\nuseradd -s /bin/bash -m -d /myhome/myme me\nchown -R me /myhome/myme\n");
  80 + "mkdir -p /myhome\nuseradd -s /bin/bash -m -d /myhome/myme me\nchown -R me /myhome/myme\n");
81 81 }
82 82
83 83 @Test(expectedExceptions = UnsupportedOperationException.class)
2  scriptbuilder/src/test/resources/test_adminaccess_flipped.sh
@@ -4,7 +4,7 @@ root ALL = (ALL) ALL
4 4 %wheel ALL = (ALL) NOPASSWD:ALL
5 5 END_OF_FILE
6 6 chmod 0440 /etc/sudoers
7   -mkdir -p /home/users/defaultAdminUsername
  7 +mkdir -p /home/users
8 8 groupadd -f wheel
9 9 useradd -s /bin/bash -g wheel -d /home/users/defaultAdminUsername -p 'crypt(0)' defaultAdminUsername
10 10 mkdir -p /home/users/defaultAdminUsername/.ssh
2  scriptbuilder/src/test/resources/test_adminaccess_params.sh
@@ -3,7 +3,7 @@ cat > /etc/sudoers <<-'END_OF_JCLOUDS_FILE'
3 3 %wheel ALL = (ALL) NOPASSWD:ALL
4 4 END_OF_JCLOUDS_FILE
5 5 chmod 0440 /etc/sudoers
6   -mkdir -p /over/ridden/foo
  6 +mkdir -p /over/ridden
7 7 groupadd -f wheel
8 8 useradd -s /bin/bash -g wheel -m -d /over/ridden/foo -p 'crypt(bar)' foo
9 9 mkdir -p /over/ridden/foo/.ssh
2  scriptbuilder/src/test/resources/test_adminaccess_plainuser.sh
... ... @@ -1,4 +1,4 @@
1   -mkdir -p /home/users/defaultAdminUsername
  1 +mkdir -p /home/users
2 2 useradd -s /bin/bash -m -d /home/users/defaultAdminUsername -p 'crypt(0)' defaultAdminUsername
3 3 mkdir -p /home/users/defaultAdminUsername/.ssh
4 4 cat >> /home/users/defaultAdminUsername/.ssh/authorized_keys <<-'END_OF_JCLOUDS_FILE'
2  scriptbuilder/src/test/resources/test_adminaccess_standard.sh
@@ -3,7 +3,7 @@ cat > /etc/sudoers <<-'END_OF_JCLOUDS_FILE'
3 3 %wheel ALL = (ALL) NOPASSWD:ALL
4 4 END_OF_JCLOUDS_FILE
5 5 chmod 0440 /etc/sudoers
6   -mkdir -p /home/users/defaultAdminUsername
  6 +mkdir -p /home/users
7 7 groupadd -f wheel
8 8 useradd -s /bin/bash -g wheel -m -d /home/users/defaultAdminUsername -p 'crypt(0)' defaultAdminUsername
9 9 mkdir -p /home/users/defaultAdminUsername/.ssh

0 comments on commit 40bd08e

Please sign in to comment.
Something went wrong with that request. Please try again.