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

feat(caller): add project and environment #81

Merged
merged 5 commits into from
Jan 8, 2025
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
require 'jwt'
require 'active_support'
require 'active_support/time'

module ForestAdminAgent
module Utils
class CallerParser
include ForestAdminDatasourceToolkit::Exceptions

def initialize(args)
@args = args
@token_data = {}
end

def parse
validate_headers
@token_data = decode_token
@token_data[:timezone] = extract_timezone
@token_data[:request] = { ip: @args[:headers]['action_dispatch.remote_ip'].to_s }
project, environment = extract_forest_context
@token_data[:project] = project
@token_data[:environment] = environment

ForestAdminDatasourceToolkit::Components::Caller.new(**@token_data.transform_keys(&:to_sym))
end

private

def validate_headers
return if @args.dig(:headers, 'HTTP_AUTHORIZATION')

raise Http::Exceptions::HttpException.new(
401,
'You must be logged in to access at this resource.'
)
end

def extract_timezone
timezone = @args[:params]['timezone']
raise ForestException, 'Missing timezone' unless timezone
raise ForestException, "Invalid timezone: #{timezone}" unless Time.find_zone(timezone)

timezone
end

def decode_token
token = @args[:headers]['HTTP_AUTHORIZATION'].split[1]
JWT.decode(
token,
Facades::Container.cache(:auth_secret),
true,
{ algorithm: 'HS256' }
)[0].tap { |data| data.delete('exp') }
end

def extract_forest_context
match_data = %r{https://[^/]*/([^/]*)/([^/]*)/([^/]*)}.match(@args[:headers]['HTTP_FOREST_CONTEXT_URL'])
return [nil, nil] unless match_data

project = match_data[1]
environment = match_data[2]
[project, environment]
rescue StandardError
[nil, nil]
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
require 'jwt'
require 'active_support'
require 'active_support/time'

module ForestAdminAgent
module Utils
class QueryStringParser
Expand Down Expand Up @@ -31,30 +27,7 @@ def self.parse_condition_tree(collection, args)
end

def self.parse_caller(args)
unless args.dig(:headers, 'HTTP_AUTHORIZATION')
raise Http::Exceptions::HttpException.new(
401,
'You must be logged in to access at this resource.'
)
end

timezone = args[:params]['timezone']
raise ForestException, 'Missing timezone' unless timezone

raise ForestException, "Invalid timezone: #{timezone}" unless Time.find_zone(timezone)

token = args[:headers]['HTTP_AUTHORIZATION'].split[1]
token_data = JWT.decode(
token,
Facades::Container.cache(:auth_secret),
true,
{ algorithm: 'HS256' }
)[0]
token_data.delete('exp')
token_data[:timezone] = timezone
token_data[:request] = { ip: args[:headers]['action_dispatch.remote_ip'].to_s }

Caller.new(**token_data.transform_keys(&:to_sym))
CallerParser.new(args).parse
end

def self.parse_projection(collection, args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
timezone: 'Europe/Paris',
permission_level: 'admin',
role: 'dev',
request: { ip: '127.0.0.1' }
request: { ip: '127.0.0.1' },
project: 'terminator',
environment: 'Development'
)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ def initialize(
timezone:,
permission_level:,
role: nil,
request: {}
request: {},
project: nil,
environment: nil
)
@id = id
@email = email
Expand All @@ -27,6 +29,8 @@ def initialize(
@permission_level = permission_level
@role = role
@request = request
@project = project
@environment = environment
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ def replace_fields
end

def override(**args)
Aggregation.new(**to_h, **args)
params = to_h.merge(args)
Aggregation.new(
operation: params[:operation],
field: params[:field],
groups: params[:groups]
)
end

def apply(records, timezone, limit = nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ module Contracts
timezone: 'Europe/Paris',
role: 1,
permission_level: 'admin',
request: { ip: '127.0.0.1' }
request: { ip: '127.0.0.1' },
project: 'foo',
environment: 'Development'
)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
timezone: 'Europe/Paris',
permission_level: 'admin',
role: 'dev',
request: { ip: '127.0.0.1' }
request: { ip: '127.0.0.1' },
project: 'terminator',
environment: 'Development'
)
end
end
Loading