|
| 1 | +//go:generate generate_permissions permissions.yaml permissions.go monday |
| 2 | +package monday |
| 3 | + |
| 4 | +import ( |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + |
| 9 | + "github.com/fatih/color" |
| 10 | + "github.com/jedib0t/go-pretty/v6/table" |
| 11 | + "github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/analyzers" |
| 12 | + "github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/config" |
| 13 | + "github.com/trufflesecurity/trufflehog/v3/pkg/context" |
| 14 | +) |
| 15 | + |
| 16 | +var _ analyzers.Analyzer = (*Analyzer)(nil) |
| 17 | + |
| 18 | +type Analyzer struct { |
| 19 | + Cfg *config.Config |
| 20 | +} |
| 21 | + |
| 22 | +type SecretInfo struct { |
| 23 | + User Me |
| 24 | + Account Account |
| 25 | + Resources []MondayResource |
| 26 | +} |
| 27 | + |
| 28 | +func (s *SecretInfo) appendResource(resource MondayResource) { |
| 29 | + s.Resources = append(s.Resources, resource) |
| 30 | +} |
| 31 | + |
| 32 | +type MondayResource struct { |
| 33 | + ID string |
| 34 | + Name string |
| 35 | + Type string |
| 36 | + MetaData map[string]string |
| 37 | + Parent *MondayResource |
| 38 | +} |
| 39 | + |
| 40 | +func (a Analyzer) Type() analyzers.AnalyzerType { |
| 41 | + return analyzers.AnalyzerTypeMonday |
| 42 | +} |
| 43 | + |
| 44 | +func (a Analyzer) Analyze(_ context.Context, credInfo map[string]string) (*analyzers.AnalyzerResult, error) { |
| 45 | + key, exist := credInfo["key"] |
| 46 | + if !exist { |
| 47 | + return nil, errors.New("key not found in credentials info") |
| 48 | + } |
| 49 | + |
| 50 | + info, err := AnalyzePermissions(a.Cfg, key) |
| 51 | + if err != nil { |
| 52 | + return nil, err |
| 53 | + } |
| 54 | + |
| 55 | + return secretInfoToAnalyzerResult(info), nil |
| 56 | +} |
| 57 | + |
| 58 | +func AnalyzeAndPrintPermissions(cfg *config.Config, key string) { |
| 59 | + info, err := AnalyzePermissions(cfg, key) |
| 60 | + if err != nil { |
| 61 | + // just print the error in cli and continue as a partial success |
| 62 | + color.Red("[x] Error : %s", err.Error()) |
| 63 | + } |
| 64 | + |
| 65 | + if info == nil { |
| 66 | + color.Red("[x] Error : %s", "No information found") |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + color.Green("[!] Valid Monday Personal Access Token\n\n") |
| 71 | + // print user information |
| 72 | + printUser(info.User) |
| 73 | + printResources(info.Resources) |
| 74 | + |
| 75 | + color.Yellow("\n[i] Expires: Never") |
| 76 | +} |
| 77 | + |
| 78 | +func AnalyzePermissions(cfg *config.Config, key string) (*SecretInfo, error) { |
| 79 | + // create http client |
| 80 | + client := analyzers.NewAnalyzeClientUnrestricted(cfg) |
| 81 | + |
| 82 | + var secretInfo = &SecretInfo{} |
| 83 | + |
| 84 | + // captureMondayData make a query to graphql API of monday to fetch all data and store it in secret info |
| 85 | + if err := captureMondayData(client, key, secretInfo); err != nil { |
| 86 | + return nil, err |
| 87 | + } |
| 88 | + |
| 89 | + return secretInfo, nil |
| 90 | +} |
| 91 | + |
| 92 | +// secretInfoToAnalyzerResult translate secret info to Analyzer Result |
| 93 | +func secretInfoToAnalyzerResult(info *SecretInfo) *analyzers.AnalyzerResult { |
| 94 | + if info == nil { |
| 95 | + return nil |
| 96 | + } |
| 97 | + |
| 98 | + result := analyzers.AnalyzerResult{ |
| 99 | + AnalyzerType: analyzers.AnalyzerTypeMonday, |
| 100 | + Metadata: map[string]any{}, |
| 101 | + Bindings: make([]analyzers.Binding, 0), |
| 102 | + } |
| 103 | + |
| 104 | + // extract information from resource to create bindings and append to result bindings |
| 105 | + for _, resource := range info.Resources { |
| 106 | + binding := analyzers.Binding{ |
| 107 | + Resource: analyzers.Resource{ |
| 108 | + Name: resource.Name, |
| 109 | + FullyQualifiedName: fmt.Sprintf("%s/%s", resource.Type, resource.ID), // e.g: Board/123 |
| 110 | + Type: resource.Type, |
| 111 | + Metadata: map[string]any{}, // to avoid panic |
| 112 | + }, |
| 113 | + Permission: analyzers.Permission{ |
| 114 | + Value: PermissionStrings[FullAccess], // token always has full access |
| 115 | + }, |
| 116 | + } |
| 117 | + |
| 118 | + for key, value := range resource.MetaData { |
| 119 | + binding.Resource.Metadata[key] = value |
| 120 | + } |
| 121 | + |
| 122 | + result.Bindings = append(result.Bindings, binding) |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | + return &result |
| 127 | +} |
| 128 | + |
| 129 | +// cli print functions |
| 130 | +func printUser(user Me) { |
| 131 | + color.Green("\n[i] User Information:") |
| 132 | + t := table.NewWriter() |
| 133 | + t.SetOutputMirror(os.Stdout) |
| 134 | + t.AppendHeader(table.Row{"ID", "Name", "Email", "Title", "Is Admin", "Is Guest"}) |
| 135 | + t.AppendRow(table.Row{color.GreenString(user.ID), color.GreenString(user.Name), color.GreenString(user.Email), |
| 136 | + color.GreenString(user.Title), color.GreenString(fmt.Sprintf("%t", user.IsAdmin)), color.GreenString(fmt.Sprintf("%t", user.IsGuest))}) |
| 137 | + t.Render() |
| 138 | +} |
| 139 | + |
| 140 | +func printResources(resources []MondayResource) { |
| 141 | + color.Green("\n[i] Resources:") |
| 142 | + t := table.NewWriter() |
| 143 | + t.SetOutputMirror(os.Stdout) |
| 144 | + t.AppendHeader(table.Row{"Name", "Type"}) |
| 145 | + for _, resource := range resources { |
| 146 | + t.AppendRow(table.Row{color.GreenString(resource.Name), color.GreenString(resource.Type)}) |
| 147 | + } |
| 148 | + t.Render() |
| 149 | +} |
0 commit comments