Skip to content

Commit

Permalink
fix(misconf): add missing fields to proto (#4861)
Browse files Browse the repository at this point in the history
* fix(misconf): add missing fields to proto

* mark deleted fields as reserved
  • Loading branch information
nikpivkin authored Jul 30, 2023
1 parent 8b8e0e8 commit e1c2a8c
Show file tree
Hide file tree
Showing 7 changed files with 832 additions and 281 deletions.
2 changes: 1 addition & 1 deletion Dockerfile.protoc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.19
FROM --platform=linux/amd64 golang:1.19

# Install protoc (cf. http://google.github.io/proto-lens/installing-protoc.html)
ENV PROTOC_ZIP=protoc-3.19.4-linux-x86_64.zip
Expand Down
2 changes: 1 addition & 1 deletion magefiles/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func Protoc() error {
if err := sh.RunV("bash", "-c", "docker build -t trivy-protoc - < Dockerfile.protoc"); err != nil {
return err
}
return sh.Run("docker", "run", "--rm", "-it", "-v", "${PWD}:/app", "-w", "/app", "trivy-protoc", "mage", "protoc")
return sh.Run("docker", "run", "--rm", "-it", "--platform", "linux/x86_64", "-v", "${PWD}:/app", "-w", "/app", "trivy-protoc", "mage", "protoc")
}

// Yacc generates parser
Expand Down
2 changes: 1 addition & 1 deletion pkg/cache/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (c RemoteCache) PutArtifact(imageID string, artifactInfo types.ArtifactInfo
func (c RemoteCache) PutBlob(diffID string, blobInfo types.BlobInfo) error {
err := rpc.Retry(func() error {
var err error
_, err = c.client.PutBlob(c.ctx, rpc.ConvertToRPCBlobInfo(diffID, blobInfo))
_, err = c.client.PutBlob(c.ctx, rpc.ConvertToRPCPutBlobRequest(diffID, blobInfo))
return err
})
if err != nil {
Expand Down
140 changes: 98 additions & 42 deletions pkg/rpc/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,18 +217,21 @@ func ConvertToRPCMisconfs(misconfs []types.DetectedMisconfiguration) []*common.D
}

rpcMisconfs = append(rpcMisconfs, &common.DetectedMisconfiguration{
Type: m.Type,
Id: m.ID,
Title: m.Title,
Description: m.Description,
Message: m.Message,
Namespace: m.Namespace,
Resolution: m.Resolution,
Severity: common.Severity(severity),
PrimaryUrl: m.PrimaryURL,
References: m.References,
Status: string(m.Status),
Layer: ConvertToRPCLayer(m.Layer),
Type: m.Type,
Id: m.ID,
AvdId: m.AVDID,
Title: m.Title,
Description: m.Description,
Message: m.Message,
Namespace: m.Namespace,
Query: m.Query,
Resolution: m.Resolution,
Severity: common.Severity(severity),
PrimaryUrl: m.PrimaryURL,
References: m.References,
Status: string(m.Status),
Layer: ConvertToRPCLayer(m.Layer),
CauseMetadata: ConvertToRPCCauseMetadata(m.CauseMetadata),
})
}
return rpcMisconfs
Expand All @@ -243,6 +246,30 @@ func ConvertToRPCLayer(layer ftypes.Layer) *common.Layer {
}
}

func ConvertToRPCPolicyMetadata(policy ftypes.PolicyMetadata) *common.PolicyMetadata {
return &common.PolicyMetadata{
Id: policy.ID,
AdvId: policy.AVDID,
Type: policy.Type,
Title: policy.Title,
Description: policy.Description,
Severity: policy.Severity,
RecommendedActions: policy.RecommendedActions,
References: policy.References,
}
}

func ConvertToRPCCauseMetadata(cause ftypes.CauseMetadata) *common.CauseMetadata {
return &common.CauseMetadata{
Resource: cause.Resource,
Provider: cause.Provider,
Service: cause.Service,
StartLine: int32(cause.StartLine),
EndLine: int32(cause.EndLine),
Code: ConvertToRPCCode(cause.Code),
}
}

// ConvertToRPCDataSource returns common.DataSource
func ConvertToRPCDataSource(ds *dbTypes.DataSource) *common.DataSource {
if ds == nil {
Expand Down Expand Up @@ -407,18 +434,21 @@ func ConvertFromRPCMisconfs(rpcMisconfs []*common.DetectedMisconfiguration) []ty
var misconfs []types.DetectedMisconfiguration
for _, rpcMisconf := range rpcMisconfs {
misconfs = append(misconfs, types.DetectedMisconfiguration{
Type: rpcMisconf.Type,
ID: rpcMisconf.Id,
Title: rpcMisconf.Title,
Description: rpcMisconf.Description,
Message: rpcMisconf.Message,
Namespace: rpcMisconf.Namespace,
Resolution: rpcMisconf.Resolution,
Severity: rpcMisconf.Severity.String(),
PrimaryURL: rpcMisconf.PrimaryUrl,
References: rpcMisconf.References,
Status: types.MisconfStatus(rpcMisconf.Status),
Layer: ConvertFromRPCLayer(rpcMisconf.Layer),
Type: rpcMisconf.Type,
ID: rpcMisconf.Id,
AVDID: rpcMisconf.AvdId,
Title: rpcMisconf.Title,
Description: rpcMisconf.Description,
Message: rpcMisconf.Message,
Namespace: rpcMisconf.Namespace,
Query: rpcMisconf.Query,
Resolution: rpcMisconf.Resolution,
Severity: rpcMisconf.Severity.String(),
PrimaryURL: rpcMisconf.PrimaryUrl,
References: rpcMisconf.References,
Status: types.MisconfStatus(rpcMisconf.Status),
Layer: ConvertFromRPCLayer(rpcMisconf.Layer),
CauseMetadata: ConvertFromRPCCauseMetadata(rpcMisconf.CauseMetadata),
})
}
return misconfs
Expand All @@ -430,8 +460,40 @@ func ConvertFromRPCLayer(rpcLayer *common.Layer) ftypes.Layer {
return ftypes.Layer{}
}
return ftypes.Layer{
Digest: rpcLayer.Digest,
DiffID: rpcLayer.DiffId,
Digest: rpcLayer.Digest,
DiffID: rpcLayer.DiffId,
CreatedBy: rpcLayer.CreatedBy,
}
}

func ConvertFromRPCPolicyMetadata(rpcPolicy *common.PolicyMetadata) ftypes.PolicyMetadata {
if rpcPolicy == nil {
return ftypes.PolicyMetadata{}
}

return ftypes.PolicyMetadata{
ID: rpcPolicy.Id,
AVDID: rpcPolicy.AdvId,
Type: rpcPolicy.Type,
Title: rpcPolicy.Title,
Description: rpcPolicy.Description,
Severity: rpcPolicy.Severity,
RecommendedActions: rpcPolicy.RecommendedActions,
References: rpcPolicy.References,
}
}

func ConvertFromRPCCauseMetadata(rpcCause *common.CauseMetadata) ftypes.CauseMetadata {
if rpcCause == nil {
return ftypes.CauseMetadata{}
}
return ftypes.CauseMetadata{
Resource: rpcCause.Resource,
Provider: rpcCause.Provider,
Service: rpcCause.Service,
StartLine: int(rpcCause.StartLine),
EndLine: int(rpcCause.EndLine),
Code: ConvertFromRPCCode(rpcCause.Code),
}
}

Expand Down Expand Up @@ -518,14 +580,10 @@ func ConvertFromRPCMisconfResults(rpcResults []*common.MisconfResult) []ftypes.M
var results []ftypes.MisconfResult
for _, r := range rpcResults {
results = append(results, ftypes.MisconfResult{
Namespace: r.Namespace,
Message: r.Message,
PolicyMetadata: ftypes.PolicyMetadata{
ID: r.Id,
Type: r.Type,
Title: r.Title,
Severity: r.Severity,
},
Namespace: r.Namespace,
Message: r.Message,
PolicyMetadata: ConvertFromRPCPolicyMetadata(r.PolicyMetadata),
CauseMetadata: ConvertFromRPCCauseMetadata(r.CauseMetadata),
})
}
return results
Expand Down Expand Up @@ -603,8 +661,8 @@ func ConvertToRPCArtifactInfo(imageID string, imageInfo ftypes.ArtifactInfo) *ca
}
}

// ConvertToRPCBlobInfo returns PutBlobRequest
func ConvertToRPCBlobInfo(diffID string, blobInfo ftypes.BlobInfo) *cache.PutBlobRequest {
// ConvertToRPCPutBlobRequest returns PutBlobRequest
func ConvertToRPCPutBlobRequest(diffID string, blobInfo ftypes.BlobInfo) *cache.PutBlobRequest {
var packageInfos []*common.PackageInfo
for _, pkgInfo := range blobInfo.PackageInfos {
packageInfos = append(packageInfos, &common.PackageInfo{
Expand Down Expand Up @@ -677,12 +735,10 @@ func ConvertToMisconfResults(results []ftypes.MisconfResult) []*common.MisconfRe
var rpcResults []*common.MisconfResult
for _, r := range results {
rpcResults = append(rpcResults, &common.MisconfResult{
Namespace: r.Namespace,
Message: r.Message,
Id: r.ID,
Type: r.Type,
Title: r.Title,
Severity: r.Severity,
Namespace: r.Namespace,
Message: r.Message,
PolicyMetadata: ConvertToRPCPolicyMetadata(r.PolicyMetadata),
CauseMetadata: ConvertToRPCCauseMetadata(r.CauseMetadata),
})
}
return rpcResults
Expand Down
Loading

0 comments on commit e1c2a8c

Please sign in to comment.