Skip to content

Commit

Permalink
No more checking in key, better determine cnf and csr location
Browse files Browse the repository at this point in the history
  • Loading branch information
Yasumoto committed Jun 16, 2018
1 parent 64082aa commit b7b33af
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
"repositoryURL": "https://github.com/kareman/SwiftShell",
"state": {
"branch": null,
"revision": "b9fd06798993153bc57cde10ee6c75ddc55c2dbd",
"version": "4.0.2"
"revision": "aebc06a6492a3d7c6d08005b6396ae2a80521773",
"version": "4.1.0"
}
}
]
Expand Down
31 changes: 27 additions & 4 deletions Sources/RenewCertificate/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,35 @@ if (args.contains("--help") || args.contains("-h")) {
exit(1)
}

guard args.count == 3, let certPath = args.popLast(), let configPath = args.popLast() else {
guard args.count == 3, let certLocation = args.popLast(), let configLocation = args.popLast() else {
print("Please pass the paths to a digicert API key file and the certificate file you need to renew!")
exit(1)
}

guard let digicertConfigData = Files.contents(atPath: configPath) else {
let manager = FileManager.default
guard let configPath = URL(string: configLocation)?.absoluteString, manager.fileExists(atPath: configPath) else {
print("Please make sure a json file with credentials exists at \(configLocation)")
exit(1)
}
guard let certPath = URL(string: certLocation)?.absoluteString, manager.fileExists(atPath: certPath) else {
print("Please make sure a certificate file exists at \(certLocation)")
exit(1)
}

var prefixBranch = false
let prefix = URL(string: certPath)!.pathComponents
.filter({ path in
if prefixBranch {
return false
}
if path == "ssl" {
prefixBranch = true
}
return true
})
.joined(separator: "/")

guard let digicertConfigData = manager.contents(atPath: configPath) else {
print("Could not read your digicert config at \(configPath)")
exit(1)
}
Expand Down Expand Up @@ -79,10 +102,10 @@ try runAndPrint(bash: "/usr/bin/git commit -a -m \"Replace certificate for \(fil

try runAndPrint(bash: certified)

try runAndPrint(bash: "/usr/bin/git add './etc/ssl/\(filename).cnf' './etc/ssl/\(filename).csr' './etc/ssl/private/\(filename).key'")
try runAndPrint(bash: "/usr/bin/git add '\(prefix)/\(filename).cnf' '\(prefix)/\(filename).csr'")
try runAndPrint(bash: "/usr/bin/git commit -a -m \"New key for \(commonName)\"")

guard let csrData = Files.contents(atPath: "./etc/ssl/\(filename).csr"), let csr = String(bytes: csrData, encoding: .utf8) else {
guard let csrData = manager.contents(atPath: "./etc/ssl/\(filename).csr"), let csr = String(bytes: csrData, encoding: .utf8) else {
print("Could not read the generated CSR ./etc/ssl/\(filename).csr")
exit(1)
}
Expand Down

0 comments on commit b7b33af

Please sign in to comment.