MCPcopy Create free account
hub / github.com/chainloop-dev/chainloop / ComputeArguments

Function ComputeArguments

pkg/policies/policies.go:511–570  ·  view source on GitHub ↗

ComputeArguments takes a list of arguments, and matches it against the expected inputs. It also applies a set of interpolations if needed.

(name string, inputs []*v1.PolicyInput, args map[string]string, bindings map[string]string, logger *zerolog.Logger)

Source from the content-addressed store, hash-verified

509
510// ComputeArguments takes a list of arguments, and matches it against the expected inputs. It also applies a set of interpolations if needed.
511func ComputeArguments(name string, inputs []*v1.PolicyInput, args map[string]string, bindings map[string]string, logger *zerolog.Logger) (map[string]string, error) {
512 result := make(map[string]string)
513
514 // Policies without inputs in the spec
515 // TODO: Remove this in next release, once users have migrated their policies
516 if len(inputs) == 0 {
517 result = args
518 }
519
520 // Check for required inputs
521 for _, input := range inputs {
522 // Illegal combination
523 if input.Required && input.Default != "" {
524 return nil, fmt.Errorf("input %s can not be required and have a default at the same time", input.Name)
525 }
526
527 // if the input exists, it might be an expression, apply bindings to see if it has a value
528 argValue := args[input.Name]
529 var err error
530 if argValue != "" {
531 argValue, err = templates.ApplyBinding(argValue, bindings)
532 if err != nil {
533 return nil, err
534 }
535 }
536
537 // if the input is not present, or the computed value is empty, we need to check if it's required
538 if _, ok := args[input.Name]; !ok || argValue == "" {
539 if input.Required {
540 return nil, fmt.Errorf("missing required input %q", input.Name)
541 }
542 // if not required, and it has a default value, let's use it
543 if argValue == "" && input.Default != "" {
544 value, err := templates.ApplyBinding(input.Default, bindings)
545 if err != nil {
546 return nil, err
547 }
548 result[input.Name] = value
549 }
550 }
551 }
552
553 // check for provided arguments
554 for k, v := range args {
555 expected := slices.ContainsFunc(inputs, func(input *v1.PolicyInput) bool {
556 return input.Name == k
557 })
558 if !expected {
559 logger.Warn().Msgf("argument %q not defined in policy %q spec, ignoring it", k, name)
560 continue
561 }
562 value, err := templates.ApplyBinding(v, bindings)
563 if err != nil {
564 return nil, err
565 }
566 result[k] = value
567 }
568

Callers 5

VerifyMaterialMethod · 0.85
VerifyStatementMethod · 0.85

Calls 1

ApplyBindingFunction · 0.92

Tested by 1