| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds 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, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.scriptbuilder.statements.ssh; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static org.jclouds.scriptbuilder.domain.Statements.appendFile; |
| 23 | import static org.jclouds.scriptbuilder.domain.Statements.exec; |
| 24 | |
| 25 | import java.util.Collections; |
| 26 | import java.util.List; |
| 27 | |
| 28 | import org.jclouds.scriptbuilder.domain.OsFamily; |
| 29 | import org.jclouds.scriptbuilder.domain.Statement; |
| 30 | import org.jclouds.scriptbuilder.domain.StatementList; |
| 31 | |
| 32 | import com.google.common.base.Joiner; |
| 33 | import com.google.common.base.Splitter; |
| 34 | import com.google.common.collect.ImmutableList; |
| 35 | import com.google.common.collect.ImmutableList.Builder; |
| 36 | |
| 37 | /** |
| 38 | * |
| 39 | * @author Adrian Cole |
| 40 | */ |
| 41 | public class AuthorizeRSAPublicKeys implements Statement { |
| 42 | private final String sshDir; |
| 43 | private final List<String> publicKeys; |
| 44 | |
| 45 | public AuthorizeRSAPublicKeys(Iterable<String> publicKeys) { |
| 46 | this("~/.ssh", publicKeys); |
| 47 | } |
| 48 | |
| 49 | public AuthorizeRSAPublicKeys(String sshDir, Iterable<String> publicKeys) { |
| 50 | this.sshDir = checkNotNull(sshDir, "sshDir"); |
| 51 | this.publicKeys = ImmutableList.copyOf(checkNotNull(publicKeys, "publicKeys")); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public Iterable<String> functionDependencies(OsFamily family) { |
| 56 | return Collections.emptyList(); |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public String render(OsFamily family) { |
| 61 | checkNotNull(family, "family"); |
| 62 | if (family == OsFamily.WINDOWS) |
| 63 | throw new UnsupportedOperationException("windows not yet implemented"); |
| 64 | Builder<Statement> statements = ImmutableList.<Statement> builder(); |
| 65 | statements.add(exec("mkdir -p " + sshDir)); |
| 66 | String authorizedKeys = sshDir + "{fs}authorized_keys"; |
| 67 | statements.add(appendFile(authorizedKeys, Splitter.on('\n').split(Joiner.on("\n\n").join(publicKeys)))); |
| 68 | statements.add(exec("chmod 600 " + authorizedKeys)); |
| 69 | return new StatementList(statements.build()).render(family); |
| 70 | } |
| 71 | } |