Skip to content

Commit

Permalink
Add SBOM list generator
Browse files Browse the repository at this point in the history
  • Loading branch information
dz0ny committed Mar 14, 2024
1 parent e8c93a6 commit 5cf43c1
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Pareto/Checks/Access Security/NoUnusedUsers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class NoUnusedUsers: ParetoCheck {
}

override var TitleOFF: String {
"Unused user accounts are present ("+accounts.joined(separator: ",")+")"
"Unused user accounts are present (" + accounts.joined(separator: ",") + ")"
}

var accounts: [String] {
Expand Down
2 changes: 1 addition & 1 deletion Pareto/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</dict>
</array>
<key>CFBundleVersion</key>
<string>5429</string>
<string>5439</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string>
<key>LSMinimumSystemVersion</key>
Expand Down
3 changes: 1 addition & 2 deletions Pareto/Teams.swift
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,11 @@ class TeamSettingsUpdater: ObservableObject {
if Defaults[.appliedIgnoredChecks] {
NoUnusedUsers.sharedInstance.isActive = false
NoAdminUser.sharedInstance.isActive = false
}else {
} else {
check.isActive = false
}
Defaults[.appliedIgnoredChecksIDs].append(check.UUID)
}

}
}
}
Expand Down
95 changes: 94 additions & 1 deletion Pareto/Views/Settings/TeamSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,77 @@
import Defaults
import SwiftUI

struct PublicApp: Codable {
let name: String
let bundle: String
let version: String

static var all: [PublicApp] {
var detectedApps: [PublicApp] = []
let allApps = try! FileManager.default.contentsOfDirectory(at: URL(string: "/Applications")!, includingPropertiesForKeys: [.isApplicationKey])
for app in allApps {
let plist = PublicApp.readPlistFile(fileURL: app.appendingPathComponent("Contents/Info.plist"))
if let appName = plist?["CFBundleName"] as? String,
let appBundle = plist?["CFBundleIdentifier"] as? String {
let bundleApp = PublicApp(
name: appName,
bundle: appBundle,
version: plist?["CFBundleShortVersionString"] as? String ?? "Unknown"
)
detectedApps.append(bundleApp)
}
}

// user apps
let homeDirURL = FileManager.default.homeDirectoryForCurrentUser
let localPath = URL(fileURLWithPath: "\(homeDirURL.path)/Applications/")
if (try? localPath.checkResourceIsReachable()) ?? false {
let userApps = try! FileManager.default.contentsOfDirectory(at: localPath, includingPropertiesForKeys: [.isApplicationKey])
for app in userApps {
let plist = PublicApp.readPlistFile(fileURL: app.appendingPathComponent("Contents/Info.plist"))
if let appName = plist?["CFBundleName"] as? String,
let appBundle = plist?["CFBundleIdentifier"] as? String {
let bundleApp = PublicApp(
name: appName,
bundle: appBundle,
version: plist?["CFBundleShortVersionString"] as? String ?? "Unknown"
)
detectedApps.append(bundleApp)
}
}
}

return detectedApps
}

static func readPlistFile(fileURL: URL) -> [String: Any]? {
guard let data = try? Data(contentsOf: fileURL) else {
return nil
}
guard let result = try? PropertyListSerialization.propertyList(from: data, options: [], format: nil) as? [String: Any] else {
return nil
}
return result
}

static func asJSON() -> String? {
var export: [PublicApp] = []

for app in PublicApp.all.sorted(by: { lha, rha in
lha.name.lowercased() < rha.name.lowercased()
}) {
export.append(app)
}

let jsonEncoder = JSONEncoder()
jsonEncoder.outputFormatting = .prettyPrinted
let jsonData = try! jsonEncoder.encode(export)
guard let json = String(data: jsonData, encoding: String.Encoding.utf8) else { return nil }

return json
}
}

struct TeamSettingsView: View {
@StateObject var teamSettings: TeamSettingsUpdater

Expand All @@ -25,6 +96,22 @@ struct TeamSettingsView: View {
NSPasteboard.general.setString("Team ID: \(teamID)\nMachine UUID: \(machineUUID)", forType: .string)
}

func copyApps() {
var logs = [String]()
logs.append("Name, Bundle, Version")
for app in PublicApp.all {
logs.append("\(app.name), \(app.bundle), \(app.version)")
}

NSPasteboard.general.clearContents()
NSPasteboard.general.setString(logs.joined(separator: "\n"), forType: .string)
let alert = NSAlert()
alert.messageText = "List of installed apps has been copied to the clipboard."
alert.alertStyle = NSAlert.Style.informational
alert.addButton(withTitle: "OK")
alert.runModal()
}

func help() {
NSWorkspace.shared.open(URL(string: "https://support.apple.com/en-ie/guide/mac-help/mchlp2322/mac#mchl8c79215b")!)
}
Expand Down Expand Up @@ -76,6 +163,11 @@ struct TeamSettingsView: View {
} else {
Toggle("Send inventory info on update", isOn: $sendHWInfo)
}
HStack {
Button("Copy App list") {
copyApps()
}
}
}
}

Expand All @@ -86,7 +178,8 @@ struct TeamSettingsView: View {
Link("Team Dashboard »",
destination: AppInfo.teamsURL())
}
}.frame(width: 380, height: 250).padding(25).onAppear {
Spacer(minLength: 2)
}.frame(width: 380, height: 290).padding(25).onAppear {
DispatchQueue.main.async {
teamSettings.update {}
}
Expand Down

0 comments on commit 5cf43c1

Please sign in to comment.