Skip to content

Commit 28cd675

Browse files
chaselancasterChase LancasterChase Lancaster
authored
Add audit logs app (#46)
* add SSO skeleton to strip down later * add proper images to the images folder * change css in public folder to login.css with relevant css * rename folders * change folder names back to original names & adjust stylesheet and picture paths * clean up '/' route in app.rb * configure basic structure for /set_org before redirect * add redirecft to /set_org * remove render and set erb variables to display in send_events.erb * add initial structure for /send_event route * Add global variables from audit_log_events to app.rb * remove auth route * add exports_events page with initial structure * add export_events route * change pathing and variables in export_events.erb * add today and last_month time to /get_events method * Add if statement logic to /get_events * remove redirect uri reference * add snackbar to css and adjust variable names for send_events method * commit to share * add snackbar onClick to each send event button * remove requires that aren't needed anymore * add up to date css * remove json require * remove comments * fix login.erb flex_column class name * remove connection_id reference and adjust version number * add readme * adjust .env.example Co-authored-by: Chase Lancaster <chaselancaster@Chases-MacBook-Pro.local> Co-authored-by: Chase Lancaster <chaselancaster@Chases-MacBook-Pro-2.local>
1 parent ec5fce4 commit 28cd675

17 files changed

+930
-0
lines changed

ruby-audit-logs-example/.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
WORKOS_API_KEY=''
2+
WORKOS_CLIENT_ID=''

ruby-audit-logs-example/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.env

ruby-audit-logs-example/.ruby-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2.9.0

ruby-audit-logs-example/Gemfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# frozen_string_literal: true
2+
3+
source 'https://rubygems.org'
4+
5+
gem 'dotenv', '2.7.6'
6+
gem 'sinatra', '2.2.0'
7+
gem 'workos', '2.9.0'
8+

ruby-audit-logs-example/Gemfile.lock

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
dotenv (2.7.6)
5+
mustermann (1.1.2)
6+
ruby2_keywords (~> 0.0.1)
7+
rack (2.2.4)
8+
rack-protection (2.2.0)
9+
rack
10+
ruby2_keywords (0.0.5)
11+
sinatra (2.2.0)
12+
mustermann (~> 1.0)
13+
rack (~> 2.2)
14+
rack-protection (= 2.2.0)
15+
tilt (~> 2.0)
16+
sinatra-flash (0.3.0)
17+
sinatra (>= 1.0.0)
18+
sorbet-runtime (0.5.10461)
19+
tilt (2.0.11)
20+
workos (2.5.0)
21+
sorbet-runtime (~> 0.5)
22+
23+
PLATFORMS
24+
ruby
25+
26+
DEPENDENCIES
27+
dotenv (= 2.7.6)
28+
sinatra (= 2.2.0)
29+
sinatra-flash
30+
workos (= 2.5.0)
31+
32+
BUNDLED WITH
33+
2.3.23

ruby-audit-logs-example/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 WorkOS
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

ruby-audit-logs-example/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# ruby-audit-logs-example
2+
3+
An example Ruby application demonstrating how to use the [WorkOS Ruby SDK](https://github.com/workos/workos-ruby) to send and retrieve Audit Log events. This example is not meant to show a real-world example of an Audit Logs implementation, but rather to show concrete examples of how events can be sent using the Python SDK.
4+
5+
## Clone and Install
6+
7+
1. Clone the main repo:
8+
9+
```sh
10+
# HTTPS
11+
$ git clone https://github.com/workos/ruby-example-applications.git
12+
```
13+
14+
15+
2. Navigate to the Audit Logs example app within the cloned repo and install dependencies:
16+
17+
```sh
18+
$ cd ruby-example-applications/ruby-audit-logs-example && bundle install
19+
```
20+
21+
## Configure your environment
22+
23+
1. Grab your [API Key](https://dashboard.workos.com/api-keys) and your [Client ID](https://dashboard.workos.com/configuration).
24+
2. Run `cp .env.example .env` and add your API key and Client ID. The `workos` gem will read your API key from the ENV variable `WORKOS_API_KEY` and your Client ID from the ENV variable `WORKOS_CLIENT_ID`. You may also set the API key and Client ID yourself by adding `WorkOS.key = $YOUR_API_KEY` and `CLIENT_ID = $YOUR_CLIENT_ID` to `app.rb`.
25+
26+
### Audit Logs Setup with WorkOS
27+
28+
1. Follow the [Audit Logs configuration steps](https://workos.com/docs/audit-logs/emit-an-audit-log-event/sign-in-to-your-workos-dashboard-account-and-configure-audit-log-event-schemas) to set up the following 5 events that are sent with this example:
29+
30+
Action title: "user.signed_in" | Target type: "team"
31+
Action title: "user.logged_out" | Target type: "team"
32+
Action title: "user.organization_set" | Target type: "team"
33+
Action title: "user.organization_deleted" | Target type: "team"
34+
Action title: "user.connection_deleted" | Target type: "team"
35+
36+
2. Next, take note of the Organization ID for the Org which you will be sending the Audit Log events for. This ID gets entered into the splash page of the example application.
37+
38+
3. Once you enter the Organization ID and submit it, you will be brought to the page where you'll be able to send the audit log events that were just configured. You'll also notice that the action of setting the Organization triggered an Audit Log already. Click the buttons to send the respective events.
39+
40+
4. To obtain a CSV of the Audit Log events that were sent for the last 30 days, click the "Export Events" button. This will bring you to a new page where you can download the events. Downloading the events is a 2 step process. First you need to create the report by clicking the "Generate CSV" button. Then click the "Access CSV" button to download a CSV of the Audit Log events for the selected Organization for the past 30 days.
41+
42+
## Run the app
43+
```sh
44+
ruby app.rb
45+
```
46+
47+
## Need help?
48+
49+
If you get stuck and aren't able to resolve the issue by reading our API reference or tutorials, you can reach out to us at support@workos.com and we'll lend a hand.

ruby-audit-logs-example/app.rb

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# frozen_string_literal: true
2+
3+
require 'dotenv/load'
4+
require 'sinatra'
5+
require 'workos'
6+
require 'date'
7+
require_relative 'audit_log_events.rb'
8+
9+
# Pull API key and Client ID from ENV variable
10+
WorkOS.key = ENV['WORKOS_API_KEY']
11+
CLIENT_ID = ENV['WORKOS_CLIENT_ID']
12+
13+
enable :sessions
14+
15+
use(
16+
Rack::Session::Cookie,
17+
key: 'rack.session',
18+
domain: 'localhost',
19+
path: '/',
20+
expire_after: 2_592_000,
21+
secret: SecureRandom.hex(16)
22+
)
23+
24+
get '/' do
25+
erb :login, :layout => :layout
26+
end
27+
28+
post '/set_org' do
29+
@organization_id = params[:org]
30+
31+
session[:organization_id] = @organization_id
32+
33+
organization = WorkOS::Organizations.get_organization(
34+
id: @organization_id
35+
)
36+
37+
@org_name = organization.name
38+
session[:organization_name] = @org_name
39+
erb :send_events, :layout => :layout
40+
end
41+
42+
get '/set_org' do
43+
@organization_id = session[:organization_id]
44+
@org_name = session[:organization_name]
45+
erb :send_events, :layout => :layout
46+
end
47+
48+
post '/send_event' do
49+
event_type = params[:event]
50+
@organization_id = session[:organization_id]
51+
@org_name = session[:organization_name]
52+
53+
events = [
54+
$user_signed_in,
55+
$user_logged_out,
56+
$user_organization_deleted,
57+
$user_connection_deleted,
58+
]
59+
60+
event = events[event_type.to_i]
61+
62+
WorkOS::AuditLogs.create_event(
63+
organization: @organization_id,
64+
event: event
65+
)
66+
67+
erb :send_events, :layout => :layout
68+
end
69+
70+
get '/export_events' do
71+
@organization_id = session[:organization_id]
72+
@org_name = session[:organization_name]
73+
erb :export_events, :layout => :layout
74+
end
75+
76+
post '/get_events' do
77+
organization_id = session[:organization_id]
78+
event_type = params[:event]
79+
today = DateTime.now.to_s
80+
last_month = DateTime.now.prev_month.to_s
81+
82+
if event_type == 'generate_csv'
83+
audit_log_export = WorkOS::AuditLogs.create_export(
84+
organization: organization_id,
85+
range_start: last_month,
86+
range_end: today
87+
)
88+
session[:export_id] = audit_log_export.id
89+
puts audit_log_export.id
90+
redirect '/export_events'
91+
end
92+
93+
if event_type == 'access_csv'
94+
export_id = session[:export_id].to_s
95+
puts export_id
96+
audit_log_export = WorkOS::AuditLogs.get_export(
97+
id: export_id
98+
)
99+
url = audit_log_export.url
100+
101+
redirect url
102+
end
103+
104+
end
105+
106+
107+
# Logout a user
108+
get '/logout' do
109+
session[:organization_id] = nil
110+
session[:organization_name] = nil
111+
session[:export_id] = nil
112+
redirect '/'
113+
end
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
require 'date'
2+
3+
$user_signed_in = {
4+
"action": "user.signed_in",
5+
"occurred_at": DateTime.now,
6+
"actor": {
7+
"type": "user",
8+
"id": "user_01GBNJC3MX9ZZJW1FSTF4C5938",
9+
},
10+
"targets": [
11+
{
12+
"type": "team",
13+
"id": "team_01GBNJD4MKHVKJGEWK42JNMBGS",
14+
},
15+
],
16+
"context": {
17+
"location": "123.123.123.123",
18+
"user_agent": "Chrome/104.0.0.0",
19+
},
20+
}
21+
22+
$user_logged_out = {
23+
"action": "user.logged_out",
24+
"occurred_at": DateTime.now,
25+
"actor": {
26+
"type": "user",
27+
"id": "user_01GBNJC3MX9ZZJW1FSTF4C5938",
28+
},
29+
"targets": [
30+
{
31+
"type": "team",
32+
"id": "team_01GBNJD4MKHVKJGEWK42JNMBGS",
33+
},
34+
],
35+
"context": {
36+
"location": "123.123.123.123",
37+
"user_agent": "Chrome/104.0.0.0",
38+
},
39+
}
40+
41+
$user_organization_set = {
42+
"action": "user.organization_set",
43+
"occurred_at": DateTime.now,
44+
"actor": {
45+
"type": "user",
46+
"id": "user_01GBNJC3MX9ZZJW1FSTF4C5938",
47+
},
48+
"targets": [
49+
{
50+
"type": "team",
51+
"id": "team_01GBNJD4MKHVKJGEWK42JNMBGS",
52+
},
53+
],
54+
"context": {
55+
"location": "123.123.123.123",
56+
"user_agent": "Chrome/104.0.0.0",
57+
},
58+
}
59+
60+
$user_organization_deleted = {
61+
"action": "user.organization_deleted",
62+
"occurred_at": DateTime.now,
63+
"actor": {
64+
"type": "user",
65+
"id": "user_01GBNJC3MX9ZZJW1FSTF4C5938",
66+
},
67+
"targets": [
68+
{
69+
"type": "team",
70+
"id": "team_01GBNJD4MKHVKJGEWK42JNMBGS",
71+
},
72+
],
73+
"context": {
74+
"location": "123.123.123.123",
75+
"user_agent": "Chrome/104.0.0.0",
76+
},
77+
}
78+
79+
$user_connection_deleted = {
80+
"action": "user.connection_deleted",
81+
"occurred_at": DateTime.now,
82+
"actor": {
83+
"type": "user",
84+
"id": "user_01GBNJC3MX9ZZJW1FSTF4C5938",
85+
},
86+
"targets": [
87+
{
88+
"type": "team",
89+
"id": "team_01GBNJD4MKHVKJGEWK42JNMBGS",
90+
},
91+
],
92+
"context": {
93+
"location": "123.123.123.123",
94+
"user_agent": "Chrome/104.0.0.0",
95+
},
96+
}
Loading

0 commit comments

Comments
 (0)