-
Notifications
You must be signed in to change notification settings - Fork 128
Allow OIDC Providers to be available via local socket #2616
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
Draft
jvanderhoof
wants to merge
4
commits into
master
Choose a base branch
from
provider-list-via-socket
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4255317
Adds an internal service to provider Provider list to the UI
jvanderhoof d883a58
Refactor Authn-Local to use the generic Socket Server
jvanderhoof d70da22
Update Changelog
jvanderhoof 6f1ab8a
Adds better logging for the UI providers socket endpoint
jvanderhoof File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
app/domain/authentication/authn_oidc/v2/commands/list_providers.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'command_class' | ||
require './app/db/repository/authenticator_repository' | ||
|
||
module Authentication | ||
module AuthnOidc | ||
module V2 | ||
module Commands | ||
ListProviders ||= CommandClass.new( | ||
dependencies: { | ||
json_lib: JSON, | ||
logger: Rails.logger | ||
}, | ||
inputs: %i[message] | ||
) do | ||
def call | ||
params = @json_lib.parse(@message) | ||
@logger.debug("#{self.class}##{__method__} - #{params}") | ||
(account = params.delete("account")) || raise("'account' is required") | ||
|
||
authenticators = DB::Repository::AuthenticatorRepository.new( | ||
data_object: Authentication::AuthnOidc::V2::DataObjects::Authenticator, | ||
logger: @logger | ||
).find_all( | ||
account: account, | ||
type: 'authn-oidc' | ||
) | ||
@logger.debug("#{self.class}##{__method__} - #{authenticators.inspect}") | ||
Authentication::AuthnOidc::V2::Views::ProviderContext.new( | ||
logger: @logger | ||
).call( | ||
authenticators: authenticators | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'command_class' | ||
|
||
module Commands | ||
module Authentication | ||
IssueToken ||= CommandClass.new( | ||
dependencies: { | ||
json_lib: JSON, | ||
slosilo_lib: Slosilo | ||
}, | ||
inputs: %i[message] | ||
) do | ||
def call | ||
claims = @json_lib.parse(@message) | ||
claims = claims.slice("account", "sub", "exp", "cidr") | ||
(account = claims.delete("account")) || raise("'account' is required") | ||
raise "'sub' is required" unless claims['sub'] | ||
|
||
key = @slosilo_lib["authn:#{account}"] | ||
if key | ||
key.issue_jwt(claims).to_json | ||
else | ||
raise "No signing key found for account #{account.inspect}" | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
require 'timeout' | ||
require 'fileutils' | ||
require 'socket' | ||
|
||
module Util | ||
class SocketService | ||
def initialize(socket:, queue_length: 5, timeout: 1, logger: Logger.new($stderr, level: Logger::INFO)) | ||
@socket = socket | ||
@queue_length = queue_length | ||
@timeout = timeout | ||
@logger = logger | ||
|
||
validate_filesystem_assumptions(socket_file: socket) | ||
end | ||
|
||
def validate_filesystem_assumptions(socket_file:) | ||
if File.exist?(socket_file) | ||
raise "Socket: #{socket_file} already exists" | ||
end | ||
|
||
socket_dir = File.dirname(socket_file) | ||
|
||
return if File.directory?(socket_dir) | ||
|
||
raise("Socket Service requires directory #{socket_dir.inspect} to exist and be a directory") | ||
end | ||
|
||
def cleanup | ||
# remove the socket on exit | ||
# !! Note !!: A logger isn't allowed in a `trap` block, so we need | ||
# to provide an IO.pipe to write the exit message to. | ||
$stderr.puts("Removing socket #{@socket}") | ||
File.unlink(@socket) | ||
end | ||
|
||
# Accepts a block for the desired response behavior. | ||
# The message passed to the socket is available as a string | ||
# inside the block as a block attribute. Ex: | ||
# | ||
# Util::SocketService.new(socket: '/socket/path').run do |passed_arguments| | ||
# Authentication::AuthnOidc::V2::Commands::ListProviders.new.call( | ||
# message: passed_arguments | ||
# ) | ||
# end | ||
# | ||
# !! Note: the response is transformed into JSON to be sent through the socket. | ||
def run(&block) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Method There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Complex method Util::SocketService#run (25.8) |
||
server = UNIXServer.new(@socket) | ||
trap(0) { cleanup } | ||
|
||
server.listen(@queue_length) | ||
@logger.info("service is listening at #{@socket}") | ||
|
||
loop do | ||
connection = server.accept | ||
begin | ||
Timeout.timeout(@timeout) do | ||
arguments = connection.gets.strip | ||
begin | ||
@logger.debug("arguments: #{arguments.inspect}") | ||
response = connection.puts(block.call(arguments).to_json) | ||
@logger.debug("response: #{response.inspect}") | ||
response | ||
rescue | ||
@logger.error("Error in service '#{@socket}': #{$ERROR_INFO}") | ||
connection.puts | ||
ensure | ||
connection.close | ||
end | ||
end | ||
rescue Timeout::Error | ||
@logger.error("Timeout::Error in service '#{@socket}'") | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
require './app/domain/util/socket_server' | ||
require './app/domain/authentication/authn_oidc/v2/commands/list_providers' | ||
|
||
namespace :ui do | ||
task run: :environment do | ||
logger = Logger.new($stderr, level: Logger::DEBUG) | ||
Util::SocketService.new( | ||
socket: '/run/ui/.socket', | ||
logger: logger | ||
).run do |passed_arguments| | ||
Authentication::AuthnOidc::V2::Commands::ListProviders.new( | ||
logger: logger | ||
).call( | ||
message: passed_arguments | ||
) | ||
end | ||
end | ||
|
||
task retrieve_providers: :environment do | ||
require 'json' | ||
require 'socket' | ||
|
||
response = UNIXSocket.open('/run/ui/.socket') do |socket| | ||
socket.puts({ account: 'cucumber' }.to_json) | ||
JSON.parse(socket.gets) | ||
end | ||
puts("received: #{response}") | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Authentication::AuthnOidc::V2::Commands has no descriptive comment