2
2
package mailgun
3
3
4
4
import (
5
- "encoding/json"
6
5
"errors"
7
- "fmt"
8
- "net/http"
9
6
"os"
10
7
"strconv"
11
8
@@ -22,6 +19,15 @@ type Analyzer struct {
22
19
Cfg * config.Config
23
20
}
24
21
22
+ type SecretInfo struct {
23
+ ID string // key id
24
+ UserName string
25
+ Type string // type of key
26
+ Role string // key role
27
+ ExpiresAt string // key expiry time if any
28
+ Domains []Domain
29
+ }
30
+
25
31
func (Analyzer ) Type () analyzers.AnalyzerType { return analyzers .AnalyzerTypeMailgun }
26
32
27
33
func (a Analyzer ) Analyze (_ context.Context , credInfo map [string ]string ) (* analyzers.AnalyzerResult , error ) {
@@ -34,19 +40,20 @@ func (a Analyzer) Analyze(_ context.Context, credInfo map[string]string) (*analy
34
40
if err != nil {
35
41
return nil , err
36
42
}
43
+
37
44
return secretInfoToAnalyzerResult (info ), nil
38
45
}
39
46
40
- func secretInfoToAnalyzerResult (info * DomainsJSON ) * analyzers.AnalyzerResult {
47
+ func secretInfoToAnalyzerResult (info * SecretInfo ) * analyzers.AnalyzerResult {
41
48
if info == nil {
42
49
return nil
43
50
}
44
51
result := analyzers.AnalyzerResult {
45
52
AnalyzerType : analyzers .AnalyzerTypeMailgun ,
46
- Bindings : make ([]analyzers.Binding , len (info .Items )),
53
+ Bindings : make ([]analyzers.Binding , len (info .Domains )),
47
54
}
48
55
49
- for idx , domain := range info .Items {
56
+ for idx , domain := range info .Domains {
50
57
result .Bindings [idx ] = analyzers.Binding {
51
58
Resource : analyzers.Resource {
52
59
Name : domain .URL ,
@@ -59,6 +66,7 @@ func secretInfoToAnalyzerResult(info *DomainsJSON) *analyzers.AnalyzerResult {
59
66
"is_disabled" : domain .IsDisabled ,
60
67
},
61
68
},
69
+
62
70
Permission : analyzers.Permission {
63
71
Value : PermissionStrings [FullAccess ],
64
72
},
@@ -67,87 +75,60 @@ func secretInfoToAnalyzerResult(info *DomainsJSON) *analyzers.AnalyzerResult {
67
75
return & result
68
76
}
69
77
70
- type Domain struct {
71
- ID string `json:"id"`
72
- URL string `json:"name"`
73
- IsDisabled bool `json:"is_disabled"`
74
- Type string `json:"type"`
75
- State string `json:"state"`
76
- CreatedAt string `json:"created_at"`
77
- }
78
-
79
- type DomainsJSON struct {
80
- Items []Domain `json:"items"`
81
- TotalCount int `json:"total_count"`
82
- }
83
-
84
- func getDomains (cfg * config.Config , apiKey string ) (DomainsJSON , int , error ) {
85
- var domainsJSON DomainsJSON
86
-
87
- client := analyzers .NewAnalyzeClient (cfg )
88
- req , err := http .NewRequest ("GET" , "https://api.mailgun.net/v4/domains" , nil )
78
+ func AnalyzeAndPrintPermissions (cfg * config.Config , apiKey string ) {
79
+ info , err := AnalyzePermissions (cfg , apiKey )
89
80
if err != nil {
90
- return domainsJSON , - 1 , err
81
+ color .Red ("[x] %s" , err .Error ())
82
+ return
91
83
}
92
84
93
- req . SetBasicAuth ( "api" , apiKey )
94
- resp , err := client . Do ( req )
95
- if err != nil {
96
- return domainsJSON , - 1 , err
97
- }
85
+ color . Green ( "[i] Valid Mailgun API key \n \n " )
86
+ printKeyInfo ( info )
87
+ printDomains ( info . Domains )
88
+ color . Yellow ( "[i] Permissions: Full Access \n \n " )
89
+ }
98
90
99
- if resp .StatusCode != 200 {
100
- return domainsJSON , resp .StatusCode , nil
101
- }
91
+ func AnalyzePermissions (cfg * config.Config , apiKey string ) (* SecretInfo , error ) {
92
+ var secretInfo SecretInfo
102
93
103
- defer resp . Body . Close ( )
94
+ var client = analyzers . NewAnalyzeClient ( cfg )
104
95
105
- err = json .NewDecoder (resp .Body ).Decode (& domainsJSON )
106
- if err != nil {
107
- return domainsJSON , resp .StatusCode , err
96
+ if err := getDomains (client , apiKey , & secretInfo ); err != nil {
97
+ return & secretInfo , err
108
98
}
109
- return domainsJSON , resp .StatusCode , nil
110
- }
111
99
112
- func AnalyzeAndPrintPermissions (cfg * config.Config , apiKey string ) {
113
- data , err := AnalyzePermissions (cfg , apiKey )
114
- if err != nil {
115
- color .Red ("[x] %s" , err .Error ())
116
- return
100
+ if err := getKeys (client , apiKey , & secretInfo ); err != nil {
101
+ return & secretInfo , err
117
102
}
118
103
119
- printMetadata ( data )
104
+ return & secretInfo , nil
120
105
}
121
106
122
- func AnalyzePermissions (cfg * config.Config , apiKey string ) (* DomainsJSON , error ) {
123
- // Get the domains associated with the API key
124
- domains , statusCode , err := getDomains (cfg , apiKey )
125
- if err != nil {
126
- return nil , fmt .Errorf ("Error getting domains: %s" , err )
107
+ func printKeyInfo (info * SecretInfo ) {
108
+ if info .ID == "" {
109
+ color .Red ("[i] Key information not found" )
110
+ return
127
111
}
128
112
129
- if statusCode != 200 {
130
- return nil , fmt .Errorf ("Invalid Mailgun API key." )
131
- }
132
- color .Green ("[i] Valid Mailgun API key\n \n " )
133
- color .Green ("[i] Permissions: Full Access\n \n " )
134
-
135
- return & domains , nil
113
+ t := table .NewWriter ()
114
+ t .SetOutputMirror (os .Stdout )
115
+ t .AppendHeader (table.Row {"Key ID" , "UserName/Requester" , "Key Type" , "Expires At" , "Role" })
116
+ t .AppendRow (table.Row {info .ID , info .UserName , info .Type , info .ExpiresAt , info .Role })
117
+ t .Render ()
136
118
}
137
-
138
- func printMetadata (domains * DomainsJSON ) {
139
- if domains .TotalCount == 0 {
119
+ func printDomains (domains []Domain ) {
120
+ if len (domains ) == 0 {
140
121
color .Red ("[i] No domains found" )
141
122
return
142
123
}
143
- color .Yellow ("[i] Found %d domain(s)" , domains .TotalCount )
124
+
125
+ color .Yellow ("[i] Found %d domain(s)" , len (domains ))
144
126
145
127
t := table .NewWriter ()
146
128
t .SetOutputMirror (os .Stdout )
147
129
t .AppendHeader (table.Row {"Domain" , "Type" , "State" , "Created At" , "Disabled" })
148
130
149
- for _ , domain := range domains .Items {
150
-
131
+ for _ , domain := range domains {
151
132
var colorFunc func (format string , a ... interface {}) string
152
133
switch {
153
134
case domain .IsDisabled :
@@ -166,5 +147,6 @@ func printMetadata(domains *DomainsJSON) {
166
147
colorFunc (strconv .FormatBool (domain .IsDisabled )),
167
148
})
168
149
}
150
+
169
151
t .Render ()
170
152
}
0 commit comments