LCOV - code coverage report
Current view: top level - gpg/api - GpgApiAdapterImpl.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 15 32 46.9 %
Date: 2022-11-19 15:00:39 Functions: 5 6 83.3 %

          Line data    Source code
       1             : // Copyright (C) 2015 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.gpg.api;
      16             : 
      17             : import static com.google.gerrit.server.api.ApiUtil.asRestApiException;
      18             : 
      19             : import com.google.gerrit.extensions.api.accounts.GpgKeyApi;
      20             : import com.google.gerrit.extensions.api.accounts.GpgKeysInput;
      21             : import com.google.gerrit.extensions.common.GpgKeyInfo;
      22             : import com.google.gerrit.extensions.common.PushCertificateInfo;
      23             : import com.google.gerrit.extensions.restapi.IdString;
      24             : import com.google.gerrit.extensions.restapi.RestApiException;
      25             : import com.google.gerrit.gpg.GerritPushCertificateCheckerFactory;
      26             : import com.google.gerrit.gpg.PushCertificateChecker;
      27             : import com.google.gerrit.gpg.server.GpgKeys;
      28             : import com.google.gerrit.gpg.server.PostGpgKeys;
      29             : import com.google.gerrit.server.GpgException;
      30             : import com.google.gerrit.server.IdentifiedUser;
      31             : import com.google.gerrit.server.account.AccountResource;
      32             : import com.google.gerrit.server.account.GpgApiAdapter;
      33             : import com.google.inject.Inject;
      34             : import com.google.inject.Provider;
      35             : import java.io.IOException;
      36             : import java.util.List;
      37             : import java.util.Map;
      38             : import org.bouncycastle.openpgp.PGPException;
      39             : import org.eclipse.jgit.errors.ConfigInvalidException;
      40             : import org.eclipse.jgit.transport.PushCertificate;
      41             : import org.eclipse.jgit.transport.PushCertificateParser;
      42             : 
      43             : public class GpgApiAdapterImpl implements GpgApiAdapter {
      44             :   private final Provider<PostGpgKeys> postGpgKeys;
      45             :   private final Provider<GpgKeys> gpgKeys;
      46             :   private final GpgKeyApiImpl.Factory gpgKeyApiFactory;
      47             :   private final GerritPushCertificateCheckerFactory pushCertCheckerFactory;
      48             : 
      49             :   @Inject
      50             :   GpgApiAdapterImpl(
      51             :       Provider<PostGpgKeys> postGpgKeys,
      52             :       Provider<GpgKeys> gpgKeys,
      53             :       GpgKeyApiImpl.Factory gpgKeyApiFactory,
      54           7 :       GerritPushCertificateCheckerFactory pushCertCheckerFactory) {
      55           7 :     this.postGpgKeys = postGpgKeys;
      56           7 :     this.gpgKeys = gpgKeys;
      57           7 :     this.gpgKeyApiFactory = gpgKeyApiFactory;
      58           7 :     this.pushCertCheckerFactory = pushCertCheckerFactory;
      59           7 :   }
      60             : 
      61             :   @Override
      62             :   public boolean isEnabled() {
      63           3 :     return true;
      64             :   }
      65             : 
      66             :   @Override
      67             :   public Map<String, GpgKeyInfo> listGpgKeys(AccountResource account)
      68             :       throws RestApiException, GpgException {
      69             :     try {
      70           1 :       return gpgKeys.get().list().apply(account).value();
      71           0 :     } catch (PGPException | IOException e) {
      72           0 :       throw new GpgException(e);
      73           0 :     } catch (Exception e) {
      74           0 :       throw asRestApiException("Cannot list GPG keys", e);
      75             :     }
      76             :   }
      77             : 
      78             :   @Override
      79             :   public Map<String, GpgKeyInfo> putGpgKeys(
      80             :       AccountResource account, List<String> add, List<String> delete)
      81             :       throws RestApiException, GpgException {
      82           2 :     GpgKeysInput in = new GpgKeysInput();
      83           2 :     in.add = add;
      84           2 :     in.delete = delete;
      85             :     try {
      86           2 :       return postGpgKeys.get().apply(account, in).value();
      87           0 :     } catch (PGPException | IOException | ConfigInvalidException e) {
      88           0 :       throw new GpgException(e);
      89           1 :     } catch (Exception e) {
      90           1 :       throw asRestApiException("Cannot put GPG keys", e);
      91             :     }
      92             :   }
      93             : 
      94             :   @Override
      95             :   public GpgKeyApi gpgKey(AccountResource account, IdString idStr)
      96             :       throws RestApiException, GpgException {
      97             :     try {
      98           1 :       return gpgKeyApiFactory.create(gpgKeys.get().parse(account, idStr));
      99           0 :     } catch (PGPException | IOException e) {
     100           0 :       throw new GpgException(e);
     101             :     }
     102             :   }
     103             : 
     104             :   @Override
     105             :   public PushCertificateInfo checkPushCertificate(String certStr, IdentifiedUser expectedUser)
     106             :       throws GpgException {
     107             :     try {
     108           0 :       PushCertificate cert = PushCertificateParser.fromString(certStr);
     109           0 :       PushCertificateChecker.Result result =
     110           0 :           pushCertCheckerFactory.create(expectedUser).setCheckNonce(false).check(cert);
     111           0 :       PushCertificateInfo info = new PushCertificateInfo();
     112           0 :       info.certificate = certStr;
     113           0 :       info.key = GpgKeys.toJson(result.getPublicKey(), result.getCheckResult());
     114           0 :       return info;
     115           0 :     } catch (IOException e) {
     116           0 :       throw new GpgException(e);
     117             :     }
     118             :   }
     119             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750