2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 22:48:58 +00:00
|
|
|
|
2023-01-04 16:07:02 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"go/format"
|
2023-02-17 21:14:46 +00:00
|
|
|
"io/fs"
|
2023-01-04 16:07:02 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
usage = "Usage: %s -input=/proto-dir-1 -input=/proto-dir-2 -output=/mappings.go\n"
|
|
|
|
|
|
|
|
fileHeader = `// generated by protoc-gen-consul-rate-limit; DO NOT EDIT.
|
|
|
|
package middleware
|
|
|
|
|
|
|
|
import "github.com/hashicorp/consul/agent/consul/rate"
|
|
|
|
`
|
|
|
|
|
|
|
|
entTags = `//go:build consulent
|
2023-01-05 10:31:14 +00:00
|
|
|
`
|
2023-01-04 16:07:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var (
|
|
|
|
inputPaths sliceFlags
|
|
|
|
outputPath string
|
|
|
|
)
|
|
|
|
flag.Var(&inputPaths, "input", "")
|
|
|
|
flag.StringVar(&outputPath, "output", "", "")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if len(inputPaths) == 0 || outputPath == "" {
|
|
|
|
fmt.Fprintf(os.Stderr, usage, os.Args[0])
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := run(inputPaths, outputPath); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func run(inputPaths []string, outputPath string) error {
|
|
|
|
if !strings.HasSuffix(outputPath, ".go") {
|
|
|
|
return errors.New("-output path must end in .go")
|
|
|
|
}
|
|
|
|
|
2023-08-22 14:46:03 +00:00
|
|
|
ce, ent, err := collectSpecs(inputPaths)
|
2023-01-04 16:07:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-22 14:46:03 +00:00
|
|
|
ceSource, err := generateCE(ce)
|
2023-01-04 16:07:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-08-22 14:46:03 +00:00
|
|
|
if err := os.WriteFile(outputPath, ceSource, 0666); err != nil {
|
2023-01-04 16:07:02 +00:00
|
|
|
return fmt.Errorf("failed to write output file: %s - %w", outputPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ent should only be non-zero in the enterprise repository.
|
|
|
|
if len(ent) > 0 {
|
|
|
|
entSource, err := generateENT(ent)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.WriteFile(enterpriseFileName(outputPath), entSource, 0666); err != nil {
|
|
|
|
return fmt.Errorf("failed to write output file: %s - %w", outputPath, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// enterpriseFileName adds the _ent filename suffix before the extension.
|
|
|
|
//
|
|
|
|
// Example:
|
2023-02-17 21:14:46 +00:00
|
|
|
//
|
2023-01-04 16:07:02 +00:00
|
|
|
// enterpriseFileName("bar/baz/foo.gen.go") => "bar/baz/foo_ent.gen.go"
|
|
|
|
func enterpriseFileName(filename string) string {
|
|
|
|
fileName := filepath.Base(filename)
|
|
|
|
extStart := strings.Index(fileName, ".")
|
|
|
|
return filepath.Join(
|
|
|
|
filepath.Dir(filename),
|
|
|
|
fileName[0:extStart]+"_ent"+fileName[extStart:],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
type spec struct {
|
2023-03-20 15:24:29 +00:00
|
|
|
MethodName string
|
|
|
|
OperationType string
|
|
|
|
Enterprise bool
|
|
|
|
OperationCategory string
|
2023-01-04 16:07:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s spec) GoOperationType() string {
|
|
|
|
switch s.OperationType {
|
|
|
|
case "OPERATION_TYPE_WRITE":
|
|
|
|
return "rate.OperationTypeWrite"
|
|
|
|
case "OPERATION_TYPE_READ":
|
|
|
|
return "rate.OperationTypeRead"
|
|
|
|
case "OPERATION_TYPE_EXEMPT":
|
|
|
|
return "rate.OperationTypeExempt"
|
|
|
|
}
|
|
|
|
panic(fmt.Sprintf("unknown rate limit operation type: %s", s.OperationType))
|
|
|
|
}
|
|
|
|
|
2023-03-20 15:24:29 +00:00
|
|
|
func (s spec) GoOperationCategory() string {
|
|
|
|
switch s.OperationCategory {
|
|
|
|
case "OPERATION_CATEGORY_ACL":
|
|
|
|
return "rate.OperationCategoryACL"
|
|
|
|
case "OPERATION_CATEGORY_PEER_STREAM":
|
|
|
|
return "rate.OperationCategoryPeerStream"
|
|
|
|
case "OPERATION_CATEGORY_CONNECT_CA":
|
|
|
|
return "rate.OperationCategoryConnectCA"
|
|
|
|
case "OPERATION_CATEGORY_PARTITION":
|
|
|
|
return "rate.OperationCategoryPartition"
|
|
|
|
case "OPERATION_CATEGORY_PEERING":
|
|
|
|
return "rate.OperationCategoryPeering"
|
2024-01-23 04:36:59 +00:00
|
|
|
case "OPERATION_CATEGORY_CONFIGENTRY":
|
|
|
|
return "rate.OperationCategoryConfigEntry"
|
2023-03-20 15:24:29 +00:00
|
|
|
case "OPERATION_CATEGORY_SERVER_DISCOVERY":
|
|
|
|
return "rate.OperationCategoryServerDiscovery"
|
|
|
|
case "OPERATION_CATEGORY_DATAPLANE":
|
|
|
|
return "rate.OperationCategoryDataPlane"
|
|
|
|
case "OPERATION_CATEGORY_DNS":
|
|
|
|
return "rate.OperationCategoryDNS"
|
|
|
|
case "OPERATION_CATEGORY_SUBSCRIBE":
|
|
|
|
return "rate.OperationCategorySubscribe"
|
|
|
|
case "OPERATION_CATEGORY_OPERATOR":
|
|
|
|
return "rate.OperationCategoryOperator"
|
|
|
|
case "OPERATION_CATEGORY_RESOURCE":
|
|
|
|
return "rate.OperationCategoryResource"
|
|
|
|
}
|
|
|
|
panic(fmt.Sprintf("unknown rate limit operation category: %s found in method: %s", s.OperationCategory, s.MethodName))
|
|
|
|
}
|
|
|
|
|
2023-01-04 16:07:02 +00:00
|
|
|
func collectSpecs(inputPaths []string) ([]spec, []spec, error) {
|
|
|
|
var specs []spec
|
2023-02-17 21:14:46 +00:00
|
|
|
var specFiles []string
|
2023-01-04 16:07:02 +00:00
|
|
|
for _, protoPath := range inputPaths {
|
2023-02-17 21:14:46 +00:00
|
|
|
err := filepath.WalkDir(protoPath, func(path string, info fs.DirEntry, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if info.Name() == ".ratelimit.tmp" {
|
|
|
|
specFiles = append(specFiles, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2023-01-04 16:07:02 +00:00
|
|
|
if err != nil {
|
2023-02-17 21:14:46 +00:00
|
|
|
return nil, nil, fmt.Errorf("failed to walk directory: %s - %w", protoPath, err)
|
2023-01-04 16:07:02 +00:00
|
|
|
}
|
2023-02-17 21:14:46 +00:00
|
|
|
}
|
2023-01-04 16:07:02 +00:00
|
|
|
|
2023-02-17 21:14:46 +00:00
|
|
|
for _, file := range specFiles {
|
|
|
|
b, err := os.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("failed to read ratelimit file: %w", err)
|
|
|
|
}
|
2023-01-04 16:07:02 +00:00
|
|
|
|
2023-02-17 21:14:46 +00:00
|
|
|
var fileSpecs []spec
|
|
|
|
if err := json.Unmarshal(b, &fileSpecs); err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("failed to unmarshal ratelimit file %s - %w", file, err)
|
2023-01-04 16:07:02 +00:00
|
|
|
}
|
2023-02-17 21:14:46 +00:00
|
|
|
specs = append(specs, fileSpecs...)
|
2023-01-04 16:07:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(specs, func(a, b int) bool {
|
|
|
|
return specs[a].MethodName < specs[b].MethodName
|
|
|
|
})
|
|
|
|
|
2023-08-22 14:46:03 +00:00
|
|
|
var ce, ent []spec
|
2023-01-04 16:07:02 +00:00
|
|
|
for _, spec := range specs {
|
|
|
|
if spec.Enterprise {
|
|
|
|
ent = append(ent, spec)
|
|
|
|
} else {
|
2023-08-22 14:46:03 +00:00
|
|
|
ce = append(ce, spec)
|
2023-01-04 16:07:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-22 14:46:03 +00:00
|
|
|
return ce, ent, nil
|
2023-01-04 16:07:02 +00:00
|
|
|
}
|
|
|
|
|
2023-08-22 14:46:03 +00:00
|
|
|
func generateCE(specs []spec) ([]byte, error) {
|
2023-01-04 16:07:02 +00:00
|
|
|
var output bytes.Buffer
|
|
|
|
output.WriteString(fileHeader)
|
|
|
|
|
2023-03-20 15:24:29 +00:00
|
|
|
fmt.Fprintln(&output, `var rpcRateLimitSpecs = map[string]rate.OperationSpec{`)
|
2023-01-04 16:07:02 +00:00
|
|
|
for _, spec := range specs {
|
2023-03-20 15:24:29 +00:00
|
|
|
fmt.Fprintf(&output, `"%s": {Type: %s, Category: %s},`, spec.MethodName, spec.GoOperationType(), spec.GoOperationCategory())
|
2023-01-04 16:07:02 +00:00
|
|
|
output.WriteString("\n")
|
|
|
|
}
|
|
|
|
output.WriteString("}")
|
|
|
|
|
|
|
|
formatted, err := format.Source(output.Bytes())
|
|
|
|
if err != nil {
|
2023-08-22 14:46:03 +00:00
|
|
|
return nil, fmt.Errorf("failed to format source in ce: %w", err)
|
2023-01-04 16:07:02 +00:00
|
|
|
}
|
|
|
|
return formatted, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateENT(specs []spec) ([]byte, error) {
|
|
|
|
var output bytes.Buffer
|
|
|
|
output.WriteString(entTags)
|
|
|
|
output.WriteString(fileHeader)
|
|
|
|
|
|
|
|
output.WriteString("func init() {\n")
|
|
|
|
for _, spec := range specs {
|
2023-03-20 15:24:29 +00:00
|
|
|
fmt.Fprintf(&output, `rpcRateLimitSpecs["%s"] = rate.OperationSpec{Type: %s, Category: %s}`, spec.MethodName, spec.GoOperationType(), spec.GoOperationCategory())
|
2023-01-04 16:07:02 +00:00
|
|
|
output.WriteString("\n")
|
|
|
|
}
|
|
|
|
output.WriteString("}")
|
|
|
|
|
|
|
|
formatted, err := format.Source(output.Bytes())
|
|
|
|
if err != nil {
|
2023-03-20 15:24:29 +00:00
|
|
|
return nil, fmt.Errorf("failed to format source in ent: %w", err)
|
2023-01-04 16:07:02 +00:00
|
|
|
}
|
|
|
|
return formatted, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type sliceFlags []string
|
|
|
|
|
|
|
|
func (i *sliceFlags) Set(value string) error {
|
|
|
|
*i = append(*i, value)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *sliceFlags) String() string { return "" }
|