Search in sources :

Example 1 with AccountSshKey

use of com.google.gerrit.server.account.AccountSshKey in project gerrit by GerritCodeReview.

the class AddSshKey method apply.

public Response<SshKeyInfo> apply(IdentifiedUser user, SshKeyInput input) throws BadRequestException, IOException, ConfigInvalidException {
    if (input == null) {
        input = new SshKeyInput();
    }
    if (input.raw == null) {
        throw new BadRequestException("SSH public key missing");
    }
    final RawInput rawKey = input.raw;
    String sshPublicKey = new ByteSource() {

        @Override
        public InputStream openStream() throws IOException {
            return rawKey.getInputStream();
        }
    }.asCharSource(UTF_8).read();
    try {
        AccountSshKey sshKey = authorizedKeys.addKey(user.getAccountId(), sshPublicKey);
        try {
            addKeyFactory.create(user, sshKey).send();
        } catch (EmailException e) {
            logger.atSevere().withCause(e).log("Cannot send SSH key added message to %s", user.getAccount().preferredEmail());
        }
        user.getUserName().ifPresent(sshKeyCache::evict);
        return Response.created(GetSshKeys.newSshKeyInfo(sshKey));
    } catch (InvalidSshKeyException e) {
        throw new BadRequestException(e.getMessage());
    }
}
Also used : SshKeyInput(com.google.gerrit.extensions.api.accounts.SshKeyInput) InvalidSshKeyException(com.google.gerrit.exceptions.InvalidSshKeyException) AccountSshKey(com.google.gerrit.server.account.AccountSshKey) RawInput(com.google.gerrit.extensions.restapi.RawInput) EmailException(com.google.gerrit.exceptions.EmailException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) ByteSource(com.google.common.io.ByteSource)

Example 2 with AccountSshKey

use of com.google.gerrit.server.account.AccountSshKey in project gerrit by GerritCodeReview.

the class SetAccountCommand method deleteSshKey.

private void deleteSshKey(SshKeyInfo i) throws AuthException, RepositoryNotFoundException, IOException, ConfigInvalidException, PermissionBackendException {
    AccountSshKey sshKey = AccountSshKey.create(user.getAccountId(), i.seq, i.sshPublicKey);
    deleteSshKey.apply(new AccountResource.SshKey(user.asIdentifiedUser(), sshKey), null);
}
Also used : AccountResource(com.google.gerrit.server.account.AccountResource) AccountSshKey(com.google.gerrit.server.account.AccountSshKey)

Example 3 with AccountSshKey

use of com.google.gerrit.server.account.AccountSshKey in project gerrit by GerritCodeReview.

the class SshKeys method parse.

public AccountResource.SshKey parse(IdentifiedUser user, IdString id) throws ResourceNotFoundException, IOException, ConfigInvalidException {
    try {
        int seq = Integer.parseInt(id.get(), 10);
        AccountSshKey sshKey = authorizedKeys.getKey(user.getAccountId(), seq);
        if (sshKey == null) {
            throw new ResourceNotFoundException(id);
        }
        return new AccountResource.SshKey(user, sshKey);
    } catch (NumberFormatException e) {
        throw new ResourceNotFoundException(id, e);
    }
}
Also used : AccountSshKey(com.google.gerrit.server.account.AccountSshKey) AccountSshKey(com.google.gerrit.server.account.AccountSshKey) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Example 4 with AccountSshKey

use of com.google.gerrit.server.account.AccountSshKey in project gerrit by GerritCodeReview.

the class SshKeyCreatorImpl method create.

@Override
public AccountSshKey create(Account.Id accountId, int seq, String encoded) throws InvalidSshKeyException {
    try {
        AccountSshKey key = AccountSshKey.create(accountId, seq, SshUtil.toOpenSshPublicKey(encoded));
        SshUtil.parse(key);
        return key;
    } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
        throw new InvalidSshKeyException(e);
    } catch (NoSuchProviderException e) {
        logger.atSevere().withCause(e).log("Cannot parse SSH key");
        throw new InvalidSshKeyException(e);
    }
}
Also used : InvalidSshKeyException(com.google.gerrit.exceptions.InvalidSshKeyException) AccountSshKey(com.google.gerrit.server.account.AccountSshKey) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) NoSuchProviderException(java.security.NoSuchProviderException)

Example 5 with AccountSshKey

use of com.google.gerrit.server.account.AccountSshKey in project gerrit by GerritCodeReview.

the class InitAdminUser method postRun.

@Override
public void postRun() throws Exception {
    if (!accounts.hasAnyAccount()) {
        welcome();
    }
    AuthType authType = flags.cfg.getEnum(AuthType.values(), "auth", null, "type", null);
    if (authType != AuthType.DEVELOPMENT_BECOME_ANY_ACCOUNT) {
        return;
    }
    if (!accounts.hasAnyAccount()) {
        ui.header("Gerrit Administrator");
        if (ui.yesno(true, "Create administrator user")) {
            Account.Id id = Account.id(sequencesOnInit.nextAccountId());
            String username = ui.readString("admin", "username");
            String name = ui.readString("Administrator", "name");
            String httpPassword = ui.readString("secret", "HTTP password");
            AccountSshKey sshKey = readSshKey(id);
            String email = readEmail(sshKey);
            List<ExternalId> extIds = new ArrayList<>(2);
            extIds.add(externalIdFactory.createUsername(username, id, httpPassword));
            if (email != null) {
                extIds.add(externalIdFactory.createEmail(id, email));
            }
            externalIds.insert("Add external IDs for initial admin user", extIds);
            Account persistedAccount = accounts.insert(Account.builder(id, TimeUtil.now()).setFullName(name).setPreferredEmail(email));
            // Only two groups should exist at this point in time and hence iterating over all of them
            // is cheap.
            Optional<GroupReference> adminGroupReference = groupsOnInit.getAllGroupReferences().filter(group -> group.getName().equals("Administrators")).findAny();
            if (!adminGroupReference.isPresent()) {
                throw new NoSuchGroupException("Administrators");
            }
            GroupReference adminGroup = adminGroupReference.get();
            groupsOnInit.addGroupMember(adminGroup.getUUID(), persistedAccount);
            if (sshKey != null) {
                VersionedAuthorizedKeysOnInit authorizedKeys = authorizedKeysFactory.create(id).load();
                authorizedKeys.addKey(sshKey.sshPublicKey());
                authorizedKeys.save("Add SSH key for initial admin user\n");
            }
            AccountState as = AccountState.forAccount(persistedAccount, extIds);
            for (AccountIndex accountIndex : accountIndexCollection.getWriteIndexes()) {
                accountIndex.replace(as);
            }
            InternalGroup adminInternalGroup = groupsOnInit.getExistingGroup(adminGroup);
            for (GroupIndex groupIndex : groupIndexCollection.getWriteIndexes()) {
                groupIndex.replace(adminInternalGroup);
            }
        }
    }
}
Also used : InternalGroup(com.google.gerrit.entities.InternalGroup) AuthType(com.google.gerrit.extensions.client.AuthType) NoSuchGroupException(com.google.gerrit.exceptions.NoSuchGroupException) Inject(com.google.inject.Inject) GroupIndexCollection(com.google.gerrit.server.index.group.GroupIndexCollection) InitStep(com.google.gerrit.pgm.init.api.InitStep) EmailValidator(org.apache.commons.validator.routines.EmailValidator) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) AccountIndex(com.google.gerrit.server.index.account.AccountIndex) ExternalIdFactory(com.google.gerrit.server.account.externalids.ExternalIdFactory) ConsoleUI(com.google.gerrit.pgm.init.api.ConsoleUI) Path(java.nio.file.Path) Files(java.nio.file.Files) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Account(com.google.gerrit.entities.Account) AccountIndexCollection(com.google.gerrit.server.index.account.AccountIndexCollection) IOException(java.io.IOException) GroupReference(com.google.gerrit.entities.GroupReference) List(java.util.List) AccountSshKey(com.google.gerrit.server.account.AccountSshKey) Paths(java.nio.file.Paths) Optional(java.util.Optional) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) GroupIndex(com.google.gerrit.server.index.group.GroupIndex) TimeUtil(com.google.gerrit.server.util.time.TimeUtil) AccountState(com.google.gerrit.server.account.AccountState) SequencesOnInit(com.google.gerrit.pgm.init.api.SequencesOnInit) InitFlags(com.google.gerrit.pgm.init.api.InitFlags) Account(com.google.gerrit.entities.Account) AccountIndex(com.google.gerrit.server.index.account.AccountIndex) AccountSshKey(com.google.gerrit.server.account.AccountSshKey) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) ArrayList(java.util.ArrayList) AccountState(com.google.gerrit.server.account.AccountState) NoSuchGroupException(com.google.gerrit.exceptions.NoSuchGroupException) InternalGroup(com.google.gerrit.entities.InternalGroup) GroupIndex(com.google.gerrit.server.index.group.GroupIndex) AuthType(com.google.gerrit.extensions.client.AuthType) GroupReference(com.google.gerrit.entities.GroupReference)

Aggregations

AccountSshKey (com.google.gerrit.server.account.AccountSshKey)6 InvalidSshKeyException (com.google.gerrit.exceptions.InvalidSshKeyException)2 Strings (com.google.common.base.Strings)1 ByteSource (com.google.common.io.ByteSource)1 Account (com.google.gerrit.entities.Account)1 GroupReference (com.google.gerrit.entities.GroupReference)1 InternalGroup (com.google.gerrit.entities.InternalGroup)1 EmailException (com.google.gerrit.exceptions.EmailException)1 NoSuchGroupException (com.google.gerrit.exceptions.NoSuchGroupException)1 SshKeyInput (com.google.gerrit.extensions.api.accounts.SshKeyInput)1 AuthType (com.google.gerrit.extensions.client.AuthType)1 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)1 RawInput (com.google.gerrit.extensions.restapi.RawInput)1 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)1 ConsoleUI (com.google.gerrit.pgm.init.api.ConsoleUI)1 InitFlags (com.google.gerrit.pgm.init.api.InitFlags)1 InitStep (com.google.gerrit.pgm.init.api.InitStep)1 SequencesOnInit (com.google.gerrit.pgm.init.api.SequencesOnInit)1 AccountResource (com.google.gerrit.server.account.AccountResource)1 AccountState (com.google.gerrit.server.account.AccountState)1