|
| 1 | +//go:generate generate_permissions permissions.yaml permissions.go netlify |
| 2 | +package netlify |
| 3 | + |
| 4 | +import ( |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/fatih/color" |
| 9 | + "github.com/jedib0t/go-pretty/v6/table" |
| 10 | + "github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/analyzers" |
| 11 | + "github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/config" |
| 12 | + "github.com/trufflesecurity/trufflehog/v3/pkg/context" |
| 13 | +) |
| 14 | + |
| 15 | +var _ analyzers.Analyzer = (*Analyzer)(nil) |
| 16 | + |
| 17 | +type Analyzer struct { |
| 18 | + Cfg *config.Config |
| 19 | +} |
| 20 | + |
| 21 | +func (a Analyzer) Type() analyzers.AnalyzerType { |
| 22 | + return analyzers.AnalyzerTypeNetlify |
| 23 | +} |
| 24 | + |
| 25 | +func (a Analyzer) Analyze(_ context.Context, credInfo map[string]string) (*analyzers.AnalyzerResult, error) { |
| 26 | + key, exist := credInfo["key"] |
| 27 | + if !exist { |
| 28 | + return nil, fmt.Errorf("key not found in credential info") |
| 29 | + } |
| 30 | + |
| 31 | + info, err := AnalyzePermissions(a.Cfg, key) |
| 32 | + if err != nil { |
| 33 | + return nil, err |
| 34 | + } |
| 35 | + |
| 36 | + return secretInfoToAnalyzerResult(info), nil |
| 37 | +} |
| 38 | + |
| 39 | +func AnalyzeAndPrintPermissions(cfg *config.Config, key string) { |
| 40 | + info, err := AnalyzePermissions(cfg, key) |
| 41 | + if err != nil { |
| 42 | + // just print the error in cli and continue as a partial success |
| 43 | + color.Red("[x] Error : %s", err.Error()) |
| 44 | + } |
| 45 | + |
| 46 | + if info == nil { |
| 47 | + color.Red("[x] Error : %s", "No information found") |
| 48 | + return |
| 49 | + } |
| 50 | + |
| 51 | + color.Green("[!] Valid Fastly API key\n\n") |
| 52 | + |
| 53 | + printUserInfo(info.UserInfo) |
| 54 | + printTokenInfo(info.listResourceByType(Token)) |
| 55 | + printResources(info.Resources) |
| 56 | + |
| 57 | + color.Yellow("\n[i] Expires: %s", "N/A (Refer to Token Information Table)") |
| 58 | +} |
| 59 | + |
| 60 | +func AnalyzePermissions(cfg *config.Config, key string) (*SecretInfo, error) { |
| 61 | + client := analyzers.NewAnalyzeClient(cfg) |
| 62 | + |
| 63 | + var secretInfo = &SecretInfo{} |
| 64 | + |
| 65 | + if err := captureUserInfo(client, key, secretInfo); err != nil { |
| 66 | + return nil, err |
| 67 | + } |
| 68 | + |
| 69 | + if err := captureTokens(client, key, secretInfo); err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + |
| 73 | + if err := captureResources(client, key, secretInfo); err != nil { |
| 74 | + return secretInfo, err |
| 75 | + } |
| 76 | + |
| 77 | + return secretInfo, nil |
| 78 | +} |
| 79 | + |
| 80 | +// secretInfoToAnalyzerResult translate secret info to Analyzer Result |
| 81 | +func secretInfoToAnalyzerResult(info *SecretInfo) *analyzers.AnalyzerResult { |
| 82 | + if info == nil { |
| 83 | + return nil |
| 84 | + } |
| 85 | + |
| 86 | + result := analyzers.AnalyzerResult{ |
| 87 | + AnalyzerType: analyzers.AnalyzerTypeNetlify, |
| 88 | + Metadata: map[string]any{}, |
| 89 | + Bindings: make([]analyzers.Binding, 0), |
| 90 | + } |
| 91 | + |
| 92 | + // extract information from resource to create bindings and append to result bindings |
| 93 | + for _, resource := range info.Resources { |
| 94 | + binding := analyzers.Binding{ |
| 95 | + Resource: analyzers.Resource{ |
| 96 | + Name: resource.Name, |
| 97 | + FullyQualifiedName: fmt.Sprintf("netlify/%s/%s", resource.Type, resource.ID), // e.g: netlify/site/123 |
| 98 | + Type: resource.Type, |
| 99 | + Metadata: map[string]any{}, // to avoid panic |
| 100 | + }, |
| 101 | + Permission: analyzers.Permission{ |
| 102 | + Value: PermissionStrings[FullAccess], // no fine grain access |
| 103 | + }, |
| 104 | + } |
| 105 | + |
| 106 | + if resource.Parent != nil { |
| 107 | + binding.Resource.Parent = &analyzers.Resource{ |
| 108 | + Name: resource.Parent.Name, |
| 109 | + FullyQualifiedName: resource.Parent.ID, |
| 110 | + Type: resource.Parent.Type, |
| 111 | + // not copying parent metadata |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + for key, value := range resource.Metadata { |
| 116 | + binding.Resource.Metadata[key] = value |
| 117 | + } |
| 118 | + |
| 119 | + result.Bindings = append(result.Bindings, binding) |
| 120 | + } |
| 121 | + |
| 122 | + return &result |
| 123 | +} |
| 124 | + |
| 125 | +// cli print functions |
| 126 | +func printUserInfo(user User) { |
| 127 | + color.Yellow("[i] User Information:") |
| 128 | + t := table.NewWriter() |
| 129 | + t.SetOutputMirror(os.Stdout) |
| 130 | + t.AppendHeader(table.Row{"Name", "Email", "Account ID", "Last Login At"}) |
| 131 | + t.AppendRow(table.Row{color.GreenString(user.Name), color.GreenString(user.Email), color.GreenString(user.AccountID), color.GreenString(user.LastLogin)}) |
| 132 | + |
| 133 | + t.Render() |
| 134 | +} |
| 135 | + |
| 136 | +func printTokenInfo(tokens []NetlifyResource) { |
| 137 | + color.Yellow("[i] Tokens Information:") |
| 138 | + t := table.NewWriter() |
| 139 | + t.SetOutputMirror(os.Stdout) |
| 140 | + t.AppendHeader(table.Row{"ID", "Name", "Personal", "Expires At"}) |
| 141 | + for _, token := range tokens { |
| 142 | + t.AppendRow(table.Row{color.GreenString(token.ID), color.GreenString(token.Name), color.GreenString(token.Metadata[tokenPersonal]), color.GreenString(token.Metadata[tokenExpiresAt])}) |
| 143 | + } |
| 144 | + t.Render() |
| 145 | +} |
| 146 | + |
| 147 | +func printResources(resources []NetlifyResource) { |
| 148 | + color.Yellow("[i] Resources:") |
| 149 | + t := table.NewWriter() |
| 150 | + t.SetOutputMirror(os.Stdout) |
| 151 | + t.AppendHeader(table.Row{"Name", "Type"}) |
| 152 | + for _, resource := range resources { |
| 153 | + // skip token type resource as we will print them separately |
| 154 | + if resource.Type == Token.String() { |
| 155 | + continue |
| 156 | + } |
| 157 | + |
| 158 | + t.AppendRow(table.Row{color.GreenString(resource.Name), color.GreenString(resource.Type)}) |
| 159 | + } |
| 160 | + t.Render() |
| 161 | +} |
0 commit comments