Rails No Master.key Generated
Rails No Master.key Generated 4,9/5 4033 reviews

May 21, 2019 The master key file generated by Rails (config/master.key) as explained above. The Rails production database password that we will pass in the DATABASEPASSWORD environment variable at runtime. Where’s the master key? You guessed it: in the aptly-named config/master.key file. This file was generated when you initially generated the Rails app. And if you look inside, you’ll see a master key that unlocks the secrets of your app! If you ever want to generate a new master key, you can run.

This article is a translation from https://qiita.com/kawasaki/items/dcaf5716c3fd5e2fe69f

Digest version to set up the secret key in the production environment in Rails 5.1

  1. $ rails secrets:setup -> copy the long secret key in the first line and set it as an environment variable RAILS_MASTER_KEY when executing rails
  2. $ rails secret -> you will get the longer key; copy it
  3. $ EDITOR=vim rails secrets:edit write the following and save it from vim

Errors in the production environment when I run 'rails server'

I've developped my application happily in the development environment. But it's high time I prepared for the production environment, and I tried

$ rails server --environment production

and got
An unhandled lowlevel error occurred. The application logs may have details.

Oh, no..
Next, I tried
$ rails secrets:setup

which was introduced in Rails 5.1. But the error still remained. Going down in to the rails library, I got the point where I was wrong.

Anyway, you'll get the following output wehn you run rails secrets:setup

The encryption key 'a1e98ed29c40d7453a06bebeb815c0f3' in the first line is the most important key to protect the whole application and you must keep it secret. The key is automatically written down to config/secrets.yml.key.
Never commit it to your git repository, NEVER!
But usually you'll not commit it because the file config/secrets.yml.key is automatically added to .gitignore, so that the file is not to be committed.

For rails execution, you need either to import config/secrets.yml.key from somewhere or to set the secret key in the environment variable RAILS_MASTER_KEY.

Actually, this in only the half of the necessary settings because what you did is to make a key to lock config/secrets.yml.enc, you treasure box. You still need to store your treasure.
This file, secrets.yml.enc is an encrypted yml file using your key. It looks like as follows.

To edit this file (if you use vim),
$ EDITOR=vim rails secrets:edit

The file looks like as follows.

Everything is commented out and nothing is specified above. You have to specify secret_key_base beneath production which is a secret key used for Cookie encryption. It is recommended to generate the secret key by using 'rails secret'.

Then, save it in your config/secrets.yml.enc by doing

Now, you have no error regarding the secret key in Rails.

By the way, you can store any kind of secret information here including database passwords. e.g.

You can use the encrypted database password from your code by
Rails.application.secrets.postgresql_password
e.g. in config/database.yml.

-->

App Service on Linux provides a highly scalable, self-patching web hosting service using the Linux operating system. This tutorial shows how to create a Ruby app and connect it to a PostgreSQL database. When you're finished, you'll have a Ruby on Rails app running on App Service on Linux.

In this tutorial, you learn how to:

  • Create a PostgreSQL database in Azure
  • Connect a Ruby on Rails app to PostgreSQL
  • Deploy the app to Azure
  • Update the data model and redeploy the app
  • Stream diagnostic logs from Azure
  • Manage the app in the Azure portal

If you don't have an Azure subscription, create a free account before you begin.

Prerequisites

To complete this tutorial:

Prepare local Postgres

In this step, you create a database in your local Postgres server for your use in this tutorial.

Rails No Master.key Generated Money

Connect to local Postgres server

Open the terminal window and run psql to connect to your local Postgres server.

If your connection is successful, your Postgres database is running. If not, make sure that your local Postgres database is started by following the steps at Downloads - PostgreSQL Core Distribution.

Type q to exit the Postgres client.

Create a Postgres user that can create databases by running the following command, using your signed-in Linux username.

Create a Ruby on Rails app locally

Rails No Master.key Generated

In this step, you get a Ruby on Rails sample application, configure its database connection, and run it locally.

Clone the sample

In the terminal window, cd to a working directory.

Run the following command to clone the sample repository.

cd to your cloned directory. Install the required packages.

Run the sample locally

Run the Rails migrations to create the tables the application needs. To see which tables are created in the migrations, look in the db/migrate directory in the Git repository.

Run the application.

Navigate to http://localhost:3000 in a browser. Add a few tasks in the page.

To stop the Rails server, type Ctrl + C in the terminal.

Use Azure Cloud Shell

Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. You can use either Bash or PowerShell with Cloud Shell to work with Azure services. You can use the Cloud Shell preinstalled commands to run the code in this article without having to install anything on your local environment.

To start Azure Cloud Shell:

OptionExample/Link
Select Try It in the upper-right corner of a code block. Selecting Try It doesn't automatically copy the code to Cloud Shell.
Go to https://shell.azure.com, or select the Launch Cloud Shell button to open Cloud Shell in your browser.
Select the Cloud Shell button on the menu bar at the upper right in the Azure portal.

To run the code in this article in Azure Cloud Shell:

  1. Start Cloud Shell.

  2. Select the Copy button on a code block to copy the code.

  3. Paste the code into the Cloud Shell session by selecting Ctrl+Shift+V on Windows and Linux or by selecting Cmd+Shift+V on macOS.

  4. Select Enter to run the code.

Create Postgres in Azure

In this step, you create a Postgres database in Azure Database for PostgreSQL. Later, you configure the Ruby on Rails application to connect to this database.

Create a resource group

A resource group is a logical container into which Azure resources like web apps, databases, and storage accounts are deployed and managed. For example, you can choose to delete the entire resource group in one simple step later.

In the Cloud Shell, create a resource group with the az group create command. The following example creates a resource group named myResourceGroup in the West Europe location. To see all supported locations for App Service on Linux in Basic tier, run the az appservice list-locations --sku B1 --linux-workers-enabled command.

You generally create your resource group and the resources in a region near you.

When the command finishes, a JSON output shows you the resource group properties.

Create a Postgres server

Create a PostgreSQL server with the az postgres server create command.

Run the following command in the Cloud Shell, and substitute a unique server name for the <postgres-server-name> placeholder. The server name needs to be unique across all servers in Azure.

When the Azure Database for PostgreSQL server is created, the Azure CLI shows information similar to the following example:

Configure server firewall

In the Cloud Shell, create a firewall rule for your Postgres server to allow client connections by using the az postgres server firewall-rule create command. When both starting IP and end IP are set to 0.0.0.0, the firewall is only opened for other Azure resources. Substitute a unique server name for the <postgres-server-name> placeholder.

Tip

I am developing a platform that uses several secret keys for several usages: key1 for hashing passwords (using pbkdf2-hmac-sha256), key2 to generate non-repeating unpredictable uuids (using aes-128 and a counter), etc. Instead of storing different keys, I thought to generate them from a single key, i.e. HMAC Generator / Tester Tool. Computes a Hash-based message authentication code (HMAC) using a secret key. A HMAC is a small set of data that helps authenticate the nature of message; it protects the integrity and the authenticity of the message. I'd like to know if there is a standard code to generate a SHA256 hash using a key. I've come across several types of code, however, they don't generate the same output. Code found at JokeCamp pr. Codeigniter 3 hmac generate key code.

You can be even more restrictive in your firewall rule by using only the outbound IP addresses your app uses.

Connect to production Postgres server locally

In the Cloud Shell, connect to the Postgres server in Azure. Use the value you specified previously for the <postgres-server-name> placeholders.

When prompted for a password, use My5up3r$tr0ngPa$w0rd!, which you specified when you created the database server.

Create a production database

At the postgres prompt, create a database.

Create a user with permissions

Create a database user called railsappuser and give it all privileges in the sampledb database.

Exit the server connection by typing q.

Connect app to Azure Postgres

In this step, you connect the Ruby on Rails application to the Postgres database you created in Azure Database for PostgreSQL.

Configure the database connection

In the repository, open config/database.yml. At the bottom of the file, replace the production variables with the following code.

Save the changes.

Test the application locally

Back in the local terminal, set the following environment variables:

Run Rails database migrations with the production values you just configured to create the tables in your Postgres database in Azure Database for PostgreSQL.

When running in the production environment, the Rails application needs precompiled assets. Generate the required assets with the following command:

The Rails production environment also uses secrets to manage security. Generate a secret key.

Save the secret key to the respective variables used by the Rails production environment. For convenience, you use the same key for both variables.

Enable the Rails production environment to serve JavaScript and CSS files.

Run the sample application in the production environment.

Navigate to http://localhost:3000. If the page loads without errors, the Ruby on Rails application is connecting to the Postgres database in Azure.

Add a few tasks in the page.

To stop the Rails server, type Ctrl + C in the terminal.

Commit your changes

Run the following Git commands to commit your changes:

Your app is ready to be deployed.

Deploy to Azure

In this step, you deploy the Postgres-connected Rails application to Azure App Service.

Configure a deployment user

FTP and local Git can deploy to an Azure web app by using a deployment user. Once you configure your deployment user, you can use it for all your Azure deployments. Your account-level deployment username and password are different from your Azure subscription credentials.

To configure the deployment user, run the az webapp deployment user set command in Azure Cloud Shell. Replace <username> and <password> with a deployment user username and password.

Rails No Master.key Generated Home

  • The username must be unique within Azure, and for local Git pushes, must not contain the ‘@’ symbol.
  • The password must be at least eight characters long, with two of the following three elements: letters, numbers, and symbols.

The JSON output shows the password as null. If you get a 'Conflict'. Details: 409 error, change the username. If you get a 'Bad Request'. Details: 400 error, use a stronger password.

Record your username and password to use to deploy your web apps.

Create an App Service plan

In the Cloud Shell, create an App Service plan in the resource group with the az appservice plan create command.

The following example creates an App Service plan named myAppServicePlan in the Free pricing tier (--sku F1) and in a Linux container (--is-linux).

When the App Service plan has been created, the Azure CLI shows information similar to the following example:

Create a web app

Create a web app in the myAppServicePlan App Service plan.

In the Cloud Shell, you can use the az webapp create command. In the following example, replace <app-name> with a globally unique app name (valid characters are a-z, 0-9, and -). The runtime is set to RUBY 2.3. To see all supported runtimes, run az webapp list-runtimes --linux.

When the web app has been created, the Azure CLI shows output similar to the following example:

You’ve created an empty new web app, with git deployment enabled.

Note

The URL of the Git remote is shown in the deploymentLocalGitUrl property, with the format https://<username>@<app-name>.scm.azurewebsites.net/<app-name>.git. Save this URL as you need it later.

Configure database settings

In App Service, you set environment variables as app settings by using the az webapp config appsettings set command in the Cloud Shell.

Rails No Master.key Generated Video

The following Cloud Shell command configures the app settings DB_HOST, DB_DATABASE, DB_USERNAME, and DB_PASSWORD. Replace the placeholders <appname> and <postgres-server-name>.

Configure Rails environment variables

In the local terminal, generate a new secret for the Rails production environment in Azure.

Configure the variables required by Rails production environment.

In the following Cloud Shell command, replace the two <output-of-rails-secret> placeholders with the new secret key you generated in the local terminal.

ASSETS_PRECOMPILE='true' tells the default Ruby container to precompile assets at each Git deployment. For more information, see Precompile assets and Serve static assets.

Push to Azure from Git

In the local terminal, add an Azure remote to your local Git repository.

Push to the Azure remote to deploy the Ruby on Rails application. You are prompted for the password you supplied earlier as part of the creation of the deployment user.

During deployment, Azure App Service communicates its progress with Git.

Browse to the Azure app

Browse to http://<app-name>.azurewebsites.net and add a few tasks to the list.

Congratulations, you're running as Overview page. Here, you can perform basic management tasks like stop, start, restart, browse, and delete.

The left menu provides pages for configuring your app.

Clean up resources

In the preceding steps, you created Azure resources in a resource group. If you don't expect to need these resources in the future, delete the resource group by running the following command in the Cloud Shell:

This command may take a minute to run.

Next steps

In this tutorial, you learned how to:

  • Create a Postgres database in Azure
  • Connect a Ruby on Rails app to Postgres
  • Deploy the app to Azure
  • Update the data model and redeploy the app
  • Stream diagnostic logs from Azure
  • Manage the app in the Azure portal

Rails No Master.key Generated Money

Advance to the next tutorial to learn how to map a custom DNS name to your app.

Rails No Master.key Generated Number

Or, check out other resources: