Skip to content

Commit

Permalink
Moved things around so relevant components are easier to locate
Browse files Browse the repository at this point in the history
Some minor function renaming
  • Loading branch information
bartreardon committed Sep 5, 2023
1 parent 101dab3 commit 1cdcbbc
Show file tree
Hide file tree
Showing 14 changed files with 656 additions and 539 deletions.
52 changes: 48 additions & 4 deletions Outset.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,18 @@
41765ACA29C97B6400D616BF /* Services.swift in Sources */ = {isa = PBXBuildFile; fileRef = 41765AC929C97B6400D616BF /* Services.swift */; };
41E28EA229ACDCD6002ADBE5 /* Outset.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4124EF90293822F4003B00F4 /* Outset.swift */; };
41E28EA329ACDCE3002ADBE5 /* FileUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4124EFA2293B2F9B003B00F4 /* FileUtils.swift */; };
41E28EA429ACDCE3002ADBE5 /* Processing.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4124EFA4293B304B003B00F4 /* Processing.swift */; };
41E28EA429ACDCE3002ADBE5 /* ItemProcessing.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4124EFA4293B304B003B00F4 /* ItemProcessing.swift */; };
41E28EA529ACDCE3002ADBE5 /* SystemUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4124EF99293824C8003B00F4 /* SystemUtils.swift */; };
41E28EA729ACDD1F002ADBE5 /* ArgumentParser in Frameworks */ = {isa = PBXBuildFile; productRef = 41E28EA629ACDD1F002ADBE5 /* ArgumentParser */; };
CC3DC8222AA70B2D0050EE16 /* Logging.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC3DC8212AA70B2D0050EE16 /* Logging.swift */; };
CC3DC8242AA70BD40050EE16 /* Preferences.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC3DC8232AA70BD40050EE16 /* Preferences.swift */; };
CC3DC8262AA70D2D0050EE16 /* Network.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC3DC8252AA70D2D0050EE16 /* Network.swift */; };
CC3DC8282AA70D930050EE16 /* SystemInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC3DC8272AA70D930050EE16 /* SystemInfo.swift */; };
CC3DC82A2AA70E380050EE16 /* Checksum.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC3DC8292AA70E380050EE16 /* Checksum.swift */; };
CC3DC82D2AA70EE60050EE16 /* Data+additions.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC3DC82C2AA70EE60050EE16 /* Data+additions.swift */; };
CC3DC82F2AA70F230050EE16 /* URL+additions.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC3DC82E2AA70F230050EE16 /* URL+additions.swift */; };
CC3DC8312AA70F790050EE16 /* String+additions.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC3DC8302AA70F790050EE16 /* String+additions.swift */; };
CC3DC8332AA7100C0050EE16 /* ShellUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC3DC8322AA7100C0050EE16 /* ShellUtils.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -56,7 +65,7 @@
4124EF90293822F4003B00F4 /* Outset.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Outset.swift; sourceTree = "<group>"; };
4124EF99293824C8003B00F4 /* SystemUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SystemUtils.swift; sourceTree = "<group>"; };
4124EFA2293B2F9B003B00F4 /* FileUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileUtils.swift; sourceTree = "<group>"; };
4124EFA4293B304B003B00F4 /* Processing.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Processing.swift; sourceTree = "<group>"; };
4124EFA4293B304B003B00F4 /* ItemProcessing.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ItemProcessing.swift; sourceTree = "<group>"; };
4124EFAD29414A5D003B00F4 /* Outset.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Outset.app; sourceTree = BUILT_PRODUCTS_DIR; };
4124EFB329414A5E003B00F4 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
4124EFB629414A5E003B00F4 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
Expand All @@ -72,6 +81,15 @@
41A67A13296BF35F000BFFCE /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
41ADC47C29AECB8B00C5B94C /* outset */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = outset; sourceTree = "<group>"; };
41ADC47E29AF649C00C5B94C /* LICENSE.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = LICENSE.md; sourceTree = "<group>"; };
CC3DC8212AA70B2D0050EE16 /* Logging.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logging.swift; sourceTree = "<group>"; };
CC3DC8232AA70BD40050EE16 /* Preferences.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Preferences.swift; sourceTree = "<group>"; };
CC3DC8252AA70D2D0050EE16 /* Network.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Network.swift; sourceTree = "<group>"; };
CC3DC8272AA70D930050EE16 /* SystemInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SystemInfo.swift; sourceTree = "<group>"; };
CC3DC8292AA70E380050EE16 /* Checksum.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Checksum.swift; sourceTree = "<group>"; };
CC3DC82C2AA70EE60050EE16 /* Data+additions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Data+additions.swift"; sourceTree = "<group>"; };
CC3DC82E2AA70F230050EE16 /* URL+additions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "URL+additions.swift"; sourceTree = "<group>"; };
CC3DC8302AA70F790050EE16 /* String+additions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+additions.swift"; sourceTree = "<group>"; };
CC3DC8322AA7100C0050EE16 /* ShellUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShellUtils.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -111,9 +129,15 @@
isa = PBXGroup;
children = (
4124EFA2293B2F9B003B00F4 /* FileUtils.swift */,
4124EFA4293B304B003B00F4 /* Processing.swift */,
4124EFA4293B304B003B00F4 /* ItemProcessing.swift */,
4124EF99293824C8003B00F4 /* SystemUtils.swift */,
41765AC929C97B6400D616BF /* Services.swift */,
CC3DC8212AA70B2D0050EE16 /* Logging.swift */,
CC3DC8232AA70BD40050EE16 /* Preferences.swift */,
CC3DC8252AA70D2D0050EE16 /* Network.swift */,
CC3DC8272AA70D930050EE16 /* SystemInfo.swift */,
CC3DC8292AA70E380050EE16 /* Checksum.swift */,
CC3DC8322AA7100C0050EE16 /* ShellUtils.swift */,
);
path = Functions;
sourceTree = "<group>";
Expand All @@ -122,6 +146,7 @@
isa = PBXGroup;
children = (
4124EF90293822F4003B00F4 /* Outset.swift */,
CC3DC82B2AA70EC90050EE16 /* Extensions */,
4124EFA6293B30D6003B00F4 /* Functions */,
4124EFC329414DA4003B00F4 /* Info.plist */,
4124EFB329414A5E003B00F4 /* Assets.xcassets */,
Expand Down Expand Up @@ -167,6 +192,16 @@
path = Scripts;
sourceTree = "<group>";
};
CC3DC82B2AA70EC90050EE16 /* Extensions */ = {
isa = PBXGroup;
children = (
CC3DC82C2AA70EE60050EE16 /* Data+additions.swift */,
CC3DC82E2AA70F230050EE16 /* URL+additions.swift */,
CC3DC8302AA70F790050EE16 /* String+additions.swift */,
);
path = Extensions;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -308,8 +343,17 @@
buildActionMask = 2147483647;
files = (
41765ACA29C97B6400D616BF /* Services.swift in Sources */,
CC3DC8282AA70D930050EE16 /* SystemInfo.swift in Sources */,
CC3DC8242AA70BD40050EE16 /* Preferences.swift in Sources */,
CC3DC82F2AA70F230050EE16 /* URL+additions.swift in Sources */,
CC3DC8262AA70D2D0050EE16 /* Network.swift in Sources */,
CC3DC8222AA70B2D0050EE16 /* Logging.swift in Sources */,
41E28EA329ACDCE3002ADBE5 /* FileUtils.swift in Sources */,
41E28EA429ACDCE3002ADBE5 /* Processing.swift in Sources */,
CC3DC82D2AA70EE60050EE16 /* Data+additions.swift in Sources */,
CC3DC8332AA7100C0050EE16 /* ShellUtils.swift in Sources */,
CC3DC82A2AA70E380050EE16 /* Checksum.swift in Sources */,
CC3DC8312AA70F790050EE16 /* String+additions.swift in Sources */,
41E28EA429ACDCE3002ADBE5 /* ItemProcessing.swift in Sources */,
41E28EA529ACDCE3002ADBE5 /* SystemUtils.swift in Sources */,
41E28EA229ACDCD6002ADBE5 /* Outset.swift in Sources */,
);
Expand Down
24 changes: 24 additions & 0 deletions Outset/Extensions/Data+additions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// Data+additions.swift
// Outset
//
// Created by Bart E Reardon on 5/9/2023.
//

import Foundation
import CommonCrypto

extension Data {
// extension to the Data class that lets us compute sha256
func sha256() -> Data {
var hash = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
self.withUnsafeBytes {
_ = CC_SHA256($0.baseAddress, CC_LONG(count), &hash)
}
return Data(hash)
}

func hexEncodedString() -> String {
return map { String(format: "%02hhx", $0) }.joined()
}
}
21 changes: 21 additions & 0 deletions Outset/Extensions/String+additions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// String+additions.swift
// Outset
//
// Created by Bart E Reardon on 5/9/2023.
//

import Foundation

extension String {
func camelCaseToUnderscored() -> String {
let regex = try? NSRegularExpression(pattern: "([a-z])([A-Z])", options: [])
let range = NSRange(location: 0, length: utf16.count)
return regex?.stringByReplacingMatches(in: self, options: [], range: range, withTemplate: "$1_$2").lowercased() ?? self
}
}

func getValueForKey(_ key: String, inArray array: [String: String]) -> String? {
// short function that treats a [String: String] as a key value pair.
return array[key]
}
14 changes: 14 additions & 0 deletions Outset/Extensions/URL+additions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// URL+additions.swift
// Outset
//
// Created by Bart E Reardon on 5/9/2023.
//

import Foundation

extension URL {
var isDirectory: Bool {
(try? resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory == true
}
}
100 changes: 100 additions & 0 deletions Outset/Functions/Checksum.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//
// Checksum.swift
// Outset
//
// Created by Bart E Reardon on 5/9/2023.
//

import Foundation

struct FileHashes: Codable {
var sha256sum: [String: String] = [String: String]()
}

func checksumLoadApprovedFiles() -> [String: String] {
// imports the list of file hashes that are approved to run
var outsetFileHashList = FileHashes()

let defaults = UserDefaults.standard
let hashes = defaults.object(forKey: "sha256sum")

if let data = hashes as? [String: String] {
for (key, value) in data {
outsetFileHashList.sha256sum[key] = value
}
}

return outsetFileHashList.sha256sum
}

func verifySHASUMForFile(filename: String, shasumArray: [String: String]) -> Bool {
// Verify that the file
var proceed = false
let errorMessage = "no required hash or file hash mismatch for: \(filename). Skipping"
writeLog("checking hash for \(filename)", logLevel: .debug)
let url = URL(fileURLWithPath: filename)
if let fileHash = sha256(for: url) {
writeLog("file hash : \(fileHash)", logLevel: .debug)
if let storedHash = getValueForKey(filename, inArray: shasumArray) {
writeLog("required hash : \(storedHash)", logLevel: .debug)
if storedHash == fileHash {
proceed = true
}
}
}
if !proceed {
writeLog(errorMessage, logLevel: .error)
}

return proceed
}

func sha256(for url: URL) -> String? {
// computes a sha256sum for the specified file path and returns a string
do {
let fileData = try Data(contentsOf: url)
let sha256 = fileData.sha256()
return sha256.hexEncodedString()
} catch {
return nil
}
}

func checksumAllFiles() {
// compute checksum (SHA256) for all files in the outset directory
// returns data in two formats to stdout:
// plaintext
// as plist format ready for import into an MDM or converting to a .mobileconfig

let url = URL(fileURLWithPath: outsetDirectory)
writeLog("CHECKSUM", logLevel: .info)
var shasumPlist = FileHashes()
if let enumerator = FileManager.default.enumerator(at: url, includingPropertiesForKeys: [.isRegularFileKey], options: [.skipsHiddenFiles, .skipsPackageDescendants]) {
for case let fileURL as URL in enumerator {
do {
let fileAttributes = try fileURL.resourceValues(forKeys: [.isRegularFileKey])
if fileAttributes.isRegularFile! && fileURL.pathExtension != "plist" && fileURL.lastPathComponent != "outset" {
if let shasum = sha256(for: fileURL) {
print("\(fileURL.relativePath) : \(shasum)")
shasumPlist.sha256sum[fileURL.relativePath] = shasum
}
}
} catch { print(error, fileURL) }
}

writeLog("PLIST", logLevel: .info)
let encoder = PropertyListEncoder()
encoder.outputFormat = .xml
do {
let data = try encoder.encode(shasumPlist)
if let plist = try PropertyListSerialization.propertyList(from: data, options: [], format: nil) as? [String: Any] {
let formatted = try PropertyListSerialization.data(fromPropertyList: plist, format: .xml, options: 0)
if let string = String(data: formatted, encoding: .utf8) {
print(string)
}
}
} catch {
writeLog("plist encoding failed", logLevel: .error)
}
}
}
Loading

0 comments on commit 1cdcbbc

Please sign in to comment.