how to generate .htaccess password locally (by ruby)? - ruby

Is there a ruby script to generate password for .htpasswd? Thanks!

HTAuth is a pure Ruby implementation of the Apache htpasswd and htdigest utilities.
It also sports an API so you can use the functionality from within your own Ruby code without running external scripts.

Related

Any way to login to Elasticsearch using terminal?

I am new at Elasticsearch and am basically working around the security aspects of it. So after defining the user-roles and creating new users, whenever I want to run a curl command using terminal, I have to specify the user credentials like,
-u {username}:{password}
So, is there any way to login to the localhost so that I enter the credentials one time only and after that I can simply run the commands without entering the credentials?
I am using the basic license of Elasticsearch.
You can use the netrc feature provided by curl. You basically store the credentials in a file and use -n flag so curl accesses creds from that file.
Reference: https://everything.curl.dev/usingcurl/netrc

Where to put API keys for Twitter app on server

I'm currently writing a couple Twitter bots for my friends using the Twitter gem for Ruby. My plan was to store the keys for them in a .txt file with the rest of the bot's code on my server, but everything I've read has said the keys shouldn't be readable within the code. Is this secure enough, and if not what would be a good solution? Thanks!
A common approach is to save the environment variables into a file called .env that is ignored by version control (and therefore won't be included on Github) but read by the code. One gem to help with this is dotenv.
add .env to the .gitignore file.
create a local .env file with all your env vars
require 'dotenv' and put Dotenv.load somewhere at the beginning of your script. In Rails, the require is unnecessary and you can place the load call in any file in the config/initializers folder
Check that your app works fine locally. The environment variables should be found in the ENV hash from Ruby code.
Save changes and push new version of app to digital ocean
manually create the .env file on the digital ocean server, in the root of the repo
run digital ocean server and check that everything works.
other notes:
see How To Read and Set Environmental and Shell Variables on a Linux VPS
some platforms like heroku have a different mechanism for setting environment variables, such as heroku config:set or web UIs.
You can set environment variables on a one-off basis using the env command in bash, for example:
env a=hello b=' world' ruby -e 'puts ENV["a"] + ENV["b"]'
# => hello world
This can give a quick way to configure a program without getting into argument parsing. For example in Rails, you can say rails c test to open a console using the test environment, but env RAILS_ENV=test rails c should do the same thing.

Ruby scripting - how to keep track of configurations

I am writing a system script in Ruby.
I'm using the classic gem structure: lib, bin, spec for RSpec.
I want to build a configurable script: I want to be able to provide options like --set-stuff and alike. A perfect example is:
git config --global user.name "Andrea"
which writes the given information out to a file, in order to be able to retrieve this information later.
How can I do this in a clean way?
I'd rather not use the environment variable solution: I know I could just set an env variable to point to a configuration file, but then I'd have to save this env variable in, say, .bashrc. Then again, how do I deal with zsh? Or how do I deal with people (like me) who keep their .bashrcs super-neat or even have a separate .env-variables file in their system?
Just stick the configuration into a Hash and serialize it into a file in the user's home directory as YAML or JSON...

What's the simplest way to run a cron job on a standalone Ruby gem?

I have a gem that packages one .rb file containing my class and associated methods as well as a corresponding .bin file.
Locally, I can run everything just fine like so:
command_to_bin input_file output_file
I don't want to run this manually every day so I'm considering using cron on a server, but I'm a little unsure how to proceed.
Do I throw everything into a directory (.gem file, input file, output file) and just point the above cron command at the directory?
I've looked at this and sort of understand what's going on. I guess what confuses me the most is that when I look at all the web hosting providers, they mention domains and applications, but I just want to know how to have the standalone script run by itself without it being built into a web application or associated with a domain.
Check out the whenever gem. It's a wonderful gem to abstract all the nastiness of cron. Just include the command as you have written above and it should be fine.
You don't need to install Rails. After you wheneverize . the directory and set the schedule in your schedule.rb file, you need to run whenever --update-crontab to set everything to the system. Otherwise your cron jobs never get converted to Unix Cron

Simplest way to run ruby CGI app

What is the simplest way to run locally a Ruby CGI app
I am looking for a very simple e.g. 5 lines of code if possible way without using external servers such as NginX and Apache etc
EDIT:
To be more precise:
Given a simple Ruby script I would like to serve it as CGI, either by requiring a Gem within it or by using another tiny .rb script.
By serve as CGI I mean to be able to interact with it using my web browser
$ gem install cgiup
$ cgiup ruby_cgi_script.rb
If you're not married to a particular webserver and don't need a ton of volume, you can set up and run Sinatra with its standalone Webrick server in about five lines of code.
CGI proper will require more setup, however the Lighttpd web server is relatively simple to configure for Ruby CGI. The only things you have to add to lighttpd.conf are:
server.modules += ( "mod_cgi" )
cgi.assign = (
".rb" => "/usr/local/bin/ruby" # or whatever your path to Ruby is
)

Resources