-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Update auth logic #3789
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
Update auth logic #3789
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,76 +8,108 @@ | |
See http://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
*/ | ||
|
||
import Basics | ||
@testable import Basics | ||
@testable import Commands | ||
import SPMTestSupport | ||
import TSCBasic | ||
import XCTest | ||
|
||
final class SwiftToolTests: XCTestCase { | ||
func testNetrcLocations() throws { | ||
func testNetrcAuthorizationProviders() throws { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. <3 |
||
fixture(name: "DependencyResolution/External/XCFramework") { packageRoot in | ||
let fs = localFileSystem | ||
|
||
let localPath = packageRoot.appending(component: ".netrc") | ||
let userHomePath = fs.homeDirectory.appending(component: ".netrc") | ||
|
||
// custom .netrc file | ||
|
||
do { | ||
let customPath = fs.homeDirectory.appending(component: UUID().uuidString) | ||
try fs.writeFileContents(customPath) { | ||
"machine mymachine.labkey.org login [email protected] password custom" | ||
} | ||
|
||
|
||
let options = try SwiftToolOptions.parse(["--package-path", packageRoot.pathString, "--netrc-file", customPath.pathString]) | ||
let tool = try SwiftTool(options: options) | ||
XCTAssertEqual(try tool.getNetrcConfigFile().map(resolveSymlinks), resolveSymlinks(customPath)) | ||
|
||
let netrcProviders = try tool.getNetrcAuthorizationProviders() | ||
XCTAssertEqual(netrcProviders.count, 1) | ||
XCTAssertEqual(netrcProviders.first.map { resolveSymlinks($0.path) }, resolveSymlinks(customPath)) | ||
|
||
let auth = try tool.getAuthorizationProvider()?.authentication(for: URL(string: "https://mymachine.labkey.org")!) | ||
XCTAssertEqual(auth?.user, "[email protected]") | ||
XCTAssertEqual(auth?.password, "custom") | ||
|
||
// delete it | ||
try localFileSystem.removeFileTree(customPath) | ||
XCTAssertThrowsError(try tool.getNetrcConfigFile(), "error expected") { error in | ||
XCTAssertThrowsError(try tool.getNetrcAuthorizationProviders(), "error expected") { error in | ||
XCTAssertEqual(error as? StringError, StringError("Did not find .netrc file at \(customPath).")) | ||
} | ||
} | ||
|
||
// local .netrc file | ||
|
||
do { | ||
let localPath = packageRoot.appending(component: ".netrc") | ||
// make sure there isn't a user home one | ||
try localFileSystem.removeFileTree(userHomePath) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The tests probably shouldn't touch user's .netrc file |
||
|
||
try fs.writeFileContents(localPath) { | ||
return "machine mymachine.labkey.org login [email protected] password local" | ||
} | ||
|
||
let options = try SwiftToolOptions.parse(["--package-path", packageRoot.pathString]) | ||
let tool = try SwiftTool(options: options) | ||
|
||
let netrcProviders = try tool.getNetrcAuthorizationProviders() | ||
XCTAssertEqual(netrcProviders.count, 1) | ||
XCTAssertEqual(netrcProviders.first.map { resolveSymlinks($0.path) }, resolveSymlinks(localPath)) | ||
|
||
XCTAssertEqual(try tool.getNetrcConfigFile().map(resolveSymlinks), resolveSymlinks(localPath)) | ||
let auth = try tool.getAuthorizationProvider()?.authentication(for: URL(string: "https://mymachine.labkey.org")!) | ||
XCTAssertEqual(auth?.user, "[email protected]") | ||
XCTAssertEqual(auth?.password, "local") | ||
} | ||
|
||
// user .netrc file | ||
|
||
do { | ||
// make sure there isn't a local one | ||
try localFileSystem.removeFileTree(packageRoot.appending(component: ".netrc")) | ||
try localFileSystem.removeFileTree(localPath) | ||
|
||
let userHomePath = fs.homeDirectory.appending(component: ".netrc") | ||
try fs.writeFileContents(userHomePath) { | ||
return "machine mymachine.labkey.org login [email protected] password user" | ||
} | ||
|
||
let options = try SwiftToolOptions.parse(["--package-path", packageRoot.pathString]) | ||
let tool = try SwiftTool(options: options) | ||
|
||
XCTAssertEqual(try tool.getNetrcConfigFile().map(resolveSymlinks), resolveSymlinks(userHomePath)) | ||
let netrcProviders = try tool.getNetrcAuthorizationProviders() | ||
XCTAssertEqual(netrcProviders.count, 1) | ||
XCTAssertEqual(netrcProviders.first.map { resolveSymlinks($0.path) }, resolveSymlinks(userHomePath)) | ||
|
||
let auth = try tool.getAuthorizationProvider()?.authentication(for: URL(string: "https://mymachine.labkey.org")!) | ||
XCTAssertEqual(auth?.user, "[email protected]") | ||
XCTAssertEqual(auth?.password, "user") | ||
} | ||
|
||
// both local and user .netrc file | ||
do { | ||
try fs.writeFileContents(localPath) { | ||
return "machine mymachine.labkey.org login [email protected] password local" | ||
} | ||
try fs.writeFileContents(userHomePath) { | ||
return "machine mymachine.labkey.org login [email protected] password user" | ||
} | ||
|
||
let options = try SwiftToolOptions.parse(["--package-path", packageRoot.pathString]) | ||
let tool = try SwiftTool(options: options) | ||
|
||
let netrcProviders = try tool.getNetrcAuthorizationProviders() | ||
XCTAssertEqual(netrcProviders.count, 2) | ||
XCTAssertEqual(netrcProviders.map { resolveSymlinks($0.path) }, [localPath, userHomePath].map(resolveSymlinks)) | ||
|
||
// local before user .netrc file | ||
let auth = try tool.getAuthorizationProvider()?.authentication(for: URL(string: "https://mymachine.labkey.org")!) | ||
XCTAssertEqual(auth?.user, "[email protected]") | ||
XCTAssertEqual(auth?.password, "local") | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my expectation as a user would be a single netrc file loaded, with the following priority:
@yim-lee @mattt did you have a different semantic in mind?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For
.netrc
files, I think that's a good option, yeah.Alternatively, we could say:
--netrc-path
, use only that./.netrc
) and fallback to the user shared (~/.netrc
)This approach would give us the convenience of cascading, while being able to keep the behavior you described (for example, if we only wanted to load the local or shared
.netrc
file, we could pass it as--netrc-file
).The only downside is that this is more complex, and lacking sufficient diagnostics, could make it difficult for the user to understand where credentials are coming from.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
personally as a use I would expect the former (ie only load a single file, no fallback / merge with shared one). maybe we can investigate what other tools are doing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also cc @neonichu @abertelrud for opinions
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like curl only supports single location, either one specified with a flag or
~/.netrc
: https://everything.curl.dev/usingcurl/netrc#the-netrc-file-formatI wonder if this is a good policy for us to due to security reasons - so users don't want to accidentally check in the
.netrc
file@robertlacroix @andyp-apple opinions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW, we added
.netrc
to the default project template.gitignore
file with #3511, which helps mitigate this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is my understanding of the expected behavior described in SE-0292 and what I think I've implemented in this PR. My assumption is that local netrc would not have as many credentials as the user one (if both files exist), so for a given URL if credentials is not found in local netrc then looking for it in user netrc sounds reasonable to me.
It sounds like based on @tomerd and @mattt's comments that a single netrc file is fine. If that's the case I will remove this part of the changes and reduce the PR to simply add keychain auth.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yim-lee yes what you did is per spec, and correct. my question is more conceptual: if we got the spec right or if that would surprise users. there is also the option of not supporting project local at all IMO. in any case, I will leave it to @mattt to decide as he is the proposal author, unless we hear from the security folks something stronger.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yim-lee @tomerd Since we have the spec from SE-0292 and this PR that implements the spec, I think we should go with that for now. If we find that this behavior is confusing or not what we want, we can amend this and update the spec / documentation in tandem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sgtm