-
Notifications
You must be signed in to change notification settings - Fork 936
chore: autogenerate audit log documentation #5862
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
51c7aee
added script for table creation
Kira-Pilot 0facafe
added tags to audit-logs.md
Kira-Pilot 963552a
removed log
Kira-Pilot e1a9fbb
removed empty block line
Kira-Pilot f2b4595
PR feedback
Kira-Pilot bcd67a2
modify check_unstaged
Kira-Pilot dfe78c3
third times the charm maybe
Kira-Pilot d9a1ab0
spelling
Kira-Pilot 1906daa
relative path
Kira-Pilot a6891dc
excluding from the right script this time
Kira-Pilot be44d6c
sorted resources to ensure table order
Kira-Pilot f96cbe3
running make cmd
Kira-Pilot 0e1903d
running make again
Kira-Pilot f3ec8f5
ensuring order on subtable
Kira-Pilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"flag" | ||
"log" | ||
"os" | ||
"sort" | ||
"strconv" | ||
"strings" | ||
|
||
"golang.org/x/xerrors" | ||
|
||
"github.com/coder/coder/enterprise/audit" | ||
) | ||
|
||
var ( | ||
auditDocFile string | ||
dryRun bool | ||
|
||
generatorPrefix = []byte("<!-- Code generated by 'make docs/admin/audit-logs.md'. DO NOT EDIT -->") | ||
generatorSuffix = []byte("<!-- End generated by 'make docs/admin/audit-logs.md'. -->") | ||
) | ||
|
||
/* | ||
* | ||
AuditableResourcesMap is derived from audit.AuditableResources | ||
and has the following structure: | ||
|
||
{ | ||
friendlyResourceName: { | ||
fieldName1: isTracked, | ||
fieldName2: isTracked, | ||
... | ||
}, | ||
... | ||
} | ||
*/ | ||
type AuditableResourcesMap map[string]map[string]bool | ||
Kira-Pilot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
func main() { | ||
flag.StringVar(&auditDocFile, "audit-doc-file", "docs/admin/audit-logs.md", "Path to audit log doc file") | ||
flag.BoolVar(&dryRun, "dry-run", false, "Dry run") | ||
flag.Parse() | ||
|
||
auditableResourcesMap := readAuditableResources() | ||
|
||
doc, err := readAuditDoc() | ||
if err != nil { | ||
log.Fatal("can't read audit doc: ", err) | ||
} | ||
|
||
doc, err = updateAuditDoc(doc, auditableResourcesMap) | ||
if err != nil { | ||
log.Fatal("can't update audit doc: ", err) | ||
} | ||
|
||
if dryRun { | ||
log.Println(string(doc)) | ||
return | ||
} | ||
|
||
err = writeAuditDoc(doc) | ||
if err != nil { | ||
log.Fatal("can't write updated audit doc: ", err) | ||
} | ||
} | ||
|
||
// Transforms audit.AuditableResources to AuditableResourcesMap, | ||
// which uses friendlier language. | ||
func readAuditableResources() AuditableResourcesMap { | ||
auditableResourcesMap := make(AuditableResourcesMap) | ||
|
||
for resourceName, resourceFields := range audit.AuditableResources { | ||
friendlyResourceName := strings.Split(resourceName, ".")[2] | ||
fieldNameMap := make(map[string]bool) | ||
for fieldName, action := range resourceFields { | ||
fieldNameMap[fieldName] = action != audit.ActionIgnore | ||
auditableResourcesMap[friendlyResourceName] = fieldNameMap | ||
} | ||
} | ||
|
||
return auditableResourcesMap | ||
} | ||
|
||
func readAuditDoc() ([]byte, error) { | ||
Kira-Pilot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
doc, err := os.ReadFile(auditDocFile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return doc, nil | ||
} | ||
|
||
// Writes a markdown table of audit log resources to a buffer | ||
func updateAuditDoc(doc []byte, auditableResourcesMap AuditableResourcesMap) ([]byte, error) { | ||
// We must sort the resources to ensure table ordering | ||
sortedResourceNames := sortKeys(auditableResourcesMap) | ||
|
||
i := bytes.Index(doc, generatorPrefix) | ||
if i < 0 { | ||
return nil, xerrors.New("generator prefix tag not found") | ||
} | ||
tableStartIndex := i + len(generatorPrefix) + 1 | ||
|
||
j := bytes.Index(doc[tableStartIndex:], generatorSuffix) | ||
if j < 0 { | ||
return nil, xerrors.New("generator suffix tag not found") | ||
} | ||
tableEndIndex := tableStartIndex + j | ||
|
||
var buffer bytes.Buffer | ||
buffer.Write(doc[:tableStartIndex]) | ||
buffer.WriteByte('\n') | ||
|
||
buffer.WriteString("|<b>Resource<b>||\n") | ||
buffer.WriteString("|--|-----------------|\n") | ||
|
||
for _, resourceName := range sortedResourceNames { | ||
buffer.WriteString("|" + resourceName + "|<table><thead><tr><th>Field</th><th>Tracked</th></tr></thead><tbody>") | ||
|
||
// We must sort the field names to ensure sub-table ordering | ||
sortedFieldNames := sortKeys(auditableResourcesMap[resourceName]) | ||
|
||
for _, fieldName := range sortedFieldNames { | ||
isTracked := auditableResourcesMap[resourceName][fieldName] | ||
buffer.WriteString("<tr><td>" + fieldName + "</td><td>" + strconv.FormatBool(isTracked) + "</td></tr>") | ||
} | ||
|
||
buffer.WriteString("</tbody></table>\n") | ||
} | ||
|
||
buffer.WriteString("\n") | ||
buffer.Write(doc[tableEndIndex:]) | ||
return buffer.Bytes(), nil | ||
} | ||
|
||
func writeAuditDoc(doc []byte) error { | ||
// G306: Expect WriteFile permissions to be 0600 or less | ||
/* #nosec G306 */ | ||
return os.WriteFile(auditDocFile, doc, 0644) | ||
} | ||
|
||
func sortKeys[T any](stringMap map[string]T) []string { | ||
var keyNames []string | ||
for key := range stringMap { | ||
keyNames = append(keyNames, key) | ||
} | ||
sort.Strings(keyNames) | ||
return keyNames | ||
} |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ source "$(dirname "${BASH_SOURCE[0]}")/lib.sh" | |
cdroot | ||
|
||
set +e | ||
find . -regex ".*\.go" | grep -v "./enterprise" | xargs grep -n "github.com/coder/coder/enterprise" | ||
find . -regex ".*\.go" | grep -v "./enterprise" | grep -v "./scripts/auditdocgen/main.go" | xargs grep -n "github.com/coder/coder/enterprise" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea! I'll add this change in this PR. |
||
# reverse the exit code because we want this script to fail if grep finds anything. | ||
status=$? | ||
set -e | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.