Skip to content

Commit edbb58d

Browse files
authored
feat: add key type column with stats (#13)
1 parent 85f506a commit edbb58d

File tree

2 files changed

+93
-26
lines changed

2 files changed

+93
-26
lines changed

.github/workflows/cd.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
package-name: audit-org-keys
1717
- uses: actions/checkout@v2
1818
- name: get and set semver
19-
run: echo "::set-env name=SEMVER::$(echo version.txt)"
19+
run: echo "::set-env name=SEMVER::$(cat version.txt)"
2020
- name: login into github package registry
2121
run: docker login "docker.pkg.github.com" -u "$GITHUB_ACTOR" -p "${{ secrets.GITHUB_TOKEN }}"
2222
- name: build nightly docker image

main.go

Lines changed: 92 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,19 @@ func printReport(ms []member) {
2828
var wg sync.WaitGroup
2929

3030
var (
31-
withSize uint32
32-
withoutSize uint32
33-
multipleSize uint32
31+
keyDsaSize uint32
32+
keyEddsaSize uint32
33+
keyEd25519Size uint32
34+
keyRsaSize uint32
35+
userDsaSize uint32
36+
userEddsaSize uint32
37+
userEd25519Size uint32
38+
userRsaSize uint32
39+
userWithKeySize uint32
40+
userWithoutKeySize uint32
41+
userWithMultipleKeySize uint32
42+
totalKeySize uint32
43+
totalUserSize = len(ms)
3444
)
3545

3646
for _, m := range ms {
@@ -39,19 +49,57 @@ func printReport(ms []member) {
3949

4050
go func() {
4151
defer wg.Done()
42-
if len(m.Keys) != 0 {
43-
atomic.AddUint32(&withSize, 1)
44-
if *showUsers == "with" || *showUsers == "all" {
52+
53+
var (
54+
hasDsa bool
55+
hasRsa bool
56+
hasEddsa bool
57+
hasEd25519 bool
58+
)
59+
60+
for _, key := range m.Keys {
61+
atomic.AddUint32(&totalKeySize, 1)
62+
63+
switch {
64+
case strings.Contains(key, "ssh-dsa"):
65+
atomic.AddUint32(&keyDsaSize, 1)
66+
hasDsa = true
67+
case strings.Contains(key, "ssh-rsa"):
68+
atomic.AddUint32(&keyRsaSize, 1)
69+
hasRsa = true
70+
case strings.Contains(key, "ssh-eddsa"):
71+
atomic.AddUint32(&keyEddsaSize, 1)
72+
hasEddsa = true
73+
case strings.Contains(key, "ssh-ed25519"):
74+
atomic.AddUint32(&keyEd25519Size, 1)
75+
hasEd25519 = true
76+
}
77+
}
78+
79+
switch {
80+
case hasDsa:
81+
atomic.AddUint32(&userDsaSize, 1)
82+
case hasRsa:
83+
atomic.AddUint32(&userRsaSize, 1)
84+
case hasEddsa:
85+
atomic.AddUint32(&userEddsaSize, 1)
86+
case hasEd25519:
87+
atomic.AddUint32(&userEd25519Size, 1)
88+
}
89+
90+
if len(m.Keys) == 0 {
91+
atomic.AddUint32(&userWithoutKeySize, 1)
92+
if *showUsers == "without" || *showUsers == "all" {
4593
zap.S().Infow("retrieved keys",
4694
"user", m.Login,
4795
"keys", m.Keys,
4896
)
4997
}
5098
}
5199

52-
if len(m.Keys) == 0 {
53-
atomic.AddUint32(&withoutSize, 1)
54-
if *showUsers == "without" || *showUsers == "all" {
100+
if len(m.Keys) > 0 {
101+
atomic.AddUint32(&userWithKeySize, 1)
102+
if *showUsers == "with" || *showUsers == "all" {
55103
zap.S().Infow("retrieved keys",
56104
"user", m.Login,
57105
"keys", m.Keys,
@@ -60,39 +108,58 @@ func printReport(ms []member) {
60108
}
61109

62110
if len(m.Keys) > 1 {
63-
atomic.AddUint32(&multipleSize, 1)
111+
atomic.AddUint32(&userWithMultipleKeySize, 1)
64112
if *showUsers == "multiple" || *showUsers == "all" {
65113
zap.S().Infow("retrieved keys",
66114
"user", m.Login,
67115
"keys", m.Keys,
68116
)
69117
}
70118
}
71-
// todo strong and weak keys
72119
}()
73120
}
74121
wg.Wait()
75122

76-
d := [][]string{
77-
{"users with keys", fmt.Sprintf("%d (%.2f%%)", withSize,
78-
float32(withSize)/float32(len(ms)) * 100)},
79-
{"users without keys", fmt.Sprintf("%d (%.2f%%)", withoutSize,
80-
float32(withoutSize)/float32(len(ms)) * 100)},
81-
{"users with multiple keys", fmt.Sprintf("%d (%.2f%%)", multipleSize,
82-
float32(multipleSize)/float32(len(ms)) * 100)},
83-
// todo: calculate bit length of keys
84-
//{"users with strong keys", fmt.Sprintf("%d", 0)},
85-
//{"users with weak keys", fmt.Sprintf("%d", 0)},
123+
withKey := [][]string{
124+
{"users with keys", "DSA",
125+
fmt.Sprintf("%d (%.2f%%)", keyDsaSize, float32(keyDsaSize)/float32(totalKeySize)*100),
126+
fmt.Sprintf("%d (%.2f%%)", userDsaSize, float32(userDsaSize)/float32(totalUserSize)*100)},
127+
{"", "RSA",
128+
fmt.Sprintf("%d (%.2f%%)", keyRsaSize, float32(keyRsaSize)/float32(totalKeySize)*100),
129+
fmt.Sprintf("%d (%.2f%%)", userRsaSize, float32(userRsaSize)/float32(totalUserSize)*100)},
130+
{"", "ECDSA",
131+
fmt.Sprintf("%d (%.2f%%)", keyEddsaSize, float32(keyEddsaSize)/float32(totalKeySize)*100),
132+
fmt.Sprintf("%d (%.2f%%)", userEddsaSize, float32(userEddsaSize)/float32(totalUserSize)*100)},
133+
{"", "Ed25519",
134+
fmt.Sprintf("%d (%.2f%%)", keyEd25519Size, float32(keyEd25519Size)/float32(totalKeySize)*100),
135+
fmt.Sprintf("%d (%.2f%%)", userEd25519Size, float32(userEd25519Size)/float32(totalUserSize)*100)},
136+
}
137+
138+
withoutKey := [][]string{
139+
{"users without keys", "", "", fmt.Sprintf("%d (%.2f%%)", userWithoutKeySize, float32(userWithoutKeySize)/float32(totalUserSize)*100)},
140+
}
141+
142+
withMultipleKey := [][]string{
143+
{"users with multiple keys", "", "", fmt.Sprintf("%d (%.2f%%)", userWithMultipleKeySize, float32(userWithMultipleKeySize)/float32(totalUserSize)*100)},
86144
}
87145

88146
t := tablewriter.NewWriter(os.Stdout)
89-
t.SetHeader([]string{"description", "# of users"})
147+
t.SetHeader([]string{"description", "key type", "# of keys", "# of users"})
90148
t.SetHeaderColor(tablewriter.Colors{tablewriter.FgCyanColor},
91149
tablewriter.Colors{tablewriter.FgCyanColor},
150+
tablewriter.Colors{tablewriter.FgCyanColor},
151+
tablewriter.Colors{tablewriter.FgCyanColor},
92152
)
93-
t.SetFooter([]string{"total users", fmt.Sprintf("%d", len(ms))})
94-
95-
t.AppendBulk(d)
153+
t.SetFooter([]string{"", "total", fmt.Sprintf("%d", totalKeySize), fmt.Sprintf("%d", totalUserSize)})
154+
t.SetFooterColor(tablewriter.Colors{tablewriter.FgCyanColor},
155+
tablewriter.Colors{tablewriter.FgCyanColor},
156+
tablewriter.Colors{tablewriter.FgCyanColor},
157+
tablewriter.Colors{tablewriter.FgCyanColor},
158+
)
159+
t.SetRowLine(true)
160+
t.AppendBulk(withKey)
161+
t.AppendBulk(withoutKey)
162+
t.AppendBulk(withMultipleKey)
96163
t.Render()
97164
}
98165

0 commit comments

Comments
 (0)