Search in sources :

Example 1 with SshKeyInput

use of com.google.gerrit.extensions.api.accounts.SshKeyInput in project gerrit by GerritCodeReview.

the class AccountApiImpl method addSshKey.

@Override
public SshKeyInfo addSshKey(String key) throws RestApiException {
    SshKeyInput in = new SshKeyInput();
    in.raw = RawInputUtil.create(key);
    try {
        return addSshKey.apply(account, in).value();
    } catch (Exception e) {
        throw asRestApiException("Cannot add SSH key", e);
    }
}
Also used : SshKeyInput(com.google.gerrit.extensions.api.accounts.SshKeyInput) ApiUtil.asRestApiException(com.google.gerrit.server.api.ApiUtil.asRestApiException) RestApiException(com.google.gerrit.extensions.restapi.RestApiException)

Example 2 with SshKeyInput

use of com.google.gerrit.extensions.api.accounts.SshKeyInput in project gerrit by GerritCodeReview.

the class SetAccountCommand method addSshKeys.

private void addSshKeys(List<String> sshKeys) throws RestApiException, IOException, ConfigInvalidException, PermissionBackendException {
    for (String sshKey : sshKeys) {
        SshKeyInput in = new SshKeyInput();
        in.raw = RawInputUtil.create(sshKey.getBytes(UTF_8), "text/plain");
        addSshKey.apply(rsrc, in);
    }
}
Also used : SshKeyInput(com.google.gerrit.extensions.api.accounts.SshKeyInput) IdString(com.google.gerrit.extensions.restapi.IdString)

Example 3 with SshKeyInput

use of com.google.gerrit.extensions.api.accounts.SshKeyInput 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)

Aggregations

SshKeyInput (com.google.gerrit.extensions.api.accounts.SshKeyInput)3 ByteSource (com.google.common.io.ByteSource)1 EmailException (com.google.gerrit.exceptions.EmailException)1 InvalidSshKeyException (com.google.gerrit.exceptions.InvalidSshKeyException)1 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)1 IdString (com.google.gerrit.extensions.restapi.IdString)1 RawInput (com.google.gerrit.extensions.restapi.RawInput)1 RestApiException (com.google.gerrit.extensions.restapi.RestApiException)1 AccountSshKey (com.google.gerrit.server.account.AccountSshKey)1 ApiUtil.asRestApiException (com.google.gerrit.server.api.ApiUtil.asRestApiException)1