Did some improvements to gsi myproxy code
authoramilaj <amilaj@unknown>
Mon, 10 Jun 2013 22:25:31 +0000 (22:25 +0000)
committeramilaj <amilaj@unknown>
Mon, 10 Jun 2013 22:25:31 +0000 (22:25 +0000)
git-svn-id: https://svn.apache.org/repos/asf/airavata/sandbox@1491626 13f79535-47bb-0310-9956-ffa450edef68

grid-tools/gsi-myproxy-client/pom.xml
grid-tools/gsi-myproxy-client/run.sh
grid-tools/gsi-myproxy-client/src/main/java/org/apache/airavata/security/myproxy/MyProxyCredentials.java
grid-tools/gsi-myproxy-client/src/main/java/org/apache/airavata/security/myproxy/SecurityContext.java [moved from grid-tools/gsi-myproxy-client/src/main/java/org/apache/airavata/security/myproxy/MyProxyLogon.java with 63% similarity]
grid-tools/gsi-myproxy-client/src/main/resources/airavata-myproxy-client.properties.template
grid-tools/gsi-myproxy-client/src/test/java/org/apache/airavata/security/myproxy/SecurityContextTest.java [new file with mode: 0644]
grid-tools/gsi-myproxy-client/src/test/resources/airavata-myproxy-client.properties [new file with mode: 0644]

index ebef881..2c55b05 100644 (file)
@@ -28,7 +28,7 @@
                <dependency>
                        <groupId>org.jglobus</groupId>
                        <artifactId>myproxy</artifactId>
-                       <version>2.0.5</version>
+                       <version>2.0.6-rc2</version>
                </dependency>
 
                <dependency>
                        <artifactId>log4j</artifactId>
                        <version>1.2.17</version>
                </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>${junit.version}</version>
+        </dependency>
 
        </dependencies>
        <build>
                                        <target>1.6</target>
                                </configuration>
                        </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-surefire-plugin</artifactId>
+                <version>${surefire.version}</version>
+                <configuration>
+                    <failIfNoTests>false</failIfNoTests>
+                    <skipTests>${skipTests}</skipTests>
+                    <workingDirectory>${project.build.testOutputDirectory}</workingDirectory>
+                </configuration>
+            </plugin>
 <!--                   <plugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-dependency-plugin</artifactId>
  -->           </plugins>
        </build>
 
+    <properties>
+        <surefire.version>2.12</surefire.version>
+        <junit.version>4.7</junit.version>
+        <skipTests>false</skipTests>
+    </properties>
+
 </project>
 
index 805bc91..56aa823 100755 (executable)
@@ -9,4 +9,4 @@ CLASSPATH=$CLASSPATH:target/airavata-myproxy-client-0.1-SNAPSHOT.jar:./config
 
 echo $CLASSPATH
 
-java -classpath $CLASSPATH org.apache.airavata.security.myproxy.MyProxyLogon $*
+java -classpath $CLASSPATH org.apache.airavata.security.myproxy.SecurityContext $*
index f48d4f3..1061cd9 100644 (file)
 
 package org.apache.airavata.security.myproxy;
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.Serializable;
-import java.security.KeyStore;
+import java.io.*;
+import java.security.PrivateKey;
 import java.security.cert.X509Certificate;
 
 import org.apache.log4j.Logger;
+import org.globus.gsi.GlobusCredential;
 import org.globus.gsi.X509Credential;
 import org.globus.gsi.gssapi.GlobusGSSCredentialImpl;
-import org.globus.gsi.provider.GlobusProvider;
-import org.globus.gsi.provider.KeyStoreParametersFactory;
+import org.globus.myproxy.GetParams;
 import org.globus.myproxy.MyProxy;
 import org.ietf.jgss.GSSCredential;
 
+/**
+ * Class to manipulate my proxy credentials. Responsible for retrieving, creating
+ * my proxy credentials.
+ */
 public class MyProxyCredentials implements Serializable {
 
     private static final long serialVersionUID = -2471014486509046212L;
-    protected String myproxyHostname;
-    protected String myproxyUserName;
-    protected String myproxyPassword;
-    protected int myproxyPortNumber;
-    protected GSSCredential gssCredential;
-    protected String portalUserName;
-    private String hostcertsKeyFile;
-    private String trustedCertsFile;
-
-    protected int myproxyLifeTime = 14400;
-    final static int SECS_PER_MIN = 60;
-    final static int SECS_PER_HOUR = 3600;
-
-    private boolean initialized = false;
-    private boolean user = true;
-    protected X509Certificate[] trustedCertificates;
+    protected String myProxyHostname;
+    protected String myProxyUserName;
+    protected String myProxyPassword;
+    protected int myProxyPortNumber;
+    protected int myProxyLifeTime = 14400;
+    protected String trustedCertificatePath;
 
     private static final Logger log = Logger.getLogger(MyProxyCredentials.class);
 
+    /**
+     * Default constructor.
+     */
     public MyProxyCredentials() {
-        // default constructor
     }
 
-    public MyProxyCredentials(String myproxyServer, int myproxyPort, String myproxyUsername, String myproxyPassphrase,
-            int myproxyLifetime) {
-        this.myproxyHostname = myproxyServer;
-        this.myproxyPortNumber = myproxyPort;
-        this.myproxyUserName = myproxyUsername;
-        this.myproxyPassword = myproxyPassphrase;
-        this.myproxyLifeTime = myproxyLifetime;
+    /**
+     * Constructor.
+     * @param myProxyServer Ip address of the my proxy server.
+     * @param myProxyPort Port which my proxy server is running.
+     * @param myProxyUsername User name to connect to my proxy server.
+     * @param myProxyPassPhrase Password for my proxy authentication.
+     * @param myProxyLifetime Lifetime of the retrieving credentials.
+     * @param trustedCerts Trusted certificate location for SSL communication.
+     */
+    public MyProxyCredentials(String myProxyServer, int myProxyPort, String myProxyUsername, String myProxyPassPhrase,
+                              int myProxyLifetime, String trustedCerts) {
+
+        this.myProxyHostname = myProxyServer;
+        this.myProxyPortNumber = myProxyPort;
+        this.myProxyUserName = myProxyUsername;
+        this.myProxyPassword = myProxyPassPhrase;
+        this.myProxyLifeTime = myProxyLifetime;
+        this.trustedCertificatePath = trustedCerts;
+
+        init();
 
     }
 
-    public GSSCredential getGssCredential() throws Exception {
-        FileInputStream fis = null;
-        try {
-            if (hostcertsKeyFile != null && !user) {
-                fis = new FileInputStream(hostcertsKeyFile);
-                X509Credential x509Cred = new X509Credential(fis);
-                this.gssCredential = new GlobusGSSCredentialImpl(x509Cred, GSSCredential.INITIATE_AND_ACCEPT);
-            } else {
-                this.gssCredential = getDefaultProxy();
-            }
-            if (gssCredential != null) {
-                return gssCredential;
-            }
-        } catch (Exception e) {
-            log.error("Failed to load proxy credential from ProxyManager");
-            e.printStackTrace();
-            throw e;
-        } finally {
-            if (fis != null) {
-                fis.close();
-            }
-        }
+    /**
+     * Gets the default proxy certificate.
+     * @return Default my proxy credentials.
+     * @throws Exception If an error occurred while retrieving credentials.
+     */
+    public GSSCredential getDefaultCredentials() throws Exception {
+        MyProxy myproxy = new MyProxy(this.myProxyHostname, this.myProxyPortNumber);
+        return myproxy.get(this.myProxyUserName, this.myProxyPassword, this.myProxyLifeTime);
+    }
 
-        // then we will try the MyProxy
-        if (getMyproxyUserName() != null && getMyproxyPassword() != null) {
-            gssCredential = renewProxy();
-        }
-        return gssCredential;
+    /**
+     * Gets a new proxy certificate given current credentials.
+     * @param credential The new proxy credentials.
+     * @return The short lived GSSCredentials
+     * @throws Exception If an error is occurred while retrieving credentials.
+     */
+    public GSSCredential getProxyCredentials(GSSCredential credential) throws Exception {
+
+        MyProxy myproxy = new MyProxy(this.myProxyHostname, this.myProxyPortNumber);
+        return myproxy.get(credential, this.myProxyUserName, this.myProxyPassword, this.myProxyLifeTime);
     }
 
-    public GSSCredential getDefaultProxy() throws Exception {
-        init();
-        MyProxy myproxy = new MyProxy(this.myproxyHostname, this.myproxyPortNumber);
-        log.info("USER=" + this.myproxyUserName + ",PASS=" + this.myproxyPassword + ",TIME=" + this.myproxyLifeTime);
-        return myproxy.get(this.myproxyUserName, this.myproxyPassword, this.myproxyLifeTime);
+    /**
+     * Renew GSSCredentials.
+     * @param credential Credentials to be renewed.
+     * @return  Renewed credentials.
+     * @throws Exception If an error occurred while renewing credentials.
+     */
+    public GSSCredential renewCredentials(GSSCredential credential) throws Exception {
+        MyProxy myproxy = new MyProxy(this.myProxyHostname, this.myProxyPortNumber);
+
+        GetParams getParams = new GetParams();
+        getParams.setAuthzCreds(credential);
+        getParams.setUserName(this.myProxyUserName);
+        getParams.setLifetime(this.getMyProxyLifeTime());
+
+        return myproxy.get(credential, getParams);
     }
 
-    private void init() {
-        if (trustedCertsFile != null) {
-            if (new File(trustedCertsFile).isDirectory()) {
-                       try {
-               KeyStore trustStore = KeyStore.getInstance(
-                                       GlobusProvider.KEYSTORE_TYPE,
-                                       GlobusProvider.PROVIDER_NAME);
-                       trustStore.load(KeyStoreParametersFactory
-                                       .createTrustStoreParameters(trustedCertsFile + "/*.0"));
-                this.trustedCertificates = CertificateManager.getTrustedCertificate(trustedCertsFile);
-                       } catch (Exception e) {
-                               e.printStackTrace(); 
-                       }
-                }
-        }
+    public GSSCredential createCredentials(X509Certificate[] x509Certificates, PrivateKey privateKey) throws Exception {
+        X509Credential newCredential = new X509Credential(privateKey,
+                x509Certificates);
+
+        return new GlobusGSSCredentialImpl(newCredential,
+                GSSCredential.INITIATE_AND_ACCEPT);
+
     }
 
-    
-    public GSSCredential renewProxy() throws Exception {
-        init();
+    public GSSCredential createCredentials(X509Certificate x509Certificate, PrivateKey privateKey) throws Exception {
+
+        X509Certificate[] x509Certificates = new X509Certificate[1];
+        x509Certificates[0] = x509Certificate;
+
+        return createCredentials(x509Certificates, privateKey);
+
+    }
 
-        FileOutputStream fout = null;
-        try {
-            String proxyloc = null;
-            MyProxy myproxy = new MyProxy(myproxyHostname, myproxyPortNumber);
-            int lifeHours = myproxyLifeTime * SECS_PER_HOUR;
-            GSSCredential proxy = myproxy.get(myproxyUserName, myproxyPassword, lifeHours);
-
-            X509Credential globusCred = null; // **
-            // X509Credential globusCred = null; //**
-            if (proxy instanceof GlobusGSSCredentialImpl) {
-                globusCred = ((GlobusGSSCredentialImpl) proxy).getX509Credential();// **
-                // globusCred = ((GlobusGSSCredentialImpl) proxy).getX509Credential();//**
-                log.info("got proxy from myproxy for " + myproxyUserName + " with " + myproxyLifeTime + " lifetime.");
-                String uid = myproxyUserName;
-                if (proxyloc == null) {
-                    log.info("uid: " + uid);
-                    proxyloc = "/tmp/x509up_u" + uid;
-                }
-                File proxyfile = new File(proxyloc);
-                log.info("proxy location: " + proxyfile.getAbsolutePath());
-                if (proxyfile.exists() == false) {
-                    String dirpath = proxyloc.substring(0, proxyloc.lastIndexOf('/'));
-                    File dir = new File(dirpath);
-                    if (dir.exists() == false) {
-                        dir.mkdirs();
-                        log.info("new directory " + dirpath + " is created.");
-                    }
-                    proxyfile.createNewFile();
-                    log.info("new proxy file " + proxyloc + " is created.");
-                }
-                fout = new FileOutputStream(proxyfile);
-                globusCred.save(fout);
-                String osName = System.getProperty("os.name");
-                if (!osName.contains("Windows")) {
-                    Runtime.getRuntime().exec("/bin/chmod 600 " + proxyloc);
-                }
-                log.info("Proxy file renewed to " + proxyloc + " for the user " + myproxyUserName + " with "
-                        + myproxyLifeTime + " lifetime.");
-
-            }
-            return proxy;
-        } catch (Exception e) {
-            throw new Exception(e);
-        } finally {
-            if (fout != null) {
-                fout.close();
-            }
+    public void init() {
+        validateTrustedCertificatePath();
+    }
+
+    private void validateTrustedCertificatePath() {
+
+        File file = new File(this.trustedCertificatePath);
+
+        if (!file.exists() || !file.canRead()) {
+            File f = new File(".");
+            System.out.println("Current directory " + f.getAbsolutePath());
+            throw new RuntimeException("Cannot read trusted certificate path " + this.trustedCertificatePath);
+        } else {
+            System.setProperty("X509_CERT_DIR", file.getAbsolutePath());
         }
     }
 
+
     /**
-     * @return the myproxyHostname
+     * @return the myProxyHostname
      */
-    public String getMyproxyHostname() {
-        return myproxyHostname;
+    public String getMyProxyHostname() {
+        return myProxyHostname;
     }
 
     /**
-     * @param myproxyHostname
-     *            the myproxyHostname to set
+     * @param myProxyHostname the myProxyHostname to set
      */
-    public void setMyproxyHostname(String myproxyHostname) {
-        this.myproxyHostname = myproxyHostname;
+    public void setMyProxyHostname(String myProxyHostname) {
+        this.myProxyHostname = myProxyHostname;
     }
 
     /**
-     * @return the myproxyUserName
+     * @return the myProxyUserName
      */
-    public String getMyproxyUserName() {
-        return myproxyUserName;
+    public String getMyProxyUserName() {
+        return myProxyUserName;
     }
 
     /**
-     * @param myproxyUserName
-     *            the myproxyUserName to set
+     * @param myProxyUserName the myProxyUserName to set
      */
-    public void setMyproxyUserName(String myproxyUserName) {
-        this.myproxyUserName = myproxyUserName;
+    public void setMyProxyUserName(String myProxyUserName) {
+        this.myProxyUserName = myProxyUserName;
     }
 
     /**
-     * @return the myproxyPassword
+     * @return the myProxyPassword
      */
-    public String getMyproxyPassword() {
-        return myproxyPassword;
+    public String getMyProxyPassword() {
+        return myProxyPassword;
     }
 
     /**
-     * @param myproxyPassword
-     *            the myproxyPassword to set
+     * @param myProxyPassword the myProxyPassword to set
      */
-    public void setMyproxyPassword(String myproxyPassword) {
-        this.myproxyPassword = myproxyPassword;
+    public void setMyProxyPassword(String myProxyPassword) {
+        this.myProxyPassword = myProxyPassword;
     }
 
     /**
-     * @return the myproxyLifeTime
+     * @return the myProxyLifeTime
      */
-    public int getMyproxyLifeTime() {
-        return myproxyLifeTime;
+    public int getMyProxyLifeTime() {
+        return myProxyLifeTime;
     }
 
     /**
-     * @param myproxyLifeTime
-     *            the myproxyLifeTime to set
+     * @param myProxyLifeTime the myProxyLifeTime to set
      */
-    public void setMyproxyLifeTime(int myproxyLifeTime) {
-        this.myproxyLifeTime = myproxyLifeTime;
+    public void setMyProxyLifeTime(int myProxyLifeTime) {
+        this.myProxyLifeTime = myProxyLifeTime;
     }
 
     /**
-     * @return the myproxyPortNumber
+     * @return the myProxyPortNumber
      */
-    public int getMyproxyPortNumber() {
-        return myproxyPortNumber;
+    public int getMyProxyPortNumber() {
+        return myProxyPortNumber;
     }
 
     /**
-     * @param myproxyPortNumber
-     *            the myproxyPortNumber to set
+     * @param myProxyPortNumber the myProxyPortNumber to set
      */
-    public void setMyproxyPortNumber(int myproxyPortNumber) {
-        this.myproxyPortNumber = myproxyPortNumber;
+    public void setMyProxyPortNumber(int myProxyPortNumber) {
+        this.myProxyPortNumber = myProxyPortNumber;
+    }
+
+    public String getTrustedCertificatePath() {
+        return trustedCertificatePath;
+    }
+
+    public void setTrustedCertificatePath(String trustedCertificatePath) {
+        this.trustedCertificatePath = trustedCertificatePath;
     }
 
     /**
      * @return the portalUserName
      */
-    public String getPortalUserName() {
+    /*public String getPortalUserName() {
         return portalUserName;
-    }
+    }*/
 
     /**
      * @param portalUserName
      *            the portalUserName to set
      */
-    public void setPortalUserName(String portalUserName) {
+    /*public void setPortalUserName(String portalUserName) {
         this.portalUserName = portalUserName;
-    }
+    }*/
 
     /**
      * Returns the initialized.
-     * 
+     *
      * @return The initialized
      */
-    public boolean isInitialized() {
+    /*public boolean isInitialized() {
         return this.initialized;
-    }
+    }*/
 
     /**
      * Sets initialized.
-     * 
+     *
      * @param initialized
      *            The initialized to set.
      */
-    public void setInitialized(boolean initialized) {
+   /* public void setInitialized(boolean initialized) {
         this.initialized = initialized;
-    }
+    }*/
 
     /**
      * @param hostcertsKeyFile
      *            the hostcertsKeyFile to set
      */
-    public void setHostcertsKeyFile(String hostcertsKeyFile) {
+    /*public void setHostcertsKeyFile(String hostcertsKeyFile) {
         this.hostcertsKeyFile = hostcertsKeyFile;
-    }
+    }*/
 
     /**
      * @return the hostcertsKeyFile
      */
-    public String getHostcertsKeyFile() {
+    /*public String getHostcertsKeyFile() {
         return hostcertsKeyFile;
-    }
+    }*/
 
     /**
      * @param trustedCertsFile
      *            the trustedCertsFile to set
      */
-    public void setTrustedCertsFile(String trustedCertsFile) {
+    /*public void setTrustedCertsFile(String trustedCertsFile) {
         this.trustedCertsFile = trustedCertsFile;
-    }
+    }*/
 
     /**
      * @return the trustedCertsFile
      */
-    public String getTrustedCertsFile() {
+    /*public String getTrustedCertsFile() {
         return trustedCertsFile;
-    }
+    }*/
 
 }
@@ -28,26 +28,26 @@ import org.apache.log4j.Logger;
 import org.globus.myproxy.MyProxy;
 import org.ietf.jgss.GSSCredential;
 
-public class MyProxyLogon {
+public class SecurityContext {
 
     /**
         * 
         */
-    public static final String MYPROXYCLIENT_PROPERTY = "airavata-myproxy-client.properties";
+    public static final String MY_PROXY_CLIENT_PROPERTY = "airavata-myproxy-client.properties";
     private Properties properties;
     protected GSSCredential gssCredential;
 
-    private MyProxyCredentials credentials;
-    private static final Logger log = Logger.getLogger(MyProxyLogon.class);
+    private MyProxyCredentials myProxyCredentials;
+    private static final Logger log = Logger.getLogger(SecurityContext.class);
 
     /**
      * 
      * Constructs a ApplicationGlobalContext.
      * 
-     * @throws GfacGUIException
+     * @throws Exception
      */
 
-    public MyProxyLogon() throws Exception {
+    public SecurityContext() throws Exception {
         log.setLevel(org.apache.log4j.Level.INFO);
         loadConfiguration();
 
@@ -55,7 +55,7 @@ public class MyProxyLogon {
 
     public static void main(String[] args) {
         try {
-            MyProxyLogon myproxy = new MyProxyLogon();
+            SecurityContext myproxy = new SecurityContext();
             myproxy.login();
             String proxyName = myproxy.getGssCredential().getName().toString();
             int proxyTime = myproxy.getGssCredential().getRemainingLifetime();
@@ -68,15 +68,23 @@ public class MyProxyLogon {
 
     /**
      * 
-     * @throws GfacException
+     * @throws Exception
      */
     public void login() throws Exception {
-        gssCredential = credentials.getGssCredential();
+        gssCredential = myProxyCredentials.getDefaultCredentials();
+    }
+
+    public GSSCredential getProxyCredentials(GSSCredential credential) throws Exception {
+        return myProxyCredentials.getProxyCredentials(credential);
+    }
+
+    public GSSCredential renewCredentials(GSSCredential credential) throws Exception {
+        return myProxyCredentials.renewCredentials(credential);
     }
 
     public static String getProperty(String name) {
         try {
-            MyProxyLogon context = new MyProxyLogon();
+            SecurityContext context = new SecurityContext();
             return context.getProperties().getProperty(name);
         } catch (Exception e) {
             e.printStackTrace();
@@ -92,39 +100,50 @@ public class MyProxyLogon {
      */
     private void loadConfiguration() throws Exception {
         try {
+
+            System.out.println("In the load configurations method .....");
+
             if (properties == null) {
                 ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
  
-                InputStream propertyStream = classLoader.getResourceAsStream(MYPROXYCLIENT_PROPERTY);
+                InputStream propertyStream = classLoader.getResourceAsStream(MY_PROXY_CLIENT_PROPERTY);
                 properties = new Properties();
-                if (credentials == null) {
-                    this.credentials = new MyProxyCredentials();
+                if (myProxyCredentials == null) {
+                    this.myProxyCredentials = new MyProxyCredentials();
                 }
                 if (propertyStream != null) {
                     properties.load(propertyStream);
                     String myproxyServerTmp = properties.getProperty(ServiceConstants.MYPROXY_SERVER);
                     if (myproxyServerTmp != null) {
-                        this.credentials.setMyproxyHostname(myproxyServerTmp.trim());
+                        this.myProxyCredentials.setMyProxyHostname(myproxyServerTmp.trim());
                     }
                     String myproxyPortTemp = properties.getProperty(ServiceConstants.MYPROXY_PORT);
                     if (myproxyPortTemp != null && myproxyPortTemp.trim().length() > 0) {
-                        this.credentials.setMyproxyPortNumber(Integer.parseInt(myproxyPortTemp.trim()));
+                        this.myProxyCredentials.setMyProxyPortNumber(Integer.parseInt(myproxyPortTemp.trim()));
                     } else {
-                        this.credentials.setMyproxyPortNumber(MyProxy.DEFAULT_PORT);
+                        this.myProxyCredentials.setMyProxyPortNumber(MyProxy.DEFAULT_PORT);
                     }
                     String myproxyuser = properties.getProperty(ServiceConstants.MYPROXY_USERNAME);
                     if (myproxyuser != null) {
-                        this.credentials.setMyproxyUserName(myproxyuser);
+                        this.myProxyCredentials.setMyProxyUserName(myproxyuser);
                     }
+
+                    System.out.println("My proxy user name " + myproxyuser);
+
                     String myproxypass = properties.getProperty(ServiceConstants.MYPROXY_PASSWD);
                     if (myproxypass != null) {
-                        this.credentials.setMyproxyPassword(myproxypass);
+                        this.myProxyCredentials.setMyProxyPassword(myproxypass);
                     }
+
                     String myproxytime = properties.getProperty(ServiceConstants.MYPROXY_LIFETIME);
                     if (myproxytime != null) {
-                        this.credentials.setMyproxyLifeTime(Integer.parseInt(myproxytime));
+                        this.myProxyCredentials.setMyProxyLifeTime(Integer.parseInt(myproxytime));
                     }
-                    this.credentials.setTrustedCertsFile(properties.getProperty(ServiceConstants.TRUSTED_CERTS_FILE));
+                    this.myProxyCredentials.setTrustedCertificatePath(properties.getProperty(ServiceConstants.TRUSTED_CERTS_FILE));
+
+                    System.out.println("Certificate path - " + properties.getProperty(ServiceConstants.TRUSTED_CERTS_FILE));
+
+                    this.myProxyCredentials.init();
                 }
             }
 
@@ -156,8 +175,12 @@ public class MyProxyLogon {
      * 
      * @return The gssCredential
      */
-    public GSSCredential getGssCredential() {
-        return this.gssCredential;
+    public GSSCredential getGssCredential() throws Exception{
+
+        if (this.gssCredential == null)
+            return null;
+
+        return renewCredentials(gssCredential);
     }
 
     /**
@@ -170,11 +193,11 @@ public class MyProxyLogon {
         this.gssCredential = gssCredential;
     }
 
-    public MyProxyCredentials getCredentials() {
-        return credentials;
+    public MyProxyCredentials getMyProxyCredentials() {
+        return myProxyCredentials;
     }
 
-    public void setCredentials(MyProxyCredentials credentials) {
-        this.credentials = credentials;
+    public void setMyProxyCredentials(MyProxyCredentials myProxyCredentials) {
+        this.myProxyCredentials = myProxyCredentials;
     }
 }
diff --git a/grid-tools/gsi-myproxy-client/src/test/java/org/apache/airavata/security/myproxy/SecurityContextTest.java b/grid-tools/gsi-myproxy-client/src/test/java/org/apache/airavata/security/myproxy/SecurityContextTest.java
new file mode 100644 (file)
index 0000000..8f03b26
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ *
+ * 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.
+ *
+ */
+
+package org.apache.airavata.security.myproxy;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+import org.globus.gsi.provider.GlobusProvider;
+
+import java.security.Security;
+
+/**
+ * User: AmilaJ (amilaj@apache.org)
+ * Date: 5/23/13
+ * Time: 2:44 PM
+ * NOTE : BEFORE RUNNING THESE TESTS UPDATE gsi-myproxy-client/src/test/resources/airavata-myproxy-client.properties
+ * FILE WITH APPROPRIATE MY PROXY SERVER CONFIGURATIONS.
+ */
+
+
+
+public class SecurityContextTest extends TestCase {
+
+    static {
+        Security.addProvider(new GlobusProvider());
+    }
+
+    public void testLogin() throws Exception {
+        Assert.assertTrue(true);
+        System.out.println("Running tests ...");
+
+        SecurityContext myProxy = new SecurityContext();
+        myProxy.login();
+
+        Assert.assertNotNull(myProxy.getGssCredential());
+    }
+
+    public void testProxyCredentials() throws Exception {
+
+        SecurityContext myProxy = new SecurityContext();
+        myProxy.login();
+
+        Assert.assertNotNull(myProxy.getProxyCredentials(myProxy.getGssCredential()));
+    }
+
+    /**
+     * Before executing you need to add your host as a trusted renewer.
+     * Execute following command
+     * > myproxy-logon -t <LIFETIME></LIFETIME> -s <MY PROXY SERVER> -l <USER NAME>
+     * E.g :- > myproxy-logon -t 264 -s myproxy.teragrid.org -l us3
+     *          Enter MyProxy pass phrase:
+     *          A credential has been received for user us3 in /tmp/x509up_u501.
+     * > myproxy-init -A --cert /tmp/x509up_u501 --key /tmp/x509up_u501 -l us3 -s myproxy.teragrid.org
+     * @throws Exception
+     */
+    public void testRenewCredentials() throws Exception {
+
+        SecurityContext myProxy = new SecurityContext();
+        myProxy.login();
+
+        Assert.assertNotNull(myProxy.renewCredentials(myProxy.getGssCredential()));
+
+    }
+}
diff --git a/grid-tools/gsi-myproxy-client/src/test/resources/airavata-myproxy-client.properties b/grid-tools/gsi-myproxy-client/src/test/resources/airavata-myproxy-client.properties
new file mode 100644 (file)
index 0000000..da00ef1
--- /dev/null
@@ -0,0 +1,7 @@
+#Download trusted certificates from - https://software.xsede.org/security/xsede-certs.tar.gz
+trustedCertsFile=./certificates
+myproxyServer=myproxy.teragrid.org
+myproxy_lifetime=17280000
+myproxyUserName=
+myproxyPasswd=
+myproxyPort=7512
\ No newline at end of file