I am currently working a scenario where we have to create a file in a shared directory in Linux as well as Windows.
I have gone through the following link to achieve it :
https://www.journaldev.com/878/java-write-to-file
https://it.toolbox.com/question/how-to-write-a-file-in-a-network-folder-without-using-ftp-031208
I was able to achieve it in windows network like giving the file name as (\\198.168.1.1\data\files)
But for the Linux first I used NFS to share a particular directory using below links :
https://alvinsim.wordpress.com/2012/06/21/mounting-nfs-from-linux-to-aix/
https://www.tecmint.com/how-to-setup-nfs-server-in-linux/
But got the following exception :
java.io.FileNotFoundException: /data/files (Read-only file system)
I found the following issue resolved in following thread, but I could not succeed :
https://askubuntu.com/questions/197459/how-to-fix-sudo-unable-to-open-read-only-file-system
Any advice is appreciated.
So finally I was able to figure what the problem was.
The entry which I made as per link in /etc/exports file is not sufficient.
We would need to use some options as well like (sync,rw,etc..) for allowing any manual modifications or modifications using any java program.
In my case, I should have used the options (rw,no_root_squash).
And it worked.
As very well explained in :
https://serverfault.com/questions/611007/unable-to-write-to-mount-point-nfs-server-getting-permission-denied/611013#611013?newreg=ce76e9417ca645da9487a5d9ccbf0371
From Docs on :
https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/5/html/deployment_guide/s1-nfs-server-config-exports
root_squash — Prevents root users connected remotely from having root privileges and assigns them the user ID for the user nfsnobody. This effectively "squashes" the power of the remote root user to the lowest local user, preventing unauthorized alteration of files on the remote server. Alternatively, the no_root_squash option turns off root squashing. To squash every remote user, including root, use the all_squash option.
Related
Hello dear Stack community,
I am looking for solution for my test execution. I am using Java & Selenium & TestNG tests kept on AzureDevOps.
Each execution needs to load input data from file stored in external source - where client have an access and can easily drag-drop files.
By now files are stored on my companys Share Drive. It works all fine when I run tests from local machine, but when using pipeline, I get FileNotFoundException in line where file is loaded.
Does anyone have an idea why does it happen and how can it be overcome?
Of course if there is any other solution, I would love to hear about it.
It works all fine when I run tests from local machine, but when using pipeline, I get FileNotFoundException in line where file is loaded.
If you use Microsoft-hosted agents to access to files, which are stored on company's Share Drive, you should check whether there is correct connection. If your organization is secured with a firewall or proxy server, you need to add certain IP addresses and domain URLs to the allowlist. See: Allowed address lists and network connections for details.
Also you could use self-hosted agents to access to this shared files so it will communicate with company's Share Drive via your internal network.
I had a performance issue with SMB file upload using JCIFS SMB. The problem was related to jcifs.smb.client.dfs.disabled property and using true fixed the issue.
Now the question is that in which case I will be having problems using jcifs.smb.client.dfs.disabled=true as it has to be static property for me? I have many different environments which is using the same configuration.
Properties document (link) says:
If this property is true, domain based DFS referrals will be disabled.
The default value is false. This property can be important in
non-domain environments where domain-based DFS referrals that normally
run when JCIFS first tries to resolve a path would timeout causing a
long startup delay (e.g. running JCIFS only on the local machine
without a network like on a laptop).
... but this is over my head, cannot understand.
Also, if I could change that dynamically, what is the indication that I need to do that? Any specific Exception or scenario that points out that true or false is needed?
Thanks!
Please read this - what is DFS.
In general you can mount your folder to a different share.
For example PC1 has Share1 and the PC2 Share2 had a folder named "not located here" and it points to PC1 Share1 (just a link).
To resolve the real location The SMB uses an IOCL query "get_referrals" and this is the implementation of the DFS.
Inside Jenkins Jobs & Builds folder, I am creating a text file and writing some content through Java program. As I am part of corporate network, I am inside firewall with many security rules where I have been restricted to write/update the program files directory under C: drive. Although I am administrator in my local machine, however the company policies are still applied which is denying me access to write/delete any files from the Jenkins directory. I see Jenkins is nicely reading/modifying/writing any files/folders without any issues which is believed to the typical behavior of Jenkins's USER.
Question 1: Is there any way I can use this Jenkins's user through my code so I can avail access on to these directories?
Question 2: Are there ways to solve this issue through Java code? (Note: I have tried writing a file with Run as Administration java code as well)
Kindly let me know if I am missing any details,.any help is highly appreciated.
It's about the user who launched the jenkins server, who might have the permissions to access the directories.
You can use the same user for your operations if available.
Question 1: Is there any way I can use this Jenkins's user through my code so I can avail access on to these directories?
Jenkins users - Jenkins server can have its own users and privileges can be set for each users differently. You cannot use these users outside of Jenkins server.
You can use the user who launched the Jenkins server, must be a user at OS level.
Question 2: Are there ways to solve this issue through Java code? (Note: I have tried writing a file with Run as Administration java code as well)
Again, only OS level users can be used and not the Jenkins users(users created inside Jenkins server)
If you want your application to run with same credentials as Jenkins user, then hold Shift+Right Click your application, select "Run as different user", provide Jenkins's user credentials and press OK.
If you are launching your Java application from command line, do the Shift+Right Click on the cmd.exe first, and once again select "Run as different user"
If you want to impersonate a Windows user from within code, then you should really reword your question body and title (and remove Jenkins references as it has nothing to do with this). But even in this case, you need to know the credentials of the user you are trying to impersonate
I have been using the Files.getOwner() to try and make a log of folders being made on a network however doing so returns an id as below.
S-1-5-21-1027050117-121514058-1707179805-77926 (Unknown)
What would be the best way to get the login name of the owner instead of the id above?
You can try System.getProperty("user.name"); This will return the environment variable USERNAME.
On Windows 7 with JDK 8 when I run the following Java code
System.out.println(Files.getOwner(Paths.get("C:\\test\\test.txt")));
on a text file I just created, I get the following output:
chris-PC\chris_2 (User)
Where chris-PC is my computer name and chris_2 is my Windows user account. When I googled this problem I found this post where someone has the same issue as you with a samba mapped drive. The response given there is as follows:
As far as I know, this is an internal user ID which Windows displays when it cannot - for >some reason - translate it to the real user name.
I don't know how Samba provides file and folder owners to Windows, but
I'd say that Windows are unable to find that user. That user exists on
unix, it probably doesn't have any relationship to the Windows machine
accessing the share. You'd probably have to set both systems in a way
which would allow them to share user definitions (eg. put them both to
a Windows domain) to be able to display the user name on Windows.
(Just guessing.)
So, I'm not sure if this problem can be reasonably solved.
I've a production env and test env. Production has win2k3+tomcat 6.0+ and network drive mapped to a drive letter for easy access (z:\app instead of \symba\files\app). My test env has windows xp (all the software is same including the source code for servlet) and test also has the same drive letter mapping to the network drive.
Tomcat is running with the same user id in both the systems and am able to access the file via windows explorer in the test m/c and production m/c.
Now the servlet in the windows xp m/c (test m/c) throws a file not found exception if try to access an existing file (reachable via windows explorer). The servlet in production is same as in test env and production servlet happily gives the file when i request it and does not throw any error what so ever.
I've see some posts that using drive letter is not a good idea and should use UNC paths instead and i've not tried this yet. I want to know the reason why test servlet fails to get file whereas production servlet works fine.
Thank you very much for your time and help
Bo
Are you running the application server as a service? Drive letter assignments are reliable only if you use interactive programs not services.
A couple of years ago, I tried to resolve a quite identical problem but, at last, I gave up and I used JCIFS to access the files.
If you run Tomcat with a security manager, the web app code is according to the default configuration only allowed file access to its deployment directory. If you want to change the application policies, you can do that in conf/catalina.policy or you can disable the security manager, if you want your web app code to gain all privileges.
You might want to consider running the Tomcat service as the user who has access to the mapped drive.
As pointed out by andcoz, the mappings will not apply for the SYSTEM user in Windows that I presume is being utilized for running Tomcat. You could change the service configuration to run as the user owning the drive mapping and verify if the failure continues to occur.
Related:
FileNotFoundException thrown when the file does exists.