Skip to content
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
19 changes: 14 additions & 5 deletions app/services/attempts_api/redis_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@

module AttemptsApi
class RedisClient
attr_reader :redis_pool
def initialize
@redis_pool = REDIS_ATTEMPTS_API_POOL
end

def write_event(event_key:, jwe:, timestamp:, issuer:)
key = key(timestamp, issuer)
REDIS_ATTEMPTS_API_POOL.with do |client|
@redis_pool.with do |client|
client.hset(key, event_key, jwe)
client.expire(key, IdentityConfig.store.attempts_api_event_ttl_seconds)
client.expire(key, event_ttl_seconds)
end
end

def read_events(issuer:, batch_size: 1000)
events = {}
hourly_keys(issuer).each do |hourly_key|
REDIS_ATTEMPTS_API_POOL.with do |client|
@redis_pool.with do |client|
client.hscan_each(hourly_key, count: batch_size) do |k, v|
break if events.keys.count == batch_size

Expand All @@ -27,7 +32,7 @@ def read_events(issuer:, batch_size: 1000)
def delete_events(issuer:, keys:)
total_deleted = 0
hourly_keys(issuer).each do |hourly_key|
REDIS_ATTEMPTS_API_POOL.with do |client|
@redis_pool.with do |client|
total_deleted += client.hdel(hourly_key, keys)
end
end
Expand All @@ -37,8 +42,12 @@ def delete_events(issuer:, keys:)

private

def event_ttl_seconds
IdentityConfig.store.attempts_api_event_ttl_seconds
end

def hourly_keys(issuer)
REDIS_ATTEMPTS_API_POOL.with do |client|
@redis_pool.with do |client|
client.keys("attempts-api-events:#{issuer}:*")
end.sort
end
Expand Down
71 changes: 41 additions & 30 deletions app/services/attempts_api/tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,44 +24,16 @@ def initialize(session_id:, request:, user:, sp:, cookie_device_uuid:,
def track_event(event_type, metadata = {})
return unless enabled?

extra_metadata =
if metadata.has_key?(:failure_reason) &&
(metadata[:failure_reason].blank? || metadata[:success].present?)
metadata.except(:failure_reason)
else
metadata
end

event_metadata = {
user_agent: request&.user_agent,
unique_session_id: hashed_session_id,
user_uuid: agency_uuid(event_type: event_type),
device_id: cookie_device_uuid,
user_ip_address: request&.remote_ip,
application_url: sp_redirect_uri,
language: user&.email_language || I18n.locale.to_s,
client_port: CloudFrontHeaderParser.new(request).client_port,
aws_region: IdentityConfig.store.aws_region,
google_analytics_cookies: google_analytics_cookies(request),
}

event_metadata.merge!(extra_metadata)

event = AttemptEvent.new(
event_type: event_type,
session_id: session_id,
occurred_at: Time.zone.now,
event_metadata: event_metadata,
)

jwe = event.to_jwe(
issuer: sp.issuer,
public_key: sp.attempts_public_key,
event_metadata: event_metadata(event_type:, metadata:),
)

redis_client.write_event(
event_key: event.jti,
jwe: jwe,
jwe: jwe(event),
timestamp: event.occurred_at,
issuer: sp.issuer,
)
Expand All @@ -83,6 +55,45 @@ def parse_failure_reason(result)

private

def jwe(event)
event.to_jwe(
issuer: sp.issuer,
public_key: sp.attempts_public_key,
)
end

def extra_attributes
{}
end

def extra_metadata(metadata:)
Comment thread
astrogeco marked this conversation as resolved.
failure_metadata(metadata:).merge(extra_attributes)
end

def failure_metadata(metadata:)
if metadata.has_key?(:failure_reason) &&
(metadata[:failure_reason].blank? || metadata[:success].present?)
metadata.except(:failure_reason)
else
metadata
end
end

def event_metadata(event_type:, metadata:)
{
user_agent: request&.user_agent,
unique_session_id: hashed_session_id,
user_uuid: agency_uuid(event_type: event_type),
device_id: cookie_device_uuid,
user_ip_address: request&.remote_ip,
application_url: sp_redirect_uri,
language: user&.email_language || I18n.locale.to_s,
client_port: CloudFrontHeaderParser.new(request).client_port,
aws_region: IdentityConfig.store.aws_region,
google_analytics_cookies: google_analytics_cookies(request),
}.merge!(extra_metadata(metadata:))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

i think the ! is technically unnecessary here but i don't think it's hurting anything (the difference between merge and merge! is whether it's replacing the object or returning a copy of it. since we turned event_metadata into a method, it doesn't need to replace the object. but, not a big deal.

end

def google_analytics_cookies(request)
return nil unless request&.cookies
request.cookies.filter do |key, value|
Expand Down