Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add token authentication support to SwiftPM #5838

Merged
merged 20 commits into from
Jan 6, 2023

Conversation

yim-lee
Copy link
Contributor

@yim-lee yim-lee commented Oct 25, 2022

Evolution proposal: swiftlang/swift-evolution#1820

@yim-lee yim-lee marked this pull request as draft October 25, 2022 01:30
// marked internal for testing
internal let path: AbsolutePath
private let fileSystem: FileSystem

private let cache = ThreadSafeKeyValueStore<String, (user: String, password: String)>()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is for adding credentials to memory so RegistryClient can read them for the login request.

@@ -96,9 +109,16 @@ public struct NetrcAuthorizationProvider: AuthorizationProvider {
callback(.failure(AuthorizationProviderError.other("Failed to update netrc file at \(self.path): \(error)")))
}
}

public func remove(for url: URL, callback: @escaping (Result<Void, Error>) -> Void) {
callback(.failure(AuthorizationProviderError.other("User must edit netrc file at \(self.path) manually to remove entries")))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can't reliably update .netrc (e.g., we might delete comments by accident), so simply not support it for now.

}

return providers.isEmpty ? .none : CompositeAuthorizationProvider(providers, observabilityScope: observabilityScope)
// Use at-most one AuthorizationProvider (i.e., no CompositeAuthorizationProvider)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

👀

Copy link
Contributor

Choose a reason for hiding this comment

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

this could be an issue for users that rely on netrc for example for binary dependencies, so we may need to add an option allowing macOS users to "force netrc mode" as they transition

Copy link
Contributor

Choose a reason for hiding this comment

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

cc @neonichu for opinions

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added forceNetrc option

guard let server = url.authenticationID else {
return callback(.failure(AuthorizationProviderError.invalidURLHost))
}

if !persist {
Copy link
Contributor

Choose a reason for hiding this comment

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

is this mainly for testing, or more broadly needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is general feature. login must test that the credentials work because persisting them.

var password: String?

@Option(help: "Access token")
var token: String?
Copy link
Contributor

Choose a reason for hiding this comment

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

are token and password mutually exclusive? if so, it think argument parser allows us to mark them as such

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Either username with(out) password, or token

Your credentials will be written out to .netrc.
""")
print("Continue? (Y/N): ")
guard readLine()?.lowercased() == "y" else {
Copy link
Contributor

Choose a reason for hiding this comment

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

@neonichu @abertelrud is this the recommended way to read input?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For plugin we do this:

                            // We can ask the user directly, so we do so.
                            let query = "Allow this plugin to write to the package directory?"
                            swiftTool.outputStream.write("\(problem)\n\(reason)\n\(query) (yes/no) ".utf8)
                            swiftTool.outputStream.flush()
                            let answer = readLine(strippingNewline: true)
                            // Throw an error if we didn't get permission.
                            if answer?.lowercased() != "yes" {
                                throw ValidationError("Plugin was denied permission to write to the package directory.")
                            }

Updating valid answers to yes/no and adding strippingNewline: true to readLine call.

@yim-lee yim-lee force-pushed the registry-authn branch 2 times, most recently from 72df48a to f69fee1 Compare November 2, 2022 03:43
@yim-lee yim-lee marked this pull request as ready for review December 13, 2022 07:08
@yim-lee yim-lee changed the title [Draft] Add token authentication support to SwiftPM Add token authentication support to SwiftPM Dec 13, 2022
@yim-lee
Copy link
Contributor Author

yim-lee commented Dec 13, 2022

@swift-ci smoke test

@yim-lee
Copy link
Contributor Author

yim-lee commented Dec 13, 2022

@swift-ci please smoke test

1 similar comment
@yim-lee
Copy link
Contributor Author

yim-lee commented Dec 13, 2022

@swift-ci please smoke test

@yim-lee
Copy link
Contributor Author

yim-lee commented Dec 14, 2022

@swift-ci please smoke test windows

@yim-lee
Copy link
Contributor Author

yim-lee commented Dec 14, 2022

This is ready for review.

@neonichu
Copy link
Contributor

@swift-ci please smoke test windows

1 similar comment
@yim-lee
Copy link
Contributor Author

yim-lee commented Dec 16, 2022

@swift-ci please smoke test windows

@yim-lee
Copy link
Contributor Author

yim-lee commented Jan 5, 2023

@swift-ci please smoke test

@yim-lee
Copy link
Contributor Author

yim-lee commented Jan 5, 2023

@swift-ci please smoke test windows

@yim-lee
Copy link
Contributor Author

yim-lee commented Jan 5, 2023

@swift-ci please smoke test

@yim-lee
Copy link
Contributor Author

yim-lee commented Jan 5, 2023

@swift-ci please smoke test windows

2 similar comments
@yim-lee
Copy link
Contributor Author

yim-lee commented Jan 5, 2023

@swift-ci please smoke test windows

@yim-lee
Copy link
Contributor Author

yim-lee commented Jan 5, 2023

@swift-ci please smoke test windows

@yim-lee
Copy link
Contributor Author

yim-lee commented Jan 5, 2023

@swift-ci please smoke test macos

@yim-lee
Copy link
Contributor Author

yim-lee commented Jan 5, 2023

@swift-ci please test macos

1 similar comment
@yim-lee
Copy link
Contributor Author

yim-lee commented Jan 5, 2023

@swift-ci please test macos

@yim-lee
Copy link
Contributor Author

yim-lee commented Jan 5, 2023

@swift-ci please smoke test macos

1 similar comment
@yim-lee
Copy link
Contributor Author

yim-lee commented Jan 6, 2023

@swift-ci please smoke test macos

@yim-lee yim-lee merged commit b61c131 into swiftlang:main Jan 6, 2023
@yim-lee yim-lee deleted the registry-authn branch January 6, 2023 19:34
@tomerd
Copy link
Contributor

tomerd commented Jan 6, 2023

🥳

yim-lee added a commit to yim-lee/swift-package-manager that referenced this pull request Jan 6, 2023
* Add 'authentication' to registry configuration
* Add support for token authn to RegistryClient
* Registry login API
* Implement login subcommand
* Remove support for project-level .netrc file
* Implement logout subcommand
* RegistryClient has its own AuthorizationProvider
* Revert part of the broader changes for future rdar://101785031
* Give option to use netrc instead of OS cred store
* Formatting fixes
yim-lee added a commit that referenced this pull request Jan 9, 2023
* Add 'authentication' to registry configuration
* Add support for token authn to RegistryClient
* Registry login API
* Implement login subcommand
* Remove support for project-level .netrc file
* Implement logout subcommand
* RegistryClient has its own AuthorizationProvider
* Revert part of the broader changes for future rdar://101785031
* Give option to use netrc instead of OS cred store
* Formatting fixes
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants