11// Package main implements a plugin that checks that all rpc methods set the
2- // required options (permissions, http).
2+ // required options. The list of options is configurable.
3+ // The default value is:
4+ // - "qdrant.cloud.common.v1.permissions"
5+ // - "google.api.http"
36//
47// To use this plugin:
58//
1114// - QDRANT_CLOUD_METHOD_OPTIONS
1215// plugins:
1316// - plugin: buf-plugin-method-options
17+ // # Uncomment in case you need to configure the list of method options to validate.
18+ // # options:
19+ // # required_method_options:
20+ // # - "qdrant.cloud.common.v1.permissions"
1421package main
1522
1623import (
@@ -19,6 +26,7 @@ import (
1926 "buf.build/go/bufplugin/check"
2027 "buf.build/go/bufplugin/check/checkutil"
2128 "buf.build/go/bufplugin/info"
29+ "buf.build/go/bufplugin/option"
2230 googleann "google.golang.org/genproto/googleapis/api/annotations"
2331 "google.golang.org/protobuf/proto"
2432 "google.golang.org/protobuf/reflect/protoreflect"
@@ -28,7 +36,10 @@ import (
2836)
2937
3038const (
39+ // methodOptionsRuleID is the Rule ID of the methodOptions rule.
3140 methodOptionsRuleID = "QDRANT_CLOUD_METHOD_OPTIONS"
41+ // methodOptionsOptionKey is the option key to override the default list of required options.
42+ methodOptionsOptionKey = "required_method_options"
3243)
3344
3445var (
@@ -49,20 +60,37 @@ var (
4960 LicenseURL : "" ,
5061 },
5162 }
52- requiredMethodOptionExtensions = [ ]* protoimpl.ExtensionInfo {
53- commonv1 .E_Permissions ,
54- googleann .E_Http ,
63+ extensionRegistry = map [ string ]* protoimpl.ExtensionInfo {
64+ "qdrant.cloud.common.v1.permissions" : commonv1 .E_Permissions ,
65+ "google.api.http" : googleann .E_Http ,
5566 }
67+ requiredMethodOptionExtensions = []string {"qdrant.cloud.common.v1.permissions" , "google.api.http" }
5668)
5769
5870func main () {
5971 check .Main (spec )
6072}
6173
6274func checkMethodOptions (ctx context.Context , responseWriter check.ResponseWriter , request check.Request , methodDescriptor protoreflect.MethodDescriptor ) error {
75+ requiredOptions := requiredMethodOptionExtensions
76+ optionValue , err := option .GetStringSliceValue (request .Options (), methodOptionsOptionKey )
77+ if err != nil {
78+ return err
79+ }
80+ if len (optionValue ) > 0 {
81+ requiredOptions = optionValue
82+ }
83+
6384 options := methodDescriptor .Options ()
6485
65- for _ , extension := range requiredMethodOptionExtensions {
86+ for _ , extensionKey := range requiredOptions {
87+ extension , found := extensionRegistry [extensionKey ]
88+ if ! found {
89+ responseWriter .AddAnnotation (
90+ check .WithMessagef ("extension key %q does not exist" , extensionKey ),
91+ )
92+ return nil
93+ }
6694 if ! proto .HasExtension (options , extension ) {
6795 responseWriter .AddAnnotation (
6896 check .WithMessagef ("Method %q does not define the %q option" , methodDescriptor .FullName (), extension .TypeDescriptor ().FullName ()),
0 commit comments