Skip to content

Commit 4a6287f

Browse files
authored
Update ruby application security add user info documentation (#30795)
* Update examples for SDK v2 * Add examples of migration to the new SDK * Apply review suggestions
1 parent 495699e commit 4a6287f

File tree

1 file changed

+72
-18
lines changed

1 file changed

+72
-18
lines changed

content/en/security/application_security/how-it-works/add-user-info.md

Lines changed: 72 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -543,46 +543,100 @@ func handler(w http.ResponseWriter, r *http.Request) {
543543
{{< /programming-lang >}}
544544
{{< programming-lang lang="ruby" >}}
545545

546-
Starting in dd-trace-rb v1.9.0, you can use the Ruby tracer's API to track user events.
546+
Starting in dd-trace-rb v1.9.0, you can use the Ruby tracer's API to track user events. Version v2.19.0 of dd-trace-rb introduces new methods under the `Datadog::Kit::AppSec::Events::V2` namespace. Existing event tracking methods are retained for compatibility.
547547

548548
The following examples show how to track login events or custom events (using signup as an example).
549549

550-
Traces containing login success/failure events can be queried using the following query `@appsec.security_activity:business_logic.users.login.success` or `@appsec.security_activity:business_logic.users.login.failure`.
551-
552550
{{% collapse-content title="Login success" level="h4" expanded="true" %}}
553551
```ruby
554-
require 'datadog/kit/appsec/events'
552+
require 'datadog/kit/appsec/events/v2'
555553

556-
trace = Datadog::Tracing.active_trace
557-
# Replace `my_user_id` by a unique identifier of the user (numeric, username, email...)
558-
Datadog::Kit::AppSec::Events.track_login_success(trace, user: { id: 'my_user_id' }, { 'usr.login': 'my_user_email' })
554+
555+
user = 'some-user-id' # any unique string identifier (i.e. id, username or email)
556+
user = { # or user could be a Hash with an id and other fields
557+
id: 'some-user-id', # id is mandatory
558+
email: '[email protected]' # other fields are optional
559+
}
560+
metadata = { 'some.key': 'value' } # any arbitrary key-value pairs
561+
562+
Datadog::Kit::AppSec::Events::V2.track_user_login_success(login, user, metadata)
559563
```
560564
{{% /collapse-content %}}
561565

562566
{{% collapse-content title="Login failure" level="h4" expanded="false" id="ruby-login-failure" %}}
563567
```ruby
564-
require 'datadog/kit/appsec/events'
565-
trace = Datadog::Tracing.active_trace
566-
567-
# Replace `my_user_id` by a unique identifier of the user (numeric, username, email...)
568+
require 'datadog/kit/appsec/events/v2'
568569

569-
# if the user exists
570-
Datadog::Kit::AppSec::Events.track_login_failure(trace, user_id: 'my_user_id', user_exists: true, { 'usr.login': 'my_user_email' })
570+
login = '[email protected]' # the string used by the user to log in
571+
user_exists = true # if the user login exists in database for example
572+
metadata = { 'some.key': 'value' } # any arbitrary key-value pairs
571573

572-
# if the user doesn't exist
573-
Datadog::Kit::AppSec::Events.track_login_failure(trace, user_id: 'my_user_id', user_exists: false, { 'usr.login': 'my_user_email' })
574+
Datadog::Kit::AppSec::Events::V2.track_user_login_failure(login, user_exists, metadata)
574575
```
575576
{{% /collapse-content %}}
576577

577578
{{% collapse-content title="Custom business logic" level="h4" expanded="false" id="ruby-custom-business" %}}
578579
```ruby
579580
require 'datadog/kit/appsec/events'
581+
582+
span = nil
580583
trace = Datadog::Tracing.active_trace
584+
metadata = { 'usr.id': 'some-user-id' }
585+
event_name = 'users.signup'
586+
587+
Datadog::Kit::AppSec::Events.track(event_name, trace, span, metadata)
588+
```
589+
{{% /collapse-content %}}
590+
591+
#### Migrating to the new login success and failure methods
592+
593+
The new methods in `Datadog::Kit::AppSec::Events::V2` introduce a more intuitive parameter order and clearer separation of concerns. Here are the key changes:
594+
595+
1. The login identifier (email, username) is the first parameter and is mandatory.
596+
2. The user object/ID is optional in success events and has been removed from failure events.
597+
3. Metadata has been simplified and no longer requires the `usr.login` field.
598+
4. The trace and span parameters are no longer required and are automatically inferred.
599+
600+
**Note**: the legacy methods `track_login_success` and `track_login_failure` are deprecated in favor of the new methods `track_user_login_success` and `track_user_login_failure`, respectively.
601+
602+
In the following example, the commented code is no longer necessary.
603+
604+
{{% collapse-content title="Login success" level="h4" expanded="true" id="ruby-v2-migration-login-success" %}}
605+
```ruby
606+
require 'datadog/kit/appsec/events/v2'
607+
608+
login = '[email protected]' # new mandatory argument
609+
user = { # same as before, but now the Hash is optional
610+
id: 'some-user-id', # providing a user ID will nonetheless help with post-compromised activity correlation
611+
612+
}
613+
metadata = {
614+
# 'usr.login': '[email protected]', this is no longer necessary in metadata, but became the required first parameter
615+
'some.key': 'value'
616+
}
617+
618+
# deprecated
619+
# Datadog::Kit::AppSec::Events.track_login_success(trace, span, user: user, **metadata)
620+
621+
Datadog::Kit::AppSec::Events::V2.track_user_login_success(login, user, metadata)
622+
```
623+
{{% /collapse-content %}}
624+
625+
{{% collapse-content title="Login failure" level="h4" expanded="false" id="ruby-v2-migration-login-failure" %}}
626+
```ruby
627+
require 'datadog/kit/appsec/events/v2'
628+
629+
login = '[email protected]' # new mandatory argument
630+
user_exists = true # if the user login exists in database for example
631+
metadata = {
632+
# 'usr.login': '[email protected]', this is no longer necessary in metadata, but became the required first parameter
633+
'some.key': 'value'
634+
}
581635

582-
# Replace `my_user_id` by a unique identifier of the user (numeric, username, email...)
636+
# deprecated
637+
# Datadog::Kit::AppSec::Events.track_login_failure(trace, span, user_exists: user_exists, user_id: login, **metadata)
583638

584-
# Leveraging custom business logic tracking to track user signups
585-
Datadog::Kit::AppSec::Events.track('users.signup', trace, nil, { 'usr.id': 'my_user_id'})
639+
Datadog::Kit::AppSec::Events::V2.track_user_login_failure(login, user_exists, metadata)
586640
```
587641
{{% /collapse-content %}}
588642

0 commit comments

Comments
 (0)