This repository was archived by the owner on Mar 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Add bazel macros for protobuf_rules_gen #29
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5c6d8e3
Make build rules public.
ribrdb 6a95a4d
Add bazel macros for generating firestore security rules.
ribrdb b1bf15e
Update the readme.
ribrdb a0460ec
Use newer bazel version on travis.
ribrdb d603f08
Merge branch 'master' of https://github.com/FirebaseExtended/protobuf…
ribrdb e53c3be
- Add golden test for the bazel example.
ribrdb 255b502
Fix merge.
ribrdb 0ca8611
Address code review comments.
ribrdb 2064473
Address code review comments.
ribrdb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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(): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good pattern for deps is to allow someone to override them. See an example here: https://github.com/grpc/grpc/blob/master/bazel/grpc_deps.bzl
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| 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 hidden or 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"], | ||
| ) | ||
rockwotj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this doesn't format nicely. Try
s/-/*/?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ping - these still aren't printing nicely.