Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a verification API #847

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions lib/proxy/verify_runtime.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module Proxy
class VerifyRuntime
class << self
# RFC5737 declares 192.0.2.0/24 as TEST-NET-1
MOCK_IP = '192.0.2.42'

def settings
Proxy::SETTINGS
end

def verify
{
reverse_proxy: verify_reverse_proxy,
}
end
Comment on lines +11 to +15
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking about the format. Perhaps it should have some global status and then a hash for modules.

{
  "global": {},
  "modules": {
    "templates": {
      "status": true,
      "reverse_proxy": true,
    },
    "registration": {
      "status": true,
    }
  }
}

Perhaps also a summary?

What if it doesn't? Should we use error codes? Human readable strings? both?


def verify_reverse_proxy
# It's valid if there is no Foreman URL
return true unless settings.foreman_url

# Only needed for templates / registration
# TODO: make this more generic
return true unless ::Proxy::Plugins.instance.any? { |p| p[:state] == :running && ['templates', 'registration'].include?(p[:name]) }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

templates has a template_url setting while registration uses the global foreman_url setting.


foreman = Proxy::HttpRequest::ForemanRequest.new
request = foreman.request_factory.create_get('/api/status', headers: {'X-Forwarded-For': MOCK_IP})
response = foreman.send_request(request)

if response.status != '200'
logger.info("Foreman status API returned #{response.status}")
return false
end

status = JSON.parse(response.body)
unless status.key?('remote_ip')
message = if ::Gem::Dependency.new('', '>= 3.5.0').match?('', status['version'])
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Foreman Proxy authentication broken"
else
"Foreman status doesn't have a remote_ip because Foreman is too old"
end
logger.info(message)
return false
end

status['remote_ip'] == MOCK_IP
rescue StandardError => e
logger.exception('Failed to verify Foreman reverse proxy setup', e)
false
end
end
end
end
7 changes: 7 additions & 0 deletions modules/root/root_api.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'proxy/verify_runtime'

class Proxy::RootApi < Sinatra::Base
helpers ::Proxy::Helpers

Expand All @@ -18,4 +20,9 @@ class Proxy::RootApi < Sinatra::Base
rescue => e
log_halt 400, e
end

get "/verify" do
content_type :json
Proxy::VerifyRuntime.verify.to_json
end
end