Skip to content

Commit

Permalink
New process
Browse files Browse the repository at this point in the history
  • Loading branch information
Yasumoto committed Jun 16, 2018
1 parent b7b33af commit c8ae876
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# RenewCertificate

Tool to update a certificate and automatically submit a request to Digicert's API.

This depends on [`certified`](https://github.com/rcrowley/certified) and a particular
file system structure to work.
17 changes: 11 additions & 6 deletions Sources/RenewCertificate/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@ guard let certPath = URL(string: certLocation)?.absoluteString, manager.fileExis
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" {
if path == "default" {
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 @@ -97,16 +97,21 @@ for san in sans {

try runAndPrint(bash: "/usr/bin/git checkout -b certificate-\(filename)")

try runAndPrint(bash: "/usr/bin/git rm \(certPath)")
sleep(1)
try runAndPrint(bash: "/usr/bin/git rm \(certPath) \(prefix)/etc/ssl/\(filename).{csr,cnf}")
try runAndPrint(bash: "/usr/bin/git rm -rf \(prefix)/etc/ssl/private/\(filename).key")
try runAndPrint(bash: "/usr/bin/git commit -a -m \"Replace certificate for \(filename)\"")

let currentDirectoryPath = manager.currentDirectoryPath
manager.changeCurrentDirectoryPath(prefix)
try runAndPrint(bash: certified)
manager.changeCurrentDirectoryPath(currentDirectoryPath)

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

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")
guard let csrData = manager.contents(atPath: "\(prefix)/etc/ssl/\(filename).csr"), let csr = String(bytes: csrData, encoding: .utf8) else {
print("Could not read the generated CSR \(prefix)/etc/ssl/\(filename).csr")
exit(1)
}

Expand Down

0 comments on commit c8ae876

Please sign in to comment.