Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions cmd/buf-plugin-method-options/main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Package main implements a plugin that checks that all rpc methods set the
// required options (permissions, http).
// required options. The list of options is configurable.
// The default value is:
// - "qdrant.cloud.common.v1.permissions"
// - "google.api.http"
//
// To use this plugin:
//
Expand All @@ -11,6 +14,10 @@
// - QDRANT_CLOUD_METHOD_OPTIONS
// plugins:
// - plugin: buf-plugin-method-options
// # Uncomment in case you need to configure the list of method options to validate.
// # options:
// # required_method_options:
// # - "qdrant.cloud.common.v1.permissions"
package main

import (
Expand All @@ -19,6 +26,7 @@ import (
"buf.build/go/bufplugin/check"
"buf.build/go/bufplugin/check/checkutil"
"buf.build/go/bufplugin/info"
"buf.build/go/bufplugin/option"
googleann "google.golang.org/genproto/googleapis/api/annotations"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
Expand All @@ -28,7 +36,10 @@ import (
)

const (
// methodOptionsRuleID is the Rule ID of the methodOptions rule.
methodOptionsRuleID = "QDRANT_CLOUD_METHOD_OPTIONS"
// methodOptionsOptionKey is the option key to override the default list of required options.
methodOptionsOptionKey = "required_method_options"
)

var (
Expand All @@ -49,20 +60,37 @@ var (
LicenseURL: "",
},
}
requiredMethodOptionExtensions = []*protoimpl.ExtensionInfo{
commonv1.E_Permissions,
googleann.E_Http,
extensionRegistry = map[string]*protoimpl.ExtensionInfo{
"qdrant.cloud.common.v1.permissions": commonv1.E_Permissions,
"google.api.http": googleann.E_Http,
}
requiredMethodOptionExtensions = []string{"qdrant.cloud.common.v1.permissions", "google.api.http"}
)

func main() {
check.Main(spec)
}

func checkMethodOptions(ctx context.Context, responseWriter check.ResponseWriter, request check.Request, methodDescriptor protoreflect.MethodDescriptor) error {
requiredOptions := requiredMethodOptionExtensions
optionValue, err := option.GetStringSliceValue(request.Options(), methodOptionsOptionKey)
if err != nil {
return err
}
if len(optionValue) > 0 {
requiredOptions = optionValue
}

options := methodDescriptor.Options()

for _, extension := range requiredMethodOptionExtensions {
for _, extensionKey := range requiredOptions {
extension, found := extensionRegistry[extensionKey]
if !found {
responseWriter.AddAnnotation(
check.WithMessagef("extension key %q does not exist", extensionKey),
)
return nil
}
if !proto.HasExtension(options, extension) {
responseWriter.AddAnnotation(
check.WithMessagef("Method %q does not define the %q option", methodDescriptor.FullName(), extension.TypeDescriptor().FullName()),
Expand Down
57 changes: 57 additions & 0 deletions cmd/buf-plugin-method-options/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,60 @@ func TestSimpleFailure(t *testing.T) {
},
}.Run(t)
}

func TestSimpleFailureWithOption(t *testing.T) {
t.Parallel()
checktest.CheckTest{
Request: &checktest.RequestSpec{
Files: &checktest.ProtoFileSpec{
DirPaths: []string{"testdata/simple_failure"},
FilePaths: []string{"simple.proto"},
},
Options: map[string]any{
methodOptionsOptionKey: []string{"qdrant.cloud.common.v1.permissions", "unknown.extension"},
},
},
Spec: spec,
ExpectedAnnotations: []checktest.ExpectedAnnotation{
{
RuleID: methodOptionsRuleID,
Message: "extension key \"unknown.extension\" does not exist",
},
{
RuleID: methodOptionsRuleID,
Message: "Method \"simple.GreeterService.HelloWorld\" does not define the \"qdrant.cloud.common.v1.permissions\" option",
FileLocation: &checktest.ExpectedFileLocation{
FileName: "simple.proto",
StartLine: 9,
StartColumn: 4,
EndLine: 12,
EndColumn: 5,
},
},
},
}.Run(t)

}

func TestSimpleFailureWithOptionWrongKey(t *testing.T) {
t.Parallel()
checktest.CheckTest{
Request: &checktest.RequestSpec{
Files: &checktest.ProtoFileSpec{
DirPaths: []string{"testdata/simple_failure"},
FilePaths: []string{"simple.proto"},
},
Options: map[string]any{
methodOptionsOptionKey: []string{"unknown.extension"},
},
},
Spec: spec,
ExpectedAnnotations: []checktest.ExpectedAnnotation{
{
RuleID: methodOptionsRuleID,
Message: "extension key \"unknown.extension\" does not exist",
},
},
}.Run(t)

}