[Swift-commit] cog r3439

swift at ci.uchicago.edu swift at ci.uchicago.edu
Thu Aug 2 01:40:03 CDT 2012


------------------------------------------------------------------------
r3439 | hategan | 2012-08-02 01:38:52 -0500 (Thu, 02 Aug 2012) | 1 line

updated gt2 provider with changes from common and added proxy mapping file
------------------------------------------------------------------------
Index: modules/provider-gt2/src/org/globus/cog/abstraction/impl/file/gridftp/old/FileResourceImpl.java
===================================================================
--- modules/provider-gt2/src/org/globus/cog/abstraction/impl/file/gridftp/old/FileResourceImpl.java	(revision 3438)
+++ modules/provider-gt2/src/org/globus/cog/abstraction/impl/file/gridftp/old/FileResourceImpl.java	(working copy)
@@ -20,10 +20,8 @@
 import java.util.List;
 
 import org.apache.log4j.Logger;
-import org.globus.cog.abstraction.impl.common.AbstractionFactory;
 import org.globus.cog.abstraction.impl.common.task.IllegalSpecException;
 import org.globus.cog.abstraction.impl.common.task.InvalidSecurityContextException;
-import org.globus.cog.abstraction.impl.common.task.ServiceContactImpl;
 import org.globus.cog.abstraction.impl.common.task.TaskSubmissionException;
 import org.globus.cog.abstraction.impl.file.DirectoryNotFoundException;
 import org.globus.cog.abstraction.impl.file.FileResourceException;
@@ -92,15 +90,15 @@
     private boolean bufferSizeChanged;
 
     /** throws InvalidProviderException */
-    public FileResourceImpl() throws Exception {
-        this(null, new ServiceContactImpl(), AbstractionFactory
-            .newSecurityContext("GridFTP"));
+    public FileResourceImpl() {
+        this(null, null, null);
     }
 
     /** constructor be used normally */
     public FileResourceImpl(String name, ServiceContact serviceContact,
             SecurityContext securityContext) {
-        super(name == null ? serviceContact.toString() : name, "gsiftp", serviceContact, securityContext);
+        super((name == null && !(serviceContact == null)) ? serviceContact.toString() : name, 
+                "gsiftp", serviceContact, securityContext);
     }
 
     /**
@@ -111,9 +109,11 @@
      */
     public void start() throws IllegalHostException,
             InvalidSecurityContextException, FileResourceException {
+        
+        ServiceContact serviceContact = getAndCheckServiceContact();
 
-        String host = getServiceContact().getHost();
-        int port = getServiceContact().getPort();
+        String host = serviceContact.getHost();
+        int port = serviceContact.getPort();
         if (port == -1) {
             port = 2811;
         }
@@ -123,6 +123,8 @@
         }
         
         try {
+            SecurityContext securityContext = getOrCreateSecurityContext("gsiftp", serviceContact);            
+            
             gridFTPClient = new GridFTPClient(host, port);
             Reply r = gridFTPClient.getLastReply();
 
@@ -140,11 +142,10 @@
             if (logger.isDebugEnabled()) {
                 logger.debug("Data channel reuse: " + dataChannelReuse);
             }
-            gridFTPClient.setClientWaitParams(MAX_REPLY_WAIT_TIME,
-                Session.DEFAULT_WAIT_DELAY);
-            GSSCredential proxy = (GSSCredential) getSecurityContext()
-                .getCredentials();
-            gridFTPClient.authenticate(proxy);
+            gridFTPClient.setClientWaitParams(MAX_REPLY_WAIT_TIME, Session.DEFAULT_WAIT_DELAY);
+            
+            GSSCredential cred = (GSSCredential) securityContext.getCredentials();
+            gridFTPClient.authenticate(cred);
             gridFTPClient.setType(Session.TYPE_IMAGE);
             if (dataChannelReuse) {
                 gridFTPClient.setMode(GridFTPSession.MODE_EBLOCK);
@@ -155,7 +156,7 @@
         }
         catch (Exception e) {
             throw translateException(
-                "Error communicating with the GridFTP server at " + host + ":" + port, e);
+                "Error connecting to the GridFTP server at " + host + ":" + port, e);
         }
     }
 
Index: modules/provider-gt2/src/org/globus/cog/abstraction/impl/file/ftp/InteractiveFTPSecurityContextImpl.java
===================================================================
--- modules/provider-gt2/src/org/globus/cog/abstraction/impl/file/ftp/InteractiveFTPSecurityContextImpl.java	(revision 3438)
+++ modules/provider-gt2/src/org/globus/cog/abstraction/impl/file/ftp/InteractiveFTPSecurityContextImpl.java	(working copy)
@@ -6,33 +6,7 @@
 
 package org.globus.cog.abstraction.impl.file.ftp;
 
-import java.net.PasswordAuthentication;
+import org.globus.cog.abstraction.impl.common.InteractivePasswordSecurityContextImpl;
 
-import org.apache.log4j.Logger;
-import org.globus.cog.abstraction.impl.common.task.SecurityContextImpl;
-
-public class InteractiveFTPSecurityContextImpl extends SecurityContextImpl {
-
-    private static Logger logger = Logger
-            .getLogger(InteractiveFTPSecurityContextImpl.class.getName());
-
-    public InteractiveFTPSecurityContextImpl() {
-    }
-
-    public InteractiveFTPSecurityContextImpl(PasswordAuthentication credentials) {
-        setCredentials(credentials);
-    }
-
-    public void setCredentials(Object credentials, String alias) {
-        setCredentials(credentials);
-    }
-
-    public synchronized Object getCredentials() {
-        Object credentials = getCredentials();
-        if (credentials == null) {
-            credentials = CredentialsDialog.showCredentialsDialog();
-            setCredentials(credentials);
-        }
-        return credentials;
-    }
+public class InteractiveFTPSecurityContextImpl extends InteractivePasswordSecurityContextImpl {
 }
Index: modules/provider-gt2/src/org/globus/cog/abstraction/impl/file/ftp/FileResourceImpl.java
===================================================================
--- modules/provider-gt2/src/org/globus/cog/abstraction/impl/file/ftp/FileResourceImpl.java	(revision 3438)
+++ modules/provider-gt2/src/org/globus/cog/abstraction/impl/file/ftp/FileResourceImpl.java	(working copy)
@@ -19,14 +19,13 @@
 import java.util.List;
 
 import org.apache.log4j.Logger;
-import org.globus.cog.abstraction.impl.common.AbstractionFactory;
 import org.globus.cog.abstraction.impl.common.task.IllegalSpecException;
 import org.globus.cog.abstraction.impl.common.task.InvalidSecurityContextException;
-import org.globus.cog.abstraction.impl.common.task.ServiceContactImpl;
 import org.globus.cog.abstraction.impl.common.task.TaskSubmissionException;
 import org.globus.cog.abstraction.impl.file.DirectoryNotFoundException;
 import org.globus.cog.abstraction.impl.file.FileResourceException;
 import org.globus.cog.abstraction.impl.file.GridFileImpl;
+import org.globus.cog.abstraction.impl.file.IllegalHostException;
 import org.globus.cog.abstraction.interfaces.ExecutableObject;
 import org.globus.cog.abstraction.interfaces.FileFragment;
 import org.globus.cog.abstraction.interfaces.FileResource;
@@ -54,14 +53,21 @@
 public class FileResourceImpl extends AbstractFTPFileResource {
     public static final String PROTOCOL = "ftp";
     
+    public static final String ANONYMOUS_USERNAME = "anonymous";
+    public static final char[] ANONYMOUS_PASSWORD;
+    static {
+        String pwd = "none at example.com";
+        ANONYMOUS_PASSWORD = new char[pwd.length()];
+        pwd.getChars(0, pwd.length(), ANONYMOUS_PASSWORD, 0);
+    }
+    
     private FTPClient ftpClient;
     public static final Logger logger = Logger.getLogger(FileResource.class
         .getName());
 
     /** throws invalidprovider exception */
-    public FileResourceImpl() throws Exception {
-        this(null, new ServiceContactImpl(), AbstractionFactory
-            .newSecurityContext("ftp"));
+    public FileResourceImpl() {
+        this(null, null, null);
     }
 
     /** the constructor to be used normally */
@@ -76,18 +82,29 @@
      * @throws FileResourceException
      *             if an exception occurs during the resource start-up
      */
-    public void start() throws InvalidSecurityContextException,
+    public void start() throws InvalidSecurityContextException, IllegalHostException,
             FileResourceException {
 
+        ServiceContact serviceContact = getAndCheckServiceContact();
+        
+        String host = getServiceContact().getHost();
+        int port = getServiceContact().getPort();
+        if (port == -1) {
+            port = 21;
+        }
+        
+        if (getName() == null) {
+            setName(host + ":" + port);
+        }
+        
+        
         try {
-            String host = getServiceContact().getHost();
-            int port = getServiceContact().getPort();
-            if (port == -1) {
-                port = 21;
-            }
+            SecurityContext securityContext = getOrCreateSecurityContext("ftp", serviceContact);
+            
+            PasswordAuthentication credentials = getCredentialsAsPasswordAuthentication(securityContext); 
+            
             ftpClient = new FTPClient(host, port);
-            PasswordAuthentication credentials = (PasswordAuthentication) getSecurityContext()
-                .getCredentials();
+
             String username = credentials.getUserName();
             String password = String.valueOf(credentials.getPassword());
 
@@ -95,11 +112,16 @@
             ftpClient.setType(Session.TYPE_IMAGE);
             setStarted(true);
         }
-        catch (Exception se) {
+        catch (Exception e) {
             throw translateException(
-                "Error while communicating with the FTP server", se);
+                "Error connecting to the FTP server at " + host + ":" + port, e);
         }
     }
+    
+    @Override
+    protected PasswordAuthentication getDefaultUsernameAndPassword() {
+        return new PasswordAuthentication(ANONYMOUS_USERNAME, ANONYMOUS_PASSWORD);
+    }
 
     /**
      * Stop the ftpClient from connecting to the server
Index: modules/provider-gt2/src/org/globus/cog/abstraction/impl/file/ftp/CredentialsDialog.java
===================================================================
--- modules/provider-gt2/src/org/globus/cog/abstraction/impl/file/ftp/CredentialsDialog.java	(revision 3438)
+++ modules/provider-gt2/src/org/globus/cog/abstraction/impl/file/ftp/CredentialsDialog.java	(working copy)
@@ -1,84 +0,0 @@
-//----------------------------------------------------------------------
-//This code is developed as part of the Java CoG Kit project
-//The terms of the license can be found at http://www.cogkit.org/license
-//This message may not be removed or altered.
-//----------------------------------------------------------------------
-
-package org.globus.cog.abstraction.impl.file.ftp;
-
-import java.awt.BorderLayout;
-import java.awt.Dimension;
-import java.awt.GridLayout;
-import java.net.PasswordAuthentication;
-
-import javax.swing.JDialog;
-import javax.swing.JLabel;
-import javax.swing.JOptionPane;
-import javax.swing.JPanel;
-import javax.swing.JPasswordField;
-import javax.swing.JTextField;
-
-public class CredentialsDialog {
-    private static final String NOTHING = "";
-
-    private JOptionPane optionPane = new JOptionPane();
-    private JDialog dialog;
-
-    private JTextField usernameField = new JTextField();
-    private JPasswordField passwordField = new JPasswordField();
-
-    private PasswordAuthentication result = null;
-
-    public CredentialsDialog() {
-        // init sizes
-        usernameField.setPreferredSize(new Dimension(125, 20));
-        passwordField.setPreferredSize(new Dimension(125, 20));
-
-        // the main panel
-        JPanel main = new JPanel(new BorderLayout());
-
-        // Labels
-        JPanel labels = new JPanel(new GridLayout(0, 1));
-        labels.add(new JLabel("Username: "));
-        labels.add(new JLabel("Password: "));
-
-        // username and password labels/fields
-        JPanel fields = new JPanel(new GridLayout(0, 1));
-        fields.add(usernameField);
-        fields.add(passwordField);
-
-        main.add(labels, BorderLayout.WEST);
-        main.add(fields, BorderLayout.CENTER);
-
-        optionPane.setMessage(main);
-        optionPane.setOptionType(JOptionPane.OK_CANCEL_OPTION);
-        dialog = optionPane.createDialog(null, "Enter Your FTP Credentials");
-    }
-
-    protected synchronized void okButtonPushed() {
-        String uname = usernameField.getText();
-        char[] passwd = passwordField.getPassword();
-        if (NOTHING.equals(uname) || NOTHING.equals(passwd)) {
-            result = null;
-        } else {
-            result = new PasswordAuthentication(uname, passwd);
-        }
-    }
-
-    public static PasswordAuthentication showCredentialsDialog() {
-        return new CredentialsDialog().getResult();
-    }
-
-    public PasswordAuthentication getResult() {
-        dialog.show();
-        if (optionPane.getValue() != null
-                && ((Integer) optionPane.getValue()).equals(new Integer(
-                        JOptionPane.OK_OPTION))) {
-            okButtonPushed();
-
-        } else {
-            result = null;
-        }
-        return result;
-    }
-}
\ No newline at end of file
Index: modules/provider-gt2/src/org/globus/cog/abstraction/impl/execution/gt2/GlobusSecurityContextImpl.java
===================================================================
--- modules/provider-gt2/src/org/globus/cog/abstraction/impl/execution/gt2/GlobusSecurityContextImpl.java	(revision 3438)
+++ modules/provider-gt2/src/org/globus/cog/abstraction/impl/execution/gt2/GlobusSecurityContextImpl.java	(working copy)
@@ -6,9 +6,20 @@
 
 package org.globus.cog.abstraction.impl.execution.gt2;
 
-import org.globus.cog.abstraction.impl.common.task.InvalidSecurityContextException;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import org.apache.log4j.Logger;
 import org.globus.cog.abstraction.impl.common.task.SecurityContextImpl;
 import org.globus.cog.abstraction.interfaces.Delegation;
+import org.globus.cog.abstraction.interfaces.ServiceContact;
+import org.globus.gsi.GlobusCredential;
+import org.globus.gsi.gssapi.GlobusGSSCredentialImpl;
 import org.globus.gsi.gssapi.auth.Authorization;
 import org.globus.gsi.gssapi.auth.HostAuthorization;
 import org.gridforum.jgss.ExtendedGSSManager;
@@ -17,14 +28,32 @@
 import org.ietf.jgss.GSSManager;
 
 public class GlobusSecurityContextImpl extends SecurityContextImpl implements Delegation {
+    public static final Logger logger = Logger.getLogger(GlobusSecurityContextImpl.class);
+    
     public static final int XML_ENCRYPTION = 1;
     public static final int XML_SIGNATURE = 2;
     
+    public static final String PROXY_HOST_PATH_MAPPING_FILE = System.getProperty("user.home") + 
+        File.separator + ".globus" + File.separator + "proxy.mapping"; 
+    
     public static final int DEFAULT_CREDENTIAL_REFRESH_INTERVAL = 30000;
-    private static GSSCredential cachedCredential;
-    private static long credentialTime;
+    private static Map<String, GSSCredential> cachedCredentials = new HashMap<String, GSSCredential>();
+    private static Map<String, Long> credentialTimes = new HashMap<String, Long>();
 
+    private static Properties proxyPaths;
+
+    public GlobusSecurityContextImpl() {
+    }
     
+    public GlobusSecurityContextImpl(String proxyPath) {
+        if (proxyPath == null) {
+            setCredentials(getDefaultCredentials());
+        }
+        else {
+            setCredentials(loadProxyFromFile(proxyPath));
+        }
+    }
+
     public void setAuthorization(Authorization authorization) {
         setAttribute("authorization", authorization);
     }
@@ -61,25 +90,104 @@
         return value.intValue();
     }
     
-    public GSSCredential getDefaultCredential() throws InvalidSecurityContextException {
-        return _getDefaultCredential();
+    public GSSCredential getDefaultCredentials() {
+        return _getDefaultCredential(getServiceContact());
     }
     
-    public static GSSCredential _getDefaultCredential() throws InvalidSecurityContextException {
-        synchronized (GlobusSecurityContextImpl.class) {
-            if (cachedCredential == null
-                    ||
-                    (System.currentTimeMillis() - credentialTime) > DEFAULT_CREDENTIAL_REFRESH_INTERVAL) {
-                credentialTime = System.currentTimeMillis();
-                GSSManager manager = ExtendedGSSManager.getInstance();
-                try {
-                    cachedCredential = manager.createCredential(GSSCredential.INITIATE_AND_ACCEPT);
+    @Override
+    public Object getCredentials() {
+        Object credentials = super.getCredentials();
+        if (credentials == null) {
+            return getDefaultCredentials();
+        }
+        else {
+            return credentials;
+        }
+    }
+
+    public static GSSCredential _getDefaultCredential(ServiceContact serviceContact) {
+        String host = null;
+        if (serviceContact != null) {
+            // null is OK
+            host = serviceContact.getHost();
+        }
+        loadProxyPaths();
+        synchronized (cachedCredentials) {
+            GSSCredential cachedCredential = cachedCredentials.get(host);
+            Long credentialTime = credentialTimes.get(host);
+            long now = System.currentTimeMillis();
+            if (cachedCredential == null || (now - credentialTime) > DEFAULT_CREDENTIAL_REFRESH_INTERVAL) {
+                if (cachedCredential == null) {
+                    if (logger.isInfoEnabled()) {
+                        logger.info("No cached credentials for " + host + ".");
+                    }
                 }
-                catch (GSSException e) {
-                    throw new InvalidSecurityContextException(e);
+                else {
+                    if (logger.isInfoEnabled()) {
+                        logger.info("Credentials for " + host + " need refreshing.");
+                    }
                 }
+                credentialTimes.put(host, now);
+                cachedCredential = loadCredential(host);
+                cachedCredentials.put(host, cachedCredential);
             }
             return cachedCredential;
         }
     }
+
+    private static GSSCredential loadCredential(String host) {
+        String proxyPath = null;
+                
+        if (host != null) {
+            proxyPath = (String) proxyPaths.get(host);
+        }
+                
+        if (proxyPath == null) {
+            if (logger.isInfoEnabled()) {
+                logger.info("No proxy mapping found for " + host + ". Loading default.");
+            }
+            return loadDefaultProxy();
+        }
+        else {
+            if (logger.isInfoEnabled()) {
+                logger.info("Proxy mapping found for " + host + ": " + proxyPath);
+            }
+            return loadProxyFromFile(proxyPath);
+        }
+    }
+
+    private static GSSCredential loadProxyFromFile(String proxyPath) {
+        try {
+            GlobusCredential cred = new GlobusCredential(proxyPath);
+            return new GlobusGSSCredentialImpl(cred, GSSCredential.INITIATE_AND_ACCEPT);
+        }
+        catch (Exception e) {
+            throw new SecurityException(e);
+        }
+    }
+
+    private static GSSCredential loadDefaultProxy() {
+        GSSManager manager = ExtendedGSSManager.getInstance();
+        try {
+            return manager.createCredential(GSSCredential.INITIATE_AND_ACCEPT);
+        }
+        catch (GSSException e) {
+            throw new SecurityException(e);
+        }
+    }
+
+    private static synchronized void loadProxyPaths() {
+        if (proxyPaths == null) {
+            proxyPaths = new Properties();
+            try {
+                proxyPaths.load(new FileInputStream(PROXY_HOST_PATH_MAPPING_FILE));
+            }
+            catch (FileNotFoundException e) {
+                // no mapping
+            }
+            catch (IOException e) {
+                logger.warn("Could not load host-proxy mapping file", e);
+            }
+        }
+    }
 }
\ No newline at end of file



More information about the Swift-commit mailing list