This repository has been archived by the owner on Jun 17, 2019. It is now read-only.
/
report-csv.go
126 lines (109 loc) · 2.41 KB
/
report-csv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"encoding/csv"
"flag"
"fmt"
"github.com/threatspec/threatspec-go/threatspec"
"os"
"strconv"
)
func fatalIfError(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func getBoundaryName(boundary *threatspec.Boundary) string {
if boundary == nil {
return ""
} else {
return boundary.Name
}
}
func main() {
outFile := flag.String("out", "threatspec.csv", "output csv file")
flag.Parse()
var err error
ts, err := threatspec.LoadFiles(flag.Args())
fatalIfError(err)
csvFile, err := os.Create(*outFile)
fatalIfError(err)
defer csvFile.Close()
writer := csv.NewWriter(csvFile)
// CSV header
writer.Write([]string{
"boundary",
"component",
"threat",
"type",
"value",
"function",
"file",
"line",
})
for projectName, _ := range ts.Projects {
for _, ms := range ts.Projects[projectName].Mitigations {
for _, m := range ms {
err = writer.Write([]string{
getBoundaryName(ts.Boundaries[m.Boundary]),
ts.Components[m.Component].Name,
ts.Threats[m.Threat].Name,
"mitigation",
m.Mitigation,
m.Source.Function,
m.Source.File,
strconv.Itoa(m.Source.Line),
})
fatalIfError(err)
}
}
for _, es := range ts.Projects[projectName].Exposures {
for _, e := range es {
err = writer.Write([]string{
getBoundaryName(ts.Boundaries[e.Boundary]),
ts.Components[e.Component].Name,
ts.Threats[e.Threat].Name,
"exposure",
e.Exposure,
e.Source.Function,
e.Source.File,
strconv.Itoa(e.Source.Line),
})
fatalIfError(err)
}
}
for _, trs := range ts.Projects[projectName].Transfers {
for _, t := range trs {
err = writer.Write([]string{
getBoundaryName(ts.Boundaries[t.Boundary]),
ts.Components[t.Component].Name,
ts.Threats[t.Threat].Name,
"transfer",
t.Transfer,
t.Source.Function,
t.Source.File,
strconv.Itoa(t.Source.Line),
})
fatalIfError(err)
}
}
for _, as := range ts.Projects[projectName].Acceptances {
for _, a := range as {
err = writer.Write([]string{
getBoundaryName(ts.Boundaries[a.Boundary]),
ts.Components[a.Component].Name,
ts.Threats[a.Threat].Name,
"acceptance",
a.Acceptance,
a.Source.Function,
a.Source.File,
strconv.Itoa(a.Source.Line),
})
fatalIfError(err)
}
}
}
fmt.Printf("Writing report to %s\n", *outFile)
writer.Flush()
csvFile.Close()
}