Skip to content
This repository has been archived by the owner on Sep 21, 2021. It is now read-only.

Commit

Permalink
Updated SSH Key and Password authentication.
Browse files Browse the repository at this point in the history
If SSH key fails, or is not defined, revert to password authentication.
  • Loading branch information
ptdeboer authored and ptdeboer committed Jan 23, 2014
1 parent e0fe2ed commit 4dea0f5
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import nl.esciencecenter.ptk.GlobalProperties;
import nl.esciencecenter.ptk.crypt.Secret;
import nl.esciencecenter.ptk.data.SecretHolder;
import nl.esciencecenter.ptk.data.StringHolder;
import nl.esciencecenter.ptk.io.FSUtil;
import nl.esciencecenter.ptk.util.StringUtil;
import nl.esciencecenter.ptk.util.logging.ClassLogger;
Expand Down Expand Up @@ -550,73 +551,105 @@ public boolean checkSameFilesystem(Path path1, Path path2)
return true;
}

public Credential createSSHCredentials(ServerInfo info) throws XenonException, VRLSyntaxException
public Credential createSSHKeyCredential(ServerInfo info,StringHolder errorReasonH) throws XenonException, VRLSyntaxException
{
String sshUser = info.getUsername();
String ssh_id_key_file = info.getAttributeValue(ServerInfo.ATTR_SSH_IDENTITY);
boolean useIdFile = false;
boolean exists = false;
char passwordChars[] = null;

if (StringUtil.isEmpty(ssh_id_key_file) == false)
if (StringUtil.isEmpty(ssh_id_key_file))
{
// ssh_id_key_file can be absolute here:
VRL idFile = getUserHome().resolvePath(".ssh").resolvePath(ssh_id_key_file);

useIdFile = FSUtil.getDefault().existsFile(idFile.getPath(), true);
if (errorReasonH!=null)
errorReasonH.value="Identity Keyfile not defined.";
return null;
}

// ssh_id_key_file can both be absole or relative.
VRL idFile = getUserHome().resolvePath(".ssh").resolvePath(ssh_id_key_file);

if (useIdFile)
{
ssh_id_key_file = idFile.getPath();
}
else
exists = FSUtil.getDefault().existsFile(idFile.getPath(), true);

if (exists==false)
{
if (errorReasonH!=null)
{
ssh_id_key_file = null; // do not use!
errorReasonH.value="Identity Keyfile does not exists:"+idFile;
}
return null;
}

Credentials creds = engine.credentials();
Credential cred;

ssh_id_key_file = idFile.getPath();

logger.debugPrintf("createSSHCredentials(): Using Username:"+sshUser);
logger.debugPrintf("createSSHCredentials(): Using ID Key file:%s\n",ssh_id_key_file);

cred = creds.newCertificateCredential("ssh",
ssh_id_key_file,
sshUser,
passwordChars, null);


return cred;
}

public Credential createSSHPasswordCredential(ServerInfo info, boolean interactive,StringHolder errorReasonH) throws XenonException, VRLSyntaxException
{
String sshUser = info.getUsername();
Secret pwd = null;
if (useIdFile == false)

pwd = info.getPassword();

if ( (pwd == null) || (pwd.isEmpty()) )
{
// fall back to password
pwd = info.getPassword();
if ((pwd == null) || (pwd.isEmpty()))
if (interactive==false)
{
String serverStr = info.getUserinfo() + "@" + info.getServerVRL().getHostname();
SecretHolder secretH = new SecretHolder();
this.vrsContext.getUI().askAuthentication("Provide password for:" + serverStr, secretH);
pwd = secretH.value;
if (errorReasonH!=null)
{
errorReasonH.value="No password given. Please specify password.";
}
return null;
}

String serverStr = info.getUserinfo() + "@" + info.getServerVRL().getHostname();
SecretHolder secretH = new SecretHolder();
this.vrsContext.getUI().askAuthentication("Provide password for:" + serverStr, secretH);
pwd = secretH.value;

}


char[] passwordChars;

if ((pwd != null) && (!pwd.isEmpty()))
{
passwordChars = pwd.getChars();
}
else
{
if (errorReasonH!=null)
{
errorReasonH.value="Password authentication cancelled.";
}
return null;
}

// logger.debugPrintf("createSSHCredentials(): Using Username:"+sshUser);
// logger.debugPrintf("createSSHCredentials(): Using ID Key file:%s\n",ssh_id_key_file);
// logger.debugPrintf("createSSHCredentials(): Using password = %s\n",(passwordChars!=null)?"Yes":"No");
logger.debugPrintf("createSSHCredentials(): Using Username:"+sshUser);
logger.debugPrintf("createSSHCredentials(): Using password = %s\n",(passwordChars!=null)?"Yes":"No");

Credentials creds = engine.credentials();
Credential cred;

if (useIdFile)
{
cred = creds.newCertificateCredential("ssh",
ssh_id_key_file,
sshUser,
passwordChars, null);
}
else
{
cred = creds.newPasswordCredential("ssh",
sshUser,
passwordChars,
null);
}

cred = creds.newPasswordCredential("ssh",
sshUser,
passwordChars,
null);

return cred;
}

public Credential createGftpCredentials(ServerInfo info) throws XenonException
{
return engine.credentials().newCertificateCredential("gsiftp", "/tmp/x509up_u1000", null, null, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public FileAttributes getAttrs(boolean update) throws VrsException
{
if ((fileAttrs==null) || (update==true))
{
fileAttrs=getOctoClient().getFileAttributes(octoPath);
fileAttrs=getXenonClient().getFileAttributes(octoPath);
}
return fileAttrs;
}
Expand Down Expand Up @@ -90,7 +90,7 @@ public boolean create(boolean force) throws VrsException
}
try
{
this.getOctoClient().mkdir(octoPath);
this.getXenonClient().mkdir(octoPath);
}
catch (XenonException e)
{
Expand All @@ -111,7 +111,7 @@ public boolean exists() throws VrsException
{
// call exists, do not fetch file attributes from a non existing file
// as this might throw an error.
return this.getOctoClient().exists(octoPath);
return this.getXenonClient().exists(octoPath);
}
}
catch (XenonException e)
Expand Down Expand Up @@ -154,7 +154,7 @@ public boolean delete(boolean recurse) throws VrsException
// delete single empty directory:
try
{
this.getOctoClient().rmdir(octoPath);
this.getXenonClient().rmdir(octoPath);
// clear attributes to indicate non existing dir!
this.fileAttrs=null;
return true;
Expand Down Expand Up @@ -222,9 +222,9 @@ public long getNrOfNodes() throws VrsException
// Protected
// ===

protected XenonClient getOctoClient()
protected XenonClient getXenonClient()
{
return this.getFileSystem().octoClient;
return this.getFileSystem().xenonClient;
}

public boolean isSymbolicLink() throws VrsException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public FileAttributes getAttrs(boolean update) throws VrsException
{
if ((fileAttrs==null) || (update==true))
{
fileAttrs=getOctoClient().getFileAttributes(octoPath);
fileAttrs=getXenonClient().getFileAttributes(octoPath);
}
return fileAttrs;
}
Expand Down Expand Up @@ -102,7 +102,7 @@ public boolean create(boolean ignoreExisting) throws VrsException
try
{
// Path is immutable, update it here ?
getOctoClient().createFile(octoPath);
getXenonClient().createFile(octoPath);
return true;
}
catch (Throwable e)
Expand Down Expand Up @@ -171,7 +171,7 @@ public InputStream createInputStream() throws IOException
{
try
{
return this.getOctoClient().createInputStream(octoPath);
return this.getXenonClient().createInputStream(octoPath);
}
catch (Throwable e)
{
Expand All @@ -183,7 +183,7 @@ public OutputStream createOutputStream() throws IOException
{
try
{
return this.getOctoClient().createNewOutputStream(octoPath,true);
return this.getXenonClient().createNewOutputStream(octoPath,true);
}
catch (Throwable e)
{
Expand All @@ -203,7 +203,7 @@ public boolean delete() throws VrsException
{
try
{
boolean result = this.getOctoClient().deleteFile(octoPath,true);
boolean result = this.getXenonClient().deleteFile(octoPath,true);
// clear attributes to indicate non existinf file!
this.fileAttrs=null;
return result;
Expand All @@ -228,7 +228,7 @@ public boolean exists() throws VrsException
{
// call exists, do not fetch file attributes from a non existing file
// as this might throw an error.
return this.getOctoClient().exists(octoPath);
return this.getXenonClient().exists(octoPath);
}
}
catch (Throwable e)
Expand Down Expand Up @@ -266,9 +266,9 @@ protected XenonVFS getFS()
return ((XenonVFS)this.getFileSystem());
}

protected XenonClient getOctoClient()
protected XenonClient getXenonClient()
{
return this.getFS().octoClient;
return this.getFS().xenonClient;
}


Expand Down
Loading

0 comments on commit 4dea0f5

Please sign in to comment.