Securing remote mongodb server to use with Mongoid - ruby-on-rails

OK SO land, I have exhausted all attempts to find an answer for my issue.
I have a RoR app running Mongoid 3.16 and a remote server with Mongodb 3.05. Both servers are on the same network and I am using internal IP address to connect.
The problem that I am having is trying to secure the connection. First off, if I try to add the app IP to the bind_ip in the mongod.conf file I get an error:
Cannot assign requested address for socket: app_ip_address
Here is the bind_ip in the mongod.conf:
#bind_ip=127.0.0.1,app_ip_address
If I uncomment it and try to restart mongodb it gives me the error about "Cannot assign requested address..." So I can connect from the app to mongodb IF I leave the bind_ip commented out. I do not like this idea because it will allow any connection.
My mongoid.yml file is:
production:
sessions:
default:
database: my_database
hosts:
- mongodb_server_ip:27017
I also set up the database to have a user but if I set the auth to true for Mongodb, it will allow connection to the "my_database" without authorization, but cannot read the data unless I authenticate again (which I can do command line but not via the mongoid.yml file)
For example if I connect command line from the app server with:
mongo mongoddb_server_ip:27017/my_database
That works but when I try to view collections I have to db.auth(....) to see them.
I guess that if I can config the mongoid.yml with the username/password for mongodb that would work but I cannot figure out how to do that.
Update 11/23:
OK, I set the mongod.conf file to auth = true. Now I can connect from the app server via ssh:
mongo mnogodb_server_ip:27017/my_database --username some_username --password some_password
That allows me to connect and access all collections etc. Now that I am requiring authentication I updated my mongoid.yml file as so:
production:
sessions:
default:
database: my_database
username: some_username
password: some_password
hosts:
- mongodb_server_ip:27017
I restart the server and I cannot connect to the remote db.

Well, after all of this I find out that moped 2 does not support authentication for MongoDB 3+.
see here:
Rails Mongoid fails to authenticate - failed with error 13: "not authorized for query on my_db.my_collection"

Related

Error connecting Rails 5 app database.yml to AWS RDS instance

I already set up SLL certificate like here:
export DATABASE_URL="mysql2://leder:password#pfhpdb.cyo1f7mucyku.eu-central-1.rds.amazonaws.com/pfhpdb?sslca=config/amazon-rds-ca-cert.pem"
when running my heroku rails 5 app w/ production environment and DB I get the following error:
ActionView::Template::Error (Can't connect to MySQL server on 'pfhpdb.cyo1f7mucyku.eu-central-1.rds.amazonaws.com' (101 "Network is unreachable")):
My database.yml is as follows:
production:
adapter: mysql2
encoding: utf8
host: pfhpdb.cyo1f7mucyku.eu-central-1.rds.amazonaws.com
database: pfhpdb
pool: 5
username: <%= ENV['RDS_USERNAME'] %>
password: <%= ENV['RDS_PASSWORD'] %>
Comment: same error when connecting to RDS w/ SQuirreL - class java.net.SocketException: Network is unreachable (connect failed)!
UPDATE 20200503:
as of this post:
Mariadb connection client: Access denied for user (using password: NO) on mysql 8.0
I cannot login w/ password in MariaDB and cannot change DB table access b/c I cannot login w/ password in MariaDB. I am shut off from my DB...
Please check security group of your RDS instance; probably you have not opened the port. Also, please try this to see if port is open or not.
$ telnet cyo1f7mucyku.eu-central-1.rds.amazonaws.com 3306
In the end the authentication error went away by resetting the AWS RDS DB administrator password in the console settings of RDS.

How to create an Oracle database inside Vagrant and connect to it in my local Rails app and SQL Developer?

I followed instructions at https://github.com/oracle/vagrant-boxes's OracleDatabase/18.3.0/README.md.
When the vagrant up installation finished, it outputs in terminal:
oracle-18c-vagrant: ORACLE PASSWORD FOR SYS, SYSTEM AND PDBADMIN: my_password
I tried to connect to this database via SQL Developer with the following values in the new database connection window:
Connection Name: my_local_oracle_db
Username: PDBADMIN
Password: my_password
Connection Type: Basic
Hostname: localhost
Port: 1521
SID: ORCLCDB
But it gives me an error: ORA-01017: invalid username/password; logon denied
Q1) Do I need to create a new database inside vagrant first? What instruction can I use to create it?
Q2) And what value should I enter to connect to it using SQL Developer?
Next, to connect it using Rails
I used this config in config/database.yml:
test:
adapter: oracle_enhanced
hostname: localhost
port: 1521
username: PDBADMIN
password: my_password
But when I did rails db:migrate, it failed with this error:
$ RAILS_ENV=test rails db:migrate
Warning: NLS_LANG is not set. fallback to US7ASCII.
rails aborted!
NoMethodError: undefined method `match' for nil:NilClass
Q3) What values should I be entering into config/database.yml?
My Rails app has ruby-oci8 and activerecord-oracle_enhanced-adapter gems installed.
Q1: No you don't have to create a database; the Vagrantfile creates a CDB (ORCLCDB) and a PDB (ORCLPDB1).
When the provisioning completes both are ready to use.
See About the Multitenant Architecture if you are unfamiliar with the Multitenant concept.
Q2: As already mentioned by Bjarte, to connect to the PDB you need to use the Service Name option instead of the SID.
The PDBADMIN user only exists in the ORCLPDB1 PDB, so you need to specify ORCLPDB1 as service.
Note that PDBADMIN does not have a lot of privileges after install, you probably want to first connect to the CDB as SYSTEM and grant more privileges to PDBADMIN -- E.g.
ALTER SESSION SET CONTAINER=ORCLPDB1
GRANT DBA TO PDBADMIN
Also note that you have a typo in the port number (1512 instead of 1521)
Q3: See the oracle-enhanced documentation
The following works for me:
development:
adapter: oracle_enhanced
database: //localhost:1521/orclpdb1
username: pdbadmin
password: MySecretPassword
$ rake db:migrate
== 20200319203904 CreateAuthors: migrating ====================================
-- create_table(:authors)
-> 0.0628s
== 20200319203904 CreateAuthors: migrated (0.0630s) ===========================
Q1) Do I need to create a new database inside vagrant first? What instruction can I use to create it?
I think the database is already created inside vagrant so I don't need to create it again.
Q2) And what value should I enter to connect to it using SQL Developer?
Enter the following values in the new database connection window:
Connection Name: my_local_oracle_db
Username: SYS
Password: my_password
Role: SYSDBA
Connection Type: Basic
Hostname: localhost
Port: 1521
SID: ORCLCDB
This will connect me to the SYS user in the database.
but I think for what I want, I need to create another user in the same database using the SYS user.
To do this, after connecting to the above database in SQL Developer, run below SQL commands:
CREATE USER C##db_name_here IDENTIFIED BY my_password2 DEFAULT TABLESPACE "USERS" TEMPORARY TABLESPACE "TEMP" ACCOUNT UNLOCK;
GRANT CONNECT TO C##db_name_here;
GRANT RESOURCE TO C##db_name_here;
GRANT UNLIMITED TABLESPACE TO C##db_name_here;
GRANT CREATE VIEW TO C##db_name_here;
GRANT CREATE MATERIALIZED VIEW TO C##db_name_here;
GRANT CREATE PROCEDURE TO C##db_name_here;
GRANT CREATE TYPE TO C##db_name_here;
GRANT CREATE DATABASE LINK TO C##db_name_here;
This user will be the one I connect to in my Rails config/database.yml
Q3) What values should I be entering into config/database.yml?
test:
adapter: oracle_enhanced
hostname: localhost
port: 1521
database: ORCLCDB
username: C##db_name_here
password: my_password2

Can't connect localhost to heroku database

I am trying to get a rails app to work with heroku but I am doing something wrong.
Database.yml
development:
adapter: postgresql
encoding: unicode
database: (heroku db)
user: (heroku db_user)
pool: 5
password: (heroku db_pass)
If I enter the information from my heroku database I get:
PG::ConnectionBad FATAL: password authentication failed for user
I am not rails guy, But this is something that i have done just now on my spring app, I wanted to connect my localhost development to heroku db.. I got db info from this:
heroku pg:credentials DATABASE
then I have passed params.. But I found that to connect remote heroku db, I needed to add this to my connection params..
ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory
I guess, you also need to specify the host and port. To find out the host try running this command:
$ heroku config | grep HEROKU_POSTGRESQL
You should see something like
HEROKU_POSTGRESQL_RED_URL: postgres://user3123:passkja83kd8#ec2-117-21-174-214.compute-1.amazonaws.com:6212/db982398
if you already added the PostgreSQL add-on. More on this read here.
Then add the host and port info to your configuration:
development:
...
host: ec2-117-21-174-214.compute-1.amazonaws.com
port: 6212
...
But do remember that it's a bad idea to work with the production database from your local dev machine, unless you really know what you are doing.

How do I set up a postgresql database on Dotcloud?

I'm trying to upload a rails app to dotcloud.
I'm getting this error:
PG::Error (could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
):
activerecord (3.2.5) lib/active_record/connection_adapters/postgresql_adapter.rb:1206:in `initialize'
I'm guessing it's because I haven't set up a postgres database. How do I do that? The docs don't appear to say.
My dotcloud.yml file looks like this:
www:
type: ruby
exclude_bundler_groups:
- development
data:
type: postgresql
My database.yml looks like this:
production:
adapter: postgresql
database: my_app_production # have not set this up on dotcloud yet as I can't find any docs on how to do it
username: root
password: mypassword
Do I have to run migrations? How do I do that, again the docs don't say.
Is there an idiots guide to setting up a rails app on dotcloud? I'm coming over from Heroku, where you just push your code and run your migrations. I've spent a good few hours struggling with the dotcloud docs and can't get this app to run.
After you create your app with that yaml file, like "dotcloud push APPNAME app-on-dotcloud/", you should run this line "dotcloud APPNAME.data info" and you get all info for connect to database that you can add to database.yml
UPDATE
You've obtained all info for access to postgresql terminal: user, password, port and host. You need run "dotcloud ssh APPNAME.www", for you connect to postgresql with "psql -h myapp-myname.dotcloud.com -p 29876 -U root", then put your password. Finally, create your database with "CREATE DATABASE 'dbname';" and update your database.yml.

Rails postgresql DB connection fails

I'm very new to Ruby and postgres.
Below is my database.yml
development:
adapter: postgresql
database: test_database
username: postgresql
password: mypassword
host: localhost
encoding: utf8
The user exists and I'm, able to login using same credentials in phpPgadmin. But when I start rails server and go to home page of app, I get FATAL: Ident authentication failed for user "postgresql".
Edit: In case pghba.conf matters,
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
#local all all peer
local all postgres md5
local all postgresql md5
Could anyone please help ?
open PostgreSQL client authentication configuration file
vi /var/lib/pgsql/data/pg_hba.conf
This file manage below stuffs
Which hosts are allowed to connect
How clients are authenticated
Which PostgreSQL user names they can use
Which databases they can access
By default Postgresql uses IDENT-based authentication. All you have to do is allow username and password based authentication for your network or webserver. IDENT will never allow you to login via -U and -W options. Append following to allow login via localhost only:
local all all trust
host all 127.0.0.1/32 trust
Save and close the file. Restart Postgresql server:
service postgresql restart OR
sudo /etc/init.d/postgresql restart
It should work
I can find my pg_hba.conf file in the path:
/etc/postgresql/8.4/main/pg_hba.conf
For anyone who still can't find their pg_hba.conf file, I'm using PostgreSQL v9.2 and I found mine in:
/var/lib/pgsql/9.2/data/pg_hba.conf

Resources