forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate event schema from proto files (gravitational#30194)
* Event Schema protoc plugin, initial commit * Generate mappings + dump table and schema views * Add license headers * Add license headers pt.2 * go mod tidy * go mod tidy pt.2 * goimports * Address feedback: consistent order, document rebuild, drop eventype mapping * Reduce the list of queryable events * Remove stale comment * fixup! Address feedback: consistent order, document rebuild, drop eventype mapping * Fix view generation - support nested dmlTypes (`array(map(...))`) - support fields with `.` in their names - use same function to generate field names for view and table - use leading commas when generating schema views * display event_date and event_table in tableSchema + add tests * Add docstrings * Address jakule's feedback * Address marco's feedback * Convert Fields from a map to a list of fields * lint
- Loading branch information
Showing
17 changed files
with
20,904 additions
and
3 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
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,70 @@ | ||
//go:build debug | ||
|
||
/* | ||
Copyright 2023 Gravitational, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
// When built with this debug tag, the protoc plugin reads its input | ||
// from a file instead of stdin. This allows to easily attach a debugger and | ||
// inspect what is happening inside the plugin. | ||
|
||
import ( | ||
"io" | ||
"os" | ||
|
||
"github.com/gogo/protobuf/proto" | ||
"github.com/gogo/protobuf/protoc-gen-gogo/generator" | ||
plugin "github.com/gogo/protobuf/protoc-gen-gogo/plugin" | ||
"github.com/gravitational/trace" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
const pluginInputPathEnvironment = "TELEPORT_PROTOC_READ_FILE" | ||
|
||
func readRequest() (*plugin.CodeGeneratorRequest, error) { | ||
inputPath := os.Getenv(pluginInputPathEnvironment) | ||
if inputPath == "" { | ||
log.Error(trace.BadParameter("When built with the 'debug' tag, the input path must be set through the environment variable: %s", pluginInputPathEnvironment)) | ||
os.Exit(-1) | ||
} | ||
log.Infof("This is a debug build, the protoc request is read from the file: '%s'", inputPath) | ||
|
||
req, err := readRequestFromFile(inputPath) | ||
if err != nil { | ||
log.WithError(err).Error("error reading request from file") | ||
os.Exit(-1) | ||
} | ||
return req, trace.Wrap(err) | ||
} | ||
|
||
func readRequestFromFile(inputPath string) (*plugin.CodeGeneratorRequest, error) { | ||
g := generator.New() | ||
inputFile, err := os.Open(inputPath) | ||
if err != nil { | ||
return nil, trace.Wrap(err) | ||
} | ||
data, err := io.ReadAll(inputFile) | ||
if err != nil { | ||
return nil, trace.WrapWithMessage(err, "failed to read input") | ||
} | ||
|
||
if err := proto.Unmarshal(data, g.Request); err != nil { | ||
return nil, trace.WrapWithMessage(err, "failed to parse input proto") | ||
} | ||
|
||
if len(g.Request.FileToGenerate) == 0 { | ||
return nil, trace.BadParameter("no files to generate") | ||
} | ||
return g.Request, nil | ||
} |
86 changes: 86 additions & 0 deletions
86
build.assets/tooling/cmd/protoc-gen-eventschema/handlerequest.go
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 @@ | ||
// Copyright 2023 Gravitational, Inc | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"github.com/gogo/protobuf/protoc-gen-gogo/generator" | ||
gogoplugin "github.com/gogo/protobuf/protoc-gen-gogo/plugin" | ||
"github.com/gogo/protobuf/vanity/command" | ||
"github.com/gravitational/trace" | ||
|
||
"github.com/gravitational/teleport/build.assets/tooling/lib/eventschema" | ||
tree "github.com/gravitational/teleport/build.assets/tooling/lib/protobuf-tree" | ||
) | ||
|
||
const outputFileName = "zz_generated.eventschema.go" | ||
|
||
func handleRequest(req *gogoplugin.CodeGeneratorRequest) error { | ||
switch inputFileCount := len(req.FileToGenerate); { | ||
case inputFileCount == 0: | ||
return trace.BadParameter("no input file provided") | ||
case inputFileCount > 1: | ||
return trace.BadParameter("too many input files") | ||
} | ||
|
||
gen, err := newGenerator(req) | ||
if err != nil { | ||
return trace.Wrap(err) | ||
} | ||
|
||
rootFileName := req.FileToGenerate[0] | ||
gen.SetFile(rootFileName) | ||
for _, fileDesc := range gen.AllFiles().File { | ||
file := gen.AddFile(fileDesc) | ||
if fileDesc.GetName() == rootFileName { | ||
if err := generateSchema(file, gen.Response); err != nil { | ||
return trace.Wrap(err) | ||
} | ||
} | ||
} | ||
|
||
command.Write(gen.Response) | ||
|
||
return nil | ||
} | ||
|
||
func newGenerator(req *gogoplugin.CodeGeneratorRequest) (*tree.Forest, error) { | ||
gen := generator.New() | ||
|
||
gen.Request = req | ||
gen.CommandLineParameters(gen.Request.GetParameter()) | ||
gen.WrapTypes() | ||
gen.SetPackageNames() | ||
gen.BuildTypeNameMap() | ||
|
||
return tree.NewForest(gen), nil | ||
} | ||
|
||
func generateSchema(file *tree.File, resp *gogoplugin.CodeGeneratorResponse) error { | ||
gen := eventschema.NewSchemaGenerator() | ||
|
||
err := gen.Process(file) | ||
if err != nil { | ||
return trace.Wrap(err) | ||
} | ||
|
||
name := outputFileName | ||
content, err := gen.Render() | ||
if err != nil { | ||
return trace.Wrap(err) | ||
} | ||
resp.File = append(resp.File, &gogoplugin.CodeGeneratorResponse_File{Name: &name, Content: &content}) | ||
|
||
return nil | ||
} |
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,37 @@ | ||
/* | ||
Copyright 2023 Gravitational, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"os" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
func main() { | ||
log.SetLevel(log.DebugLevel) | ||
log.SetOutput(os.Stderr) | ||
req, err := readRequest() | ||
if err != nil { | ||
log.WithError(err).Error("Failed to read request") | ||
os.Exit(-1) | ||
} | ||
if err := handleRequest(req); err != nil { | ||
log.WithError(err).Error("Failed to generate schema") | ||
os.Exit(-1) | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
build.assets/tooling/cmd/protoc-gen-eventschema/readrequest.go
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 @@ | ||
//go:build !debug | ||
|
||
/* | ||
Copyright 2023 Gravitational, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
plugin "github.com/gogo/protobuf/protoc-gen-gogo/plugin" | ||
"github.com/gogo/protobuf/vanity/command" | ||
) | ||
|
||
func readRequest() (*plugin.CodeGeneratorRequest, error) { | ||
req := command.Read() | ||
return req, nil | ||
} |
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
Oops, something went wrong.