Parcourir la source

GPG: support git config gpg.program

Add it to the GpgConfig. Change GpgConfig to load the values once only.
Add a parameter to the GpgObjectSigner interface's operations to pass
in a GpgConfig. Update CommitCommand and TagCommand to pass the value
to the signer. Let the signer decide whether it can actually produce
the wanted signature type (openpgp or x509).

No behavior change. But this makes it possible to implement different
signers that might support x509 signatures, or use gpg.program and
shell out to an external GPG executable for signing.

Change-Id: I427f83eb1ece81c310e1cddd85315f6f88cc99ea
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
tags/v5.11.0.202102240950-m3
Thomas Wolf il y a 3 ans
Parent
révision
6d462e5fe9

+ 34
- 2
org.eclipse.jgit.gpg.bc/src/org/eclipse/jgit/gpg/bc/internal/BouncyCastleGpgSigner.java Voir le fichier

import org.eclipse.jgit.annotations.Nullable; import org.eclipse.jgit.annotations.Nullable;
import org.eclipse.jgit.api.errors.CanceledException; import org.eclipse.jgit.api.errors.CanceledException;
import org.eclipse.jgit.api.errors.JGitInternalException; import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.api.errors.UnsupportedSigningFormatException;
import org.eclipse.jgit.errors.UnsupportedCredentialItem; import org.eclipse.jgit.errors.UnsupportedCredentialItem;
import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.lib.CommitBuilder; import org.eclipse.jgit.lib.CommitBuilder;
import org.eclipse.jgit.lib.GpgConfig;
import org.eclipse.jgit.lib.GpgSignature; import org.eclipse.jgit.lib.GpgSignature;
import org.eclipse.jgit.lib.GpgSigner; import org.eclipse.jgit.lib.GpgSigner;
import org.eclipse.jgit.lib.GpgObjectSigner; import org.eclipse.jgit.lib.GpgObjectSigner;
import org.eclipse.jgit.lib.ObjectBuilder; import org.eclipse.jgit.lib.ObjectBuilder;
import org.eclipse.jgit.lib.PersonIdent; import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.GpgConfig.GpgFormat;
import org.eclipse.jgit.transport.CredentialsProvider; import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.util.StringUtils; import org.eclipse.jgit.util.StringUtils;


public boolean canLocateSigningKey(@Nullable String gpgSigningKey, public boolean canLocateSigningKey(@Nullable String gpgSigningKey,
PersonIdent committer, CredentialsProvider credentialsProvider) PersonIdent committer, CredentialsProvider credentialsProvider)
throws CanceledException { throws CanceledException {
try {
return canLocateSigningKey(gpgSigningKey, committer,
credentialsProvider, null);
} catch (UnsupportedSigningFormatException e) {
// Cannot occur with a null config
return false;
}
}

@Override
public boolean canLocateSigningKey(@Nullable String gpgSigningKey,
PersonIdent committer, CredentialsProvider credentialsProvider,
GpgConfig config)
throws CanceledException, UnsupportedSigningFormatException {
if (config != null && config.getKeyFormat() != GpgFormat.OPENPGP) {
throw new UnsupportedSigningFormatException(
JGitText.get().onlyOpenPgpSupportedForSigning);
}
try (BouncyCastleGpgKeyPassphrasePrompt passphrasePrompt = new BouncyCastleGpgKeyPassphrasePrompt( try (BouncyCastleGpgKeyPassphrasePrompt passphrasePrompt = new BouncyCastleGpgKeyPassphrasePrompt(
credentialsProvider)) { credentialsProvider)) {
BouncyCastleGpgKey gpgKey = locateSigningKey(gpgSigningKey, BouncyCastleGpgKey gpgKey = locateSigningKey(gpgSigningKey,
public void sign(@NonNull CommitBuilder commit, public void sign(@NonNull CommitBuilder commit,
@Nullable String gpgSigningKey, @NonNull PersonIdent committer, @Nullable String gpgSigningKey, @NonNull PersonIdent committer,
CredentialsProvider credentialsProvider) throws CanceledException { CredentialsProvider credentialsProvider) throws CanceledException {
signObject(commit, gpgSigningKey, committer, credentialsProvider);
try {
signObject(commit, gpgSigningKey, committer, credentialsProvider,
null);
} catch (UnsupportedSigningFormatException e) {
// Cannot occur with a null config
}
} }


@Override @Override
public void signObject(@NonNull ObjectBuilder object, public void signObject(@NonNull ObjectBuilder object,
@Nullable String gpgSigningKey, @NonNull PersonIdent committer, @Nullable String gpgSigningKey, @NonNull PersonIdent committer,
CredentialsProvider credentialsProvider) throws CanceledException {
CredentialsProvider credentialsProvider, GpgConfig config)
throws CanceledException, UnsupportedSigningFormatException {
if (config != null && config.getKeyFormat() != GpgFormat.OPENPGP) {
throw new UnsupportedSigningFormatException(
JGitText.get().onlyOpenPgpSupportedForSigning);
}
try (BouncyCastleGpgKeyPassphrasePrompt passphrasePrompt = new BouncyCastleGpgKeyPassphrasePrompt( try (BouncyCastleGpgKeyPassphrasePrompt passphrasePrompt = new BouncyCastleGpgKeyPassphrasePrompt(
credentialsProvider)) { credentialsProvider)) {
BouncyCastleGpgKey gpgKey = locateSigningKey(gpgSigningKey, BouncyCastleGpgKey gpgKey = locateSigningKey(gpgSigningKey,

+ 48
- 7
org.eclipse.jgit/src/org/eclipse/jgit/api/CommitCommand.java Voir le fichier

import org.eclipse.jgit.lib.FileMode; import org.eclipse.jgit.lib.FileMode;
import org.eclipse.jgit.lib.GpgConfig; import org.eclipse.jgit.lib.GpgConfig;
import org.eclipse.jgit.lib.GpgConfig.GpgFormat; import org.eclipse.jgit.lib.GpgConfig.GpgFormat;
import org.eclipse.jgit.lib.GpgObjectSigner;
import org.eclipse.jgit.lib.GpgSigner; import org.eclipse.jgit.lib.GpgSigner;
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectInserter; import org.eclipse.jgit.lib.ObjectInserter;


private GpgSigner gpgSigner; private GpgSigner gpgSigner;


private GpgConfig gpgConfig;

private CredentialsProvider credentialsProvider; private CredentialsProvider credentialsProvider;


/** /**
throw new ServiceUnavailableException( throw new ServiceUnavailableException(
JGitText.get().signingServiceUnavailable); JGitText.get().signingServiceUnavailable);
} }
gpgSigner.sign(commit, signingKey, committer,
credentialsProvider);
if (gpgSigner instanceof GpgObjectSigner) {
((GpgObjectSigner) gpgSigner).signObject(commit,
signingKey, committer, credentialsProvider,
gpgConfig);
} else {
if (gpgConfig.getKeyFormat() != GpgFormat.OPENPGP) {
throw new UnsupportedSigningFormatException(JGitText
.get().onlyOpenPgpSupportedForSigning);
}
gpgSigner.sign(commit, signingKey, committer,
credentialsProvider);
}
} }


ObjectId commitId = odi.insert(commit); ObjectId commitId = odi.insert(commit);
// an explicit message // an explicit message
throw new NoMessageException(JGitText.get().commitMessageNotSpecified); throw new NoMessageException(JGitText.get().commitMessageNotSpecified);


GpgConfig gpgConfig = new GpgConfig(repo.getConfig());
if (gpgConfig == null) {
gpgConfig = new GpgConfig(repo.getConfig());
}
if (signCommit == null) { if (signCommit == null) {
signCommit = gpgConfig.isSignCommits() ? Boolean.TRUE signCommit = gpgConfig.isSignCommits() ? Boolean.TRUE
: Boolean.FALSE; : Boolean.FALSE;
signingKey = gpgConfig.getSigningKey(); signingKey = gpgConfig.getSigningKey();
} }
if (gpgSigner == null) { if (gpgSigner == null) {
if (gpgConfig.getKeyFormat() != GpgFormat.OPENPGP) {
throw new UnsupportedSigningFormatException(
JGitText.get().onlyOpenPgpSupportedForSigning);
}
gpgSigner = GpgSigner.getDefault(); gpgSigner = GpgSigner.getDefault();
} }
} }
return this; return this;
} }


/**
* Sets the {@link GpgSigner} to use if the commit is to be signed.
*
* @param signer
* to use; if {@code null}, the default signer will be used
* @return {@code this}
* @since 5.11
*/
public CommitCommand setGpgSigner(GpgSigner signer) {
checkCallable();
this.gpgSigner = signer;
return this;
}

/**
* Sets an external {@link GpgConfig} to use. Whether it will be used is at
* the discretion of the {@link #setGpgSigner(GpgSigner)}.
*
* @param config
* to set; if {@code null}, the config will be loaded from the
* git config of the repository
* @return {@code this}
* @since 5.11
*/
public CommitCommand setGpgConfig(GpgConfig config) {
checkCallable();
this.gpgConfig = config;
return this;
}

/** /**
* Sets a {@link CredentialsProvider} * Sets a {@link CredentialsProvider}
* *

+ 44
- 12
org.eclipse.jgit/src/org/eclipse/jgit/api/TagCommand.java Voir le fichier

import org.eclipse.jgit.internal.JGitText; import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.GpgConfig; import org.eclipse.jgit.lib.GpgConfig;
import org.eclipse.jgit.lib.GpgConfig.GpgFormat;
import org.eclipse.jgit.lib.GpgObjectSigner; import org.eclipse.jgit.lib.GpgObjectSigner;
import org.eclipse.jgit.lib.GpgSigner; import org.eclipse.jgit.lib.GpgSigner;
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.RepositoryState; import org.eclipse.jgit.lib.RepositoryState;
import org.eclipse.jgit.lib.TagBuilder; import org.eclipse.jgit.lib.TagBuilder;
import org.eclipse.jgit.lib.GpgConfig.GpgFormat;
import org.eclipse.jgit.revwalk.RevObject; import org.eclipse.jgit.revwalk.RevObject;
import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.transport.CredentialsProvider; import org.eclipse.jgit.transport.CredentialsProvider;


private String signingKey; private String signingKey;


private GpgConfig gpgConfig;

private GpgObjectSigner gpgSigner; private GpgObjectSigner gpgSigner;


private CredentialsProvider credentialsProvider; private CredentialsProvider credentialsProvider;


if (gpgSigner != null) { if (gpgSigner != null) {
gpgSigner.signObject(newTag, signingKey, tagger, gpgSigner.signObject(newTag, signingKey, tagger,
credentialsProvider);
credentialsProvider, gpgConfig);
} }


// write the tag object // write the tag object
} }
// Figure out whether to sign. // Figure out whether to sign.
if (!(Boolean.FALSE.equals(signed) && signingKey == null)) { if (!(Boolean.FALSE.equals(signed) && signingKey == null)) {
GpgConfig gpgConfig = new GpgConfig(repo.getConfig());
if (gpgConfig == null) {
gpgConfig = new GpgConfig(repo.getConfig());
}
boolean doSign = isSigned() || gpgConfig.isSignAllTags(); boolean doSign = isSigned() || gpgConfig.isSignAllTags();
if (!Boolean.TRUE.equals(annotated) && !doSign) { if (!Boolean.TRUE.equals(annotated) && !doSign) {
doSign = gpgConfig.isSignAnnotated(); doSign = gpgConfig.isSignAnnotated();
if (signingKey == null) { if (signingKey == null) {
signingKey = gpgConfig.getSigningKey(); signingKey = gpgConfig.getSigningKey();
} }
if (gpgConfig.getKeyFormat() != GpgFormat.OPENPGP) {
throw new UnsupportedSigningFormatException(
JGitText.get().onlyOpenPgpSupportedForSigning);
}
GpgSigner signer = GpgSigner.getDefault();
if (!(signer instanceof GpgObjectSigner)) {
throw new ServiceUnavailableException(
JGitText.get().signingServiceUnavailable);
if (gpgSigner == null) {
GpgSigner signer = GpgSigner.getDefault();
if (!(signer instanceof GpgObjectSigner)) {
throw new ServiceUnavailableException(
JGitText.get().signingServiceUnavailable);
}
gpgSigner = (GpgObjectSigner) signer;
} }
gpgSigner = (GpgObjectSigner) signer;
// The message of a signed tag must end in a newline because // The message of a signed tag must end in a newline because
// the signature will be appended. // the signature will be appended.
if (message != null && !message.isEmpty() if (message != null && !message.isEmpty()
return this; return this;
} }


/**
* Sets the {@link GpgSigner} to use if the commit is to be signed.
*
* @param signer
* to use; if {@code null}, the default signer will be used
* @return {@code this}
* @since 5.11
*/
public TagCommand setGpgSigner(GpgObjectSigner signer) {
checkCallable();
this.gpgSigner = signer;
return this;
}

/**
* Sets an external {@link GpgConfig} to use. Whether it will be used is at
* the discretion of the {@link #setGpgSigner(GpgObjectSigner)}.
*
* @param config
* to set; if {@code null}, the config will be loaded from the
* git config of the repository
* @return {@code this}
* @since 5.11
*/
public TagCommand setGpgConfig(GpgConfig config) {
checkCallable();
this.gpgConfig = config;
return this;
}

/** /**
* Sets the tagger of the tag. If the tagger is null, a PersonIdent will be * Sets the tagger of the tag. If the tagger is null, a PersonIdent will be
* created from the info in the repository. * created from the info in the repository.

+ 8
- 0
org.eclipse.jgit/src/org/eclipse/jgit/lib/ConfigConstants.java Voir le fichier

*/ */
public static final String CONFIG_KEY_FORMAT = "format"; public static final String CONFIG_KEY_FORMAT = "format";


/**
* The "program" key
*
* @since 5.11
*/
public static final String CONFIG_KEY_PROGRAM = "program";

/** /**
* The "signingKey" key * The "signingKey" key
*
* @since 5.2 * @since 5.2
*/ */
public static final String CONFIG_KEY_SIGNINGKEY = "signingKey"; public static final String CONFIG_KEY_SIGNINGKEY = "signingKey";

+ 70
- 15
org.eclipse.jgit/src/org/eclipse/jgit/lib/GpgConfig.java Voir le fichier

/* /*
* Copyright (C) 2018, Salesforce. and others
* Copyright (C) 2018, 2021 Salesforce and others
* *
* This program and the accompanying materials are made available under the * This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at * terms of the Eclipse Distribution License v. 1.0 which is available at
} }
} }


private final Config config;
private final GpgFormat keyFormat;

private final String signingKey;

private final String program;

private final boolean signCommits;

private final boolean signAllTags;

private final boolean forceAnnotated;


/** /**
* Create a new GPG config, which will read configuration from config.
* Create a {@link GpgConfig} with the given parameters and default
* {@code true} for signing commits and {@code false} for tags.
*
* @param keySpec
* to use
* @param format
* to use
* @param gpgProgram
* to use
* @since 5.11
*/
public GpgConfig(String keySpec, GpgFormat format, String gpgProgram) {
keyFormat = format;
signingKey = keySpec;
program = gpgProgram;
signCommits = true;
signAllTags = false;
forceAnnotated = false;
}

/**
* Create a new GPG config that reads the configuration from config.
* *
* @param config * @param config
* the config to read from * the config to read from
*/ */
public GpgConfig(Config config) { public GpgConfig(Config config) {
this.config = config;
keyFormat = config.getEnum(GpgFormat.values(),
ConfigConstants.CONFIG_GPG_SECTION, null,
ConfigConstants.CONFIG_KEY_FORMAT, GpgFormat.OPENPGP);
signingKey = config.getString(ConfigConstants.CONFIG_USER_SECTION, null,
ConfigConstants.CONFIG_KEY_SIGNINGKEY);

String exe = config.getString(ConfigConstants.CONFIG_GPG_SECTION,
keyFormat.toConfigValue(), ConfigConstants.CONFIG_KEY_PROGRAM);
if (exe == null) {
exe = config.getString(ConfigConstants.CONFIG_GPG_SECTION, null,
ConfigConstants.CONFIG_KEY_PROGRAM);
}
program = exe;
signCommits = config.getBoolean(ConfigConstants.CONFIG_COMMIT_SECTION,
ConfigConstants.CONFIG_KEY_GPGSIGN, false);
signAllTags = config.getBoolean(ConfigConstants.CONFIG_TAG_SECTION,
ConfigConstants.CONFIG_KEY_GPGSIGN, false);
forceAnnotated = config.getBoolean(ConfigConstants.CONFIG_TAG_SECTION,
ConfigConstants.CONFIG_KEY_FORCE_SIGN_ANNOTATED, false);
} }


/** /**
* @return the {@link org.eclipse.jgit.lib.GpgConfig.GpgFormat} * @return the {@link org.eclipse.jgit.lib.GpgConfig.GpgFormat}
*/ */
public GpgFormat getKeyFormat() { public GpgFormat getKeyFormat() {
return config.getEnum(GpgFormat.values(),
ConfigConstants.CONFIG_GPG_SECTION, null,
ConfigConstants.CONFIG_KEY_FORMAT, GpgFormat.OPENPGP);
return keyFormat;
}

/**
* Retrieves the value of the configured GPG program to use, as defined by
* gpg.openpgp.program, gpg.x509.program (depending on the defined
* {@link #getKeyFormat() format}), or gpg.program.
*
* @return the program string configured, or {@code null} if none
* @since 5.11
*/
public String getProgram() {
return program;
} }


/** /**
* @return the value of user.signingKey (may be <code>null</code>) * @return the value of user.signingKey (may be <code>null</code>)
*/ */
public String getSigningKey() { public String getSigningKey() {
return config.getString(ConfigConstants.CONFIG_USER_SECTION, null,
ConfigConstants.CONFIG_KEY_SIGNINGKEY);
return signingKey;
} }


/** /**
* @return the value of commit.gpgSign (defaults to <code>false</code>) * @return the value of commit.gpgSign (defaults to <code>false</code>)
*/ */
public boolean isSignCommits() { public boolean isSignCommits() {
return config.getBoolean(ConfigConstants.CONFIG_COMMIT_SECTION,
ConfigConstants.CONFIG_KEY_GPGSIGN, false);
return signCommits;
} }


/** /**
* @since 5.11 * @since 5.11
*/ */
public boolean isSignAllTags() { public boolean isSignAllTags() {
return config.getBoolean(ConfigConstants.CONFIG_TAG_SECTION,
ConfigConstants.CONFIG_KEY_GPGSIGN, false);
return signAllTags;
} }


/** /**
* @since 5.11 * @since 5.11
*/ */
public boolean isSignAnnotated() { public boolean isSignAnnotated() {
return config.getBoolean(ConfigConstants.CONFIG_TAG_SECTION,
ConfigConstants.CONFIG_KEY_FORCE_SIGN_ANNOTATED, false);
return forceAnnotated;
} }
} }

+ 37
- 1
org.eclipse.jgit/src/org/eclipse/jgit/lib/GpgObjectSigner.java Voir le fichier

import org.eclipse.jgit.annotations.NonNull; import org.eclipse.jgit.annotations.NonNull;
import org.eclipse.jgit.annotations.Nullable; import org.eclipse.jgit.annotations.Nullable;
import org.eclipse.jgit.api.errors.CanceledException; import org.eclipse.jgit.api.errors.CanceledException;
import org.eclipse.jgit.api.errors.UnsupportedSigningFormatException;
import org.eclipse.jgit.transport.CredentialsProvider; import org.eclipse.jgit.transport.CredentialsProvider;


/** /**
* @param credentialsProvider * @param credentialsProvider
* provider to use when querying for signing key credentials (eg. * provider to use when querying for signing key credentials (eg.
* passphrase) * passphrase)
* @param config
* GPG settings from the git config
* @throws CanceledException * @throws CanceledException
* when signing was canceled (eg., user aborted when entering * when signing was canceled (eg., user aborted when entering
* passphrase) * passphrase)
* @throws UnsupportedSigningFormatException
* if a config is given and the wanted key format is not
* supported
*/ */
void signObject(@NonNull ObjectBuilder object, void signObject(@NonNull ObjectBuilder object,
@Nullable String gpgSigningKey, @NonNull PersonIdent committer, @Nullable String gpgSigningKey, @NonNull PersonIdent committer,
CredentialsProvider credentialsProvider) throws CanceledException;
CredentialsProvider credentialsProvider, GpgConfig config)
throws CanceledException, UnsupportedSigningFormatException;

/**
* Indicates if a signing key is available for the specified committer
* and/or signing key.
*
* @param gpgSigningKey
* the signing key to locate (passed as is to the GPG signing
* tool as is; eg., value of <code>user.signingkey</code>)
* @param committer
* the signing identity (to help with key lookup in case signing
* key is not specified)
* @param credentialsProvider
* provider to use when querying for signing key credentials (eg.
* passphrase)
* @param config
* GPG settings from the git config
* @return <code>true</code> if a signing key is available,
* <code>false</code> otherwise
* @throws CanceledException
* when signing was canceled (eg., user aborted when entering
* passphrase)
* @throws UnsupportedSigningFormatException
* if a config is given and the wanted key format is not
* supported
*/
public abstract boolean canLocateSigningKey(@Nullable String gpgSigningKey,
@NonNull PersonIdent committer,
CredentialsProvider credentialsProvider, GpgConfig config)
throws CanceledException, UnsupportedSigningFormatException;


} }

Chargement…
Annuler
Enregistrer