|
| 1 | +//go:generate generate_permissions permissions.yaml permissions.go launchdarkly |
| 2 | +package launchdarkly |
| 3 | + |
| 4 | +import ( |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + |
| 9 | + "github.com/fatih/color" |
| 10 | + "github.com/jedib0t/go-pretty/v6/table" |
| 11 | + |
| 12 | + "github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/analyzers" |
| 13 | + "github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/config" |
| 14 | + "github.com/trufflesecurity/trufflehog/v3/pkg/context" |
| 15 | +) |
| 16 | + |
| 17 | +var _ analyzers.Analyzer = (*Analyzer)(nil) |
| 18 | + |
| 19 | +type Analyzer struct { |
| 20 | + Cfg *config.Config |
| 21 | +} |
| 22 | + |
| 23 | +func (a Analyzer) Type() analyzers.AnalyzerType { |
| 24 | + return analyzers.AnalyzerTypeLaunchDarkly |
| 25 | +} |
| 26 | + |
| 27 | +func (a Analyzer) Analyze(_ context.Context, credInfo map[string]string) (*analyzers.AnalyzerResult, error) { |
| 28 | + // check if the `key` exist in the credentials info |
| 29 | + key, exist := credInfo["key"] |
| 30 | + if !exist { |
| 31 | + return nil, errors.New("key not found in credentials info") |
| 32 | + } |
| 33 | + |
| 34 | + info, err := AnalyzePermissions(a.Cfg, key) |
| 35 | + if err != nil { |
| 36 | + return nil, err |
| 37 | + } |
| 38 | + |
| 39 | + return secretInfoToAnalyzerResult(info), nil |
| 40 | +} |
| 41 | + |
| 42 | +func AnalyzeAndPrintPermissions(cfg *config.Config, token string) { |
| 43 | + info, err := AnalyzePermissions(cfg, token) |
| 44 | + if err != nil { |
| 45 | + // just print the error in cli and continue as a partial success |
| 46 | + color.Red("[x] Error : %s", err.Error()) |
| 47 | + } |
| 48 | + |
| 49 | + if info == nil { |
| 50 | + color.Red("[x] Error : %s", "No information found") |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + color.Green("[i] Valid LaunchDarkly Token\n") |
| 55 | + printUser(info.User) |
| 56 | + printPermissionsType(info.User.Token) |
| 57 | + printResources(info.Resources) |
| 58 | + |
| 59 | + color.Yellow("\n[!] Expires: Never") |
| 60 | +} |
| 61 | + |
| 62 | +// AnalyzePermissions will collect all the scopes assigned to token along with resource it can access |
| 63 | +func AnalyzePermissions(cfg *config.Config, token string) (*SecretInfo, error) { |
| 64 | + // create the http client |
| 65 | + client := analyzers.NewAnalyzeClient(cfg) |
| 66 | + |
| 67 | + var secretInfo = &SecretInfo{} |
| 68 | + |
| 69 | + // capture user information in secretInfo |
| 70 | + if err := CaptureUserInformation(client, token, secretInfo); err != nil { |
| 71 | + return nil, fmt.Errorf("failed to fetch caller identity: %v", err) |
| 72 | + } |
| 73 | + |
| 74 | + // capture resources in secretInfo |
| 75 | + if err := CaptureResources(client, token, secretInfo); err != nil { |
| 76 | + return nil, fmt.Errorf("failed to fetch resources: %v", err) |
| 77 | + } |
| 78 | + |
| 79 | + return secretInfo, nil |
| 80 | +} |
| 81 | + |
| 82 | +// secretInfoToAnalyzerResult translate secret info to Analyzer Result |
| 83 | +func secretInfoToAnalyzerResult(info *SecretInfo) *analyzers.AnalyzerResult { |
| 84 | + if info == nil { |
| 85 | + return nil |
| 86 | + } |
| 87 | + |
| 88 | + result := analyzers.AnalyzerResult{ |
| 89 | + AnalyzerType: analyzers.AnalyzerTypeElevenLabs, |
| 90 | + Metadata: map[string]any{}, |
| 91 | + Bindings: make([]analyzers.Binding, 0), |
| 92 | + } |
| 93 | + |
| 94 | + // extract information from resource to create bindings and append to result bindings |
| 95 | + for _, resource := range info.Resources { |
| 96 | + binding := analyzers.Binding{ |
| 97 | + Resource: *secretInfoResourceToAnalyzerResource(resource), |
| 98 | + Permission: analyzers.Permission{ |
| 99 | + Value: getPermissionType(info.User.Token), |
| 100 | + }, |
| 101 | + } |
| 102 | + |
| 103 | + if resource.ParentResource != nil { |
| 104 | + binding.Resource.Parent = secretInfoResourceToAnalyzerResource(*resource.ParentResource) |
| 105 | + } |
| 106 | + |
| 107 | + result.Bindings = append(result.Bindings, binding) |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | + return &result |
| 112 | +} |
| 113 | + |
| 114 | +// secretInfoResourceToAnalyzerResource translate secret info resource to analyzer resource for binding |
| 115 | +func secretInfoResourceToAnalyzerResource(resource Resource) *analyzers.Resource { |
| 116 | + analyzerRes := analyzers.Resource{ |
| 117 | + FullyQualifiedName: resource.ID, |
| 118 | + Name: resource.Name, |
| 119 | + Type: resource.Type, |
| 120 | + Metadata: map[string]any{}, |
| 121 | + } |
| 122 | + |
| 123 | + for key, value := range resource.MetaData { |
| 124 | + analyzerRes.Metadata[key] = value |
| 125 | + } |
| 126 | + |
| 127 | + return &analyzerRes |
| 128 | +} |
| 129 | + |
| 130 | +// getPermissionType return what type of permission is assigned to token |
| 131 | +func getPermissionType(token Token) string { |
| 132 | + switch { |
| 133 | + case token.Role != "": |
| 134 | + return token.Role |
| 135 | + case token.hasInlineRole(): |
| 136 | + return "Inline Policy" |
| 137 | + case token.hasCustomRoles(): |
| 138 | + return "Custom Roles" |
| 139 | + default: |
| 140 | + return "" |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +// printUser print User information from secret info to cli |
| 145 | +func printUser(user User) { |
| 146 | + // print caller information |
| 147 | + color.Green("\n[i] User Information:") |
| 148 | + callerTable := table.NewWriter() |
| 149 | + callerTable.SetOutputMirror(os.Stdout) |
| 150 | + callerTable.AppendHeader(table.Row{"Account ID", "Member ID", "Name", "Email", "Role"}) |
| 151 | + callerTable.AppendRow(table.Row{color.GreenString(user.AccountID), color.GreenString(user.MemberID), |
| 152 | + color.GreenString(user.Name), color.GreenString(user.Email), color.GreenString(user.Role)}) |
| 153 | + |
| 154 | + callerTable.Render() |
| 155 | + |
| 156 | + // print token information |
| 157 | + color.Green("\n[i] Token Information") |
| 158 | + tokenTable := table.NewWriter() |
| 159 | + tokenTable.SetOutputMirror(os.Stdout) |
| 160 | + |
| 161 | + tokenTable.AppendHeader(table.Row{"ID", "Name", "Role", "Is Service Token", "Default API Version", |
| 162 | + "No of Custom Roles Assigned", "Has Inline Policy"}) |
| 163 | + |
| 164 | + tokenTable.AppendRow(table.Row{color.GreenString(user.Token.ID), color.GreenString(user.Token.Name), color.GreenString(user.Token.Role), |
| 165 | + color.GreenString(fmt.Sprintf("%t", user.Token.IsServiceToken)), color.GreenString(fmt.Sprintf("%d", user.Token.APIVersion)), |
| 166 | + color.GreenString(fmt.Sprintf("%d", len(user.Token.CustomRoles))), color.GreenString(fmt.Sprintf("%t", user.Token.hasInlineRole()))}) |
| 167 | + |
| 168 | + tokenTable.Render() |
| 169 | + |
| 170 | + // print custom roles information |
| 171 | + if !user.Token.hasCustomRoles() { |
| 172 | + return |
| 173 | + } |
| 174 | + |
| 175 | + // print token information |
| 176 | + color.Green("\n[i] Custom Roles Assigned to Token") |
| 177 | + rolesTable := table.NewWriter() |
| 178 | + rolesTable.SetOutputMirror(os.Stdout) |
| 179 | + rolesTable.AppendHeader(table.Row{"ID", "Key", "Name", "Base Permission", "Assigned to members", "Assigned to teams"}) |
| 180 | + for _, customRole := range user.Token.CustomRoles { |
| 181 | + rolesTable.AppendRow(table.Row{color.GreenString(customRole.ID), color.GreenString(customRole.Key), color.GreenString(customRole.Name), |
| 182 | + color.GreenString(customRole.BasePermission), color.GreenString(fmt.Sprintf("%d", customRole.AssignedToMembers)), |
| 183 | + color.GreenString(fmt.Sprintf("%d", customRole.AssignedToTeams))}) |
| 184 | + } |
| 185 | + rolesTable.Render() |
| 186 | +} |
| 187 | + |
| 188 | +// printPermissionsType print permissions type token has |
| 189 | +func printPermissionsType(token Token) { |
| 190 | + // print permission type. It can be either admin, writer, reader or has inline policy or any custom roles assigned |
| 191 | + color.Green("\n[i] Permission Type: %s", getPermissionType(token)) |
| 192 | +} |
| 193 | + |
| 194 | +func printResources(resources []Resource) { |
| 195 | + // print resources |
| 196 | + color.Green("\n[i] Resources:") |
| 197 | + callerTable := table.NewWriter() |
| 198 | + callerTable.SetOutputMirror(os.Stdout) |
| 199 | + callerTable.AppendHeader(table.Row{"Name", "Type"}) |
| 200 | + for _, resource := range resources { |
| 201 | + callerTable.AppendRow(table.Row{color.GreenString(resource.Name), color.GreenString(resource.Type)}) |
| 202 | + } |
| 203 | + callerTable.Render() |
| 204 | +} |
0 commit comments