(Basic authentication) environment variables in rails and Docker

Basic authentication on Docker

I implemented Basic authentication on Docker, but I couldn't enter the site, so I thought about the reason.

~~ Conclusion ~~

~~ Super easy ~~ ** Please read to the bottom. ** **

I will set environment variables on Docker

docker-compose.yml


version: '3'
services:
  web:
    environment:
      BASIC_AUTH_USER: 'admin' #Temporary user
      BASIC_AUTH_PASSWORD: '0000' #Temporary password

As a reason

app/controllers/application_controller.rb


class ApplicationController < ActionController::Base
  before_action :basic_auth


  private

  def basic_auth
    authenticate_or_request_with_http_basic do |username, password|
      username == ENV["BASIC_AUTH_USER"] && password == ENV["BASIC_AUTH_PASSWORD"]#Description to read environment variables
    end
  end
end

I think that BASIC authentication is implemented with the above description. You may be calling by setting environment variables on the terminal, but ** you have to set environment variables on Docker ** as well. The reason is that Docker creates a container as a virtual machine separate from the PC you are currently using and runs the application on it.

~~ Impressions ~~

I thought it was a relief, but please wait a moment.

** Can't you give it to Git? .. ?? ** **

If you merge in the current state, ** the user id and password of Basic authentication are completely visible ** from the Git repository, and there is no security.

So

Conclusion

docker-compose.yml


version: '3'
services:
  web:
    environment:
      BASIC_AUTH_USER: ${BASIC_AUTH_USER:-default}
      BASIC_AUTH_PASSWORD: ${BASIC_AUTH_PASSWORD-default}

By writing as above, you can bring the environment variables of the server and set them so that they cannot be seen from the outside. ** Don't forget to build because I touched the docker-compose.yml file **.

Impressions

I'm a beginner, so I'm sorry if I knew it. .. I would appreciate it if you could tell me if there is a bad part! I'm glad I noticed it before merging, and it seems that it can be used when using other APIs.

Recommended Posts

(Basic authentication) environment variables in rails and Docker
[Docker] Use environment variables in Nginx conf
Show Better Errors in Rails + Docker environment
Rails Docker environment construction
Introduce dotenv to Docker + Rails to manage environment variables
Summary of frequently used commands in Rails and Docker
Migration error after Activerecord association in Rails5 + Docker environment (2)
Migration error after Activerecord association in Rails5 + Docker environment
SSL in the local environment of Docker / Rails / puma
Implement LTI authentication in Rails
Implement Basic authentication in Java
[Environment variables] What are rails environment variables?
[Docker] Rails 5.2 environment construction with docker
How to set and describe environment variables using Rails zsh
Exit code 1 occurs when Rails is stopped in Docker environment
Create Rails5 and postgresql environment with Docker and make pgadmin available
Rails + MySQL environment construction with Docker
Run LibreOffice Basic macros in Docker
Enable jQuery and Bootstrap in Rails 6 (Rails 6)
Build environment with vue.js + rails + docker
Build Rails environment with Docker Compose
Install laravel/Dusk in docker environment (laravel6)
Rails on Docker environment construction procedure
Remove "assets" and "turbolinks" in "Rails6".
CRUD features and MVC in Rails
[Environment construction with Docker] Rails 6 & MySQL 8
[Rails 6] Customize Bootstrap in Rails + Bootstrap 5.0.0-alpha environment
Pass environment variables to docker container
Setting project environment variables in intelliJ
Check MySQL logs in Docker environment
When log data accumulates in Rails and the environment stops working
Install Rails in the development environment and create a new application
I built a rails environment with docker and mysql, but I got stuck
Alert slack with alert manager in Docker environment
[Environment construction] Rails + MySQL + Docker (Beginners can also use it in 30 minutes!)
Rails environment construction with Docker (personal apocalypse)
Kind @ Mac in Docker and vctl container
Create Rails 6 + MySQL environment with Docker compose
Migrate existing Rails 6 apps to Docker environment
Use docker in proxy environment on ubuntu 20.04.1
Edit Mysql with commands in Docker environment
Handle system environment variables in Spring application.properties
[Docker] Development environment construction Rails6 / Ruby2.7 / MySQL8
[Docker] How to create a virtual environment for Rails and Nuxt.js apps
[Rails 6] Register and log in with Devise + SNS authentication (multiple links allowed)
Launching the production environment with docker + rails (ver5.2) and errors that occurred
Build a bulletin board API with authentication and authorization with Rails 6 # 1 Environment construction
[Rails 5] Countermeasures for git commit failure from another branch in Docker environment
[Node.js express Docker] How to define Docker environment variables and load them with node.js
How to use environment variables in RubyOnRails
[Rails] Reset the database in the production environment
How to apply Rails environment variables to Docker container (aws :: Sigv4 :: Errors solution)
Install Docker and create Java runtime environment
How to build Rails 6 environment with Docker
Introduce Docker to the development environment and test environment of existing Rails and MySQL applications
Run Docker environment Rails MySQL on Heroku. devise and hiding the twitter API
Solved DB connection and CORS problem when developing Laravel × Nuxt.js in Docker environment
Build a development environment for Docker + Rails6 + Postgresql
Create authentication function in Rails application using devise
Just install Laravel8 on docker in PHP8 environment
[Rails] Run LINEBot in local environment using ngrok