@@ -4,6 +4,7 @@ package anthropic
4
4
import (
5
5
"errors"
6
6
"os"
7
+ "strings"
7
8
8
9
"github.com/fatih/color"
9
10
"github.com/jedib0t/go-pretty/v6/table"
@@ -17,7 +18,8 @@ var _ analyzers.Analyzer = (*Analyzer)(nil)
17
18
18
19
const (
19
20
// Key Types
20
- APIKey = "API-Key"
21
+ APIKey = "API-Key"
22
+ AdminKey = "Admin-Key"
21
23
)
22
24
23
25
type Analyzer struct {
@@ -28,7 +30,6 @@ type Analyzer struct {
28
30
type SecretInfo struct {
29
31
Valid bool
30
32
Type string // key type - TODO: Handle Anthropic Admin Keys
31
- Reference string
32
33
AnthropicResources []AnthropicResource
33
34
Permissions string // always full_access
34
35
Misc map [string ]string
@@ -39,6 +40,7 @@ type AnthropicResource struct {
39
40
ID string
40
41
Name string
41
42
Type string
43
+ Parent * AnthropicResource
42
44
Metadata map [string ]string
43
45
}
44
46
@@ -73,7 +75,7 @@ func AnalyzeAndPrintPermissions(cfg *config.Config, key string) {
73
75
}
74
76
75
77
if info .Valid {
76
- color .Green ("[!] Valid Anthropic API key \n \n " )
78
+ color .Green ("[!] Valid Anthropic %s \n \n " , info . Type )
77
79
// no user information
78
80
// print full access permission
79
81
printPermission (info .Permissions )
@@ -88,16 +90,23 @@ func AnalyzePermissions(cfg *config.Config, key string) (*SecretInfo, error) {
88
90
// create a HTTP client
89
91
client := analyzers .NewAnalyzeClient (cfg )
90
92
91
- var secretInfo = & SecretInfo {
92
- Type : APIKey , // TODO: implement Admin-Key type as well
93
- }
93
+ keyType := getKeyType (key )
94
94
95
- if err := listModels ( client , key , secretInfo ); err != nil {
96
- return nil , err
95
+ var secretInfo = & SecretInfo {
96
+ Type : keyType ,
97
97
}
98
98
99
- if err := listMessageBatches (client , key , secretInfo ); err != nil {
100
- return nil , err
99
+ switch keyType {
100
+ case APIKey :
101
+ if err := captureAPIKeyResources (client , key , secretInfo ); err != nil {
102
+ return nil , err
103
+ }
104
+ case AdminKey :
105
+ if err := captureAdminKeyResources (client , key , secretInfo ); err != nil {
106
+ return nil , err
107
+ }
108
+ default :
109
+ return nil , errors .New ("unsupported key type" )
101
110
}
102
111
103
112
// anthropic key has full access only
@@ -133,6 +142,14 @@ func secretInfoToAnalyzerResult(info *SecretInfo) *analyzers.AnalyzerResult {
133
142
},
134
143
}
135
144
145
+ if Anthropicresource .Parent != nil {
146
+ binding .Resource .Parent = & analyzers.Resource {
147
+ Name : Anthropicresource .Parent .Name ,
148
+ FullyQualifiedName : Anthropicresource .Parent .ID ,
149
+ Type : Anthropicresource .Parent .Type ,
150
+ }
151
+ }
152
+
136
153
for key , value := range Anthropicresource .Metadata {
137
154
binding .Resource .Metadata [key ] = value
138
155
}
@@ -162,3 +179,14 @@ func printAnthropicResources(resources []AnthropicResource) {
162
179
}
163
180
t .Render ()
164
181
}
182
+
183
+ // getKeyType return the type of key
184
+ func getKeyType (key string ) string {
185
+ if strings .Contains (key , "sk-ant-admin01" ) {
186
+ return AdminKey
187
+ } else if strings .Contains (key , "sk-ant-api03" ) {
188
+ return APIKey
189
+ }
190
+
191
+ return ""
192
+ }
0 commit comments