Skip to content

Include signing entity in version metadata #6363

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
Apr 4, 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
3 changes: 3 additions & 0 deletions Sources/PackageMetadata/PackageMetadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public struct Package {
}

public struct PackageSearchClient {
private let fileSystem: FileSystem
private let registryClient: RegistryClient
private let indexAndCollections: PackageIndexAndCollections
private let observabilityScope: ObservabilityScope
Expand All @@ -59,6 +60,7 @@ public struct PackageSearchClient {
) {
self.registryClient = registryClient
self.indexAndCollections = PackageIndexAndCollections(fileSystem: fileSystem, observabilityScope: observabilityScope)
self.fileSystem = fileSystem
self.observabilityScope = observabilityScope
}

Expand Down Expand Up @@ -87,6 +89,7 @@ public struct PackageSearchClient {
self.registryClient.getPackageVersionMetadata(
package: package,
version: version,
fileSystem: self.fileSystem,
observabilityScope: observabilityScope,
callbackQueue: DispatchQueue.sharedConcurrent
) { result in
Expand Down
26 changes: 25 additions & 1 deletion Sources/PackageRegistry/RegistryClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ public final class RegistryClient: Cancellable {
package: PackageIdentity,
version: Version,
timeout: DispatchTimeInterval? = .none,
fileSystem: FileSystem,
observabilityScope: ObservabilityScope,
callbackQueue: DispatchQueue,
completion: @escaping (Result<PackageVersionMetadata, Error>) -> Void
Expand All @@ -286,6 +287,7 @@ public final class RegistryClient: Cancellable {
package: registryIdentity,
version: version,
timeout: timeout,
fileSystem: fileSystem,
observabilityScope: observabilityScope,
callbackQueue: callbackQueue,
completion: completion
Expand Down Expand Up @@ -314,6 +316,7 @@ public final class RegistryClient: Cancellable {
package: PackageIdentity.RegistryIdentity,
version: Version,
timeout: DispatchTimeInterval?,
fileSystem: FileSystem,
observabilityScope: ObservabilityScope,
callbackQueue: DispatchQueue,
completion: @escaping (Result<PackageVersionMetadata, Error>) -> Void
Expand Down Expand Up @@ -343,6 +346,22 @@ public final class RegistryClient: Cancellable {
signatureBase64Encoded: $0.signatureBase64Encoded,
signatureFormat: $0.signatureFormat
)
},
signingEntity: $0.signing.flatMap {
guard let signatureData = Data(base64Encoded: $0.signatureBase64Encoded) else {
return nil
}
guard let signatureFormat = SignatureFormat(rawValue: $0.signatureFormat) else {
return nil
}
let configuration = self.configuration.signing(for: package, registry: registry)
return try? tsc_await { SignatureValidation.extractSigningEntity(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the method itself is async, so we wont need to use tsc_await here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can see, nothing in RegistryClient is async.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what I mean is that this method takes a completion handler. so we dont need to use tsc_await, we can chain the handlers

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, now I get what you mean. We could do that but I think that'll make this code much more complicated tbh. We can have zero and also more than one resource with signing info, so there'll be a bunch of logic for aggregating the results. IMO seems better to just do this for now and eventually replace with real await instead.

signature: [UInt8](signatureData),
signatureFormat: signatureFormat,
configuration: configuration,
fileSystem: fileSystem,
completion: $0
) }
}
)
},
Expand Down Expand Up @@ -501,6 +520,7 @@ public final class RegistryClient: Cancellable {
package: package,
version: version,
timeout: timeout,
fileSystem: localFileSystem,
observabilityScope: observabilityScope,
callbackQueue: callbackQueue
) { result in
Expand Down Expand Up @@ -733,6 +753,7 @@ public final class RegistryClient: Cancellable {
package: package,
version: version,
timeout: timeout,
fileSystem: localFileSystem,
observabilityScope: observabilityScope,
callbackQueue: callbackQueue
) { result in
Expand Down Expand Up @@ -950,6 +971,7 @@ public final class RegistryClient: Cancellable {
package: package,
version: version,
timeout: timeout,
fileSystem: fileSystem,
observabilityScope: observabilityScope,
callbackQueue: callbackQueue
) { result in
Expand Down Expand Up @@ -1839,12 +1861,14 @@ extension RegistryClient {
public let type: String
public let checksum: String?
public let signing: Signing?
public let signingEntity: SigningEntity?

public init(name: String, type: String, checksum: String?, signing: Signing?) {
public init(name: String, type: String, checksum: String?, signing: Signing?, signingEntity: SigningEntity?) {
self.name = name
self.type = type
self.checksum = checksum
self.signing = signing
self.signingEntity = signingEntity
}
}

Expand Down
24 changes: 24 additions & 0 deletions Sources/PackageRegistry/SignatureValidation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,30 @@ struct SignatureValidation {
}
}
}

// MARK: - signing entity

static func extractSigningEntity(
signature: [UInt8],
signatureFormat: SignatureFormat,
configuration: RegistryConfiguration.Security.Signing,
fileSystem: FileSystem,
completion: @Sendable @escaping (Result<SigningEntity?, Error>) -> Void
) {
Task {
do {
let verifierConfiguration = try VerifierConfiguration.from(configuration, fileSystem: fileSystem)
let signingEntity = try await SignatureProvider.extractSigningEntity(
signature: signature,
format: signatureFormat,
verifierConfiguration: verifierConfiguration
)
return completion(.success(signingEntity))
} catch {
return completion(.failure(error))
}
}
}
}

extension VerifierConfiguration {
Expand Down
24 changes: 24 additions & 0 deletions Sources/PackageSigning/SignatureProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ public enum SignatureProvider {
observabilityScope: observabilityScope
)
}

public static func extractSigningEntity(
signature: [UInt8],
format: SignatureFormat,
verifierConfiguration: VerifierConfiguration
) async throws -> SigningEntity {
let provider = format.provider
return try await provider.extractSigningEntity(
signature: signature,
verifierConfiguration: verifierConfiguration
)
}
}

public struct VerifierConfiguration {
Expand Down Expand Up @@ -162,6 +174,11 @@ protocol SignatureProviderProtocol {
verifierConfiguration: VerifierConfiguration,
observabilityScope: ObservabilityScope
) async throws -> SignatureStatus

func extractSigningEntity(
signature: [UInt8],
verifierConfiguration: VerifierConfiguration
) async throws -> SigningEntity
}

// MARK: - CMS signature provider
Expand Down Expand Up @@ -232,6 +249,13 @@ struct CMSSignatureProvider: SignatureProviderProtocol {
}
}

func extractSigningEntity(
signature: [UInt8],
verifierConfiguration: VerifierConfiguration
) async throws -> SigningEntity {
throw StringError("not implemented")
}

func status(
signature: [UInt8],
content: [UInt8],
Expand Down
2 changes: 1 addition & 1 deletion Sources/PackageSigning/SigningEntity/SigningEntity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

// MARK: - SigningEntity is the entity that generated the signature

public enum SigningEntity: Hashable, Codable, CustomStringConvertible {
public enum SigningEntity: Hashable, Codable, CustomStringConvertible, Sendable {
case recognized(type: SigningEntityType, name: String, organizationalUnit: String, organization: String)
case unrecognized(name: String?, organizationalUnit: String?, organization: String?)

Expand Down
1 change: 1 addition & 0 deletions Tests/PackageRegistryTests/RegistryClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3837,6 +3837,7 @@ extension RegistryClient {
self.getPackageVersionMetadata(
package: package,
version: version,
fileSystem: InMemoryFileSystem(),
observabilityScope: ObservabilitySystem.NOOP,
callbackQueue: .sharedConcurrent,
completion: $0
Expand Down