Svnkit + ssh authentication error (E170001) - java

I am a bit new here but I need some help with my code. Here it is:
SVNRepositoryFactoryImpl.setup();
SVNURL url =
SVNURL.
parseURIEncoded("svn+ssh://xxx");
File keyFile =
new File("C://Users/xxxx/Documents/priave_key/private_key.ppk");
System.out.println("PPK file space:"+keyFile.getTotalSpace());
SVNSSHAuthentication sshCredentials =
new SVNSSHAuthentication(Settings.name, keyFile, Settings.pass, 22, false, url, false);
System.out.println(sshCredentials.getPrivateKey()==null);
System.out.println("hasPrivateKey(): "+sshCredentials.hasPrivateKey());
SVNUserNameAuthentication authorNameCredentials = new SVNUserNameAuthentication(Settings.name, false);
SVNPasswordAuthentication passwordCredentials = new SVNPasswordAuthentication(Settings.name, Settings.pass, false);
ISVNAuthenticationManager authManager =
new BasicAuthenticationManager(new SVNAuthentication[] {
sshCredentials,
authorNameCredentials,
passwordCredentials});
SVNRepository repository = SVNRepositoryFactory.create(url);
repository.setAuthenticationManager(authManager);
try {
SVNDirEntry info = repository.info("", -1);
System.out.println(info);
} finally {
repository.closeSession();
}
So after running this I get this output:
PPK file space:107374178304
false
hasPrivateKey(): true
Exception in thread "main" org.tmatesoft.svn.core.SVNAuthenticationException: svn: E170001: Authentication required for 'xxx#svn+ssh://xxx'
at org.tmatesoft.svn.core.internal.wc.SVNErrorManager.authenticationFailed(SVNErrorManager.jav a:47)
at org.tmatesoft.svn.core.internal.wc.SVNErrorManager.authenticationFailed(SVNErrorManager.jav a:41)
at
org.tmatesoft.svn.core.auth.BasicAuthenticationManager.getNextAuthentication(BasicAuthentic ationManager.java:223)
at org.tmatesoft.svn.core.internal.io.svn.SVNSSHConnector.open(SVNSSHConnector.java:141)
at org.tmatesoft.svn.core.internal.io.svn.SVNConnection.open(SVNConnection.java:77)
at org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryImpl.openConnection(SVNRepositoryImpl.j ava:1252)
at org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryImpl.info(SVNRepositoryImpl.java:1195)
at SVNTestNew.main(SVNTestNew.java:40)
I think this can be a problem with SVNSSHAuthentication class because it gives null for getPrivateKey() and true for hasPrivateKey(.) Or is this a wrong idea? Can anyone tell me how can this problem be solved?

According to me your public and private keys are not at proper place.
If you are using Linux machine then,
For private key you have default path :-
${USER_HOME}/.ssh/abc.rsa or abc.dsa
And you have to Store your public key at server in :
Default Path :-
${USER_HOME}/.ssh/authorized_keys
You can change your Path in /etc/sshd_config file

Related

SmbException failed to connect hostname/IP_address throwing with proper credentials in Java

I need to connect to a shared folder with proper user credentials (username, password, domain).
Then when I have access to the folder, I need to list the subfolders and files in it.
Im trying with the jcifs.smb.SmbFile class and jcifs.smb.NtlmPasswordAuthentication for the authentication.
My code is the following:
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("domainName", "userName", "password");
SmbFile smbFile = new SmbFile("smb://servername/someFolder", auth);
for (String fileName : smbFile.list()) {
System.out.println(fileName);
}
I would be able to connect to the server with these credentials, but I'm getting this error:
Exception in thread "main" jcifs.smb.SmbException: Failed to connect: servername/IP_ADDR
jcifs.util.transport.TransportException
java.net.SocketException: Connection reset
at java.base/sun.nio.ch.NioSocketImpl.implRead(NioSocketImpl.java:323)
at java.base/sun.nio.ch.NioSocketImpl.read(NioSocketImpl.java:350)
at java.base/sun.nio.ch.NioSocketImpl$1.read(NioSocketImpl.java:803)
at java.base/java.net.Socket$SocketInputStream.read(Socket.java:981)
...
Anyone has any idea why am I unable to connect?
SmbFile - https://www.jcifs.org/src/docs/api/jcifs/smb/SmbFile.html
NtlmPasswordAuthentication - https://javadoc.io/static/eu.agno3.jcifs/jcifs-ng/2.1.3/jcifs/smb/NtlmPasswordAuthentication.html
I found the solution!
Because of my OS (Windows 10) I needed to use SMB2 not SMB1 (this is the default).
Solution:
Open powershell as an administrator
You need to set a property: Set -SmbServerConfiguration -EnableSMB2Protocol $true
Optional: I think it isn't necessarry but I turned off the SMB1 protocol wit the
Set -SmbServerConfiguration -EnableSMB1Protocol $false command.
Then you can check the properties with: Get -SmbServerConfiguration command, and be sure about that all of the properties have the right value.
Import the proper dependency to the pom.xml:
<dependency>
<groupId>eu.agno3.jcifs</groupId>
<artifactId>jcifs-ng</artifactId>
<version>2.1.6</version>
</dependency>
https://github.com/AgNO3/jcifs-ng
Finally the code:
public static void sendRequest() throws Exception {
CIFSContext base = SingletonContext.getInstance();
CIFSContext authed1 = base.withCredentials(new NtlmPasswordAuthentication(base, "domainName",
"userName", "password"));
try (SmbFile f = new SmbFile("smb:\\serverName\folder", authed1)) {
if (f.exists()) {
for (SmbFile file : f.listFiles()) {
System.out.println(file.getName());
}
}
}
}

Getting java.lang.IllegalStateException: Connection is not open

I am getting the below error while my code is trying to access a 3rd party endpoint
This request is sent via Service Stub generated code
We are using Axis2 version 1.7.7
Caused by: java.lang.IllegalStateException: Connection is not open
at org.apache.commons.httpclient.HttpConnection.assertOpen(HttpConnection.java:1277)
at org.apache.commons.httpclient.HttpConnection.getResponseInputStream(HttpConnection.java:858)
at org.apache.commons.httpclient.MultiThreadedHttpConnectionManager$HttpConnectionAdapter.getResponseInputStream(MultiThreadedHttpConnectionManager.java:1297)
What could be the reason for this and how can we solve this.
Below is the code for reference
public com.test.Token validateToken(com.test.Token actionToken)throws java.rmi.RemoteException
{
org.apache.axis2.context.MessageContext _messageContext = new org.apache.axis2.context.MessageContext();
try{
org.apache.axis2.client.OperationClient _operationClient = _serviceClient.createClient(_operations[0].getName());
_operationClient.getOptions().setAction("http://serviceURL/action");
_operationClient.getOptions().setExceptionToBeThrownOnSOAPFault(true);
addPropertyToOperationClient(_operationClient,org.apache.axis2.description.WSDL2Constants.ATTR_WHTTP_QUERY_PARAMETER_SEPARATOR,"&");
org.apache.axiom.soap.SOAPEnvelope env = null;
env = toEnvelope(getFactory(_operationClient.getOptions().getSoapVersionURI()),
actionToken,
optimizeContent(new javax.xml.namespace.QName("http://serviceURL", "action")),
new javax.xml.namespace.QName("http://serviceURL", "action"));
_serviceClient.addHeadersToEnvelope(env);
_messageContext.setEnvelope(env);
_operationClient.addMessageContext(_messageContext);
_operationClient.execute(true);
org.apache.axis2.context.MessageContext _returnMessageContext = _operationClient.getMessageContext(
org.apache.axis2.wsdl.WSDLConstants.MESSAGE_LABEL_IN_VALUE);
org.apache.axiom.soap.SOAPEnvelope _returnEnv = _returnMessageContext.getEnvelope();
java.lang.Object object = fromOM(
_returnEnv.getBody().getFirstElement() ,
com.test.Token.class);
return (com.test.Token)object;
}

svnkit authentication problem with ntlm in Java

I have a servers file on my computer with http-auth-types = Negotiate;ntlm;basic
The following Java program with svnkit 1.10.2 should connect to a repository :
public static void main(String[] args) throws SVNException
{
SVNClientManager clientManager = SVNClientManager.newInstance();
ISVNAuthenticationManager manager = SVNWCUtil.createDefaultAuthenticationManager();
clientManager.setAuthenticationManager(manager);
SVNURL repositoryURL = SVNURL.parseURIEncoded(args[0]);
SVNRepository repository = clientManager.createRepository(repositoryURL, true);
Collection<SVNDirEntry> contentOfFolder = repository.getDir("", SVNRevision.HEAD.getNumber(),
null, SVNDirEntry.DIRENT_ALL, (Collection<SVNDirEntry>) null);
System.out.println(contentOfFolder);
}
Unfortunately, I get
org.tmatesoft.svn.core.SVNAuthenticationException: svn: E170001: Authentication required for ...
What am I doing wrong? Do I somehow need to tell svnkit to use ntlm?

testing whether embedded Mina FTPServer really started

i wrote programmatically a startup code of the apache Server like this:
public void _start()
{
String Path = "C:\\Dokumente und Einstellungen\\andjock\\Desktop\\ab";
File ftpDirectory = new File(Path);
ftpDirectory.mkdirs();
FtpServerFactory serverFactory = new FtpServerFactory();
ListenerFactory factory = new ListenerFactory();
factory.setPort(2221);
try {
serverFactory.addListener("default", factory.createListener());
PropertiesUserManagerFactory userFactory = new PropertiesUserManagerFactory();
File userFile = new File("C:\\Dokumente und Einstellungen\\andjock\\Desktop\\ftpusers.properties");
userFactory.setFile(userFile);
UserManager um = userFactory.createUserManager();
BaseUser user = new BaseUser();
user.setName("myNewUser");
user.setPassword("secret");
user.setHomeDirectory(Path);
um.save(user);
serverFactory.setUserManager(um);
FtpServer ftpServer = serverFactory.createServer();
ftpServer.start();
} catch (Exception e) {
Logger LOGGER = Logger.getLogger(TestapacheFtpServer.class);
LOGGER.log(Level.FATAL, "Unable to start test ftpserver", e);
}
How do i know that the server is really working ?
how can i access this apache Server , from the "outside"?
i tried with a telnet and ftp (ftp 127.0.0.1) on my machine but i received:
FTP: connect : unknown error code
does someone got any idea ? i just don't want to rely on the jvm log, but rather test it , and accesing the started it
i figure it out!! i wrote a client using the FTP client library (the apache commons library) to test connectivity and list the files ; something like that
FTPClient ftp = new FTPClient();
ftp.connect(InetAddress.getLocalHost(), 2221);// or "localhost" in your case
String loging_success = ftp.login("myNewUser", "secret") == true ? "success" : "failed";
System.out.println("login: "+ loging_success);
FTPFile[] files = ftp.listFiles();
System.out.println("Listed "+files.length+" files.");
for(FTPFile file : files) {
System.out.println(file.getName());
}

Apache Commons FTPClient.listFiles

I am using org.apache.commons.net.ftp.FTPClient in one of my applications to work with a FTP server. I am able to connect, login, pwd and cwd. However, when I try to list the files it doesn't return the list of files in that directory, where I know for sure that there are files. I am using the method FTPFile[] listFiles(), it returns an empty array of FTPFile.
Please find below the code snippet where I am trying this:
String hostname = properties.getProperty("FTP_SERVER");
String user = properties.getProperty("FTP_USER");
String passwd = properties.getProperty("FTP_PASSWD");
FTPClient client = new FTPClient();
client.connect(hostname);
client.login(user, passwd);
String reply = client.getStatus();
System.out.println(reply);
client.enterRemotePassiveMode();
client.changeWorkingDirectory("/uploads");
FTPFile[] files = client.listFiles();
System.out.println(files.length);
for (FTPFile file : files) {
System.out.println(file.getName());
}
String[] fileNames = client.listNames();
if (fileNames != null) {
for (String file : fileNames) {
System.out.println(file);
}
}
client.disconnect();
This seems like the same issue I had (and solved), see this answer:
Apache Commons Net FTPClient and listFiles()
After I set the mode as PASV it is working fine now!
Thanks for all your efforts and suggestions!
I added client.enterLocalPassiveMode() and it works:
client.connect("xxx.com");
boolean login = client.login("xxx", "xxx");
client.enterLocalPassiveMode();
Just a silly suggestion... can you do a listing on the /uploads folder using a normal FTP client. I ask this because some FTP servers are setup to not display the listing of an upload folder.
First, make sure the listing works in other programs. If so, one possibility is that the file listing isn't being parsed correctly. You can try explicitly specifying the parser to use with initiateListParsing.
I had to same problem and it turned out to be that it couldn't parse what the server was returning for a file listing. I this line after connecting to the ftp server ftpClient.setParserFactory(new MyFTPFileEntryParserFactory());
public class MyFTPFileEntryParserFactory implements FTPFileEntryParserFactory {
private final static FTPFileEntryParser parser = new UnixFTPEntryParser() {
#Override public FTPFile parseFTPEntry(String entry) {
FTPFile ftpFile = new FTPFile();
ftpFile.setTimestamp(getCalendar(entry));
ftpFile.setSize(get(entry));
ftpFile.setName(getName(entry));
return ftpFile;
}
};
#Override public FTPFileEntryParser createFileEntryParser(FTPClientConfig config) throws ParserInitializationException {
return parser;
}
#Override public FTPFileEntryParser createFileEntryParser(String key) throws ParserInitializationException {
return parser;
}
}
In my case, on top of applying enterLocalPassiveMode and indicating correct operation system, I also need to set UnparseableEntries to true to make the listFile method work.
FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
conf.setUnparseableEntries(true);
f.configure(conf);
boolean isLoginSuccess = client.login(username, password);

Categories

Resources