Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rule_type create: Add option to create multiple rule types at once or read all filed in directory #748

Merged
merged 4 commits into from
Aug 25, 2023
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
90 changes: 55 additions & 35 deletions cmd/cli/app/rule_type/rule_type_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ package rule_type

import (
"fmt"
"io"
"os"
"path/filepath"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/exp/slices"

"github.com/stacklok/mediator/internal/engine"
"github.com/stacklok/mediator/internal/util"
Expand All @@ -41,58 +40,79 @@ within a mediator control plane.`,
}
},
RunE: func(cmd *cobra.Command, args []string) error {
f := util.GetConfigValue("file", "file", cmd, "").(string)

var err error

var preader io.Reader

if f == "" {
return fmt.Errorf("error: file must be set")
files, err := cmd.Flags().GetStringArray("file")
if err != nil {
return fmt.Errorf("error getting file flag: %w", err)
}

if f == "-" {
preader = os.Stdin
} else {
f = filepath.Clean(f)
fopen, err := os.Open(f)
if err != nil {
return fmt.Errorf("error opening file: %w", err)
}

defer fopen.Close()

preader = fopen
if err := validateFilesArg(files); err != nil {
return fmt.Errorf("error validating file arg: %w", err)
}

conn, err := util.GrpcForCommand(cmd)
util.ExitNicelyOnError(err, "Error getting grpc connection")
if err != nil {
jhrozek marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("error getting grpc connection: %w", err)
}
Comment on lines +53 to +55
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(non-blocking curiousity) Why remove util.ExitNicelyOnError? We seem to use it in a bunch of places.

defer conn.Close()

client := pb.NewPolicyServiceClient(conn)
ctx, cancel := util.GetAppContext()
defer cancel()

r, err := engine.ParseRuleType(preader)
expfiles, err := util.ExpandFileArgs(files)
if err != nil {
return fmt.Errorf("error parsing rule type: %w", err)
return fmt.Errorf("error expanding file args: %w", err)
}

// create a policy
resp, err := client.CreateRuleType(ctx, &pb.CreateRuleTypeRequest{
RuleType: r,
})
if err != nil {
return fmt.Errorf("error creating rule type: %w", err)
for _, f := range expfiles {
preader, closer, err := util.OpenFileArg(f, cmd.InOrStdin())
if err != nil {
return fmt.Errorf("error opening file arg: %w", err)
}
defer closer()

r, err := engine.ParseRuleType(preader)
if err != nil {
return fmt.Errorf("error parsing rule type: %w", err)
}

// create a rule
resp, err := client.CreateRuleType(ctx, &pb.CreateRuleTypeRequest{
RuleType: r,
})
if err != nil {
return fmt.Errorf("error creating rule type: %w", err)
}
out, err := util.GetJsonFromProto(resp)
if err != nil {
return fmt.Errorf("error getting json from proto: %w", err)
}

fmt.Println(out)
}
out, err := util.GetJsonFromProto(resp)
util.ExitNicelyOnError(err, "Error getting json from proto")
fmt.Println(out)

return nil
},
}

func validateFilesArg(files []string) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this be already useful in util?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe. I plan to migrate policy creation to a similar pattern. Mind if I move this in that PR instead?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, whatever is easier for you

if files == nil {
return fmt.Errorf("error: file must be set")
}
Comment on lines +99 to +101
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to check len(files) > 0?


if slices.Contains(files, "") {
return fmt.Errorf("error: file must be set")
}

if slices.Contains(files, "-") && len(files) > 1 {
return fmt.Errorf("error: cannot use stdin with other files")
}

return nil
}

func init() {
ruleTypeCmd.AddCommand(RuleType_createCmd)
RuleType_createCmd.Flags().StringP("file", "f", "", "Path to the YAML defining the rule type (or - for stdin)")
RuleType_createCmd.Flags().StringArrayP("file", "f", []string{},
"Path to the YAML defining the rule type (or - for stdin). Can be specified multiple times. Can be a directory.")
}
68 changes: 68 additions & 0 deletions internal/util/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"net"
"os"
Expand Down Expand Up @@ -374,3 +375,70 @@ func GetBytesFromProto(message protoreflect.ProtoMessage) ([]byte, error) {
m := getProtoMarshalOptions()
return m.Marshal(message)
}

// OpenFileArg opens a file argument and returns a descriptor, closer, and error
// If the file is "-", it will return whatever is passed in as dashOpen and a no-op closer
func OpenFileArg(f string, dashOpen io.Reader) (desc io.Reader, closer func(), err error) {
if f == "-" {
desc = dashOpen
closer = func() {}
return desc, closer, nil
}

f = filepath.Clean(f)
ftemp, err := os.Open(f)
if err != nil {
return nil, nil, fmt.Errorf("error opening file: %w", err)
}

closer = func() {
err := ftemp.Close()
if err != nil {
fmt.Fprintf(os.Stderr, "error closing file: %v\n", err)
}
}

desc = ftemp
return desc, closer, nil
}

// ExpandFileArgs expands a list of file arguments into a list of files.
// If the file list contains "-" or regular files, it will leave them as-is.
// If the file list contains directories, it will expand them into a list of files.
func ExpandFileArgs(files []string) ([]string, error) {
var expandedFiles []string
for _, f := range files {
if f == "-" {
expandedFiles = append(expandedFiles, f)
continue
}
f = filepath.Clean(f)
fi, err := os.Stat(f)
if err != nil {
return nil, fmt.Errorf("error getting file info: %w", err)
}

if fi.IsDir() {
// expand directory
err := filepath.Walk(f, func(path string, info fs.FileInfo, err error) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the stdlib documentation seems to suggest that WalkDir might be faster, but at the same time I don't think it matters for our use-case

if err != nil {
return fmt.Errorf("error walking directory: %w", err)
}

if !info.IsDir() {
expandedFiles = append(expandedFiles, path)
}

return nil
})
if err != nil {
return nil, fmt.Errorf("error walking directory: %w", err)
}
} else {
// add file
expandedFiles = append(expandedFiles, f)
}
}

return expandedFiles, nil
}