we can now query

This commit is contained in:
Kasper Juul Hermansen 2022-09-18 16:09:54 +02:00
parent cf2418c72b
commit e35672c262
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
3 changed files with 32 additions and 18 deletions

View File

@ -51,27 +51,27 @@ func (a *Action) Execute(ctx context.Context, area *storage.Area) error {
return nil
}
func (a *Action) Query(ctx context.Context, area *storage.Area) ([]string, error) {
func (a *Action) Query(ctx context.Context, area *storage.Area) ([]string, bool, error) {
for _, query := range a.Schema.Queries {
switch query.Type {
case "grep":
exe, err := querier.NewRipGrep(zap.L()).Build(ctx, a.SchemaPath, query.Query)
if err != nil {
return nil, err
return nil, false, err
}
output, err := exe(ctx, area.Path)
output, found, err := exe(ctx, area.Path)
if err != nil {
return nil, err
return nil, false, err
}
zap.L().Debug("Execution done")
return output, nil
return output, found, nil
default:
return nil, errors.New("could not determine query type")
return nil, false, errors.New("could not determine query type")
}
}
return nil, nil
return nil, false, nil
}

View File

@ -19,7 +19,7 @@ func NewRipGrep(logger *zap.Logger) *RipGrep {
return &RipGrep{logger: logger}
}
type RipGrepCommand func(ctx context.Context, victimPath string) ([]string, error)
type RipGrepCommand func(ctx context.Context, victimPath string) ([]string, bool, error)
func (g *RipGrep) Build(ctx context.Context, modulePath, query string) (RipGrepCommand, error) {
g.logger.Debug("Pulling docker image", zap.String("actiondir", modulePath), zap.String("query", query))
@ -54,14 +54,18 @@ func (g *RipGrep) Build(ctx context.Context, modulePath, query string) (RipGrepC
g.logger.Debug("Docker image pulled")
return func(ctx context.Context, victimPath string) ([]string, error) {
return func(ctx context.Context, victimPath string) ([]string, bool, error) {
g.logger.Debug("Executing script", zap.String("victim", victimPath))
runRipGrepCmd := fmt.Sprintf("docker run --rm -v %s/:/data:ro mbologna/docker-ripgrep rg -i '%s' || true", victimPath, query)
g.logger.Debug("Execute ripgrep query", zap.String("command", runRipGrepCmd))
cmd := exec.CommandContext(
ctx,
"/bin/bash",
"-c",
fmt.Sprintf("docker run --rm -v %s/:/data mbologna/docker-ripgrep rg -i %s", victimPath, query),
runRipGrepCmd,
)
runDockerWriter := &zapio.Writer{
@ -71,25 +75,32 @@ func (g *RipGrep) Build(ctx context.Context, modulePath, query string) (RipGrepC
defer runDockerWriter.Close()
builder := &strings.Builder{}
combinedWriter := io.MultiWriter(runDockerWriter, builder)
cmd.Stdout = combinedWriter
cmd.Stderr = runDockerWriter
cmd.Stderr = combinedWriter
err = cmd.Start()
if err != nil {
return nil, err
return nil, false, err
}
err = cmd.Wait()
if err != nil {
return nil, err
return nil, false, err
}
contents := strings.Split(builder.String(), "\n")
validatedOutput := make([]string, 0)
return contents, nil
for _, c := range contents {
if !strings.Contains(c, "WARNING:") {
validatedOutput = append(validatedOutput, c)
}
}
found := len(validatedOutput) > 0
return validatedOutput, found, nil
}, nil
}

View File

@ -113,12 +113,15 @@ func (pr *ProcessRepos) processRepo(ctx context.Context, repoUrl string, action
}
if len(action.Schema.Queries) > 0 {
result, err := action.Query(ctx, area)
result, found, err := action.Query(ctx, area)
if err != nil {
return err
}
pr.logger.Info("Query result", zap.Strings("result", result))
if found {
pr.logger.Info("Query result", zap.Strings("result", result))
// TODO: Append to real result, and return together
}
}
if len(action.Schema.Actions) > 0 {