Skip to content

Commit 2a4e45d

Browse files
authored
fix: Loosen firebasedatabase domain restriction in legacy event conversion (#109)
1 parent 95703e7 commit 2a4e45d

File tree

2 files changed

+50
-13
lines changed

2 files changed

+50
-13
lines changed

lib/functions_framework/legacy_event_converter.rb

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,22 @@ def convert_source service, resource, domain
135135

136136
match = CE_SERVICE_TO_RESOURCE_RE[service].match resource
137137
return [nil, nil] unless match
138+
resource_fragment = match[1]
139+
subject = match[2]
138140

139141
if service == "firebasedatabase.googleapis.com"
140-
return [nil, nil] if domain.nil?
141-
location = "us-central1"
142-
if domain != "firebaseio.com"
143-
location_match = domain.match(/^([\w-]+)\.firebasedatabase\.app$/)
144-
return [nil, nil] unless location_match
145-
location = location_match[1]
146-
end
147-
["//#{service}/projects/_/locations/#{location}/#{match[1]}", match[2]]
142+
location =
143+
case domain
144+
when "firebaseio.com"
145+
"us-central1"
146+
when /^([\w-]+)\./
147+
Regexp.last_match[1]
148+
else
149+
return [nil, nil]
150+
end
151+
["//#{service}/projects/_/locations/#{location}/#{resource_fragment}", subject]
148152
else
149-
["//#{service}/#{match[1]}", match[2]]
153+
["//#{service}/#{resource_fragment}", subject]
150154
end
151155
end
152156

test/test_legacy_event_converter.rb

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,27 @@
1313
# limitations under the License.
1414

1515
require "helper"
16+
require "json"
17+
require "stringio"
1618

1719
describe FunctionsFramework::LegacyEventConverter do
1820
let(:data_dir) { File.join __dir__, "legacy_events_data" }
1921

20-
def load_legacy_event filename, url_path: nil, encoding: "utf-8"
21-
path = File.join data_dir, filename
22-
File.open path, encoding: encoding do |io|
22+
def load_legacy_event filename_or_json, url_path: nil, encoding: "utf-8"
23+
converter = FunctionsFramework::LegacyEventConverter.new
24+
case filename_or_json
25+
when String
26+
path = File.join data_dir, filename_or_json
27+
File.open path, encoding: encoding do |io|
28+
env = { "rack.input" => io, "CONTENT_TYPE" => "application/json", "PATH_INFO" => url_path }
29+
converter.decode_rack_env env
30+
end
31+
when Hash
32+
io = StringIO.new JSON.dump filename_or_json
2333
env = { "rack.input" => io, "CONTENT_TYPE" => "application/json", "PATH_INFO" => url_path }
24-
converter = FunctionsFramework::LegacyEventConverter.new
2534
converter.decode_rack_env env
35+
else
36+
raise ArgumentError, filename_or_json.class.name
2637
end
2738
end
2839

@@ -218,4 +229,26 @@ def load_legacy_event filename, url_path: nil, encoding: "utf-8"
218229
assert_equal "refs/gcf-test/abc", event.subject
219230
assert_equal "2020-05-21T11:56:12+00:00", event.time.rfc3339
220231
end
232+
233+
it "declines to convert firebasedatabase without a domain" do
234+
json = {
235+
"eventType" => "providers/google.firebase.database/eventTypes/ref.write",
236+
"params" => {
237+
"child" => "xyz"
238+
},
239+
"auth" => {
240+
"admin" => true
241+
},
242+
"data" => {
243+
"data" => nil,
244+
"delta" => {
245+
"grandchild" => "other"
246+
}
247+
},
248+
"resource" => "projects/_/instances/my-project-id/refs/gcf-test/xyz",
249+
"timestamp" => "2020-05-21T11:15:34.178Z",
250+
"eventId" => "/SnHth9OSlzK1Puj85kk4tDbF90="
251+
}
252+
assert_nil load_legacy_event json
253+
end
221254
end

0 commit comments

Comments
 (0)