Skip installing certificates when they already exist

Fixes #1
This commit is contained in:
Simon Kågedal Reimer 2019-06-01 19:27:38 +02:00
parent 5239909668
commit ba79a12cd8
3 changed files with 29 additions and 3 deletions

View File

@ -47,16 +47,34 @@ class InstallCACommand: Command {
func run() throws {
let url = URL(fileURLWithPath: options.path!)
let certificate = try Certificate.load(from: url)
let sha1 = certificate.sha1
let certificateName = certificate.subjectSummary ?? "<unknown certificate>"
for device in try Simctl.flatListDevices().filter(using: filteringOptions) {
let trustStore = TrustStore(uuid: device.udid)
if !trustStore.exists && options.dryRun {
print("Would install '\(certificateName)' into '\(device.name)'.")
continue
}
let connection = try trustStore.open()
if try connection.hasCertificate(with: sha1) {
if options.dryRun {
print("Would skip installing '\(certificateName)' into '\(device.name)' it's already there.")
} else {
print("Not installing '\(certificateName)' into '\(device.name)' it's already there.")
}
continue
}
if options.dryRun {
print("Would install \(certificateName) into \(device.name).")
print("Would install '\(certificateName)' into '\(device.name)'.")
} else {
print("Installing \(certificateName) into \(device.name).")
let trustStore = TrustStore(uuid: device.udid)
print("Installing '\(certificateName)' into '\(device.name)'.")
try trustStore.open().addCertificate(certificate)
}
}
}
}

View File

@ -41,6 +41,10 @@ struct Certificate {
return SecCertificateCopySubjectSummary(certificate) as String?
}
var sha1: Data {
return Digest.sha1(data)
}
func normalizedSubjectSequence() throws -> Data {
var error: Unmanaged<CFError>?
guard let data = SecCertificateCopyNormalizedSubjectContent(certificate, &error) else {

View File

@ -85,5 +85,9 @@ struct TrustStore {
)
try connection.run(insert)
}
func hasCertificate(with sha1: Data) throws -> Bool {
return try connection.pluck(tsettings.where(sha1Column == sha1.datatypeValue)) != nil
}
}
}