Skip to content

Commit a8d0f6b

Browse files
authored
Adjust to PackageCollectionSigning changes (#63)
swiftlang/swift-package-manager#6609
1 parent d54eee8 commit a8d0f6b

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ let package = Package(
1212
.executable(name: "package-collection-diff", targets: ["PackageCollectionDiff"]),
1313
],
1414
dependencies: [
15-
.package(url: "https://github.com/apple/swift-argument-parser.git", .upToNextMajor(from: "1.0.2")),
15+
.package(url: "https://github.com/apple/swift-argument-parser.git", .upToNextMajor(from: "1.2.2")),
1616
.package(url: "https://github.com/apple/swift-package-manager.git", branch: "main"),
1717
.package(url: "https://github.com/swift-server/swift-backtrace.git", .upToNextMajor(from: "1.1.0")),
1818
],

Sources/PackageCollectionSigner/PackageCollectionSign.swift

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import PackageCollectionsSigning
2525
import Utilities
2626

2727
@main
28-
public struct PackageCollectionSign: ParsableCommand {
28+
public struct PackageCollectionSign: AsyncParsableCommand {
2929
public static let configuration = CommandConfiguration(
3030
abstract: "Sign a package collection."
3131
)
@@ -49,11 +49,11 @@ public struct PackageCollectionSign: ParsableCommand {
4949

5050
public init() {}
5151

52-
public func run() throws {
53-
try self._run(signer: nil)
52+
public func run() async throws {
53+
try await self._run(signer: nil)
5454
}
5555

56-
internal func _run(signer: PackageCollectionSigner?) throws {
56+
internal func _run(signer: PackageCollectionSigner?) async throws {
5757
Backtrace.install()
5858

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

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

7979
// Sign the collection
8080
let signer = signer ?? PackageCollectionSigning(trustedRootCertsDir: tmpDir.asURL,
81-
observabilityScope: ObservabilitySystem { _, diagnostic in print(diagnostic) }.topScope,
82-
callbackQueue: DispatchQueue.global())
83-
let signedCollection = try temp_await { callback in
84-
signer.sign(collection: collection, certChainPaths: certChainURLs, certPrivateKeyPath: privateKeyURL, certPolicyKey: .default, callback: callback)
85-
}
81+
observabilityScope: ObservabilitySystem { _, diagnostic in print(diagnostic) }.topScope)
82+
let signedCollection = try await signer.sign(
83+
collection: collection,
84+
certChainPaths: certChainURLs,
85+
certPrivateKeyPath: privateKeyURL,
86+
certPolicyKey: .default
87+
)
8688

8789
// Make sure the output directory exists
8890
let outputAbsolutePath = try ensureAbsolute(path: self.outputPath)

Tests/PackageCollectionSignerTests/PackageCollectionSignTests.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ final class PackageCollectionSignTests: XCTestCase {
2929
.stdout.contains("USAGE: package-collection-sign <input-path> <output-path> <private-key-path> <cert-chain-paths> ... [--verbose]"))
3030
}
3131

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

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

5151
let jsonDecoder = JSONDecoder.makeWithDefaults()
5252

@@ -59,18 +59,19 @@ final class PackageCollectionSignTests: XCTestCase {
5959
}
6060

6161
private struct MockPackageCollectionSigner: PackageCollectionSigner {
62-
func sign(collection: Model.Collection,
63-
certChainPaths: [URL],
64-
privateKeyPEM: Data,
65-
certPolicyKey: CertificatePolicyKey,
66-
callback: @escaping (Result<Model.SignedCollection, Error>) -> Void) {
62+
func sign(
63+
collection: Model.Collection,
64+
certChainPaths: [URL],
65+
privateKeyPEM: Data,
66+
certPolicyKey: CertificatePolicyKey
67+
) async throws -> Model.SignedCollection {
6768
let signature = Model.Signature(
6869
signature: "test signature",
6970
certificate: Model.Signature.Certificate(
7071
subject: Model.Signature.Certificate.Name(userID: "test user id", commonName: "test subject", organizationalUnit: "test unit", organization: "test org"),
7172
issuer: Model.Signature.Certificate.Name(userID: nil, commonName: "test issuer", organizationalUnit: "test unit", organization: "test org")
7273
)
7374
)
74-
callback(.success(Model.SignedCollection(collection: collection, signature: signature)))
75+
return Model.SignedCollection(collection: collection, signature: signature)
7576
}
7677
}

0 commit comments

Comments
 (0)