27
27
import org .apache .maven .plugin .MojoFailureException ;
28
28
import org .apache .maven .plugins .annotations .Component ;
29
29
import org .apache .maven .plugins .annotations .Parameter ;
30
+ import org .apache .maven .project .MavenProject ;
31
+ import org .apache .maven .settings .Server ;
32
+ import org .apache .maven .settings .Settings ;
33
+ import org .sonatype .plexus .components .sec .dispatcher .SecDispatcher ;
34
+ import org .sonatype .plexus .components .sec .dispatcher .SecDispatcherException ;
30
35
31
36
/**
32
37
* @author Benjamin Bentmann
@@ -248,14 +253,47 @@ public abstract class AbstractGpgMojo extends AbstractMojo {
248
253
@ Component
249
254
protected MavenSession session ;
250
255
256
+ // === Deprecated stuff
257
+
258
+ /**
259
+ * Switch to lax plugin enforcement of "best practices". If set to {@code false}, plugin will retain all the
260
+ * backward compatibility regarding getting secrets (but will warn). By default, plugin enforces "best practices"
261
+ * and in such cases plugin fails.
262
+ *
263
+ * @since 3.2.0
264
+ * @deprecated
265
+ */
266
+ @ Deprecated
267
+ @ Parameter (property = "gpg.bestPractices" , defaultValue = "true" )
268
+ private boolean bestPractices ;
269
+
270
+ /**
271
+ * Current user system settings for use in Maven.
272
+ *
273
+ * @since 1.6
274
+ * @deprecated
275
+ */
276
+ @ Deprecated
277
+ @ Parameter (defaultValue = "${settings}" , readonly = true , required = true )
278
+ private Settings settings ;
279
+
280
+ /**
281
+ * Maven Security Dispatcher.
282
+ *
283
+ * @since 1.6
284
+ * @deprecated
285
+ */
286
+ @ Deprecated
287
+ @ Component
288
+ private SecDispatcher secDispatcher ;
289
+
251
290
@ Override
252
291
public final void execute () throws MojoExecutionException , MojoFailureException {
253
292
if (skip ) {
254
293
// We're skipping the signing stuff
255
294
return ;
256
295
}
257
- if ((passphrase != null && !passphrase .trim ().isEmpty ())
258
- || (passphraseServerId != null && !passphraseServerId .trim ().isEmpty ())) {
296
+ if (bestPractices && (isNotBlank (passphrase ) || isNotBlank (passphraseServerId ))) {
259
297
// Stop propagating worst practices: passphrase MUST NOT be in any file on disk
260
298
throw new MojoFailureException (
261
299
"Do not store passphrase in any file (disk or SCM repository), rely on GnuPG agent or provide passphrase in "
@@ -267,7 +305,19 @@ public final void execute() throws MojoExecutionException, MojoFailureException
267
305
268
306
protected abstract void doExecute () throws MojoExecutionException , MojoFailureException ;
269
307
270
- protected AbstractGpgSigner newSigner () throws MojoFailureException {
308
+ private void logBestPracticeWarning (String source ) {
309
+ getLog ().warn ("" );
310
+ getLog ().warn ("W A R N I N G" );
311
+ getLog ().warn ("" );
312
+ getLog ().warn ("Do not store passphrase in any file (disk or SCM repository)," );
313
+ getLog ().warn ("instead rely on GnuPG agent in interactive sessions, or provide passphrase in " );
314
+ getLog ().warn (passphraseEnvName + " environment variable for batch mode." );
315
+ getLog ().warn ("" );
316
+ getLog ().warn ("Sensitive content loaded from " + source );
317
+ getLog ().warn ("" );
318
+ }
319
+
320
+ protected AbstractGpgSigner newSigner (MavenProject mavenProject ) throws MojoFailureException {
271
321
AbstractGpgSigner signer ;
272
322
if (GpgSigner .NAME .equals (this .signer )) {
273
323
signer = new GpgSigner (executable );
@@ -294,10 +344,32 @@ protected AbstractGpgSigner newSigner() throws MojoFailureException {
294
344
signer .setLockMode (lockMode );
295
345
signer .setArgs (gpgArguments );
296
346
347
+ // "new way": env prevails
297
348
String passphrase =
298
349
(String ) session .getRepositorySession ().getConfigProperties ().get ("env." + passphraseEnvName );
299
- if (passphrase != null ) {
350
+ if (isNotBlank ( passphrase ) ) {
300
351
signer .setPassPhrase (passphrase );
352
+ } else if (!bestPractices ) {
353
+ // "old way": mojo config
354
+ passphrase = this .passphrase ;
355
+ if (isNotBlank (passphrase )) {
356
+ logBestPracticeWarning ("Mojo configuration" );
357
+ signer .setPassPhrase (passphrase );
358
+ } else {
359
+ // "old way": serverId + settings
360
+ passphrase = loadGpgPassphrase ();
361
+ if (isNotBlank (passphrase )) {
362
+ logBestPracticeWarning ("settings.xml" );
363
+ signer .setPassPhrase (passphrase );
364
+ } else {
365
+ // "old way": project properties
366
+ passphrase = getPassphrase (mavenProject );
367
+ if (isNotBlank (passphrase )) {
368
+ logBestPracticeWarning ("Project properties" );
369
+ signer .setPassPhrase (passphrase );
370
+ }
371
+ }
372
+ }
301
373
}
302
374
303
375
// gpg signer: always failed if no passphrase and no agent and not interactive: retain this behavior
@@ -310,4 +382,56 @@ protected AbstractGpgSigner newSigner() throws MojoFailureException {
310
382
311
383
return signer ;
312
384
}
385
+
386
+ private boolean isNotBlank (String string ) {
387
+ return string != null && !string .trim ().isEmpty ();
388
+ }
389
+
390
+ // Below is attic, to be thrown out
391
+
392
+ @ Deprecated
393
+ private static final String GPG_PASSPHRASE = "gpg.passphrase" ;
394
+
395
+ @ Deprecated
396
+ private String loadGpgPassphrase () throws MojoFailureException {
397
+ if (isNotBlank (passphrase )) {
398
+ Server server = settings .getServer (passphraseServerId );
399
+ if (server != null ) {
400
+ if (isNotBlank (server .getPassphrase ())) {
401
+ try {
402
+ return secDispatcher .decrypt (server .getPassphrase ());
403
+ } catch (SecDispatcherException e ) {
404
+ throw new MojoFailureException ("Unable to decrypt gpg passphrase" , e );
405
+ }
406
+ }
407
+ }
408
+ }
409
+ return null ;
410
+ }
411
+
412
+ @ Deprecated
413
+ public String getPassphrase (MavenProject project ) {
414
+ String pass = null ;
415
+ if (project != null ) {
416
+ pass = project .getProperties ().getProperty (GPG_PASSPHRASE );
417
+ if (pass == null ) {
418
+ MavenProject prj2 = findReactorProject (project );
419
+ pass = prj2 .getProperties ().getProperty (GPG_PASSPHRASE );
420
+ }
421
+ }
422
+ if (project != null ) {
423
+ findReactorProject (project ).getProperties ().setProperty (GPG_PASSPHRASE , pass );
424
+ }
425
+ return pass ;
426
+ }
427
+
428
+ @ Deprecated
429
+ private MavenProject findReactorProject (MavenProject prj ) {
430
+ if (prj .getParent () != null
431
+ && prj .getParent ().getBasedir () != null
432
+ && prj .getParent ().getBasedir ().exists ()) {
433
+ return findReactorProject (prj .getParent ());
434
+ }
435
+ return prj ;
436
+ }
313
437
}
0 commit comments