Line data Source code
1 : // Copyright (C) 2013 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.restapi.account; 16 : 17 : import static java.nio.charset.StandardCharsets.UTF_8; 18 : 19 : import com.google.common.flogger.FluentLogger; 20 : import com.google.common.io.ByteSource; 21 : import com.google.gerrit.exceptions.EmailException; 22 : import com.google.gerrit.exceptions.InvalidSshKeyException; 23 : import com.google.gerrit.extensions.api.accounts.SshKeyInput; 24 : import com.google.gerrit.extensions.common.SshKeyInfo; 25 : import com.google.gerrit.extensions.restapi.AuthException; 26 : import com.google.gerrit.extensions.restapi.BadRequestException; 27 : import com.google.gerrit.extensions.restapi.RawInput; 28 : import com.google.gerrit.extensions.restapi.Response; 29 : import com.google.gerrit.extensions.restapi.RestCollectionModifyView; 30 : import com.google.gerrit.server.CurrentUser; 31 : import com.google.gerrit.server.IdentifiedUser; 32 : import com.google.gerrit.server.account.AccountResource; 33 : import com.google.gerrit.server.account.AccountSshKey; 34 : import com.google.gerrit.server.account.VersionedAuthorizedKeys; 35 : import com.google.gerrit.server.mail.send.AddKeySender; 36 : import com.google.gerrit.server.permissions.GlobalPermission; 37 : import com.google.gerrit.server.permissions.PermissionBackend; 38 : import com.google.gerrit.server.permissions.PermissionBackendException; 39 : import com.google.gerrit.server.ssh.SshKeyCache; 40 : import com.google.inject.Inject; 41 : import com.google.inject.Provider; 42 : import com.google.inject.Singleton; 43 : import java.io.IOException; 44 : import java.io.InputStream; 45 : import org.eclipse.jgit.errors.ConfigInvalidException; 46 : 47 : /** 48 : * REST endpoint to add an SSH key for an account. 49 : * 50 : * <p>This REST endpoint handles {@code POST /accounts/<account-identifier>/sshkeys/} requests. 51 : */ 52 : @Singleton 53 : public class AddSshKey 54 : implements RestCollectionModifyView<AccountResource, AccountResource.SshKey, SshKeyInput> { 55 148 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 56 : 57 : private final Provider<CurrentUser> self; 58 : private final PermissionBackend permissionBackend; 59 : private final VersionedAuthorizedKeys.Accessor authorizedKeys; 60 : private final SshKeyCache sshKeyCache; 61 : private final AddKeySender.Factory addKeyFactory; 62 : 63 : @Inject 64 : AddSshKey( 65 : Provider<CurrentUser> self, 66 : PermissionBackend permissionBackend, 67 : VersionedAuthorizedKeys.Accessor authorizedKeys, 68 : SshKeyCache sshKeyCache, 69 148 : AddKeySender.Factory addKeyFactory) { 70 148 : this.self = self; 71 148 : this.permissionBackend = permissionBackend; 72 148 : this.authorizedKeys = authorizedKeys; 73 148 : this.sshKeyCache = sshKeyCache; 74 148 : this.addKeyFactory = addKeyFactory; 75 148 : } 76 : 77 : @Override 78 : public Response<SshKeyInfo> apply(AccountResource rsrc, SshKeyInput input) 79 : throws AuthException, BadRequestException, IOException, ConfigInvalidException, 80 : PermissionBackendException { 81 5 : if (!self.get().hasSameAccountId(rsrc.getUser())) { 82 3 : permissionBackend.currentUser().check(GlobalPermission.ADMINISTRATE_SERVER); 83 : } 84 5 : return apply(rsrc.getUser(), input); 85 : } 86 : 87 : public Response<SshKeyInfo> apply(IdentifiedUser user, SshKeyInput input) 88 : throws BadRequestException, IOException, ConfigInvalidException { 89 5 : if (input == null) { 90 0 : input = new SshKeyInput(); 91 : } 92 5 : if (input.raw == null) { 93 0 : throw new BadRequestException("SSH public key missing"); 94 : } 95 : 96 5 : final RawInput rawKey = input.raw; 97 5 : String sshPublicKey = 98 5 : new ByteSource() { 99 : @Override 100 : public InputStream openStream() throws IOException { 101 5 : return rawKey.getInputStream(); 102 : } 103 5 : }.asCharSource(UTF_8).read(); 104 : 105 : try { 106 5 : AccountSshKey sshKey = authorizedKeys.addKey(user.getAccountId(), sshPublicKey); 107 : 108 : try { 109 5 : addKeyFactory.create(user, sshKey).send(); 110 0 : } catch (EmailException e) { 111 0 : logger.atSevere().withCause(e).log( 112 0 : "Cannot send SSH key added message to %s", user.getAccount().preferredEmail()); 113 5 : } 114 : 115 5 : user.getUserName().ifPresent(sshKeyCache::evict); 116 5 : return Response.created(GetSshKeys.newSshKeyInfo(sshKey)); 117 0 : } catch (InvalidSshKeyException e) { 118 0 : throw new BadRequestException(e.getMessage()); 119 : } 120 : } 121 : }