Skip to content

Commit c161591

Browse files
committed
fix: fix named queries on migrator without compliance
1 parent f0fef55 commit c161591

File tree

3 files changed

+159
-238
lines changed

3 files changed

+159
-238
lines changed

jobs/post-install-job/job/migrations/compliance/populate.go

Lines changed: 0 additions & 193 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,7 @@ package compliance
22

33
import (
44
"context"
5-
"errors"
65
"fmt"
7-
"github.com/goccy/go-yaml"
8-
authApi "github.com/opengovern/og-util/pkg/api"
9-
"github.com/opengovern/og-util/pkg/httpclient"
10-
"github.com/opengovern/og-util/pkg/model"
11-
"github.com/opengovern/opensecurity/jobs/post-install-job/utils"
12-
"github.com/opengovern/opensecurity/services/core/db/models"
13-
integrationClient "github.com/opengovern/opensecurity/services/integration/client"
14-
"io/fs"
15-
"os"
16-
"path/filepath"
17-
"strings"
18-
196
"github.com/opengovern/og-util/pkg/postgres"
207
"github.com/opengovern/opensecurity/jobs/post-install-job/config"
218
"github.com/opengovern/opensecurity/services/compliance/db"
@@ -223,185 +210,5 @@ func (m Migration) Run(ctx context.Context, conf config.MigratorConfig, logger *
223210
logger.Info("inserted controls and benchmarks", zap.Error(err))
224211
return err
225212
}
226-
227-
err = populateQueries(ctx, logger, dbCore, conf)
228-
if err != nil {
229-
return err
230-
}
231-
232-
return nil
233-
}
234-
235-
func populateQueries(ctx context.Context, logger *zap.Logger, db db.Database, conf config.MigratorConfig) error {
236-
iClient := integrationClient.NewIntegrationServiceClient(conf.Integration.BaseURL)
237-
pluginTables, err := iClient.GetPluginsTables(&httpclient.Context{Ctx: ctx, UserRole: authApi.AdminRole})
238-
if err != nil {
239-
logger.Error("failed to get plugin tables", zap.Error(err))
240-
return nil
241-
}
242-
tablesPluginMap := make(map[string]string)
243-
for _, p := range pluginTables {
244-
for _, t := range p.Tables {
245-
tablesPluginMap[t] = p.PluginID
246-
}
247-
}
248-
249-
err = db.Orm.Transaction(func(tx *gorm.DB) error {
250-
err := filepath.Walk(config.QueriesGitPath, func(path string, info fs.FileInfo, err error) error {
251-
if !info.IsDir() && strings.HasSuffix(path, ".yaml") {
252-
return populateFinderItem(logger, tx, path, info, tablesPluginMap)
253-
}
254-
return nil
255-
})
256-
if err != nil && !errors.Is(err, fs.ErrNotExist) {
257-
logger.Error("failed to get queries", zap.Error(err))
258-
return err
259-
}
260-
return nil
261-
})
262-
if err != nil {
263-
return err
264-
}
265-
return nil
266-
}
267-
268-
func populateFinderItem(logger *zap.Logger, tx *gorm.DB, path string, info fs.FileInfo, tablesPluginMap map[string]string) error {
269-
id := strings.TrimSuffix(info.Name(), ".yaml")
270-
271-
content, err := os.ReadFile(path)
272-
if err != nil {
273-
return err
274-
}
275-
276-
var item NamedQuery
277-
err = yaml.Unmarshal(content, &item)
278-
if err != nil {
279-
logger.Error("failure in unmarshal", zap.String("path", path), zap.Error(err))
280-
return err
281-
}
282-
283-
if item.ID != "" {
284-
id = item.ID
285-
}
286-
287-
var integrationTypes []string
288-
for _, c := range item.IntegrationTypes {
289-
integrationTypes = append(integrationTypes, string(c))
290-
}
291-
292-
isBookmarked := false
293-
tags := make([]models.NamedQueryTag, 0, len(item.Tags))
294-
for k, v := range item.Tags {
295-
if k == "platform_queries_bookmark" {
296-
isBookmarked = true
297-
}
298-
tag := models.NamedQueryTag{
299-
NamedQueryID: id,
300-
Tag: model.Tag{
301-
Key: k,
302-
Value: v,
303-
},
304-
}
305-
tags = append(tags, tag)
306-
}
307-
308-
listOfTables, err := utils.ExtractTableRefsFromPolicy("sql", item.Query)
309-
if err != nil {
310-
logger.Error("failed to extract table refs from query", zap.String("query-id", id), zap.Error(err))
311-
}
312-
if len(integrationTypes) == 0 {
313-
integrationTypesMap := make(map[string]bool)
314-
for _, t := range listOfTables {
315-
if v, ok := tablesPluginMap[t]; ok {
316-
integrationTypesMap[v] = true
317-
}
318-
}
319-
for it := range integrationTypesMap {
320-
integrationTypes = append(integrationTypes, it)
321-
}
322-
}
323-
324-
namedQuery := models.NamedQuery{
325-
ID: id,
326-
IntegrationTypes: integrationTypes,
327-
Title: item.Title,
328-
Description: item.Description,
329-
IsBookmarked: isBookmarked,
330-
QueryID: &id,
331-
}
332-
333-
parameters, err := utils.ExtractParameters("sql", item.Query)
334-
if err != nil {
335-
logger.Error("extract control failed: failed to extract parameters from query", zap.String("control-id", namedQuery.ID), zap.Error(err))
336-
return nil
337-
}
338-
queryParams := []models.QueryParameter{}
339-
for _, p := range parameters {
340-
queryParams = append(queryParams, models.QueryParameter{
341-
QueryID: namedQuery.ID,
342-
Key: p,
343-
})
344-
}
345-
346-
query := models.Query{
347-
ID: namedQuery.ID,
348-
QueryToExecute: item.Query,
349-
ListOfTables: listOfTables,
350-
Engine: "sql",
351-
Parameters: queryParams,
352-
}
353-
err = tx.Clauses(clause.OnConflict{
354-
Columns: []clause.Column{{Name: "id"}}, // key column
355-
DoNothing: true,
356-
}).Create(&query).Error
357-
if err != nil {
358-
logger.Error("failure in Creating Policy", zap.String("query_id", id), zap.Error(err))
359-
return err
360-
}
361-
for _, param := range query.Parameters {
362-
err = tx.Clauses(clause.OnConflict{
363-
Columns: []clause.Column{{Name: "key"}, {Name: "query_id"}}, // key columns
364-
DoNothing: true,
365-
}).Create(&param).Error
366-
if err != nil {
367-
return fmt.Errorf("failure in query parameter insert: %v", err)
368-
}
369-
}
370-
371-
err = tx.Model(&models.NamedQuery{}).Clauses(clause.OnConflict{
372-
Columns: []clause.Column{{Name: "id"}}, // key column
373-
DoNothing: true, // column needed to be updated
374-
}).Create(namedQuery).Error
375-
if err != nil {
376-
logger.Error("failure in insert query", zap.Error(err))
377-
return err
378-
}
379-
380-
if len(tags) > 0 {
381-
for _, tag := range tags {
382-
err = tx.Model(&models.NamedQueryTag{}).Create(&tag).Error
383-
if err != nil {
384-
logger.Error("failure in insert tags", zap.Error(err))
385-
return err
386-
}
387-
}
388-
}
389-
390-
for _, p := range item.Parameters {
391-
err := tx.Clauses(clause.OnConflict{
392-
Columns: []clause.Column{{Name: "key"}, {Name: "control_id"}},
393-
DoUpdates: clause.Assignments(map[string]interface{}{
394-
"value": gorm.Expr("CASE WHEN policy_parameter_values.value = '' THEN ? ELSE policy_parameter_values.value END", p.Value),
395-
}),
396-
}).Create(&models.PolicyParameterValues{
397-
Key: p.Key,
398-
ControlID: "",
399-
Value: p.Value,
400-
}).Error
401-
if err != nil {
402-
return err
403-
}
404-
}
405-
406213
return nil
407214
}

0 commit comments

Comments
 (0)