GpgSigner.java

  1. /*
  2.  * Copyright (C) 2018, Salesforce. and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */
  10. package org.eclipse.jgit.lib;

  11. import java.util.Iterator;
  12. import java.util.ServiceConfigurationError;
  13. import java.util.ServiceLoader;

  14. import org.eclipse.jgit.annotations.NonNull;
  15. import org.eclipse.jgit.annotations.Nullable;
  16. import org.eclipse.jgit.api.errors.CanceledException;
  17. import org.eclipse.jgit.transport.CredentialsProvider;
  18. import org.slf4j.Logger;
  19. import org.slf4j.LoggerFactory;

  20. /**
  21.  * Creates GPG signatures for Git objects.
  22.  *
  23.  * @since 5.3
  24.  */
  25. public abstract class GpgSigner {
  26.     private static final Logger LOG = LoggerFactory.getLogger(GpgSigner.class);

  27.     private static GpgSigner defaultSigner = loadGpgSigner();

  28.     private static GpgSigner loadGpgSigner() {
  29.         try {
  30.             ServiceLoader<GpgSigner> loader = ServiceLoader
  31.                     .load(GpgSigner.class);
  32.             Iterator<GpgSigner> iter = loader.iterator();
  33.             if (iter.hasNext()) {
  34.                 return iter.next();
  35.             }
  36.         } catch (ServiceConfigurationError e) {
  37.             LOG.error(e.getMessage(), e);
  38.         }
  39.         return null;
  40.     }

  41.     /**
  42.      * Get the default signer, or <code>null</code>.
  43.      *
  44.      * @return the default signer, or <code>null</code>.
  45.      */
  46.     public static GpgSigner getDefault() {
  47.         return defaultSigner;
  48.     }

  49.     /**
  50.      * Set the default signer.
  51.      *
  52.      * @param signer
  53.      *            the new default signer, may be <code>null</code> to select no
  54.      *            default.
  55.      */
  56.     public static void setDefault(GpgSigner signer) {
  57.         GpgSigner.defaultSigner = signer;
  58.     }

  59.     /**
  60.      * Signs the specified commit.
  61.      *
  62.      * <p>
  63.      * Implementors should obtain the payload for signing from the specified
  64.      * commit via {@link CommitBuilder#build()} and create a proper
  65.      * {@link GpgSignature}. The generated signature must be set on the
  66.      * specified {@code commit} (see
  67.      * {@link CommitBuilder#setGpgSignature(GpgSignature)}).
  68.      * </p>
  69.      * <p>
  70.      * Any existing signature on the commit must be discarded prior obtaining
  71.      * the payload via {@link CommitBuilder#build()}.
  72.      * </p>
  73.      *
  74.      * @param commit
  75.      *            the commit to sign (must not be <code>null</code> and must be
  76.      *            complete to allow proper calculation of payload)
  77.      * @param gpgSigningKey
  78.      *            the signing key to locate (passed as is to the GPG signing
  79.      *            tool as is; eg., value of <code>user.signingkey</code>)
  80.      * @param committer
  81.      *            the signing identity (to help with key lookup in case signing
  82.      *            key is not specified)
  83.      * @param credentialsProvider
  84.      *            provider to use when querying for signing key credentials (eg.
  85.      *            passphrase)
  86.      * @throws CanceledException
  87.      *             when signing was canceled (eg., user aborted when entering
  88.      *             passphrase)
  89.      */
  90.     public abstract void sign(@NonNull CommitBuilder commit,
  91.             @Nullable String gpgSigningKey, @NonNull PersonIdent committer,
  92.             CredentialsProvider credentialsProvider) throws CanceledException;

  93.     /**
  94.      * Indicates if a signing key is available for the specified committer
  95.      * and/or signing key.
  96.      *
  97.      * @param gpgSigningKey
  98.      *            the signing key to locate (passed as is to the GPG signing
  99.      *            tool as is; eg., value of <code>user.signingkey</code>)
  100.      * @param committer
  101.      *            the signing identity (to help with key lookup in case signing
  102.      *            key is not specified)
  103.      * @param credentialsProvider
  104.      *            provider to use when querying for signing key credentials (eg.
  105.      *            passphrase)
  106.      * @return <code>true</code> if a signing key is available,
  107.      *         <code>false</code> otherwise
  108.      * @throws CanceledException
  109.      *             when signing was canceled (eg., user aborted when entering
  110.      *             passphrase)
  111.      */
  112.     public abstract boolean canLocateSigningKey(@Nullable String gpgSigningKey,
  113.             @NonNull PersonIdent committer,
  114.             CredentialsProvider credentialsProvider) throws CanceledException;

  115. }