Line data Source code
1 : // Copyright (C) 2010 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.sshd.commands; 16 : 17 : import static java.nio.charset.StandardCharsets.UTF_8; 18 : 19 : import com.google.common.collect.Lists; 20 : import com.google.gerrit.common.Nullable; 21 : import com.google.gerrit.common.data.GlobalCapability; 22 : import com.google.gerrit.entities.AccountGroup; 23 : import com.google.gerrit.extensions.annotations.RequiresCapability; 24 : import com.google.gerrit.extensions.api.accounts.AccountInput; 25 : import com.google.gerrit.extensions.restapi.IdString; 26 : import com.google.gerrit.extensions.restapi.RestApiException; 27 : import com.google.gerrit.extensions.restapi.TopLevelResource; 28 : import com.google.gerrit.server.permissions.PermissionBackendException; 29 : import com.google.gerrit.server.restapi.account.CreateAccount; 30 : import com.google.gerrit.sshd.CommandMetaData; 31 : import com.google.gerrit.sshd.SshCommand; 32 : import com.google.inject.Inject; 33 : import java.io.BufferedReader; 34 : import java.io.IOException; 35 : import java.io.InputStreamReader; 36 : import java.util.ArrayList; 37 : import java.util.List; 38 : import org.eclipse.jgit.errors.ConfigInvalidException; 39 : import org.kohsuke.args4j.Argument; 40 : import org.kohsuke.args4j.Option; 41 : 42 : /** Create a new user account. * */ 43 : @RequiresCapability(GlobalCapability.CREATE_ACCOUNT) 44 : @CommandMetaData(name = "create-account", description = "Create a new batch/role account") 45 1 : final class CreateAccountCommand extends SshCommand { 46 1 : @Option( 47 : name = "--group", 48 : aliases = {"-g"}, 49 : metaVar = "GROUP", 50 : usage = "groups to add account to") 51 : private List<AccountGroup.Id> groups = new ArrayList<>(); 52 : 53 : @Option(name = "--full-name", metaVar = "NAME", usage = "display name of the account") 54 : private String fullName; 55 : 56 : @Option(name = "--email", metaVar = "EMAIL", usage = "email address of the account") 57 : private String email; 58 : 59 : @Option(name = "--ssh-key", metaVar = "-|KEY", usage = "public key for SSH authentication") 60 : private String sshKey; 61 : 62 : @Option( 63 : name = "--http-password", 64 : metaVar = "PASSWORD", 65 : usage = "password for HTTP authentication") 66 : private String httpPassword; 67 : 68 : @Argument(index = 0, required = true, metaVar = "USERNAME", usage = "name of the user account") 69 : private String username; 70 : 71 : @Inject private CreateAccount createAccount; 72 : 73 : @Override 74 : protected void run() 75 : throws IOException, ConfigInvalidException, UnloggedFailure, PermissionBackendException { 76 0 : enableGracefulStop(); 77 0 : AccountInput input = new AccountInput(); 78 0 : input.username = username; 79 0 : input.email = email; 80 0 : input.name = fullName; 81 0 : input.sshKey = readSshKey(); 82 0 : input.httpPassword = httpPassword; 83 0 : input.groups = Lists.transform(groups, AccountGroup.Id::toString); 84 : try { 85 0 : createAccount.apply(TopLevelResource.INSTANCE, IdString.fromDecoded(username), input); 86 0 : } catch (RestApiException e) { 87 0 : throw die(e.getMessage()); 88 0 : } 89 0 : } 90 : 91 : @Nullable 92 : private String readSshKey() throws IOException { 93 0 : if (sshKey == null) { 94 0 : return null; 95 : } 96 0 : if ("-".equals(sshKey)) { 97 0 : sshKey = ""; 98 0 : BufferedReader br = new BufferedReader(new InputStreamReader(in, UTF_8)); 99 : String line; 100 0 : while ((line = br.readLine()) != null) { 101 0 : sshKey += line + "\n"; 102 : } 103 : } 104 0 : return sshKey; 105 : } 106 : }