Authenticate_with_http_basic block not executed

6/18/2019

I am observing some weird behavior in my Rails 5.2 app where the basic authentication portion is skipped and is always falsey.

This code runs perfectly fine under local dev environment but authenticate_with_http_basic block is never hit under a kubernetes deployment.

The kubernetes app deployment is fronted by a nginx proxy

     class ApplicationController < ActionController::Base
       before_action :authenticate

       def authenticate
         if _valid_credentials?
           # never gets to this part
           true
         else
           # enters the else block
           request_http_basic_authentication
        end
      end

      def _valid_credentials?
        Rails.logger.debug("Function is entered")

        authenticate_with_http_basic do |username, password|
          cred = "#{username}|#{password}"
          # Nothing is printed with Rails.logger.debug
          Rails.logger.debug("Received credentials: #{cred}")
          # Running rails console reveals the correct ::Configuration.credentials
          SecureCompare.compare(cred, ::Configuration.credentials)
        end
      end
    end

kubernetes logs:

nginx xxx.xxx.xx.xx 0.004 0.002930 - test_user [18/Jun/2019:20:11:26 +0000]  "GET / HTTP/1.1" 401 38 "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3829.0 Safari/537.36" b9af3f83-b2b8-43e1-bee7-1603f5ada6c2 -
app-deployment-name Processing by XXXController#index as HTML
app-deployment-name Function is entered
app-deployment-name Filter chain halted as :authenticate rendered or redirected
app-deployment-name Completed 401 Unauthorized in 0ms (ActiveRecord: 0.0ms)

Any idea or suggestion is welcome.

Thanks.

-- cyc115
kubernetes
ruby
ruby-on-rails-5

0 Answers