Skip to content

Commit 782efe9

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

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
@@ -194,7 +194,7 @@ struct GitHubPackageMetadataProvider: PackageMetadataProvider {
194194
options.validResponseCodes = validResponseCodes
195195
options.authorizationProvider = { url in
196196
url.host.flatMap { host in
197-
self.configuration.authTokens?[.github(host)].flatMap { token in
197+
self.configuration.authTokens()?[.github(host)].flatMap { token in
198198
"token \(token)"
199199
}
200200
}
@@ -212,22 +212,22 @@ struct GitHubPackageMetadataProvider: PackageMetadataProvider {
212212
}
213213

214214
public struct Configuration {
215+
public var authTokens: () -> [AuthTokenType: String]?
215216
public var apiLimitWarningThreshold: Int
216-
public var authTokens: [AuthTokenType: String]?
217217
public var cacheDir: AbsolutePath
218-
public var cacheSizeInMegabytes: Int
219218
public var cacheTTLInSeconds: Int
219+
public var cacheSizeInMegabytes: Int
220220

221-
public init(authTokens: [AuthTokenType: String]? = nil,
221+
public init(authTokens: @escaping () -> [AuthTokenType: String]? = { nil },
222222
apiLimitWarningThreshold: Int? = nil,
223223
cacheDir: AbsolutePath? = nil,
224224
cacheTTLInSeconds: Int? = nil,
225225
cacheSizeInMegabytes: Int? = nil) {
226226
self.authTokens = authTokens
227227
self.apiLimitWarningThreshold = apiLimitWarningThreshold ?? 5
228228
self.cacheDir = cacheDir.map(resolveSymlinks) ?? localFileSystem.swiftPMCacheDirectory.appending(components: "package-metadata")
229-
self.cacheSizeInMegabytes = cacheSizeInMegabytes ?? 10
230229
self.cacheTTLInSeconds = cacheTTLInSeconds ?? 3600
230+
self.cacheSizeInMegabytes = cacheSizeInMegabytes ?? 10
231231
}
232232
}
233233

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("api.github.com"): token]
338+
configuration.authTokens = { [.github("api.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)