Incorrect credentials response in Rails 6 - ruby-on-rails

Rails 6
I created the credentials file, as follows:
EDITOR=vi rails credentials:edit
production:
mysql:
db: acme-production
user: deploy
password: xxxxxxxxxxxx
smartagent:
token: lnroftb7sgr8c7f1ogqvij24xl
test:
mysql:
db: acme-test
user: deploy
password: xxxxxxxxxxxx
smartagent:
token: lnroftb7sgr8c7f1ogqvij24xl
secret_key_base: xxxxxxxxxxxxxx
master.key is in the correct place (in the config folder, locally, and as an environment setting on the server).
I am having trouble with the smartagent token.
When I do:
rails c
and
Rails.application.credentials.dig(Rails.env.to_sym, :mysql, :db)
I get:
=> "acme-test"
However, when I do:
Rails.application.credentials.dig(Rails.env.to_sym, :smartagent, :token)
I get:
=> nil
Any idea why this might be happening?
Solution:
I deleted the credentials file, re-populated it, and restarted the Rails app. That fixed the problem, so this must have been either an indentation issue (spaces vs tabs), or that the server had not restarted properly, after earlier changes were made

Related

Google Cloud build fails due to rails db:migrate

I'm following the 'Running Rails on the Cloud Run environment' instructions and have hit a snag. I used their provided github repo and the google cloud shell and I had success in launching the working application.
Now, I am trying to integrate Cloud Run into my rails template. While 'Deploying the app to Cloud Run' using the cloudbuild.yaml file provided, the build crashes during database migration. I am using postgreSQL. Here are the error details:
The error
"bundle exec rails db:migrate" ->
"ActiveRecord::ConnectionNotEstablished: could not connect to server: No such file or directory"
I think I've traced it to database.yml file where Google recommends this host:
production:
  <<: *default
  database: <%= ENV["PRODUCTION_DB_NAME"] %>
  username: <%= ENV["PRODUCTION_DB_USERNAME"] %>
  password: <%= Rails.application.credentials.gcp[:db_password] %>
  host: "<%= ENV.fetch("DB_SOCKET_DIR") { '/cloudsql' } %>/<%= ENV["CLOUD_SQL_CONNECTION_NAME"] %>"
It is unclear where this ENV.fetch("DB_SOCKET_DIR") comes from [at least to me, I'm new]. Their git repo holds a folder where I found templates for another build that included an app.standard.yaml and a config/database_unix.yml that I've tried integrating.
app.standard.yaml:
entrypoint: bundle exec rackup --port $PORT
runtime: ruby27
env_variables:
SECRET_KEY_BASE: <SECRET_KEY>
RAILS_ENV: production
INSTANCE_UNIX_SOCKET: /cloudsql/<PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>
DB_USER: <YOUR_DB_USER_NAME>
DB_PASS: <YOUR_DB_PASSWORD>
DB_NAME: <YOUR_DB_NAME>
beta_settings:
cloud_sql_instances: <PROJECT-ID>:<INSTANCE-REGION>:<INSTANCE-NAME>
database_unix.yml:
# [START cloud_sql_postgres_activerecord_connect_unix]
unix: &unix
adapter: postgresql
# Configure additional properties here.
# [END cloud_sql_postgres_activerecord_connect_unix]
pool: 5
timeout: 5000
# [START cloud_sql_postgres_activerecord_connect_unix]
# Note: Saving credentials in environment variables is convenient, but not
# secure - consider a more secure solution such as
# Cloud Secret Manager (https://cloud.google.com/secret-manager) to help
# keep secrets safe.
username: <%= ENV["DB_USER"] %> # e.g. "my-database-user"
password: <%= ENV["DB_PASS"] %> # e.g. "my-database-password"
database: <%= ENV.fetch("DB_NAME") { "vote_development" } %>
# Specify the Unix socket path as host
host: "<%= ENV["INSTANCE_UNIX_SOCKET"] %>"
# [END cloud_sql_postgres_activerecord_connect_unix]
development:
<<: *unix
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *unix
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 1 } %>
database: <%= ENV.fetch("DB_NAME") { "vote_test" } %>
production:
<<: *unix
database: <%= ENV.fetch("DB_NAME") { "vote_production" } %>
Some other solutions I've seen mention this instead of host:
socket: “/cloudsql/project_id:us-central1:photo-album-production”
I tried this with no luck. I cloned the repo to my machine and re-tried the Cloud Run instructions. No success this time as it's getting the same migration error. Am I thinking about this wrong?
Check this out: https://cloud.google.com/sql/docs/postgres/connect-build.
In short, you'll need to run the Cloud SQL Auth Proxy to create a Unix socket in Cloud Build so your app can connect.
If you're using a private IP instance, you'll need to make sure you're using private pools.

Postgresql won't accept password

I just created a new app and installed the 'pg' gem, created a psql user and database, and created a database.yml file. However, the server won't accept my password, even though it's the correct one.
Here are the commands I ran to create my user and database:
Project$ sudo -i -u postgres
postgres#user-F243:~$ psql
postgres=# CREATE USER Project WITH PASSWORD 'Project' CREATEDB;
postgres=# CREATE DATABASE Project OWNER Project;
which all went through with no errors. Then I created the following database.yml file:
development:
adapter: postgresql
database: Project
username: Project
password: Project
host: localhost
pool: 5
timeout: 5000
test:
adapter: postgresql
database: Project
username: Project
pool: 5
timeout: 5000
production:
adapter: postgresql
database: Project
username: Project
pool: 5
timeout: 5000
But when I start my rails server and try to load a page, I get this error:
PG::ConnectionBad
FATAL: password authentication failed for user "Project" FATAL: password authentication failed for user "Project"
Extracted source (around line #56):
#54 ### Convenience alias for PG::Connection.new.
#55 def self::connect( *args )
#56 return PG::Connection.new( *args ) #line 56
#57 end
I have tried multiple different passwords and user/database names, and I tried making the user a superuser, but I always get the same result. I've done all this a million times and it's always worked. What's going on??
postgres=# CREATE USER Project WITH PASSWORD 'Project' CREATEDB;
This code creates user 'project' instead of 'Project'. If you really want to use uppercase in user names, you have to double quote them:
postgres=# CREATE USER "Project" WITH PASSWORD 'Project' CREATEDB;
The same goes with table and column names - both creating and querying them.
Try removing host: localhost from the development: group. I've had similar problems in the past and this fixed them for me.

Rails application Error Incomplete response received from application

I have 3 applications ruby on rails in shared hosting, one of them SOMETIMES throw an error:
White screen and message:
Incomplete response received from application
I can't reproduce the error, only sometimes the application throw the error.
In the application log there aren't any error.
How you said me in the comments I search for config/secrets.yml. This doesn't exist in my application.
I create this with the config/initializer/secret_token.rb.
My config/secrets.yml:
development:
secret_key_base: "key in secret_token"
test:
secret_key_base: "key in secret_token"
production:
secret_key_base: "key in secret_token"
Then I deleted secret_token and restart server: "touch tmp/restart.txt".
The error still appears.

Mongoid only working in development. Throws error both is test and production

When I start my rails console as:
$ RAILS_ENV=development rails console
every thing seems to be working fine.
Mongoid is able to connect to mongodb and fetch records.
But with:
$ RAILS_ENV=test rails console
$ RAILS_ENV=production rails console
it's throwing up errors as:
Rack::File headers parameter replaces cache_control after Rack 1.5.
/usr/local/lib/ruby/gems/1.9.1/gems/mongoid-3.0.16/lib/mongoid/criteria.rb:585:in `check_for_missing_documents!': (Mongoid::Errors::DocumentNotFound)
Problem:
Document(s) not found for class Actor with id(s) 50e5259f53c205d815000001.
Summary:
When calling Actor.find with an id or array of ids, each parameter must match a document in the database or this error will be raised. The search was for the id(s): 50e5259f53c205d815000001 ... (1 total) and the following ids were not found: 50e5259f53c205d815000001.
Resolution:
Search for an id that is in the database or set the Mongoid.raise_not_found_error configuration option to false, which will cause a nil to be returned instead of raising this error when searching for a single id, or only the matched documents when searching for multiples.
My config/mongoid.yml has the exact same set of lines for all three environments.
I'm not able to figure out why it isn't able to connect in test and production.
Update:
Mongoid.yml
development:
sessions:
default:
database: tgmd
hosts:
- localhost:27017
test:
sessions:
default:
database: tgmd
hosts:
- localhost:27017
production:
sessions:
default:
uri: <%= ENV['MONGO_URL'] %>
I temporarily solved the issue by placing a:
options:
raise_not_found_error: false
in production:
Also moved out a few scripts from jobs/ folder. It worked then.
Can anyone enlighten me on this?
I think your ENV['MONGO_URL'] contains something that is not correct.
Try to use your development database in production:
production:
sessions:
default:
database: tgmd
hosts:
- localhost:27017
if it work. Check your ENV['MONGO_URL']

Unable to perform queries on mongodb started with --auth switch in rails 3 with mongoid.

Simplified case:
I create a new rails 3.2 project, without active record. I add mongoid 3.0.0.rc to the Gemfile and then rails g mongoid:config. I edit my mongoid.yml to look like the one I have posted below (except that hosts is now set to localhost:27068).
I have added an admin user to mongodb:
$ mongo localhost/admin
> db.addUser(myadmin,adminpass)
Also I have added a regular user to my database:
use mydb
> db.addUser(myuser, mypassword)
I confirm that I can connect to my database:
$ mongo localhost/mydb -u myuser -pmypassword
MongoDB shell version: 2.0.4
connecting to: localhost/mydb
> _
After that, I start mongod with --auth switch to force authentication:
$ mongod --auth --dbpath /my/db/path
Now that everything seems to be OK, I create some random scaffold like:
$ rails g scaffold User name email
and try to run the project in the browser: localhost:3000/users. BOOM! I'm hit with the error message posted below.
Is this a bug in mongoid? Or am I missing something?
Original Question
I'm unable to do anything on my MongoHQ hosted database in a Rails 3.2 project with mongoid 3 rc. A simple query for login action gives me something like this error message:
The operation: #<Moped::Protocol::Query
#length=83
#request_id=3
#response_to=0
#op_code=2004
#flags=[]
#full_collection_name="mydb.users"
#skip=0
#limit=-1
#selector={"name"=>"Abbas"}
#fields=nil>
failed with error 10057: "unauthorized db:mydb lock type:-1 client [some ip]"
Here's what my mongoid.yml looks like:
development:
sessions:
default:
database: mydb
user: myuser
password: mypassword
hosts:
- flame.mongohq.com:27068
options:
consistency: :strong
options:
include_type_for_serialization: true
So I'm doing it the wrong way. The db user is not marked as "Read-only" in MongoHQ panel. And I'm NOT deploying to Heroku; just testing on my localhost.
Any help is appreciated.

Resources