| 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.login; |
| 20 | |
| 21 | import static com.google.common.base.Charsets.UTF_8; |
| 22 | |
| 23 | import java.io.File; |
| 24 | import java.io.IOException; |
| 25 | import java.util.Map; |
| 26 | |
| 27 | import javax.inject.Singleton; |
| 28 | |
| 29 | import org.jclouds.crypto.Sha512Crypt; |
| 30 | import org.jclouds.crypto.SshKeys; |
| 31 | import org.jclouds.scriptbuilder.statements.login.AdminAccess.Configuration; |
| 32 | import org.jclouds.util.PasswordGenerator; |
| 33 | |
| 34 | import com.google.common.base.Function; |
| 35 | import com.google.common.base.Supplier; |
| 36 | import com.google.common.base.Suppliers; |
| 37 | import com.google.common.collect.ImmutableMap; |
| 38 | import com.google.common.io.Files; |
| 39 | |
| 40 | /** |
| 41 | * |
| 42 | * @author Adrian Cole |
| 43 | * |
| 44 | */ |
| 45 | @Singleton |
| 46 | public class DefaultConfiguration implements Configuration { |
| 47 | |
| 48 | private final Supplier<String> defaultAdminUsername = Suppliers.ofInstance(System.getProperty("user.name")); |
| 49 | private final Supplier<Map<String, String>> defaultAdminSshKeys = new Supplier<Map<String, String>>() { |
| 50 | |
| 51 | @Override |
| 52 | public Map<String, String> get() { |
| 53 | try { |
| 54 | return ImmutableMap.of("public", |
| 55 | Files.toString(new File(System.getProperty("user.home") + "/.ssh/id_rsa.pub"), UTF_8), "private", |
| 56 | Files.toString(new File(System.getProperty("user.home") + "/.ssh/id_rsa"), UTF_8)); |
| 57 | } catch (IOException e) { |
| 58 | return SshKeys.generate(); |
| 59 | } |
| 60 | } |
| 61 | }; |
| 62 | private final Supplier<String> passwordGenerator = PasswordGenerator.INSTANCE; |
| 63 | private final Function<String, String> cryptFunction = Sha512Crypt.function(); |
| 64 | |
| 65 | @Override |
| 66 | public Supplier<String> defaultAdminUsername() { |
| 67 | return defaultAdminUsername; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public Supplier<Map<String, String>> defaultAdminSshKeys() { |
| 72 | return defaultAdminSshKeys; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public Supplier<String> passwordGenerator() { |
| 77 | return passwordGenerator; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public Function<String, String> cryptFunction() { |
| 82 | return cryptFunction; |
| 83 | } |
| 84 | } |