-
Notifications
You must be signed in to change notification settings - Fork 21
feat: add Keycloak JWT support via metadata provider and injection in… #377
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
Open
0xNadeem
wants to merge
1
commit into
temporalio:main
Choose a base branch
from
0xNadeem:feat/keycloak-support
base: main
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.
Open
Changes from all commits
Commits
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
107 changes: 107 additions & 0 deletions
107
temporalio/lib/temporalio/client/metadata_injection_interceptor.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,107 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'temporalio/client/metadata_provider' | ||
|
|
||
| module Temporalio | ||
| class Client | ||
| # Interceptor that injects metadata from a provider into all requests | ||
| class MetadataInjectionInterceptor | ||
| include Interceptor | ||
|
|
||
| def initialize(metadata_provider) | ||
| @metadata_provider = metadata_provider | ||
| end | ||
|
|
||
| def intercept_client(next_interceptor) | ||
| MetadataInjectingOutbound.new(next_interceptor, @metadata_provider) | ||
| end | ||
|
|
||
| # Outbound that injects metadata | ||
| class MetadataInjectingOutbound | ||
| def initialize(next_outbound, metadata_provider) | ||
| @next_outbound = next_outbound | ||
| @metadata_provider = metadata_provider | ||
| end | ||
|
|
||
| def start_workflow(input) | ||
| inject_metadata(input) | ||
| @next_outbound.start_workflow(input) | ||
| end | ||
|
|
||
| def signal_with_start_workflow(input) | ||
| inject_metadata(input) | ||
| @next_outbound.signal_with_start_workflow(input) | ||
| end | ||
|
|
||
| def start_update_with_start_workflow(input) | ||
| inject_metadata(input) | ||
| @next_outbound.start_update_with_start_workflow(input) | ||
| end | ||
|
|
||
| def list_workflow_page(input) | ||
| inject_metadata(input) | ||
| @next_outbound.list_workflow_page(input) | ||
| end | ||
|
|
||
| def count_workflows(input) | ||
| inject_metadata(input) | ||
| @next_outbound.count_workflows(input) | ||
| end | ||
|
|
||
| def describe_workflow(input) | ||
| inject_metadata(input) | ||
| @next_outbound.describe_workflow(input) | ||
| end | ||
|
|
||
| def fetch_workflow_history_events(input) | ||
| inject_metadata(input) | ||
| @next_outbound.fetch_workflow_history_events(input) | ||
| end | ||
|
|
||
| def query_workflow(input) | ||
| inject_metadata(input) | ||
| @next_outbound.query_workflow(input) | ||
| end | ||
|
|
||
| def signal_workflow(input) | ||
| inject_metadata(input) | ||
| @next_outbound.signal_workflow(input) | ||
| end | ||
|
|
||
| def signal_workflow_with_start(input) | ||
| inject_metadata(input) | ||
| @next_outbound.signal_workflow_with_start(input) | ||
| end | ||
|
|
||
| def request_cancel_workflow(input) | ||
| inject_metadata(input) | ||
| @next_outbound.request_cancel_workflow(input) | ||
| end | ||
|
|
||
| def terminate_workflow(input) | ||
| inject_metadata(input) | ||
| @next_outbound.terminate_workflow(input) | ||
| end | ||
|
|
||
| def update_workflow(input) | ||
| inject_metadata(input) | ||
| @next_outbound.update_workflow(input) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def inject_metadata(input) | ||
| provider_metadata = @metadata_provider.metadata | ||
| return if provider_metadata.empty? | ||
|
|
||
| if input.respond_to?(:rpc_options=) | ||
| current_rpc_options = input.rpc_options || {} | ||
| current_metadata = current_rpc_options.is_a?(Hash) ? current_rpc_options : {} | ||
| merged_metadata = current_metadata.merge(provider_metadata) | ||
| input.rpc_options = merged_metadata | ||
| 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,95 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Temporalio | ||
| class Client | ||
| # Base class for providing metadata (headers) to gRPC requests | ||
| class MetadataProvider | ||
| def metadata | ||
| raise NotImplementedError, 'Subclasses must implement metadata method' | ||
| end | ||
| end | ||
|
|
||
| # Simple JWT token provider | ||
| class JwtTokenProvider < MetadataProvider | ||
| def initialize(token) | ||
| @token = token | ||
| end | ||
|
|
||
| def metadata | ||
| { 'authorization' => "Bearer #{@token}" } | ||
| end | ||
| end | ||
|
|
||
| # JWT supplier provider (calls lambda for token) | ||
| class JwtTokenSupplierProvider < MetadataProvider | ||
| def initialize(supplier) | ||
| @supplier = supplier | ||
| end | ||
|
|
||
| def metadata | ||
| token = @supplier.call | ||
| token ? { 'authorization' => "Bearer #{token}" } : {} | ||
| end | ||
| end | ||
|
|
||
| # Keycloak integration | ||
| class KeycloakJwtProvider < MetadataProvider | ||
| require 'net/http' | ||
| require 'json' | ||
|
|
||
| def initialize(keycloak_url, realm, client_id, client_secret, cache_duration: 3600) | ||
| @keycloak_url = keycloak_url | ||
| @realm = realm | ||
| @client_id = client_id | ||
| @client_secret = client_secret | ||
| @cache_duration = cache_duration | ||
| @cached_token = nil | ||
| @token_expiry = nil | ||
| @mutex = Mutex.new | ||
| end | ||
|
|
||
| def metadata | ||
| token = ensure_token | ||
| token ? { 'authorization' => "Bearer #{token}" } : {} | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def ensure_token | ||
| @mutex.synchronize do | ||
| fetch_new_token if @cached_token.nil? || token_expired? | ||
| @cached_token | ||
| end | ||
| end | ||
|
|
||
| def token_expired? | ||
| @token_expiry.nil? || Time.now > @token_expiry | ||
| end | ||
|
|
||
| def fetch_new_token | ||
| token_url = "#{@keycloak_url}/realms/#{@realm}/protocol/openid-connect/token" | ||
| uri = URI(token_url) | ||
|
|
||
| request = Net::HTTP::Post.new(uri.path) | ||
| request['Content-Type'] = 'application/x-www-form-urlencoded' | ||
| request.set_form_data( | ||
| grant_type: 'client_credentials', | ||
| client_id: @client_id, | ||
| client_secret: @client_secret | ||
| ) | ||
|
|
||
| http = Net::HTTP.new(uri.host, uri.port) | ||
| http.use_ssl = uri.scheme == 'https' | ||
| response = http.request(request) | ||
|
|
||
| if response.is_a?(Net::HTTPSuccess) | ||
| body = JSON.parse(response.body) | ||
| @cached_token = body['access_token'] | ||
| @token_expiry = Time.now + body['expires_in'] - 60 | ||
| else | ||
| raise "Keycloak token fetch failed: #{response.code}" | ||
| end | ||
| end | ||
| end | ||
| 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.
Uh oh!
There was an error while loading. Please reload this page.
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.
While this is kind of a good example of why we have interceptors (so anyone can build these), not sure we want this one built in to the SDK.
There are a couple of problems with this approach. You can't really use client interceptors to inject RPC options. Not every client call goes through interceptors. This is especially true for internal client calls that the workers use.
The way that auth bearer tokes should be set is by using the
api_keysetter method on the client'sconnectionthat we very intentionally expose for this exact reason (orrpc_metadataif it wasn't an auth bearer token). So you have to push auth updates, it cannot be a lazy pull model. This is because the client is actually in Rust and does not call back in to Ruby for every call.