| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * 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.jclouds.scriptbuilder.statements.login; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.util.List; |
| 24 | |
| 25 | import javax.annotation.Nullable; |
| 26 | import javax.inject.Named; |
| 27 | |
| 28 | import org.jclouds.crypto.Sha512Crypt; |
| 29 | import org.jclouds.scriptbuilder.domain.OsFamily; |
| 30 | import org.jclouds.scriptbuilder.domain.Statement; |
| 31 | import org.jclouds.scriptbuilder.domain.StatementList; |
| 32 | import org.jclouds.scriptbuilder.domain.Statements; |
| 33 | import org.jclouds.scriptbuilder.statements.ssh.AuthorizeRSAPublicKeys; |
| 34 | import org.jclouds.scriptbuilder.statements.ssh.InstallRSAPrivateKey; |
| 35 | |
| 36 | import com.google.common.annotations.VisibleForTesting; |
| 37 | import com.google.common.base.Function; |
| 38 | import com.google.common.base.Joiner; |
| 39 | import com.google.common.base.Throwables; |
| 40 | import com.google.common.collect.ImmutableList; |
| 41 | import com.google.common.collect.ImmutableMap; |
| 42 | import com.google.common.collect.Lists; |
| 43 | import com.google.inject.Inject; |
| 44 | |
| 45 | /** |
| 46 | * |
| 47 | * @author Adrian Cole |
| 48 | */ |
| 49 | public class UserAdd implements Statement { |
| 50 | public static UserAdd.Builder builder() { |
| 51 | return new Builder(); |
| 52 | } |
| 53 | |
| 54 | public static class Builder { |
| 55 | private String defaultHome = "/home/users"; |
| 56 | private String login; |
| 57 | private String password; |
| 58 | private String RSAPrivateKey; |
| 59 | private List<String> groups = Lists.newArrayList(); |
| 60 | private List<String> authorizeRSAPublicKeys = Lists.newArrayList(); |
| 61 | private String shell = "/bin/bash"; |
| 62 | |
| 63 | public UserAdd.Builder defaultHome(String defaultHome) { |
| 64 | this.defaultHome = defaultHome; |
| 65 | return this; |
| 66 | } |
| 67 | |
| 68 | public UserAdd.Builder login(String login) { |
| 69 | this.login = login; |
| 70 | return this; |
| 71 | } |
| 72 | |
| 73 | public UserAdd.Builder password(String password) { |
| 74 | this.password = password; |
| 75 | return this; |
| 76 | } |
| 77 | |
| 78 | public UserAdd.Builder group(String group) { |
| 79 | this.groups.add(checkNotNull(group, "group")); |
| 80 | return this; |
| 81 | } |
| 82 | |
| 83 | public UserAdd.Builder groups(Iterable<String> groups) { |
| 84 | this.groups = ImmutableList.<String> copyOf(checkNotNull(groups, "groups")); |
| 85 | return this; |
| 86 | } |
| 87 | |
| 88 | public UserAdd.Builder installRSAPrivateKey(String RSAPrivateKey) { |
| 89 | this.RSAPrivateKey = RSAPrivateKey; |
| 90 | return this; |
| 91 | } |
| 92 | |
| 93 | public UserAdd.Builder authorizeRSAPublicKey(String RSAPublicKey) { |
| 94 | this.authorizeRSAPublicKeys.add(checkNotNull(RSAPublicKey, "RSAPublicKey")); |
| 95 | return this; |
| 96 | } |
| 97 | |
| 98 | public UserAdd.Builder authorizeRSAPublicKeys(Iterable<String> RSAPublicKeys) { |
| 99 | this.authorizeRSAPublicKeys = ImmutableList.<String> copyOf(checkNotNull(RSAPublicKeys, "RSAPublicKeys")); |
| 100 | return this; |
| 101 | } |
| 102 | |
| 103 | public UserAdd.Builder shell(String shell) { |
| 104 | this.shell = shell; |
| 105 | return this; |
| 106 | } |
| 107 | |
| 108 | public UserAdd build() { |
| 109 | return new UserAdd(login, groups, password, RSAPrivateKey, authorizeRSAPublicKeys, defaultHome, shell); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | public UserAdd(String login, List<String> groups, @Nullable String password, @Nullable String installRSAPrivateKey, |
| 114 | List<String> authorizeRSAPublicKeys, String defaultHome, String shell) { |
| 115 | this.login = checkNotNull(login, "login"); |
| 116 | this.password = password; |
| 117 | this.groups = ImmutableList.copyOf(checkNotNull(groups, "groups")); |
| 118 | this.installRSAPrivateKey = installRSAPrivateKey; |
| 119 | this.authorizeRSAPublicKeys = ImmutableList |
| 120 | .copyOf(checkNotNull(authorizeRSAPublicKeys, "authorizeRSAPublicKeys")); |
| 121 | this.defaultHome = checkNotNull(defaultHome, "defaultHome"); |
| 122 | this.shell = checkNotNull(shell, "shell"); |
| 123 | } |
| 124 | |
| 125 | private final String defaultHome; |
| 126 | private final String login; |
| 127 | private final List<String> groups; |
| 128 | private final String password; |
| 129 | private final String installRSAPrivateKey; |
| 130 | private final List<String> authorizeRSAPublicKeys; |
| 131 | private final String shell; |
| 132 | |
| 133 | private Function<String, String> cryptFunction = Sha512Crypt.function(); |
| 134 | |
| 135 | @Inject(optional = true) |
| 136 | @Named("CRYPT") |
| 137 | @VisibleForTesting |
| 138 | UserAdd cryptFunction(Function<String, String> cryptFunction) { |
| 139 | this.cryptFunction = cryptFunction; |
| 140 | return this; |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public Iterable<String> functionDependencies(OsFamily family) { |
| 145 | return ImmutableList.of(); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public String render(OsFamily family) { |
| 150 | checkNotNull(family, "family"); |
| 151 | if (family == OsFamily.WINDOWS) |
| 152 | throw new UnsupportedOperationException("windows not yet implemented"); |
| 153 | String homeDir = defaultHome + "{fs}" + login; |
| 154 | ImmutableList.Builder<Statement> statements = ImmutableList.<Statement> builder(); |
| 155 | statements.add(Statements.exec("{md} " + homeDir)); |
| 156 | |
| 157 | ImmutableMap.Builder<String, String> userAddOptions = ImmutableMap.<String, String> builder(); |
| 158 | userAddOptions.put("-s", shell); |
| 159 | if (groups.size() > 0) { |
| 160 | for (String group : groups) |
| 161 | statements.add(Statements.exec("groupadd -f " + group)); |
| 162 | |
| 163 | List<String> groups = Lists.newArrayList(this.groups); |
| 164 | String primaryGroup = groups.remove(0); |
| 165 | userAddOptions.put("-g", primaryGroup); |
| 166 | if (groups.size() > 0) |
| 167 | userAddOptions.put("-G", Joiner.on(',').join(groups)); |
| 168 | |
| 169 | } |
| 170 | userAddOptions.put("-d", homeDir); |
| 171 | if (password != null) { |
| 172 | try { |
| 173 | userAddOptions.put("-p", "'" + cryptFunction.apply(password) + "'"); |
| 174 | } catch (Exception e) { |
| 175 | Throwables.propagate(e); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | String options = Joiner.on(' ').withKeyValueSeparator(" ").join(userAddOptions.build()); |
| 180 | |
| 181 | statements.add(Statements.exec(String.format("useradd %s %s", options, login))); |
| 182 | |
| 183 | if (authorizeRSAPublicKeys.size() > 0 || installRSAPrivateKey != null) { |
| 184 | String sshDir = homeDir + "{fs}.ssh"; |
| 185 | if (authorizeRSAPublicKeys.size() > 0) |
| 186 | statements.add(new AuthorizeRSAPublicKeys(sshDir, authorizeRSAPublicKeys)); |
| 187 | if (installRSAPrivateKey != null) |
| 188 | statements.add(new InstallRSAPrivateKey(sshDir, installRSAPrivateKey)); |
| 189 | } |
| 190 | statements.add(Statements.exec(String.format("chown -R %s %s", login, homeDir))); |
| 191 | return new StatementList(statements.build()).render(family); |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public int hashCode() { |
| 196 | final int prime = 31; |
| 197 | int result = 1; |
| 198 | result = prime * result + ((login == null) ? 0 : login.hashCode()); |
| 199 | return result; |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public boolean equals(Object obj) { |
| 204 | if (this == obj) |
| 205 | return true; |
| 206 | if (obj == null) |
| 207 | return false; |
| 208 | if (getClass() != obj.getClass()) |
| 209 | return false; |
| 210 | UserAdd other = (UserAdd) obj; |
| 211 | if (login == null) { |
| 212 | if (other.login != null) |
| 213 | return false; |
| 214 | } else if (!login.equals(other.login)) |
| 215 | return false; |
| 216 | return true; |
| 217 | } |
| 218 | } |