Skip to content

Commit

Permalink
Create the codes json file during docs build
Browse files Browse the repository at this point in the history
this will be downloaded by the vscode plugin for ignore resolution
  • Loading branch information
Owen Rumney committed Jul 27, 2021
1 parent ff1bf69 commit ed3bc5c
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 7 deletions.
79 changes: 79 additions & 0 deletions cmd/tfsec-docs/extension_codes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package main

import (
"encoding/json"
"fmt"
"os"
)

const extensionJsonTemplate = `
{
"checks": [
{{ range c := .Checks }}
{
"code": "{{ c.ID }}",
"legacy_code": "{{ c.LegacyID }}",
"service": "{{ c.Service }}",
"provider": "{{ c.Provider }}",
"description": "{{ c.Documentation.Summary }}",
"impact": "{{ c.Documentation.Impact }}",
"resolution": "{{ c.Documentation.Resolution }}",
"doc_url": "https://tfsec.dev/docs/aws/{{ c.Service }}/{{ c.ShortCode }}
}
{{ end }}
]
}
`

type checkBlock struct {
Code string `json:"code"`
LegacyCode string `json:"legacy_code"`
Service string `json:"service"`
Provider string `json:"provider"`
Description string `json:"description"`
Impact string `json:"impact"`
Resolution string `json:"resolution"`
DocUrl string `json:"doc_url"`
}

type checksBlock struct {
Checks []checkBlock `json:"checks"`
}

func generateExtensionCodeFile(registeredChecks []*FileContent) error {
var blocks []checkBlock

for _, c := range registeredChecks {
for _, check := range c.Checks {
blocks = append(blocks, checkBlock{
Code: check.ID(),
LegacyCode: check.LegacyID,
Service: check.Service,
Provider: string(check.Provider),
Description: check.Documentation.Summary,
Impact: check.Documentation.Impact,
Resolution: check.Documentation.Resolution,
DocUrl: fmt.Sprintf("https://tfsec.dev/docs/%s/%s/%s/", check.Provider, check.Service, check.ShortCode),
})

}
}

root := checksBlock{
Checks: blocks,
}

file, err := os.Create("checkdocs/codes.json")
if err != nil {
panic(err)
}

out, err := json.MarshalIndent(root, "", " ")
if err != nil {
panic(err)
}

_, err = file.Write(out)

return err
}
4 changes: 4 additions & 0 deletions cmd/tfsec-docs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ var rootCmd = &cobra.Command{
return err
}

if err := generateExtensionCodeFile(fileContents); err != nil {
return err
}

return generateWebPages(fileContents)
},
}
Expand Down
16 changes: 9 additions & 7 deletions example/custom/custom_check.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ resource "aws_instance" "non_compliant" {
}

resource "aws_instance" "compliant" {
ami = "ami-12345"
instance_type = "t2.small"
ami = "ami-12345"
instance_type = "t2.small"
cpu_core_count = 4

tags = {
Expand All @@ -24,6 +24,7 @@ resource "aws_s3_bucket" "unversioned_bucket" {
acl = "private"
}

#tfsec:ignore:AWS017:exp:2021-01-01:ws:testworkspace
resource "aws_s3_bucket" "versioned_bucket" {
bucket = "my-tf-test-bucket"
acl = "private"
Expand All @@ -32,7 +33,7 @@ resource "aws_s3_bucket" "versioned_bucket" {
enabled = true
}
}

# tfsec:ignore:AWS017
resource "aws_s3_bucket" "disabled_versioned_bucket" {
bucket = "my-tf-test-bucket"
acl = "private"
Expand All @@ -48,11 +49,12 @@ module "custom_bucket" {
acl = "private"
}

#tfsec:ignore:aws-s3-enable-bucket-encryption
resource "aws_s3_bucket" "bucket_with_public_acl" {
bucket = "my-tf-test-bucket"
// acl = "public-read"
//
versioning {
// acl = "public-read"
//
versioning {
enabled = true
}
}
}
1 change: 1 addition & 0 deletions scripts/publish-docs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ clone_site
go run ./cmd/tfsec-docs
cp -r checkdocs/docs/* ./_site/_docs/
cp -r checkdocs/data/* ./_site/_data/
cp =r checkdocs/codes.json ./_site/assets/codes.json
deploy

rm -rf checkdocs

0 comments on commit ed3bc5c

Please sign in to comment.