Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.codehaus.mojo.rpm;
  
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
  * with the License.  You may obtain a copy of the License at
  *
  *   http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
  */
 
 import java.io.File;
 
Utility that uses expect to sign rpms automatically (no user interaction).

Author(s):
Brett Okken, Cerner Corporation
Since:
2.0-beta-4
 
 final class RPMSigner
 {
    
GPG path.
 
     private final File gpgPath;

    
GPG name as defined in the rpm database.
 
     private final String gpgName;

    
The passphrase for the gpg key.
 
     private final char[] passphrase;

    
Log to log to.
 
     private final Log log;

    
Constructor takes all necessary attributes to sign an rpm.

Parameters:
gpgPath Directory containing key data.
gpgName The name of the gpg key in the rpm database.
passphrase The passphrase for the gpg key.
log Used for logging information in the signing process.
 
     public RPMSignerFile gpgPathString gpgNamechar[] passphraseLog log )
     {
         this. = gpgPath;
         this. = gpgName;
         this. = passphrase;
         this. = log;
     }

    
Signs the rpm using the gpgName and passphrase given. All output from the signing will be written to log.

Parameters:
rpm RPM file to sign. Must exist and be readable.
Throws:
java.io.IOException
 
     public void signfinal File rpm )
         throws IOException
     {
         if ( !rpm.exists() || !rpm.canRead() )
         {
             throw new IllegalStateExceptionrpm.getAbsolutePath() + " is not a valid rpm file or cannot be read" );
         }
 
         // use option to provide "script" via stdin
         final Commandline cl = new Commandline();
         cl.setExecutable"expect" );
         cl.setWorkingDirectoryrpm.getParentFile() );
         cl.createArg().setValue"-c" );
         //work around to allow expect execution time to read in the input during heavy system usage
        cl.createArg().setValue"sleep 1" ); //MRPM-176 and PLXUTILS-174
        cl.createArg().setValue"-" );
        final StreamConsumer stdout = new LogStreamConsumer. );
        final StreamConsumer stderr = new LogStreamConsumer. );
        try
        {
            if ( .isDebugEnabled() )
            {
                .debug"About to execute \'" + cl.toString() + "\'" );
            }
            final InputStream is = new ByteArrayInputStreamwriteExpectScriptFilerpm ) );
            int result = CommandLineUtils.executeCommandLineclisstdoutstderr );
            if ( result != 0 )
            {
                throw new IllegalStateException"RPM sign execution returned: \'" + result + "\' executing \'"
                    + cl.toString() + "\'" );
            }
        }
        catch ( CommandLineException e )
        {
            final IllegalStateException ise = new IllegalStateException"Unable to sign the RPM" );
            ise.initCausee );
            throw ise;
        }
    }

    
Writes the expect "script".

Parameters:
rpm The rpm to sign.
Returns:
The expect script as a byte[].
Throws:
java.io.IOException
    private byte[] writeExpectScriptFilefinal File rpm )
        throws IOException
    {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream( 512 );
        final PrintWriter writer = new PrintWriternew OutputStreamWriterbaos ) );
        try
        {
            writer.println"set timeout -1" );
            writer.print"spawn rpm --define \"_gpg_name " );
            writer.print );
            writer.print"\"" );
            if (  != null )
            {
                writer.print" --define \"_gpg_path " );
                writer.print + "\"" );
            }
            writer.print" --addsign " );
            writer.printlnrpm.getName() );
            writer.println"expect -exact \"Enter pass phrase: \"" );
            writer.print"send -- \"" );
            writer.printnew Stringthis. ) );
            writer.println"\r\"" );
            writer.println"expect {" );
            writer.println" \"Pass phrase is good.\" {" );
            writer.println"      expect eof" );
            writer.println"      exit 0" );
            writer.println"  }" );
            writer.println" \"signing failed\" {" );
            writer.println"      expect eof" );
            writer.println"      exit 1" );
            writer.println"  }" );
            writer.println"}" );
            writer.println();
            writer.flush();
        }
        finally
        {
            writer.close();
        }
        return baos.toByteArray();
    }
New to GrepCode? Check out our FAQ X