Skip to content

Commit

Permalink
Adding ability for template context to work with GRPC protoc and addi…
Browse files Browse the repository at this point in the history
…ng ability to detect default and remove if additional config is found
  • Loading branch information
shawn-hurley committed Jun 28, 2024
1 parent 45650a3 commit 7affd17
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 12 deletions.
2 changes: 2 additions & 0 deletions cmd/analyzer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func AnalysisCmd() *cobra.Command {
builtinConfigs := []provider.InitConfig{}
providerLocations := []string{}
for _, config := range configs {
fmt.Printf("here: %#v\n", config)
config.ContextLines = contextLines
for _, ind := range config.InitConfig {
providerLocations = append(providerLocations, ind.Location)
Expand Down Expand Up @@ -250,6 +251,7 @@ func AnalysisCmd() *cobra.Command {
}

if builtinClient, ok := needProviders["builtin"]; ok {
fmt.Printf("here: %v\n", builtinClient)
if _, err = builtinClient.ProviderInit(ctx, builtinConfigs); err != nil {
errLog.Error(err, "unable to init builtin provider")
os.Exit(1)
Expand Down
3 changes: 1 addition & 2 deletions external-providers/java-external-provider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"flag"
"fmt"
"io"
"os"

"github.com/bombsimon/logrusr/v3"
Expand All @@ -28,7 +27,7 @@ func main() {
flag.Parse()

logrusLog := logrus.New()
logrusLog.SetOutput(io.MultiWriter(os.Stderr, os.Stdout))
logrusLog.SetOutput(os.Stdout)
logrusLog.SetFormatter(&logrus.TextFormatter{})
logrusLog.SetLevel(logrus.Level(5))
log := logrusr.New(logrusLog)
Expand Down
16 changes: 15 additions & 1 deletion provider/internal/builtin/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,25 @@ func (p *builtinProvider) Capabilities() []provider.Capability {
}

func (p *builtinProvider) ProviderInit(ctx context.Context, additionalInitConfigs []provider.InitConfig) ([]provider.InitConfig, error) {
p.log.Info("provider init", "init config", p.config.InitConfig, "additional configs", additionalInitConfigs)
// First load all the tags for all init configs.
for _, c := range p.config.InitConfig {
p.loadTags(c)
}

if additionalInitConfigs != nil {
var defaultIndex *int
for i, initConfigs := range p.config.InitConfig {
if _, ok := initConfigs.ProviderSpecificConfig["default"]; ok {
defaultIndex = &i
}
}

if additionalInitConfigs != nil && len(additionalInitConfigs) != 0 {
if len(p.config.InitConfig) == 1 && defaultIndex != nil {
p.config.InitConfig = additionalInitConfigs
} else if defaultIndex != nil {
p.config.InitConfig = append(p.config.InitConfig[:*defaultIndex], p.config.InitConfig[*defaultIndex+1:]...)
}
p.config.InitConfig = append(p.config.InitConfig, additionalInitConfigs...)
}

Expand Down Expand Up @@ -186,6 +199,7 @@ func (p *builtinProvider) Init(ctx context.Context, log logr.Logger, config prov
if config.AnalysisMode != provider.AnalysisMode("") {
p.log.V(5).Info("skipping analysis mode setting for builtin")
}

return &builtinServiceClient{
config: config,
tags: p.tags,
Expand Down
13 changes: 12 additions & 1 deletion provider/internal/builtin/service_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,15 @@ var _ provider.ServiceClient = &builtinServiceClient{}

func (p *builtinServiceClient) Stop() {}

func convertToInterface(s []string) []interface{} {
n := []interface{}{}
for _, v := range s {
n = append(n, v)
}
return n
}
func (p *builtinServiceClient) Evaluate(ctx context.Context, cap string, conditionInfo []byte) (provider.ProviderEvaluateResponse, error) {
fmt.Printf("\nhere: %v\n", cap)
var cond builtinCondition
err := yaml.Unmarshal(conditionInfo, &cond)
if err != nil {
Expand All @@ -59,7 +67,7 @@ func (p *builtinServiceClient) Evaluate(ctx context.Context, cap string, conditi
return response, fmt.Errorf("unable to find files using pattern `%s`: %v", c.Pattern, err)
}

response.TemplateContext = map[string]interface{}{"filepaths": matchingFiles}
response.TemplateContext = map[string]interface{}{"filepaths": convertToInterface(matchingFiles)}
for _, match := range matchingFiles {
absPath := match
if !filepath.IsAbs(match) {
Expand Down Expand Up @@ -147,14 +155,17 @@ func (p *builtinServiceClient) Evaluate(ctx context.Context, cap string, conditi
}
return response, nil
case "xml":
p.log.Info("Here for XML", "cond", cond, "location", p.config.Location)
query, err := xpath.CompileWithNS(cond.XML.XPath, cond.XML.Namespaces)
if query == nil || err != nil {
return response, fmt.Errorf("could not parse provided xpath query '%s': %v", cond.XML.XPath, err)
}
p.log.Info("Here for XML", "cond", cond)
xmlFiles, err := findXMLFiles(p.config.Location, cond.XML.Filepaths)
if err != nil {
return response, fmt.Errorf("unable to find XML files: %v", err)
}
p.log.Info("Here for XML", "cond", cond)
for _, file := range xmlFiles {
nodes, err := queryXMLFile(file, query)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ func init() {
builtinConfig.InitConfig = []InitConfig{
{
Location: c,
ProviderSpecificConfig: map[string]interface{}{
"default": true,
},
},
}
}
Expand Down Expand Up @@ -168,6 +171,7 @@ func GetConfig(filepath string) ([]Config, error) {
if c.Proxy == nil {
c.Proxy = (*Proxy)(httpproxy.FromEnvironment())
}

for jdx := range c.InitConfig {
ic := &c.InitConfig[jdx]
// if a specific proxy config not present
Expand All @@ -180,7 +184,6 @@ func GetConfig(filepath string) ([]Config, error) {
return configs, err
}
ic.ProviderSpecificConfig = newConfig

}
}
if !foundBuiltin {
Expand Down
4 changes: 3 additions & 1 deletion provider/server/builtin-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ type builtinServer struct {
}

func NewBuiltinProviderServer(log logr.Logger) (libgrpc.ProviderServiceServer, error) {
builtin := builtin.NewBuiltinProvider(provider.Config{}, log)
l := log.WithValues("builtin", "builtin")
builtin := builtin.NewBuiltinProvider(provider.Config{}, l)
return &builtinServer{
server: server{
Client: builtin,
mutex: sync.RWMutex{},
clients: map[int64]clientMapItem{},
rand: rand.Rand{},
Log: l,
},
}, nil
}
Expand Down
9 changes: 3 additions & 6 deletions provider/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ func (s *server) Evaluate(ctx context.Context, req *libgrpc.EvaluateRequest) (*l

s.Log.Info("here", "cap", req.Cap)
r, err := client.client.Evaluate(ctx, req.Cap, []byte(req.ConditionInfo))
s.Log.Info("here", "r", r, "err", err)

if err != nil {
return &libgrpc.EvaluateResponse{
Expand All @@ -323,14 +324,9 @@ func (s *server) Evaluate(ctx context.Context, req *libgrpc.EvaluateRequest) (*l
}, nil
}

for _, v := range r.TemplateContext {
if _, ok := v.([]string); ok {
s.Log.Info("here")
}
}

templateContext, err := structpb.NewStruct(r.TemplateContext)
if err != nil {
s.Log.Info("here 329", "r", r, "err", err)
return &libgrpc.EvaluateResponse{
Error: err.Error(),
Successful: false,
Expand All @@ -355,6 +351,7 @@ func (s *server) Evaluate(ctx context.Context, req *libgrpc.EvaluateRequest) (*l

variables, err := structpb.NewStruct(i.Variables)
if err != nil {
s.Log.Info("here 354", "r", r, "err", err)
return &libgrpc.EvaluateResponse{
Error: err.Error(),
Successful: false,
Expand Down

0 comments on commit 7affd17

Please sign in to comment.