Line data Source code
1 : // Copyright (C) 2009 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.server.args4j; 16 : 17 : import static com.google.gerrit.util.cli.Localizable.localizable; 18 : 19 : import com.google.common.flogger.FluentLogger; 20 : import com.google.gerrit.entities.Account; 21 : import com.google.gerrit.exceptions.StorageException; 22 : import com.google.gerrit.extensions.client.AuthType; 23 : import com.google.gerrit.extensions.restapi.UnprocessableEntityException; 24 : import com.google.gerrit.server.account.AccountException; 25 : import com.google.gerrit.server.account.AccountManager; 26 : import com.google.gerrit.server.account.AccountResolver; 27 : import com.google.gerrit.server.account.AuthRequest; 28 : import com.google.gerrit.server.account.externalids.ExternalId; 29 : import com.google.gerrit.server.config.AuthConfig; 30 : import com.google.inject.Inject; 31 : import com.google.inject.assistedinject.Assisted; 32 : import java.io.IOException; 33 : import org.eclipse.jgit.errors.ConfigInvalidException; 34 : import org.kohsuke.args4j.CmdLineException; 35 : import org.kohsuke.args4j.CmdLineParser; 36 : import org.kohsuke.args4j.OptionDef; 37 : import org.kohsuke.args4j.spi.OptionHandler; 38 : import org.kohsuke.args4j.spi.Parameters; 39 : import org.kohsuke.args4j.spi.Setter; 40 : 41 : public class AccountIdHandler extends OptionHandler<Account.Id> { 42 91 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 43 : 44 : private final AccountResolver accountResolver; 45 : private final AccountManager accountManager; 46 : private final AuthType authType; 47 : private final AuthRequest.Factory authRequestFactory; 48 : 49 : @Inject 50 : public AccountIdHandler( 51 : AccountResolver accountResolver, 52 : AccountManager accountManager, 53 : AuthConfig authConfig, 54 : AuthRequest.Factory authRequestFactory, 55 : @Assisted CmdLineParser parser, 56 : @Assisted OptionDef option, 57 : @Assisted Setter<Account.Id> setter) { 58 91 : super(parser, option, setter); 59 91 : this.accountResolver = accountResolver; 60 91 : this.accountManager = accountManager; 61 91 : this.authType = authConfig.getAuthType(); 62 91 : this.authRequestFactory = authRequestFactory; 63 91 : } 64 : 65 : @Override 66 : public int parseArguments(Parameters params) throws CmdLineException { 67 7 : String token = params.getParameter(0); 68 : Account.Id accountId; 69 : try { 70 : try { 71 7 : accountId = accountResolver.resolve(token).asUnique().account().id(); 72 0 : } catch (UnprocessableEntityException e) { 73 0 : switch (authType) { 74 : case HTTP_LDAP: 75 : case CLIENT_SSL_CERT_LDAP: 76 : case LDAP: 77 0 : accountId = createAccountByLdap(token); 78 0 : break; 79 : case CUSTOM_EXTENSION: 80 : case DEVELOPMENT_BECOME_ANY_ACCOUNT: 81 : case HTTP: 82 : case LDAP_BIND: 83 : case OAUTH: 84 : case OPENID: 85 : case OPENID_SSO: 86 : default: 87 0 : String msg = "user \"%s\" not found"; 88 0 : logger.atSevere().withCause(e).log(msg, token); 89 0 : throw new CmdLineException(owner, localizable(msg), token); 90 : } 91 7 : } 92 0 : } catch (StorageException e) { 93 0 : CmdLineException newException = new CmdLineException(owner, localizable("database is down")); 94 0 : newException.initCause(e); 95 0 : throw newException; 96 0 : } catch (IOException e) { 97 0 : throw new CmdLineException(owner, "Failed to load account", e); 98 0 : } catch (ConfigInvalidException e) { 99 0 : throw new CmdLineException(owner, "Invalid account config", e); 100 7 : } 101 7 : setter.addValue(accountId); 102 7 : return 1; 103 : } 104 : 105 : private Account.Id createAccountByLdap(String user) throws CmdLineException, IOException { 106 0 : if (!ExternalId.isValidUsername(user)) { 107 0 : throw new CmdLineException(owner, localizable("user \"%s\" not found"), user); 108 : } 109 : 110 : try { 111 0 : AuthRequest req = authRequestFactory.createForUser(user); 112 0 : req.setSkipAuthentication(true); 113 0 : return accountManager.authenticate(req).getAccountId(); 114 0 : } catch (AccountException e) { 115 0 : String msg = "user \"%s\" not found"; 116 0 : logger.atSevere().withCause(e).log(msg, user); 117 0 : throw new CmdLineException(owner, localizable(msg), user); 118 : } 119 : } 120 : 121 : @Override 122 : public final String getDefaultMetaVariable() { 123 1 : return "EMAIL"; 124 : } 125 : }