-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add bazel macros for protobuf_rules_gen (#29)
* Make build rules public. * Add bazel macros for generating firestore security rules. * Update the readme. * Use newer bazel version on travis. * - Add golden test for the bazel example. - Move bazel definitions to a subdirectory. - Update readme formatting. * Fix merge. * Address code review comments. * Address code review comments.
- Loading branch information
Showing
16 changed files
with
256 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ os: | |
- osx | ||
|
||
env: | ||
- V=0.9.0 | ||
- V=0.14.1 | ||
|
||
before_install: | ||
- OS=linux | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,5 @@ | ||
workspace(name = "proto_gen_firebase_rules") | ||
|
||
protobuf_commit = "099d99759101c295244c24d8954ec85b8ac65ce3" | ||
load("//bazel:repositories.bzl", "protobuf_rules_gen_repositories") | ||
|
||
http_archive( | ||
name = "com_google_protobuf", | ||
sha256 = "c0ab1b088e220c1d56446f34001f0178e590270efdef1c46a77da4b9faa9d7b0", | ||
strip_prefix = "protobuf-" + protobuf_commit, | ||
url = "https://github.com/google/protobuf/archive/" + protobuf_commit + ".tar.gz", | ||
) | ||
|
||
http_archive( | ||
name = "com_google_protobuf_cc", | ||
sha256 = "c0ab1b088e220c1d56446f34001f0178e590270efdef1c46a77da4b9faa9d7b0", | ||
strip_prefix = "protobuf-" + protobuf_commit, | ||
url = "https://github.com/google/protobuf/archive/" + protobuf_commit + ".tar.gz", | ||
) | ||
protobuf_rules_gen_repositories() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
load("@com_google_protobuf//:protobuf.bzl", "proto_gen") | ||
|
||
def _outputs(srcs, ext): | ||
return [s[:-len(".proto")] + ".pb" + ext for s in srcs] | ||
|
||
def firestore_rules_library_impl(ctx): | ||
files = depset(order="postorder", direct=ctx.files.srcs, transitive=[d.fsrules.files for d in ctx.attr.deps]) | ||
return struct(fsrules=struct(files=files)) | ||
|
||
def firestore_rules_binary_impl(ctx): | ||
files = depset(order="postorder", direct=ctx.files.srcs, transitive=[d.fsrules.files for d in ctx.attr.deps]) | ||
args = ctx.actions.args() | ||
args.add_all(files) | ||
ctx.actions.run_shell(outputs=[ctx.outputs.bin], inputs=files, command='cat >"%s" "$@"' % ctx.outputs.bin.path, arguments=[args]) | ||
|
||
firestore_rules_library = rule( | ||
attrs = { | ||
"srcs": attr.label_list( | ||
allow_files = True, | ||
allow_empty = False, | ||
), | ||
"deps": attr.label_list(providers = ["fsrules"]), | ||
}, | ||
implementation = firestore_rules_library_impl, | ||
) | ||
|
||
firestore_rules_binary = rule( | ||
attrs = { | ||
"srcs": attr.label_list( | ||
allow_files = True, | ||
allow_empty = False, | ||
), | ||
"deps": attr.label_list(providers = ["fsrules"]), | ||
}, | ||
outputs = { | ||
"bin": "%{name}.rules", | ||
}, | ||
implementation = firestore_rules_binary_impl, | ||
) | ||
|
||
def firestore_rules_proto_library( | ||
name, | ||
srcs = [], | ||
deps = [], | ||
include = None, | ||
protoc = "@com_google_protobuf//:protoc", | ||
**kargs): | ||
"""Bazel rule to generate firestore security rules from protobuf schema | ||
Args: | ||
name: the name of the firestore_rules_proto_library. | ||
srcs: the .proto files of the firestore_rules_proto_library. | ||
deps: a list of dependency labels; must be firestore_rules_proto_library. | ||
include: a string indicating the include path of the .proto files. | ||
protoc: the label of the protocol compiler to generate the sources. | ||
**kargs: other keyword arguments that are passed to ts_library. | ||
""" | ||
outs = _outputs(srcs, ".rules") | ||
|
||
includes = [] | ||
if include != None: | ||
includes = [include] | ||
|
||
plugin = "//firebase_rules_generator:protoc-gen-firebase_rules" | ||
|
||
proto_gen( | ||
name = name + "_genproto_rules", | ||
srcs = srcs, | ||
deps = [s + "_genproto_rules" for s in deps]+ ["//proto:firebase_rules_options_genproto_rules"], | ||
includes = includes, | ||
protoc = protoc, | ||
outs = outs, | ||
visibility = ["//visibility:public"], | ||
plugin = plugin, | ||
plugin_language = "protoc-gen-firebase_rules", | ||
plugin_options = ["bazel"], | ||
) | ||
|
||
firestore_rules_library( | ||
name = name, | ||
srcs = outs, | ||
deps = deps, | ||
**kargs | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
protobuf_commit = "099d99759101c295244c24d8954ec85b8ac65ce3" | ||
|
||
protobuf_sha256 = "c0ab1b088e220c1d56446f34001f0178e590270efdef1c46a77da4b9faa9d7b0" | ||
|
||
|
||
def protobuf_rules_gen_repositories(): | ||
if "com_google_protobuf" not in native.existing_rules(): | ||
native.http_archive( | ||
name = "com_google_protobuf", | ||
sha256 = protobuf_sha256, | ||
strip_prefix = "protobuf-" + protobuf_commit, | ||
url = "https://github.com/google/protobuf/archive/" + protobuf_commit + ".tar.gz", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
load("@proto_gen_firebase_rules//bazel:defs.bzl", "firestore_rules_proto_library", "firestore_rules_binary") | ||
|
||
firestore_rules_proto_library( | ||
name = "schema", | ||
srcs = ["schema.proto"], | ||
) | ||
|
||
firestore_rules_binary( | ||
name = "example", | ||
srcs = ["main.rules"], | ||
deps = [":schema"], | ||
visibility = ["//visibility:public"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
service cloud.firestore { | ||
match /databases/{database}/documents { | ||
match /users/{userId} { | ||
allow read: if true; | ||
allow write: if isPersonMessage(request.resource.data) && | ||
request.auth.uid == userId; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
syntax = "proto3"; | ||
package example; | ||
|
||
message Person { | ||
string name = 1; | ||
string email = 2; | ||
|
||
enum PhoneType { | ||
MOBILE = 0; | ||
HOME = 1; | ||
WORK = 2; | ||
} | ||
message PhoneNumber { | ||
string number = 1; | ||
PhoneType type = 2; | ||
} | ||
|
||
PhoneNumber phone = 3; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
exports_files(["golden.rules"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// @@START_GENERATED_FUNCTIONS@@ | ||
function isPersonMessage(resource) { | ||
return resource.keys().hasAll([]) && | ||
(resource.keys().hasOnly(['phone','email','name'])) && | ||
((!resource.keys().hasAny(['name'])) || (resource.name is string)) && | ||
((!resource.keys().hasAny(['email'])) || (resource.email is string)) && | ||
((!resource.keys().hasAny(['phone'])) || (isPerson_PhoneNumberMessage(resource.phone))); | ||
} | ||
function isPerson_PhoneNumberMessage(resource) { | ||
return resource.keys().hasAll([]) && | ||
(resource.keys().hasOnly(['type','number'])) && | ||
((!resource.keys().hasAny(['number'])) || (resource.number is string)) && | ||
((!resource.keys().hasAny(['type'])) || (isPerson_PhoneTypeEnum(resource.type))); | ||
} | ||
function isPerson_PhoneTypeEnum(resource) { | ||
return resource == 0 || | ||
resource == 1 || | ||
resource == 2; | ||
} | ||
// @@END_GENERATED_FUNCTIONS@@ | ||
service cloud.firestore { | ||
match /databases/{database}/documents { | ||
match /users/{userId} { | ||
allow read: if true; | ||
allow write: if isPersonMessage(request.resource.data) && | ||
request.auth.uid == userId; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters