-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
libstore: Add the ability to use different auth methods with Http Binary Caches #10584
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
Open
georgyo
wants to merge
2
commits into
NixOS:master
Choose a base branch
from
georgyo:authmethod
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 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
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -351,7 +351,9 @@ struct curlFileTransfer : public FileTransfer | |||||||||||
| curl_easy_setopt(req, CURLOPT_PIPEWAIT, 1); | ||||||||||||
| #endif | ||||||||||||
| #if LIBCURL_VERSION_NUM >= 0x072f00 | ||||||||||||
| if (fileTransferSettings.enableHttp2) | ||||||||||||
| // Our writeCallbackWrapper does not support rewinding which breaks | ||||||||||||
| // negotiate/kerberos auth over http/2. | ||||||||||||
| if (fileTransferSettings.enableHttp2 && request.authmethod != HttpAuthMethod::NEGOTIATE) | ||||||||||||
| curl_easy_setopt(req, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2TLS); | ||||||||||||
| else | ||||||||||||
| curl_easy_setopt(req, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); | ||||||||||||
|
|
@@ -397,6 +399,14 @@ struct curlFileTransfer : public FileTransfer | |||||||||||
| curl_easy_setopt(req, CURLOPT_SOCKOPTFUNCTION, cloexec_callback); | ||||||||||||
| #endif | ||||||||||||
|
|
||||||||||||
| curl_easy_setopt(req, CURLOPT_HTTPAUTH, request.authmethod); | ||||||||||||
| if (request.authmethod == HttpAuthMethod::NEGOTIATE) { | ||||||||||||
| curl_easy_setopt(req, CURLOPT_USERNAME, ""); | ||||||||||||
| curl_easy_setopt(req, CURLOPT_PASSWORD, ""); | ||||||||||||
|
Comment on lines
+404
to
+405
Member
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.
Suggested change
|
||||||||||||
| } else if (request.authmethod == HttpAuthMethod::BEARER) { | ||||||||||||
| curl_easy_setopt(req, CURLOPT_XOAUTH2_BEARER, request.bearer_token.c_str()); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| curl_easy_setopt(req, CURLOPT_CONNECTTIMEOUT, fileTransferSettings.connectTimeout.get()); | ||||||||||||
|
|
||||||||||||
| curl_easy_setopt(req, CURLOPT_LOW_SPEED_LIMIT, 1L); | ||||||||||||
|
|
||||||||||||
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 |
|---|---|---|
|
|
@@ -72,6 +72,7 @@ mkMesonLibrary (finalAttrs: { | |
| propagatedBuildInputs = [ | ||
| nix-util | ||
| nlohmann_json | ||
| curl | ||
| ]; | ||
|
|
||
| mesonFlags = | ||
|
|
||
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 |
|---|---|---|
| @@ -0,0 +1,245 @@ | ||
| # Run with: | ||
| # cd nixpkgs | ||
| # nix-build -A nixosTests.http-binary-cache-auth | ||
|
|
||
| # Test HTTP binary cache authentication methods (Basic, Bearer) | ||
| { | ||
| lib, | ||
| config, | ||
| nixpkgs, | ||
| ... | ||
| }: | ||
|
|
||
| let | ||
| pkgs = config.nodes.client.nixpkgs.pkgs; | ||
|
|
||
| # Test package to copy | ||
| testPkg = pkgs.hello; | ||
|
|
||
| # Basic auth credentials | ||
| basicUser = "testuser"; | ||
| basicPassword = "testpassword"; | ||
|
|
||
| # Bearer token for bearer auth test | ||
| bearerToken = "test-bearer-token-12345"; | ||
|
|
||
| in | ||
| { | ||
| name = "http-binary-cache-auth"; | ||
|
|
||
| nodes = { | ||
| # HTTP Binary Cache Server with authentication | ||
| server = | ||
| { config, pkgs, ... }: | ||
| { | ||
| virtualisation.writableStore = true; | ||
| virtualisation.additionalPaths = [ testPkg ]; | ||
|
|
||
| networking.firewall.allowedTCPPorts = [ | ||
| 80 | ||
| 8080 | ||
| 8081 | ||
| ]; | ||
|
|
||
| # Nginx with different auth methods | ||
| services.nginx = { | ||
| enable = true; | ||
|
|
||
| # For basic auth testing | ||
| virtualHosts."basic-auth" = { | ||
| listen = [ | ||
| { | ||
| addr = "0.0.0.0"; | ||
| port = 8080; | ||
| } | ||
| ]; | ||
|
|
||
| locations."/" = { | ||
| root = "/var/cache/nix-cache"; | ||
| extraConfig = '' | ||
| autoindex on; | ||
| auth_basic "Nix Binary Cache"; | ||
| auth_basic_user_file /etc/nginx/htpasswd; | ||
| ''; | ||
| }; | ||
| }; | ||
|
|
||
| # For bearer token auth testing | ||
| virtualHosts."bearer-auth" = { | ||
| listen = [ | ||
| { | ||
| addr = "0.0.0.0"; | ||
| port = 8081; | ||
| } | ||
| ]; | ||
|
|
||
| locations."/" = { | ||
| root = "/var/cache/nix-cache"; | ||
| extraConfig = '' | ||
| autoindex on; | ||
| set $auth_header $http_authorization; | ||
| if ($auth_header != "Bearer ${bearerToken}") { | ||
| return 401; | ||
| } | ||
| ''; | ||
| }; | ||
| }; | ||
|
|
||
| # No auth (for control test) | ||
| virtualHosts."no-auth" = { | ||
| listen = [ | ||
| { | ||
| addr = "0.0.0.0"; | ||
| port = 80; | ||
| } | ||
| ]; | ||
|
|
||
| locations."/" = { | ||
| root = "/var/cache/nix-cache"; | ||
| extraConfig = '' | ||
| autoindex on; | ||
| ''; | ||
| }; | ||
| }; | ||
| }; | ||
|
|
||
| environment.systemPackages = [ pkgs.curl ]; | ||
|
|
||
| nix.settings.substituters = lib.mkForce [ ]; | ||
| }; | ||
|
|
||
| # Client machine | ||
| client = | ||
| { config, pkgs, ... }: | ||
| { | ||
| virtualisation.writableStore = true; | ||
|
|
||
| environment.systemPackages = [ pkgs.curl ]; | ||
|
|
||
| nix.settings.substituters = lib.mkForce [ ]; | ||
| }; | ||
| }; | ||
|
|
||
| testScript = | ||
| { nodes }: | ||
| '' | ||
| import time | ||
|
|
||
| # fmt: off | ||
| start_all() | ||
|
|
||
| # Wait for services to be ready | ||
| server.wait_for_unit("nginx.service") | ||
| server.wait_for_open_port(80) | ||
| server.wait_for_open_port(8080) | ||
| server.wait_for_open_port(8081) | ||
|
|
||
| # Set up basic auth htpasswd file | ||
| server.succeed(""" | ||
| echo '${basicPassword}' | htpasswd -i -c /etc/nginx/htpasswd ${basicUser} | ||
| systemctl reload nginx | ||
| """) | ||
|
|
||
| # Create binary cache on server | ||
| server.succeed(""" | ||
| mkdir -p /var/cache/nix-cache | ||
| nix copy --to file:///var/cache/nix-cache ${testPkg} | ||
| """) | ||
|
|
||
| # Test that the binary cache was created properly | ||
| server.succeed("ls -la /var/cache/nix-cache/") | ||
| server.succeed("ls -la /var/cache/nix-cache/nar/") | ||
|
|
||
| # Test 1: No authentication (control) | ||
| print("Testing no authentication...") | ||
| client.succeed("curl -f http://server/") | ||
| client.succeed(""" | ||
| nix copy --from 'http://server' ${testPkg} --no-check-sigs | ||
| """) | ||
| client.succeed("nix path-info ${testPkg}") | ||
| client.succeed("nix-store --delete ${testPkg}") | ||
|
|
||
| # Test 2: Basic authentication | ||
| print("Testing Basic authentication...") | ||
|
|
||
| # Verify we can't access without auth | ||
| client.fail("curl -f http://server:8080/") | ||
|
|
||
| # Test curl with basic auth works | ||
| client.succeed("curl -f -u ${basicUser}:${basicPassword} http://server:8080/") | ||
|
|
||
| # Test nix with basic auth (default authmethod) | ||
| # Note: Nix uses libcurl which reads credentials from .netrc | ||
| client.succeed(""" | ||
| cat > ~/.netrc << EOF | ||
| machine server | ||
| login ${basicUser} | ||
| password ${basicPassword} | ||
| EOF | ||
| chmod 600 ~/.netrc | ||
| """) | ||
|
|
||
| client.succeed(""" | ||
| nix copy --from 'http://server:8080' ${testPkg} --no-check-sigs | ||
| """) | ||
|
|
||
| client.succeed("nix path-info ${testPkg}") | ||
| client.succeed("nix-store --delete ${testPkg}") | ||
| client.succeed("rm ~/.netrc") | ||
|
|
||
| # Test 3: Bearer token authentication | ||
| print("Testing Bearer token authentication...") | ||
|
|
||
| # Verify we can't access without auth | ||
| client.fail("curl -f http://server:8081/") | ||
|
|
||
| # Test curl with bearer token | ||
| client.succeed('curl -f -H "Authorization: Bearer ${bearerToken}" http://server:8081/') | ||
|
|
||
| # Test nix with bearer auth | ||
| client.succeed(""" | ||
| nix copy --from 'http://server:8081?authmethod=bearer&bearer-token=${bearerToken}' ${testPkg} --no-check-sigs | ||
| """) | ||
|
|
||
| client.succeed("nix path-info ${testPkg}") | ||
| client.succeed("nix-store --delete ${testPkg}") | ||
|
|
||
| # Test 4: Wrong bearer token should fail | ||
| print("Testing bearer token failure...") | ||
| client.fail(""" | ||
| nix copy --from 'http://server:8081?authmethod=bearer&bearer-token=wrong-token' ${testPkg} --no-check-sigs 2>&1 | ||
| """) | ||
|
|
||
| # Test 5: Using wrong auth method should fail | ||
| print("Testing auth method mismatch...") | ||
| # Try basic auth on bearer endpoint | ||
| client.fail(""" | ||
| cat > ~/.netrc << EOF | ||
| machine server | ||
| login ${basicUser} | ||
| password ${basicPassword} | ||
| EOF | ||
| chmod 600 ~/.netrc | ||
| nix copy --from 'http://server:8081?authmethod=basic' ${testPkg} --no-check-sigs 2>&1 | ||
| """) | ||
| client.succeed("rm ~/.netrc") | ||
|
|
||
| # Test 6: Test that authmethod parameter is parsed correctly | ||
| print("Testing authmethod parameter parsing...") | ||
|
|
||
| # Valid auth methods should not error on parsing | ||
| for method in ["basic", "digest", "negotiate", "ntlm", "bearer", "any", "anysafe"]: | ||
| # We expect these to fail due to auth, but not due to parsing | ||
| client.fail(f""" | ||
| timeout 5 nix copy --from 'http://server:8080?authmethod={method}' ${testPkg} --no-check-sigs 2>&1 | grep -v "unknown auth method" | ||
| """) | ||
|
|
||
| # Invalid auth method should error | ||
| result = client.fail(""" | ||
| nix copy --from 'http://server:8080?authmethod=invalid' ${testPkg} --no-check-sigs 2>&1 | ||
| """) | ||
| assert "unknown auth method 'invalid'" in result | ||
|
|
||
| print("All authentication tests passed!") | ||
| ''; | ||
| } |
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.
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.