Skip to content

Adjust to PackageCollectionSigning changes #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let package = Package(
.executable(name: "package-collection-diff", targets: ["PackageCollectionDiff"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser.git", .upToNextMajor(from: "1.0.2")),
.package(url: "https://github.com/apple/swift-argument-parser.git", .upToNextMajor(from: "1.2.2")),
.package(url: "https://github.com/apple/swift-package-manager.git", branch: "main"),
.package(url: "https://github.com/swift-server/swift-backtrace.git", .upToNextMajor(from: "1.1.0")),
],
Expand Down
22 changes: 12 additions & 10 deletions Sources/PackageCollectionSigner/PackageCollectionSign.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import PackageCollectionsSigning
import Utilities

@main
public struct PackageCollectionSign: ParsableCommand {
public struct PackageCollectionSign: AsyncParsableCommand {
public static let configuration = CommandConfiguration(
abstract: "Sign a package collection."
)
Expand All @@ -49,11 +49,11 @@ public struct PackageCollectionSign: ParsableCommand {

public init() {}

public func run() throws {
try self._run(signer: nil)
public func run() async throws {
try await self._run(signer: nil)
}

internal func _run(signer: PackageCollectionSigner?) throws {
internal func _run(signer: PackageCollectionSigner?) async throws {
Backtrace.install()

guard !self.certChainPaths.isEmpty else {
Expand All @@ -69,7 +69,7 @@ public struct PackageCollectionSign: ParsableCommand {
let privateKeyURL = URL(fileURLWithPath: self.privateKeyPath)
let certChainURLs: [URL] = try self.certChainPaths.map { try ensureAbsolute(path: $0).asURL }

try withTemporaryDirectory(removeTreeOnDeinit: true) { tmpDir in
try await withTemporaryDirectory(removeTreeOnDeinit: true) { tmpDir in
// The last item in the array is the root certificate and we want to trust it, so here we
// create a temp directory, copy the root certificate to it, and make it the trustedRootCertsDir.
let rootCertPath = try AbsolutePath(validating: certChainURLs.last!.path) // !-safe since certChain cannot be empty at this point
Expand All @@ -78,11 +78,13 @@ public struct PackageCollectionSign: ParsableCommand {

// Sign the collection
let signer = signer ?? PackageCollectionSigning(trustedRootCertsDir: tmpDir.asURL,
observabilityScope: ObservabilitySystem { _, diagnostic in print(diagnostic) }.topScope,
callbackQueue: DispatchQueue.global())
let signedCollection = try temp_await { callback in
signer.sign(collection: collection, certChainPaths: certChainURLs, certPrivateKeyPath: privateKeyURL, certPolicyKey: .default, callback: callback)
}
observabilityScope: ObservabilitySystem { _, diagnostic in print(diagnostic) }.topScope)
let signedCollection = try await signer.sign(
collection: collection,
certChainPaths: certChainURLs,
certPrivateKeyPath: privateKeyURL,
certPolicyKey: .default
)

// Make sure the output directory exists
let outputAbsolutePath = try ensureAbsolute(path: self.outputPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ final class PackageCollectionSignTests: XCTestCase {
.stdout.contains("USAGE: package-collection-sign <input-path> <output-path> <private-key-path> <cert-chain-paths> ... [--verbose]"))
}

func test_endToEnd() throws {
try withTemporaryDirectory(prefix: "PackageCollectionToolTests", removeTreeOnDeinit: true) { tmpDir in
func test_endToEnd() async throws {
try await withTemporaryDirectory(prefix: "PackageCollectionToolTests", removeTreeOnDeinit: true) { tmpDir in
let inputPath = try AbsolutePath(validating: #file).parentDirectory.appending(components: "Inputs", "test.json")
let outputPath = tmpDir.appending(component: "signed-test.json")
// These are not actually used since we are using MockPackageCollectionSigner
Expand All @@ -46,7 +46,7 @@ final class PackageCollectionSignTests: XCTestCase {

// We don't have real certs so we have to use a mock signer
let signer = MockPackageCollectionSigner()
try cmd._run(signer: signer)
try await cmd._run(signer: signer)

let jsonDecoder = JSONDecoder.makeWithDefaults()

Expand All @@ -59,18 +59,19 @@ final class PackageCollectionSignTests: XCTestCase {
}

private struct MockPackageCollectionSigner: PackageCollectionSigner {
func sign(collection: Model.Collection,
certChainPaths: [URL],
privateKeyPEM: Data,
certPolicyKey: CertificatePolicyKey,
callback: @escaping (Result<Model.SignedCollection, Error>) -> Void) {
func sign(
collection: Model.Collection,
certChainPaths: [URL],
privateKeyPEM: Data,
certPolicyKey: CertificatePolicyKey
) async throws -> Model.SignedCollection {
let signature = Model.Signature(
signature: "test signature",
certificate: Model.Signature.Certificate(
subject: Model.Signature.Certificate.Name(userID: "test user id", commonName: "test subject", organizationalUnit: "test unit", organization: "test org"),
issuer: Model.Signature.Certificate.Name(userID: nil, commonName: "test issuer", organizationalUnit: "test unit", organization: "test org")
)
)
callback(.success(Model.SignedCollection(collection: collection, signature: signature)))
return Model.SignedCollection(collection: collection, signature: signature)
}
}