agave-common/common-core/src/main/java/org/iplantc/service/common/auth/MyProxy.java
/**
     * Bootstraps trustroot information from the MyProxy server.
     *
     * @exception MyProxyException
     *         If an error occurred during the operation.
     */
Bootstraps trustroot information from the MyProxy server.
@exception MyProxyException
If an error occurred during the operation.
Bootstraps trustroot information from the MyProxy server . @exception MyProxyException If an error occurred during the operation .
@Override
    public void bootstrapTrust() throws MyProxyException {
        try {
            SSLContext sc = SSLContext.getInstance("SSL");
            MyTrustManager myTrustManager = new MyTrustManager();
            TrustManager[] trustAllCerts = new TrustManager[] { myTrustManager };
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            SSLSocketFactory sf = sc.getSocketFactory();
            SSLSocket socket = (SSLSocket)sf.createSocket(this.host, this.port);
            socket.setEnabledProtocols(new String[] { "SSLv3", "TLSv1" });
            socket.startHandshake();
            socket.close();
            X509Certificate[] acceptedIssuers = myTrustManager.getAcceptedIssuers();
            if (acceptedIssuers == null) {
                throw new MyProxyException("Failed to determine MyProxy server trust roots in bootstrapTrust.");
            }
            for (int idx = 0; idx < acceptedIssuers.length; idx++)
            {
                File x509Dir = new File(this.trustrootPath);
                StringBuffer newSubject = new StringBuffer();
                String[] subjArr = acceptedIssuers[idx].getSubjectDN().getName().split(", ");
                for(int i = (subjArr.length - 1); i > -1; i--)
                {
                    newSubject.append("/");
                    newSubject.append(subjArr[i]);
                }
                String subject = newSubject.toString();
                File tmpDir = new File(getTrustRootPath() + "-" +
                                       System.currentTimeMillis());
                if (tmpDir.mkdir() == true)
                {
                    String hash = opensslHash(acceptedIssuers[idx]);
                    String filename = tmpDir.getPath() + File.separator + hash + ".0";
                    FileOutputStream os = new FileOutputStream(new File(filename));
                    CertificateIOUtil.writeCertificate(os, acceptedIssuers[idx]);
                    os.close();
                    if (logger.isDebugEnabled()) {
                        logger.debug("wrote trusted certificate to " + filename);
                    }
                    filename = tmpDir.getPath() + File.separator + hash + ".signing_policy";
                    os = new FileOutputStream(new File(filename));
                    Writer wr = new OutputStreamWriter(os, StandardCharsets.UTF_8);
                    wr.write("access_id_CA X509 '");
                    wr.write(subject);
                    wr.write("'\npos_rights globus CA:sign\ncond_subjects globus \"*\"\n");
                    wr.flush();
                    wr.close();
                    os.close();
                    if (logger.isDebugEnabled()) {
                        logger.debug("wrote trusted certificate policy to " + filename);
                    }
                    if (x509Dir.exists())
                    {
                        for (File certFile: tmpDir.listFiles()) {
                            FileUtils.copyFileToDirectory(certFile, x509Dir);
                            if (logger.isDebugEnabled()) {
                                logger.debug("copied " + certFile.getPath() + " to " +
                                             x509Dir.getPath());
                            }
                        }
                    }
                    else {
                        if (tmpDir.renameTo(x509Dir) == true)
                        {
                            if (logger.isDebugEnabled()) {
                                logger.debug("renamed " + tmpDir.getPath() + " to " +
                                             x509Dir.getPath());
                            }
                        }
                        else
                        {
                            throw new MyProxyException("Unable to rename " + tmpDir.getPath() + " to " +
                                                x509Dir.getPath());
                        }
                    }
                }
                else
                {
                    throw new MyProxyException("Cannot create temporary directory: " + tmpDir.getName());
                }
            }
        } catch(Exception e) {
        	throw new MyProxyException("MyProxy bootstrapTrust failed.", e);
        }
    }
@ Override public void bootstrapTrust ( ) throws MyProxyException { try { SSLContext sc = SSLContext . getInstance ( "SSL" ) ; MyTrustManager myTrustManager = new MyTrustManager ( ) ; TrustManager [ ] trustAllCerts = new TrustManager [ ] { myTrustManager } ; sc . init ( null , trustAllCerts , new java . security . SecureRandom ( ) ) ; SSLSocketFactory sf = sc . getSocketFactory ( ) ; SSLSocket socket = ( SSLSocket ) sf . createSocket ( this . host , this . port ) ; socket . setEnabledProtocols ( new String [ ] { "SSLv3" , "TLSv1" } ) ; socket . startHandshake ( ) ; socket . close ( ) ; X509Certificate [ ] acceptedIssuers = myTrustManager . getAcceptedIssuers ( ) ; if ( acceptedIssuers == null ) { throw new MyProxyException ( "Failed to determine MyProxy server trust roots in bootstrapTrust." ) ; } for ( int idx = 0 ; idx < acceptedIssuers . length ; idx ++ ) { File x509Dir = new File ( this . trustrootPath ) ; StringBuffer newSubject = new StringBuffer ( ) ; String [ ] subjArr = acceptedIssuers [ idx ] . getSubjectDN ( ) . getName ( ) . split ( ", " ) ; for ( int i = ( subjArr . length - 1 ) ; i > - 1 ; i -- ) { newSubject . append ( "/" ) ; newSubject . append ( subjArr [ i ] ) ; } String subject = newSubject . toString ( ) ; File tmpDir = new File ( getTrustRootPath ( ) + "-" + System . currentTimeMillis ( ) ) ; if ( tmpDir . mkdir ( ) == true ) { String hash = opensslHash ( acceptedIssuers [ idx ] ) ; String filename = tmpDir . getPath ( ) + File . separator + hash + ".0" ; FileOutputStream os = new FileOutputStream ( new File ( filename ) ) ; CertificateIOUtil . writeCertificate ( os , acceptedIssuers [ idx ] ) ; os . close ( ) ; if ( logger . isDebugEnabled ( ) ) { logger . debug ( "wrote trusted certificate to " + filename ) ; } filename = tmpDir . getPath ( ) + File . separator + hash + ".signing_policy" ; os = new FileOutputStream ( new File ( filename ) ) ; Writer wr = new OutputStreamWriter ( os , StandardCharsets . UTF_8 ) ; wr . write ( "access_id_CA X509 '" ) ; wr . write ( subject ) ; wr . write ( "'\npos_rights globus CA:sign\ncond_subjects globus \"*\"\n" ) ; wr . flush ( ) ; wr . close ( ) ; os . close ( ) ; if ( logger . isDebugEnabled ( ) ) { logger . debug ( "wrote trusted certificate policy to " + filename ) ; } if ( x509Dir . exists ( ) ) { for ( File certFile : tmpDir . listFiles ( ) ) { FileUtils . copyFileToDirectory ( certFile , x509Dir ) ; if ( logger . isDebugEnabled ( ) ) { logger . debug ( "copied " + certFile . getPath ( ) + " to " + x509Dir . getPath ( ) ) ; } } } else { if ( tmpDir . renameTo ( x509Dir ) == true ) { if ( logger . isDebugEnabled ( ) ) { logger . debug ( "renamed " + tmpDir . getPath ( ) + " to " + x509Dir . getPath ( ) ) ; } } else { throw new MyProxyException ( "Unable to rename " + tmpDir . getPath ( ) + " to " + x509Dir . getPath ( ) ) ; } } } else { throw new MyProxyException ( "Cannot create temporary directory: " + tmpDir . getName ( ) ) ; } } } catch ( Exception e ) { throw new MyProxyException ( "MyProxy bootstrapTrust failed." , e ) ; } }
Bootstraps trustroot information from the MyProxy server.
Bootstraps trustroot information from the MyProxy server .
// success. commit the bootstrapped directory if a ca directory was not already there // copy in each file, overwriting the existing one