Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 20 additions & 19 deletions pkg/analyzer/analyzers/groq/groq.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Analyzer struct {
Cfg *config.Config
}

// SecretInfo hold the information about the groq key
// SecretInfo holds information gathered about a Groq API key.
type SecretInfo struct {
Valid bool
Reference string
Expand All @@ -28,7 +28,7 @@ type SecretInfo struct {
Misc map[string]string
}

// GroqResource is a single groq resource which can be accessed with groq api key
// GroqResource is a single resource accessible with a Groq API key.
type GroqResource struct {
ID string
Name string
Expand All @@ -37,17 +37,17 @@ type GroqResource struct {
Metadata map[string]string
}

// appendGroqResource append the single groq resource to secretinfo groqresources list
// appendGroqResource appends a resource onto the secret info list.
func (s *SecretInfo) appendGroqResource(resource GroqResource) {
s.GroqResources = append(s.GroqResources, resource)
}

// updateMetadata safely update the metadata of the groq resource
func (g GroqResource) updateMetadata(key, value string) {
// updateMetadata sets a metadata key on the resource. Pointer receiver so
// writes persist when callers mutate before appending.
func (g *GroqResource) updateMetadata(key, value string) {
if g.Metadata == nil {
g.Metadata = map[string]string{}
}

g.Metadata[key] = value
}

Expand All @@ -74,7 +74,6 @@ func (a Analyzer) Analyze(_ context.Context, credInfo map[string]string) (*analy
func AnalyzeAndPrintPermissions(cfg *config.Config, key string) {
info, err := AnalyzePermissions(cfg, key)
if err != nil {
// just print the error in cli and continue as a partial success
color.Red("[x] Invalid Groq API key\n")
color.Red("[x] Error : %s", err.Error())
return
Expand All @@ -96,35 +95,41 @@ func AnalyzeAndPrintPermissions(cfg *config.Config, key string) {
}

func AnalyzePermissions(cfg *config.Config, apiKey string) (*SecretInfo, error) {
// create a HTTP client
client := analyzers.NewAnalyzeClient(cfg)
secretInfo := &SecretInfo{Valid: true}

var secretInfo = &SecretInfo{Valid: true}

// Models are on every plan and guarantee analyze bindings. Batches and files
// are paid Developer-tier APIs; when available we surface those resources too,
// and when the plan denies them we skip without failing the analysis.
if err := captureModels(client, apiKey, secretInfo); err != nil {
return nil, err
}
if err := captureBatches(client, apiKey, secretInfo); err != nil {
return nil, err
}

if err := captureFiles(client, apiKey, secretInfo); err != nil {
return nil, err
}

return secretInfo, nil
}

// secretInfoToAnalyzerResult translate secret info to Analyzer Result
// secretInfoToAnalyzerResult translates secret info into Analyzer Result bindings.
// Permissions in the UI come from these bindings; an empty list looks like "no
// permissions" even when the CLI printed Full Access.
func secretInfoToAnalyzerResult(info *SecretInfo) *analyzers.AnalyzerResult {
if info == nil {
return nil
}

fullAccess := analyzers.Permission{Value: PermissionStrings[FullAccess]}
result := analyzers.AnalyzerResult{
AnalyzerType: analyzers.AnalyzerTypeGroq,
Metadata: map[string]any{"Valid_Key": info.Valid},
Bindings: make([]analyzers.Binding, len(info.GroqResources)),
// Cap only — length 0 so append does not leave zero-value placeholders.
Bindings: make([]analyzers.Binding, 0, len(info.GroqResources)),
}

// extract information to create bindings and append to result bindings
for _, groqResource := range info.GroqResources {
binding := analyzers.Binding{
Resource: analyzers.Resource{
Expand All @@ -133,15 +138,11 @@ func secretInfoToAnalyzerResult(info *SecretInfo) *analyzers.AnalyzerResult {
Type: groqResource.Type,
Metadata: map[string]any{},
},
Permission: analyzers.Permission{
Value: groqResource.Permission,
},
Permission: fullAccess,
}

for key, value := range groqResource.Metadata {
binding.Resource.Metadata[key] = value
}

result.Bindings = append(result.Bindings, binding)
}

Expand Down
33 changes: 16 additions & 17 deletions pkg/analyzer/analyzers/groq/groq_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package groq

import (
_ "embed"
"encoding/json"
"fmt"
"testing"
Expand All @@ -25,13 +24,11 @@ func TestAnalyzer_Analyze(t *testing.T) {
tests := []struct {
name string
apiKey string
want string
wantErr bool
}{
{
name: "valid dockerhub credentials",
name: "valid groq credentials",
apiKey: apiKey,
want: `{"AnalyzerType":2,"Bindings":[],"UnboundedResources":null,"Metadata":{"Valid_Key":true}}`,
wantErr: false,
},
}
Expand All @@ -45,27 +42,29 @@ func TestAnalyzer_Analyze(t *testing.T) {
return
}

// marshal the actual result to JSON
gotJSON, err := json.Marshal(got)
if err != nil {
t.Fatalf("could not marshal got to JSON: %s", err)
}

fmt.Println(string(gotJSON))

// compare the JSON strings
if string(gotJSON) != string(tt.want) {
// pretty-print both JSON strings for easier comparison
var gotIndented, wantIndented []byte
gotIndented, err = json.MarshalIndent(got, "", " ")
if err != nil {
t.Fatalf("could not marshal got to indented JSON: %s", err)
}
wantIndented, err = json.MarshalIndent(tt.want, "", " ")
if err != nil {
t.Fatalf("could not marshal want to indented JSON: %s", err)
if got == nil {
t.Fatal("Analyzer.Analyze() returned nil result")
}

if got.Metadata["Valid_Key"] != true {
t.Fatalf("expected Valid_Key metadata to be true, got %#v", got.Metadata["Valid_Key"])
}

if len(got.Bindings) == 0 {
t.Fatal("expected at least one model binding for a valid Groq API key")
}

for _, binding := range got.Bindings {
if binding.Permission.Value != PermissionStrings[FullAccess] {
t.Fatalf("expected full_access permission, got %q", binding.Permission.Value)
}
t.Errorf("Analyzer.Analyze() = %s, want %s", gotIndented, wantIndented)
}
})
}
Expand Down
Loading
Loading