-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathscrubber.rb
More file actions
168 lines (139 loc) · 3.88 KB
/
Copy pathscrubber.rb
File metadata and controls
168 lines (139 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# Getting started with Equipe Extensions
#
# install required gems
# gem install sinatra jwt faraday faker rackup rerun
#
# The extension needs to be configured with this actions in the UI of Equipe
#
# | id | name | path | role | title | target |
# |----|----------|------|--------|----------|--------|
# | 1 | scrubber | | member | Scrubber | modal |
#
# rerun "EXTENSION_SECRET=<SECRET> ruby scrubber.rb"
require 'sinatra'
require 'json'
require 'jwt'
require 'faraday'
require 'faker'
require 'securerandom'
class Equipe
attr_reader :token
def initialize(token)
@token = token
@data, _ = JWT.decode token, ENV['EXTENSION_SECRET']
end
def style_url
data.dig('payload', 'style_url')
end
def view_name
data.dig('payload', 'name').to_sym
end
def people
JSON.parse(Faraday.get(people_url, {}, headers).body)
end
def horses
JSON.parse(Faraday.get(horses_url, {}, headers).body)
end
def post(batch)
Faraday.post(batch_url, JSON.pretty_generate(batch), headers)
end
private
attr_reader :data
def batch_url
"#{data.dig('payload', 'meeting_url')}/batch"
end
def people_url
"#{data.dig('payload', 'meeting_url')}/people"
end
def horses_url
"#{data.dig('payload', 'meeting_url')}/horses"
end
def headers
{
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'X-TRANSACTION-UUID' => SecureRandom.uuid, # This will group the updates into a single update in Equipe it will be possible to undo all at once.
'X-API-KEY' => data['api_key']
}
end
end
# important removes X-Frame-Options or sets it to X-Frame-Options: ALLOWALL
set :protection, except: :frame_options
# Receive a POST request for webhooks from Equipe and print the payload
post '/' do
puts JSON.parse(request.body.read)
status 200
end
get '/' do
@equipe = Equipe.new(params[:token])
erb @equipe.view_name
end
post '/scrubber' do
@equipe = Equipe.new(params[:token])
@batch = {
people: {
unique_by: :rnr,
records: []
},
horses: {
unique_by: :rnr,
records: []
}
}
if params[:people]
@equipe.people.each do |person|
@batch[:people][:records].push(
rnr: person['rnr'],
first_name: Faker::Name.unique.first_name,
last_name: Faker::Name.unique.last_name,
address: Faker::Address.unique.street_name,
zipcode: Faker::Address.unique.zip_code,
city: Faker::Address.unique.city,
cell_phone: Faker::PhoneNumber.unique.cell_phone,
work_phone: Faker::PhoneNumber.unique.phone_number,
home_phone: Faker::PhoneNumber.unique.phone_number,
email: Faker::Internet.unique.email
)
end
end
if params[:horses]
@equipe.horses.each do |horse|
@batch[:horses][:records].push(
hnr: horse['hnr'],
name: Faker::Creature::Horse.name
)
end
end
# Send the batch to Equipe
@equipe.post @batch
erb :scrubber_result
end
__END__
@@ layout
<!DOCTYPE html>
<html>
<head>
<title>Equipe Extension</title>
<link rel="stylesheet" href="<%= @equipe.style_url %>" media="all">
</head>
<body class="extension">
<%= yield %>
</body>
</html>
@@ scrubber
<h2>Scrubb sensitive user data</h2>
<p>This will replace person and horse data with fake data</p>
<form action="/scrubber" method="post">
<input type="hidden" name="token" value="<%= @equipe.token %>">
<div class="checkbox">
<label><input type="checkbox" name="people"> People</label>
</div>
<div class="checkbox">
<label><input type="checkbox" name="horses"> Horses</label>
</div>
<button type="submit" class="btn btn-primary w-100">Perform</button>
</form>
@@ scrubber_result
<div class="alert alert-success">
<strong>Success!</strong> Scrubbed <%= @batch[:people][:records].size %> people and <%= @batch[:horses][:records].size %> horses.
</div>