Skip to content

[Collections] Auth tokens need to be mutable #3443

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 2 commits into from
Apr 28, 2021
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
This source file is part of the Swift.org open source project

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

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

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

public init(authTokens: [AuthTokenType: String]? = nil) {
public init(authTokens: @escaping () -> [AuthTokenType: String]? = { nil }) {
self.authTokens = authTokens
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/PackageCollections/PackageCollections.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public struct PackageCollections: PackageCollectionsProtocol {
static let isSupportedPlatform = false
#endif

private let configuration: Configuration
let configuration: Configuration
private let diagnosticsEngine: DiagnosticsEngine?
private let storageContainer: (storage: Storage, owned: Bool)
private let collectionProviders: [Model.CollectionSourceType: PackageCollectionProvider]
private let metadataProvider: PackageMetadataProvider
let metadataProvider: PackageMetadataProvider

private var storage: Storage {
self.storageContainer.storage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ struct GitHubPackageMetadataProvider: PackageMetadataProvider {
options.validResponseCodes = validResponseCodes
options.authorizationProvider = { url in
url.host.flatMap { host in
self.configuration.authTokens?[.github(host)].flatMap { token in
self.configuration.authTokens()?[.github(host)].flatMap { token in
"token \(token)"
}
}
Expand All @@ -212,22 +212,22 @@ struct GitHubPackageMetadataProvider: PackageMetadataProvider {
}

public struct Configuration {
public var authTokens: () -> [AuthTokenType: String]?
public var apiLimitWarningThreshold: Int
public var authTokens: [AuthTokenType: String]?
public var cacheDir: AbsolutePath
public var cacheSizeInMegabytes: Int
public var cacheTTLInSeconds: Int
public var cacheSizeInMegabytes: Int

public init(authTokens: [AuthTokenType: String]? = nil,
public init(authTokens: @escaping () -> [AuthTokenType: String]? = { nil },
apiLimitWarningThreshold: Int? = nil,
cacheDir: AbsolutePath? = nil,
cacheTTLInSeconds: Int? = nil,
cacheSizeInMegabytes: Int? = nil) {
self.authTokens = authTokens
self.apiLimitWarningThreshold = apiLimitWarningThreshold ?? 5
self.cacheDir = cacheDir.map(resolveSymlinks) ?? localFileSystem.swiftPMCacheDirectory.appending(components: "package-metadata")
self.cacheSizeInMegabytes = cacheSizeInMegabytes ?? 10
self.cacheTTLInSeconds = cacheTTLInSeconds ?? 3600
self.cacheSizeInMegabytes = cacheSizeInMegabytes ?? 10
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class GitHubPackageMetadataProviderTests: XCTestCase {
var configuration = GitHubPackageMetadataProvider.Configuration()
configuration.cacheDir = tmpPath
var provider = GitHubPackageMetadataProvider(configuration: configuration, httpClient: httpClient)
provider.configuration.authTokens = authTokens
provider.configuration.authTokens = { authTokens }
defer { XCTAssertNoThrow(try provider.close()) }

let reference = PackageReference(repository: RepositorySpecifier(url: repoURL))
Expand Down Expand Up @@ -335,7 +335,7 @@ class GitHubPackageMetadataProviderTests: XCTestCase {
httpClient.configuration.requestHeaders!.add(name: "Cache-Control", value: "no-cache")
var configuration = GitHubPackageMetadataProvider.Configuration()
if let token = ProcessEnv.vars["GITHUB_API_TOKEN"] {
configuration.authTokens = [.github("api.github.com"): token]
configuration.authTokens = { [.github("api.github.com"): token] }
}
configuration.apiLimitWarningThreshold = 50
configuration.cacheTTLInSeconds = -1 // Disable cache so we hit the API
Expand Down
33 changes: 33 additions & 0 deletions Tests/PackageCollectionsTests/PackageCollectionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,39 @@ import TSCBasic
import TSCUtility

final class PackageCollectionsTests: XCTestCase {
func testUpdateAuthTokens() throws {
var authTokens: [AuthTokenType: String]? = [:]

let configuration = PackageCollections.Configuration(authTokens: { authTokens })
let storage = makeMockStorage()
defer { XCTAssertNoThrow(try storage.close()) }

// disable cache for this test to avoid setup/cleanup
let metadataProviderConfig = GitHubPackageMetadataProvider.Configuration(authTokens: configuration.authTokens, cacheTTLInSeconds: -1)
let metadataProvider = GitHubPackageMetadataProvider(configuration: metadataProviderConfig)
let packageCollections = PackageCollections(configuration: configuration, storage: storage, collectionProviders: [:], metadataProvider: metadataProvider)

XCTAssertEqual(0, packageCollections.configuration.authTokens()?.count)
do {
guard let githubMetadataProvider = packageCollections.metadataProvider as? GitHubPackageMetadataProvider else {
return XCTFail("Expected GitHubPackageMetadataProvider")
}
XCTAssertEqual(0, githubMetadataProvider.configuration.authTokens()?.count)
}

authTokens![.github("github.test")] = "topsekret"

// Check that authTokens change is propagated to PackageMetadataProvider
XCTAssertEqual(1, packageCollections.configuration.authTokens()?.count)
do {
guard let githubMetadataProvider = packageCollections.metadataProvider as? GitHubPackageMetadataProvider else {
return XCTFail("Expected GitHubPackageMetadataProvider")
}
XCTAssertEqual(1, githubMetadataProvider.configuration.authTokens()?.count)
XCTAssertEqual(authTokens, githubMetadataProvider.configuration.authTokens())
}
}

func testBasicRegistration() throws {
try skipIfUnsupportedPlatform()

Expand Down