Skip to content

Commit d713594

Browse files
committed
fix: update tables
1 parent 7116782 commit d713594

File tree

6 files changed

+231
-15
lines changed

6 files changed

+231
-15
lines changed
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
package opengovernance_client
2+
3+
import (
4+
"context"
5+
"runtime"
6+
7+
steampipesdk "github.com/opengovern/og-util/pkg/steampipe"
8+
9+
es "github.com/opengovern/og-util/pkg/opengovernance-es-sdk"
10+
"github.com/opengovern/opensecurity/pkg/cloudql/sdk/config"
11+
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
12+
)
13+
14+
const (
15+
ArtifactPackageListIndex = "artifact_package_list"
16+
)
17+
18+
type ArtifactPackageList struct {
19+
ImageURL string `json:"image_url"`
20+
ArtifactID string `json:"artifact_id"`
21+
Packages []Package `json:"packages"`
22+
}
23+
type Package struct {
24+
Ecosystem string `json:"ecosystem"`
25+
Name string `json:"name"`
26+
Version string `json:"version"`
27+
}
28+
29+
type ArtifactPackageListResult struct {
30+
PlatformID string `json:"platform_id"`
31+
ResourceID string `json:"resource_id"`
32+
ResourceName string `json:"resource_name"`
33+
Description ArtifactPackageList `json:"Description"`
34+
TaskType string `json:"task_type"`
35+
ResultType string `json:"result_type"`
36+
Metadata map[string]string `json:"metadata"`
37+
DescribedBy string `json:"described_by"`
38+
DescribedAt int64 `json:"described_at"`
39+
}
40+
41+
type ArtifactPackageListHit struct {
42+
ID string `json:"_id"`
43+
Score float64 `json:"_score"`
44+
Index string `json:"_index"`
45+
Type string `json:"_type"`
46+
Version int64 `json:"_version,omitempty"`
47+
Source ArtifactPackageListResult `json:"_source"`
48+
Sort []any `json:"sort"`
49+
}
50+
51+
type ArtifactPackageListHits struct {
52+
Total es.SearchTotal `json:"total"`
53+
Hits []ArtifactPackageListHit `json:"hits"`
54+
}
55+
56+
type ArtifactPackageListResponse struct {
57+
PitID string `json:"pit_id"`
58+
Hits ArtifactPackageListHits `json:"hits"`
59+
}
60+
61+
type ArtifactPackageListPaginator struct {
62+
paginator *es.BaseESPaginator
63+
}
64+
65+
func (k Client) NewArtifactPackageListPaginator(filters []es.BoolFilter, limit *int64) (ArtifactPackageListPaginator, error) {
66+
paginator, err := es.NewPaginator(k.ES.ES(), ArtifactPackageListIndex, filters, limit)
67+
if err != nil {
68+
return ArtifactPackageListPaginator{}, err
69+
}
70+
71+
p := ArtifactPackageListPaginator{
72+
paginator: paginator,
73+
}
74+
75+
return p, nil
76+
}
77+
78+
func (p ArtifactPackageListPaginator) HasNext() bool {
79+
return !p.paginator.Done()
80+
}
81+
82+
func (p ArtifactPackageListPaginator) Close(ctx context.Context) error {
83+
return p.paginator.Deallocate(ctx)
84+
}
85+
86+
func (p ArtifactPackageListPaginator) NextPage(ctx context.Context) ([]ArtifactPackageListResult, error) {
87+
var response ArtifactPackageListResponse
88+
err := p.paginator.SearchWithLog(ctx, &response, true)
89+
if err != nil {
90+
return nil, err
91+
}
92+
93+
var values []ArtifactPackageListResult
94+
for _, hit := range response.Hits.Hits {
95+
values = append(values, hit.Source)
96+
}
97+
98+
hits := int64(len(response.Hits.Hits))
99+
if hits > 0 {
100+
p.paginator.UpdateState(hits, response.Hits.Hits[hits-1].Sort, response.PitID)
101+
} else {
102+
p.paginator.UpdateState(hits, nil, "")
103+
}
104+
105+
return values, nil
106+
}
107+
108+
var artifactPackageListMapping = map[string]string{
109+
"image_url": "Description.ImageURL",
110+
"artifact_id": "Description.ArtifactID",
111+
}
112+
113+
func ListArtifactPackageList(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (any, error) {
114+
plugin.Logger(ctx).Trace("ListArtifactSboms", d)
115+
runtime.GC()
116+
// create service
117+
cfg := config.GetConfig(d.Connection)
118+
ke, err := config.NewClientCached(cfg, d.ConnectionCache, ctx)
119+
if err != nil {
120+
plugin.Logger(ctx).Error("ListArtifactSboms NewClientCached", "error", err)
121+
return nil, err
122+
}
123+
k := Client{ES: ke}
124+
125+
sc, err := steampipesdk.NewSelfClientCached(ctx, d.ConnectionCache)
126+
if err != nil {
127+
plugin.Logger(ctx).Error("ListArtifactSboms NewSelfClientCached", "error", err)
128+
return nil, err
129+
}
130+
encodedResourceCollectionFilters, err := sc.GetConfigTableValueOrNil(ctx, steampipesdk.OpenGovernanceConfigKeyResourceCollectionFilters)
131+
if err != nil {
132+
plugin.Logger(ctx).Error("ListArtifactSboms GetConfigTableValueOrNil for resource_collection_filters", "error", err)
133+
return nil, err
134+
}
135+
clientType, err := sc.GetConfigTableValueOrNil(ctx, steampipesdk.OpenGovernanceConfigKeyClientType)
136+
if err != nil {
137+
plugin.Logger(ctx).Error("ListLookupResources GetConfigTableValueOrNil for client_type", "error", err)
138+
return nil, err
139+
}
140+
141+
plugin.Logger(ctx).Trace("Columns", d.FetchType)
142+
paginator, err := k.NewArtifactPackageListPaginator(
143+
es.BuildFilterWithDefaultFieldName(ctx, d.QueryContext, artifactPackageListMapping,
144+
nil, encodedResourceCollectionFilters, clientType, true),
145+
d.QueryContext.Limit)
146+
if err != nil {
147+
plugin.Logger(ctx).Error("ListArtifactSboms NewArtifactSbomPaginator", "error", err)
148+
return nil, err
149+
}
150+
151+
for paginator.HasNext() {
152+
page, err := paginator.NextPage(ctx)
153+
if err != nil {
154+
plugin.Logger(ctx).Error("ListArtifactSboms NextPage", "error", err)
155+
return nil, err
156+
}
157+
plugin.Logger(ctx).Trace("ListArtifactSboms", "next page")
158+
159+
for _, v := range page {
160+
d.StreamListItem(ctx, v)
161+
}
162+
}
163+
164+
err = paginator.Close(ctx)
165+
if err != nil {
166+
return nil, err
167+
}
168+
169+
return nil, nil
170+
}

pkg/cloudql/client/artifact_sbom.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const (
1818
type ArtifactSbom struct {
1919
ImageURL string `json:"image_url"`
2020
ArtifactID string `json:"artifact_id"`
21-
Packages []string `json:"packages"`
21+
Packages []Package `json:"packages"`
2222
SbomSpdxJson interface{} `json:"sbom_spdx_json"`
2323
SbomCyclonedxJson interface{} `json:"sbom_cyclonedx_json"`
2424
}
@@ -105,7 +105,6 @@ func (p ArtifactSbomPaginator) NextPage(ctx context.Context) ([]ArtifactSbomResu
105105
var artifactSbomsMapping = map[string]string{
106106
"image_url": "Description.ImageURL",
107107
"artifact_id": "Description.ArtifactID",
108-
"packages": "Description.Packages",
109108
}
110109

111110
func ListArtifactSboms(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (any, error) {

pkg/cloudql/tables/plugin.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,18 @@ func Plugin(ctx context.Context) *plugin.Plugin {
1919
Schema: config.Schema(),
2020
},
2121
TableMap: map[string]*plugin.Table{
22-
"platform_findings": tablePlatformFindings(ctx),
23-
"platform_resources": tablePlatformResources(ctx),
24-
"platform_lookup": tablePlatformLookup(ctx),
25-
"platform_integrations": tablePlatformConnections(ctx),
26-
"platform_integration_groups": tablePlatformIntegrationGroups(ctx),
27-
"platform_api_benchmark_summary": tablePlatformApiBenchmarkSummary(ctx),
28-
"platform_api_benchmark_controls": tablePlatformApiBenchmarkControls(ctx),
29-
"platform_artifact_vulnerabilities": tablePlatformArtifactVulnerabilities(ctx),
30-
"platform_artifact_sbom": tablePlatformArtifactSboms(ctx),
31-
"platform_package_vulnerabilities": tablePlatformPackageVulnerabilities(ctx),
32-
"platform_osv_vulnerability_details": tablePlatformOsvVulnerabilityDetails(ctx),
22+
"platform_findings": tablePlatformFindings(ctx),
23+
"platform_resources": tablePlatformResources(ctx),
24+
"platform_lookup": tablePlatformLookup(ctx),
25+
"platform_integrations": tablePlatformConnections(ctx),
26+
"platform_integration_groups": tablePlatformIntegrationGroups(ctx),
27+
"platform_api_benchmark_summary": tablePlatformApiBenchmarkSummary(ctx),
28+
"platform_api_benchmark_controls": tablePlatformApiBenchmarkControls(ctx),
29+
"platform_artifact_vulnerabilities": tablePlatformArtifactVulnerabilities(ctx),
30+
"platform_artifact_sbom": tablePlatformArtifactSboms(ctx),
31+
"packages_with_vulnerabilities": tablePlatformPackageVulnerabilities(ctx),
32+
"osv_vulnerability_details": tablePlatformOsvVulnerabilityDetails(ctx),
33+
"artifact_package_list": tableArtifactPackageList(ctx),
3334
},
3435
}
3536

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package opengovernance
2+
3+
import (
4+
"context"
5+
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"
6+
7+
og_client "github.com/opengovern/opensecurity/pkg/cloudql/client"
8+
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
9+
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
10+
)
11+
12+
func tableArtifactPackageList(_ context.Context) *plugin.Table {
13+
return &plugin.Table{
14+
Name: "artifact_package_list",
15+
Description: "Platform Artifact SBOMs",
16+
Cache: &plugin.TableCacheOptions{
17+
Enabled: false,
18+
},
19+
List: &plugin.ListConfig{
20+
Hydrate: og_client.ListArtifactPackageList,
21+
},
22+
Columns: []*plugin.Column{
23+
{
24+
Name: "image_url",
25+
Transform: transform.FromField("Description.ImageURL"),
26+
Type: proto.ColumnType_STRING,
27+
},
28+
{
29+
Name: "artifact_id",
30+
Transform: transform.FromField("Description.ArtifactID"),
31+
Type: proto.ColumnType_STRING,
32+
},
33+
{
34+
Name: "packages",
35+
Transform: transform.FromField("Description.Packages"),
36+
Type: proto.ColumnType_JSON,
37+
},
38+
{
39+
Name: "platform_description",
40+
Type: proto.ColumnType_JSON,
41+
Description: "The full model description of the resource",
42+
Transform: transform.FromField("Description").Transform(marshalJSON),
43+
},
44+
},
45+
}
46+
}

pkg/cloudql/tables/table_platform_osv_vulnerabilities_details.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
func tablePlatformOsvVulnerabilityDetails(_ context.Context) *plugin.Table {
1313
return &plugin.Table{
14-
Name: "platform_osv_vulnerability_details",
14+
Name: "osv_vulnerability_details",
1515
Description: "Provides detailed information about OSV vulnerabilities.",
1616
List: &plugin.ListConfig{
1717
Hydrate: og_client.ListOsvVulnerabilityDetail,

pkg/cloudql/tables/table_platform_package_vulnerabilities.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
func tablePlatformPackageVulnerabilities(_ context.Context) *plugin.Table {
1313
return &plugin.Table{
14-
Name: "platform_package_vulnerabilities",
14+
Name: "packages_with_vulnerabilities",
1515
Description: "Platform Package Vulnerabilities",
1616
Cache: &plugin.TableCacheOptions{
1717
Enabled: false,

0 commit comments

Comments
 (0)