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

템플릿, 템플릿 버전 저장 시 rego 문법 validation #514

Merged
Merged
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
36 changes: 36 additions & 0 deletions internal/usecase/policy-template.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,24 @@ func (u *PolicyTemplateUsecase) Create(ctx context.Context, dto model.PolicyTemp
}
}

compileResult, err := u.RegoCompile(&domain.RegoCompileRequest{Rego: dto.Rego, Libs: dto.Libs}, false)

if err != nil {
return uuid.Nil, httpErrors.NewBadRequestError(fmt.Errorf("rego compile error"), "PT_INVALID_REGO_SYNTAX", "")
}

if len(compileResult.Errors) > 0 {
compileErrors := []string{}

for _, errorResult := range compileResult.Errors {
compileErrors = append(compileErrors, errorResult.Text)
}

detail := strings.Join(compileErrors, "\n")

return uuid.Nil, httpErrors.NewBadRequestError(fmt.Errorf("rego compile error"), "PT_INVALID_REGO_SYNTAX", detail)
}

if dto.IsTksTemplate() {
exists, err := u.repo.ExistByName(ctx, dto.TemplateName)
if err == nil && exists {
Expand Down Expand Up @@ -541,6 +559,24 @@ func (u *PolicyTemplateUsecase) CreatePolicyTemplateVersion(ctx context.Context,
}
}

compileResult, err := u.RegoCompile(&domain.RegoCompileRequest{Rego: rego, Libs: libs}, false)

if err != nil {
return "", httpErrors.NewBadRequestError(fmt.Errorf("rego compile error"), "PT_INVALID_REGO_SYNTAX", "")
}

if len(compileResult.Errors) > 0 {
compileErrors := []string{}

for _, errorResult := range compileResult.Errors {
compileErrors = append(compileErrors, errorResult.Text)
}

detail := strings.Join(compileErrors, "\n")

return "", httpErrors.NewBadRequestError(fmt.Errorf("rego compile error"), "PT_INVALID_REGO_SYNTAX", detail)
}

if policyTemplate == nil {
return "", httpErrors.NewBadRequestError(fmt.Errorf(
"failed to fetch policy template"),
Expand Down
Loading