Skip to content

Commit ed508ae

Browse files
authored
[Collections] Auth tokens need to be mutable (#3443) (#3458)
* [Collections] Auth tokens need to be mutable rdar://77095793 * Change authTokens to be a function instead
1 parent b310705 commit ed508ae

File tree

5 files changed

+45
-12
lines changed

5 files changed

+45
-12
lines changed

Sources/PackageCollections/PackageCollections+Configuration.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2020 Apple Inc. and the Swift project authors
4+
Copyright (c) 2020-2021 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See http://swift.org/LICENSE.txt for license information
@@ -16,9 +16,9 @@ extension PackageCollections {
1616
// JSONPackageCollectionValidator: maximumPackageCount, maximumMajorVersionCount, maximumMinorVersionCount
1717

1818
/// Auth tokens for the collections or metadata provider
19-
public var authTokens: [AuthTokenType: String]?
19+
public var authTokens: () -> [AuthTokenType: String]?
2020

21-
public init(authTokens: [AuthTokenType: String]? = nil) {
21+
public init(authTokens: @escaping () -> [AuthTokenType: String]? = { nil }) {
2222
self.authTokens = authTokens
2323
}
2424
}

Sources/PackageCollections/PackageCollections.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ public struct PackageCollections: PackageCollectionsProtocol {
2121
static let isSupportedPlatform = false
2222
#endif
2323

24-
private let configuration: Configuration
24+
let configuration: Configuration
2525
private let diagnosticsEngine: DiagnosticsEngine?
2626
private let storageContainer: (storage: Storage, owned: Bool)
2727
private let collectionProviders: [Model.CollectionSourceType: PackageCollectionProvider]
28-
private let metadataProvider: PackageMetadataProvider
28+
let metadataProvider: PackageMetadataProvider
2929

3030
private var storage: Storage {
3131
self.storageContainer.storage

Sources/PackageCollections/Providers/GitHubPackageMetadataProvider.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ struct GitHubPackageMetadataProvider: PackageMetadataProvider {
197197
options.authorizationProvider = { url in
198198
url.host.flatMap { host in
199199
let host = host.hasPrefix(Self.apiHostPrefix) ? String(host.dropFirst(Self.apiHostPrefix.count)) : host
200-
return self.configuration.authTokens?[.github(host)].flatMap { token in
200+
return self.configuration.authTokens()?[.github(host)].flatMap { token in
201201
"token \(token)"
202202
}
203203
}
@@ -215,22 +215,22 @@ struct GitHubPackageMetadataProvider: PackageMetadataProvider {
215215
}
216216

217217
public struct Configuration {
218+
public var authTokens: () -> [AuthTokenType: String]?
218219
public var apiLimitWarningThreshold: Int
219-
public var authTokens: [AuthTokenType: String]?
220220
public var cacheDir: AbsolutePath
221-
public var cacheSizeInMegabytes: Int
222221
public var cacheTTLInSeconds: Int
222+
public var cacheSizeInMegabytes: Int
223223

224-
public init(authTokens: [AuthTokenType: String]? = nil,
224+
public init(authTokens: @escaping () -> [AuthTokenType: String]? = { nil },
225225
apiLimitWarningThreshold: Int? = nil,
226226
cacheDir: AbsolutePath? = nil,
227227
cacheTTLInSeconds: Int? = nil,
228228
cacheSizeInMegabytes: Int? = nil) {
229229
self.authTokens = authTokens
230230
self.apiLimitWarningThreshold = apiLimitWarningThreshold ?? 5
231231
self.cacheDir = cacheDir.map(resolveSymlinks) ?? localFileSystem.swiftPMCacheDirectory.appending(components: "package-metadata")
232-
self.cacheSizeInMegabytes = cacheSizeInMegabytes ?? 10
233232
self.cacheTTLInSeconds = cacheTTLInSeconds ?? 3600
233+
self.cacheSizeInMegabytes = cacheSizeInMegabytes ?? 10
234234
}
235235
}
236236

Tests/PackageCollectionsTests/GitHubPackageMetadataProviderTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ class GitHubPackageMetadataProviderTests: XCTestCase {
226226
var configuration = GitHubPackageMetadataProvider.Configuration()
227227
configuration.cacheDir = tmpPath
228228
var provider = GitHubPackageMetadataProvider(configuration: configuration, httpClient: httpClient)
229-
provider.configuration.authTokens = authTokens
229+
provider.configuration.authTokens = { authTokens }
230230
defer { XCTAssertNoThrow(try provider.close()) }
231231

232232
let reference = PackageReference(repository: RepositorySpecifier(url: repoURL))
@@ -335,7 +335,7 @@ class GitHubPackageMetadataProviderTests: XCTestCase {
335335
httpClient.configuration.requestHeaders!.add(name: "Cache-Control", value: "no-cache")
336336
var configuration = GitHubPackageMetadataProvider.Configuration()
337337
if let token = ProcessEnv.vars["GITHUB_API_TOKEN"] {
338-
configuration.authTokens = [.github("github.com"): token]
338+
configuration.authTokens = { [.github("github.com"): token] }
339339
}
340340
configuration.apiLimitWarningThreshold = 50
341341
configuration.cacheTTLInSeconds = -1 // Disable cache so we hit the API

Tests/PackageCollectionsTests/PackageCollectionsTests.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,39 @@ import TSCBasic
1818
import TSCUtility
1919

2020
final class PackageCollectionsTests: XCTestCase {
21+
func testUpdateAuthTokens() throws {
22+
var authTokens: [AuthTokenType: String]? = [:]
23+
24+
let configuration = PackageCollections.Configuration(authTokens: { authTokens })
25+
let storage = makeMockStorage()
26+
defer { XCTAssertNoThrow(try storage.close()) }
27+
28+
// disable cache for this test to avoid setup/cleanup
29+
let metadataProviderConfig = GitHubPackageMetadataProvider.Configuration(authTokens: configuration.authTokens, cacheTTLInSeconds: -1)
30+
let metadataProvider = GitHubPackageMetadataProvider(configuration: metadataProviderConfig)
31+
let packageCollections = PackageCollections(configuration: configuration, storage: storage, collectionProviders: [:], metadataProvider: metadataProvider)
32+
33+
XCTAssertEqual(0, packageCollections.configuration.authTokens()?.count)
34+
do {
35+
guard let githubMetadataProvider = packageCollections.metadataProvider as? GitHubPackageMetadataProvider else {
36+
return XCTFail("Expected GitHubPackageMetadataProvider")
37+
}
38+
XCTAssertEqual(0, githubMetadataProvider.configuration.authTokens()?.count)
39+
}
40+
41+
authTokens![.github("github.test")] = "topsekret"
42+
43+
// Check that authTokens change is propagated to PackageMetadataProvider
44+
XCTAssertEqual(1, packageCollections.configuration.authTokens()?.count)
45+
do {
46+
guard let githubMetadataProvider = packageCollections.metadataProvider as? GitHubPackageMetadataProvider else {
47+
return XCTFail("Expected GitHubPackageMetadataProvider")
48+
}
49+
XCTAssertEqual(1, githubMetadataProvider.configuration.authTokens()?.count)
50+
XCTAssertEqual(authTokens, githubMetadataProvider.configuration.authTokens())
51+
}
52+
}
53+
2154
func testBasicRegistration() throws {
2255
try skipIfUnsupportedPlatform()
2356

0 commit comments

Comments
 (0)