Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Properties files to read the tokes and passwords outside of the perl scripts.

by chandantul (Scribe)
on Dec 14, 2020 at 04:15 UTC ( [id://11125149]=perlquestion: print w/replies, xml ) Need Help??

chandantul has asked for the wisdom of the Perl Monks concerning the following question:

Hello All, I will have a challenge to keep some properties file that will have token secret and password and scripts will read the passwords and tokens from outside properties files. Scripts should contain any tokens or constant passwords. Can you please share an example code for the same?

  • Comment on Properties files to read the tokes and passwords outside of the perl scripts.

Replies are listed 'Best First'.
Re: Properties files to read the tokes and passwords outside of the perl scripts.
by salva (Canon) on Dec 14, 2020 at 07:30 UTC
    The CPAN module Config::Properties allows one to read Java property files. Its documentation contains example code.

      Its works great. Thanks for your help

      open my $fh, '<', "C:/" . "conf.prop" or die "unable to open configura +tion file"; my $properties = Config::Properties->new(); $properties->load($fh); my $value = $properties->getProperty('my.token');

      This might help somebody. Simple question with simple answers.Thanks

Re: Properties files to read the tokes and passwords outside of the perl scripts.
by GrandFather (Saint) on Dec 14, 2020 at 04:36 UTC

    These days it is unusual to store passwords. Most often cryptographic hashes of passwords are stored and compared against hashes calculated for candidate passwords. This may not apply to your situation, but you haven't told us what that is. Maybe if you tell us more about the problem you are trying to solve we can give a better answer?

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
      These days it is unusual to store passwords.

      I read the question differently to you GrandFather - I took it that the OP wants to store passwords that they want to use rather than check. Something like a password that the script needs to connect to a database or similar.

      Does this difference of understanding come from a badly asked question?

        There are two parts to the answer:

        1. The question doesn't make it clear how the passwords might be used
        2. If the intended use is to feed the passwords into something else my answer still holds. It is best practice to use a public/private key pair instead of bare passwords. I guess that falls under "This may not apply to your situation, but ..."

        Yes, it is a badly asked question.

        Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
Re: Properties files to read the tokes and passwords outside of the perl scripts.
by haukex (Archbishop) on Dec 14, 2020 at 08:26 UTC

    There are lots of different ways to do this. JSON, YAML, XML, INI (simpler), .properties as mentioned by salva, even Perl (/shameless plug), and many more (see e.g. the Config:: namespace on CPAN). As for locating the files in the filesystem, there's also lots of ways to do this. Hardcoded paths (e.g. in /etc or perhaps /usr/local/etc), paths relative to the script's location via FindBin plus File::Spec or e.g. Path::Class for filename manipulations, having the config in the user's $HOME ($ENV{HOME} in Perl), in Windows via the registry, passing the config filename on the command line or in an environment variable, something as simple as the current working directory, or for example if you're running inside Docker, Docker secrets.

    Update: Expanded the lists above a little bit.

Re: Properties files to read the tokes and passwords outside of the perl scripts.
by stevieb (Canon) on Dec 14, 2020 at 05:54 UTC
    "Can you please share an example code for the same?"

    This is your challenge, not ours. This site is not a free code writing service.

    Post what you've completed so far, and ask questions on the part that doesn't work. You'll learn nothing if you simply expect people to do all of your work for you. That may seem harsh, but it's reality, and a valuable lesson.

Re: Properties files to read the tokes and passwords outside of the perl scripts.
by perlfan (Vicar) on Dec 14, 2020 at 19:42 UTC
    No matter what you store in a file, make sure the file with the secrets is properly set to be read only by the effective user (i.e., chmod 600). Another thing to note is that setting an environmental variable explicitly in the parent process using the information in this file will allow child processes to extract the password without require the child to re-read the file or have to pass it via commandline argument (which will show your password in the process list).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11125149]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-19 20:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found