|
| 1 | +package detector |
| 2 | + |
| 3 | +import ( |
| 4 | + "path/filepath" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/step-security/dev-machine-guard/internal/model" |
| 8 | +) |
| 9 | + |
| 10 | +// eclipseFeatureDirs are Eclipse feature directories to scan. |
| 11 | +// Features represent installed plugins/extensions (both bundled and user-installed). |
| 12 | +var eclipseFeatureDirs = []string{ |
| 13 | + "/Applications/Eclipse.app/Contents/Eclipse/features", |
| 14 | + "/Applications/Eclipse.app/Contents/Eclipse/dropins", |
| 15 | +} |
| 16 | + |
| 17 | +// eclipseBundledPrefixes are feature ID prefixes that ship as part of the |
| 18 | +// base Eclipse platform. Features matching these are tagged as "bundled". |
| 19 | +var eclipseBundledPrefixes = []string{ |
| 20 | + "org.eclipse.platform", |
| 21 | + "org.eclipse.rcp", |
| 22 | + "org.eclipse.e4.rcp", |
| 23 | + "org.eclipse.equinox.", |
| 24 | + "org.eclipse.help", |
| 25 | + "org.eclipse.justj.", |
| 26 | + "org.eclipse.oomph.", |
| 27 | + "org.eclipse.epp.package.", |
| 28 | +} |
| 29 | + |
| 30 | +// DetectEclipsePlugins scans Eclipse feature directories and returns |
| 31 | +// all features tagged as "bundled" or "user_installed". |
| 32 | +func (d *ExtensionDetector) DetectEclipsePlugins() []model.Extension { |
| 33 | + var results []model.Extension |
| 34 | + for _, dir := range eclipseFeatureDirs { |
| 35 | + if !d.exec.DirExists(dir) { |
| 36 | + continue |
| 37 | + } |
| 38 | + results = append(results, d.collectEclipseFeatures(dir)...) |
| 39 | + } |
| 40 | + return results |
| 41 | +} |
| 42 | + |
| 43 | +// collectEclipseFeatures reads Eclipse features from a directory. |
| 44 | +// Each feature is tagged as "bundled" or "user_installed". |
| 45 | +func (d *ExtensionDetector) collectEclipseFeatures(featuresDir string) []model.Extension { |
| 46 | + entries, err := d.exec.ReadDir(featuresDir) |
| 47 | + if err != nil { |
| 48 | + return nil |
| 49 | + } |
| 50 | + |
| 51 | + var results []model.Extension |
| 52 | + for _, entry := range entries { |
| 53 | + name := entry.Name() |
| 54 | + baseName := strings.TrimSuffix(name, ".jar") |
| 55 | + |
| 56 | + ext := parseEclipsePluginName(baseName) |
| 57 | + if ext == nil { |
| 58 | + continue |
| 59 | + } |
| 60 | + |
| 61 | + // Tag as bundled or user_installed |
| 62 | + if isEclipseBundled(ext.ID) { |
| 63 | + ext.Source = "bundled" |
| 64 | + } else { |
| 65 | + ext.Source = "user_installed" |
| 66 | + } |
| 67 | + |
| 68 | + path := filepath.Join(featuresDir, name) |
| 69 | + info, err := d.exec.Stat(path) |
| 70 | + if err == nil { |
| 71 | + ext.InstallDate = info.ModTime().Unix() |
| 72 | + } |
| 73 | + |
| 74 | + results = append(results, *ext) |
| 75 | + } |
| 76 | + |
| 77 | + return results |
| 78 | +} |
| 79 | + |
| 80 | +func isEclipseBundled(pluginID string) bool { |
| 81 | + for _, prefix := range eclipseBundledPrefixes { |
| 82 | + if strings.HasPrefix(pluginID, prefix) { |
| 83 | + return true |
| 84 | + } |
| 85 | + } |
| 86 | + return false |
| 87 | +} |
| 88 | + |
| 89 | +// parseEclipsePluginName parses "id_version" format. |
| 90 | +// Example: "com.github.spotbugs.plugin.eclipse_4.9.8.r202510181643-c1fa7f2" |
| 91 | +// |
| 92 | +// → id=com.github.spotbugs.plugin.eclipse, version=4.9.8.r202510181643-c1fa7f2 |
| 93 | +func parseEclipsePluginName(name string) *model.Extension { |
| 94 | + lastUnderscore := -1 |
| 95 | + for i := len(name) - 1; i >= 0; i-- { |
| 96 | + if name[i] == '_' { |
| 97 | + if i+1 < len(name) && name[i+1] >= '0' && name[i+1] <= '9' { |
| 98 | + lastUnderscore = i |
| 99 | + break |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + if lastUnderscore < 1 { |
| 105 | + return nil |
| 106 | + } |
| 107 | + |
| 108 | + pluginID := name[:lastUnderscore] |
| 109 | + version := name[lastUnderscore+1:] |
| 110 | + |
| 111 | + if pluginID == "" || version == "" { |
| 112 | + return nil |
| 113 | + } |
| 114 | + |
| 115 | + publisher := "unknown" |
| 116 | + parts := strings.SplitN(pluginID, ".", 3) |
| 117 | + if len(parts) >= 2 { |
| 118 | + publisher = parts[0] + "." + parts[1] |
| 119 | + } |
| 120 | + |
| 121 | + return &model.Extension{ |
| 122 | + ID: pluginID, |
| 123 | + Name: pluginID, |
| 124 | + Version: version, |
| 125 | + Publisher: publisher, |
| 126 | + IDEType: "eclipse", |
| 127 | + } |
| 128 | +} |
0 commit comments