Skip to content

Commit

Permalink
fix: Loosen firebasedatabase domain restriction in legacy event conve…
Browse files Browse the repository at this point in the history
…rsion (#109)
  • Loading branch information
dazuma authored Jun 28, 2021
1 parent 95703e7 commit 2a4e45d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 13 deletions.
22 changes: 13 additions & 9 deletions lib/functions_framework/legacy_event_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,22 @@ def convert_source service, resource, domain

match = CE_SERVICE_TO_RESOURCE_RE[service].match resource
return [nil, nil] unless match
resource_fragment = match[1]
subject = match[2]

if service == "firebasedatabase.googleapis.com"
return [nil, nil] if domain.nil?
location = "us-central1"
if domain != "firebaseio.com"
location_match = domain.match(/^([\w-]+)\.firebasedatabase\.app$/)
return [nil, nil] unless location_match
location = location_match[1]
end
["//#{service}/projects/_/locations/#{location}/#{match[1]}", match[2]]
location =
case domain
when "firebaseio.com"
"us-central1"
when /^([\w-]+)\./
Regexp.last_match[1]
else
return [nil, nil]
end
["//#{service}/projects/_/locations/#{location}/#{resource_fragment}", subject]
else
["//#{service}/#{match[1]}", match[2]]
["//#{service}/#{resource_fragment}", subject]
end
end

Expand Down
41 changes: 37 additions & 4 deletions test/test_legacy_event_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,27 @@
# limitations under the License.

require "helper"
require "json"
require "stringio"

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

def load_legacy_event filename, url_path: nil, encoding: "utf-8"
path = File.join data_dir, filename
File.open path, encoding: encoding do |io|
def load_legacy_event filename_or_json, url_path: nil, encoding: "utf-8"
converter = FunctionsFramework::LegacyEventConverter.new
case filename_or_json
when String
path = File.join data_dir, filename_or_json
File.open path, encoding: encoding do |io|
env = { "rack.input" => io, "CONTENT_TYPE" => "application/json", "PATH_INFO" => url_path }
converter.decode_rack_env env
end
when Hash
io = StringIO.new JSON.dump filename_or_json
env = { "rack.input" => io, "CONTENT_TYPE" => "application/json", "PATH_INFO" => url_path }
converter = FunctionsFramework::LegacyEventConverter.new
converter.decode_rack_env env
else
raise ArgumentError, filename_or_json.class.name
end
end

Expand Down Expand Up @@ -218,4 +229,26 @@ def load_legacy_event filename, url_path: nil, encoding: "utf-8"
assert_equal "refs/gcf-test/abc", event.subject
assert_equal "2020-05-21T11:56:12+00:00", event.time.rfc3339
end

it "declines to convert firebasedatabase without a domain" do
json = {
"eventType" => "providers/google.firebase.database/eventTypes/ref.write",
"params" => {
"child" => "xyz"
},
"auth" => {
"admin" => true
},
"data" => {
"data" => nil,
"delta" => {
"grandchild" => "other"
}
},
"resource" => "projects/_/instances/my-project-id/refs/gcf-test/xyz",
"timestamp" => "2020-05-21T11:15:34.178Z",
"eventId" => "/SnHth9OSlzK1Puj85kk4tDbF90="
}
assert_nil load_legacy_event json
end
end

0 comments on commit 2a4e45d

Please sign in to comment.