Skip to content

Commit

Permalink
fix: Classify policy parse errors as User (#716)
Browse files Browse the repository at this point in the history
* fix/classify_policy_parse_errors

* Fix linting
  • Loading branch information
roneli committed May 15, 2022
1 parent 7c78974 commit f5947bf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
3 changes: 3 additions & 0 deletions pkg/policy/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ func decodePolicyContent(labels []string, content *hcl.BodyContent, ctx *hcl.Eva
case "policy":
inner, innerDiags := DecodePolicyBlock(block, ctx)
diags = append(diags, innerDiags...)
if diags.HasErrors() {
return nil, diags
}
if len(inner.Identifiers) == 0 {
inner.Identifiers = p.Identifiers
}
Expand Down
24 changes: 12 additions & 12 deletions pkg/policy/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,23 @@ func Snapshot(ctx context.Context, storage database.Storage, policy *Policy, out

return StoreOutput(ctx, e, policy, snapShotPath)
}
func Load(ctx context.Context, directory string, policy *Policy) (*Policy, error) {
var err error
func Load(ctx context.Context, directory string, policy *Policy) (*Policy, diag.Diagnostics) {
var dd diag.Diagnostics
// if policy is configured with source we load it first
if policy.Source != "" {
log.Debug().Str("policy", policy.Name).Str("source", policy.Source).Msg("loading policy from source")
policy, err = loadPolicyFromSource(ctx, directory, policy.Name, policy.SubPolicy(), policy.Source)
if err != nil {
return nil, err
policy, dd = loadPolicyFromSource(ctx, directory, policy.Name, policy.SubPolicy(), policy.Source)
if dd.HasDiags() {
return nil, dd
}
}
// TODO: add recursive stop
// load inner policies
for i, p := range policy.Policies {
log.Debug().Str("policy", policy.Name).Str("inner_policy", p.Name).Msg("loading inner policy from source")
policy.Policies[i], err = Load(ctx, directory, p)
if err != nil {
return nil, err
policy.Policies[i], dd = Load(ctx, directory, p)
if dd.HasErrors() {
return nil, dd
}
}
return policy, nil
Expand Down Expand Up @@ -184,18 +184,18 @@ func run(ctx context.Context, storage database.Storage, request *ExecuteRequest)
return NewExecutor(db, progressUpdate).Execute(ctx, request, &filteredPolicy, nil)
}

func loadPolicyFromSource(ctx context.Context, directory, name, subPolicy, sourceURL string) (*Policy, error) {
func loadPolicyFromSource(ctx context.Context, directory, name, subPolicy, sourceURL string) (*Policy, diag.Diagnostics) {
data, meta, err := LoadSource(ctx, directory, sourceURL)
if err != nil {
return nil, err
return nil, diag.FromError(err, diag.INTERNAL)
}
f, dd := hclsyntax.ParseConfig(data, name, hcl.Pos{Byte: 0, Line: 1, Column: 1})
if dd.HasErrors() {
return nil, dd
return nil, diag.FromError(dd, diag.USER)
}
policy, dd := DecodePolicy(f.Body, nil, meta.Directory)
if dd.HasErrors() {
return nil, dd
return nil, diag.FromError(dd, diag.USER)
}
policy.meta = meta
if subPolicy != "" {
Expand Down
8 changes: 4 additions & 4 deletions pkg/ui/console/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,10 +401,10 @@ func (c Client) TestPolicies(ctx context.Context, policySource, snapshotDestinat
return err
}

p, err := policy.Load(ctx, c.cfg.CloudQuery.PolicyDirectory, &policy.Policy{Name: "test-policy", Source: policySource})
if err != nil {
log.Error().Err(err).Msg("failed to create policy manager")
return err
p, diags := policy.Load(ctx, c.cfg.CloudQuery.PolicyDirectory, &policy.Policy{Name: "test-policy", Source: policySource})
if diags.HasErrors() {
log.Error().Err(err).Msg("failed to load policy")
return diags
}

e := policy.NewExecutor(conn, nil)
Expand Down

0 comments on commit f5947bf

Please sign in to comment.