Skip to content

feat(auth): Add netrc support for upstream authentication - #310

Merged
kalbasit merged 11 commits into
kalbasit:mainfrom
aciceri:netrc-support
Sep 24, 2025
Merged

kalbasit merged 11 commits into
kalbasit:mainfrom
aciceri:netrc-support

Conversation

@aciceri

@aciceri aciceri commented Sep 22, 2025 •

Copy link
Copy Markdown
Contributor

@kalbasit this is a proof of concept (the netrc path is still hardcoded) but seems to work. What do you think?

It works even if the netrc entry is without the username, exactly as nix does.

Since I'm not a go developer I got help form an LLM but I've double checked everything and it makes sense to me.

Assuming that you like what I did: how do you think that we should retrieve the netrc path? I see the following solutions:

  • we add an extra CLI argument for the netrc path
  • we call nix show-config --json (or do we parse /etc/nix/nix.conf?) and use the netrc file specified there

Closes #307

@aciceri aciceri changed the title Add netrc support feat: add netrc support Sep 22, 2025
@kalbasit

Copy link
Copy Markdown
Owner

nice @aciceri it looks good!

We should provide a command line flag --netrc-file that defaults to $HOME/.netrc. You can get to the use's homedir with this function. In my opinion we should not add support for parsing netrc from nix as not everyone runs it in a Nix environment; For instance, I run ncps on my Kubernetes cluster in a container.

@aciceri

aciceri commented Sep 23, 2025

Copy link
Copy Markdown
Contributor Author

Oh right, I didn't think about it. I'll add a --netrc-file argument that defaults to $HOME/.netrc then. If the file doesn't exist, both if you explicitly set the argument or leave the default, I'll just print out a warning, ok?

After having done this I just update the README and the PR is ready? I saw that the NixOS module lives in nixpkgs (I may open a PR there too). Do we want to add a test?

However the CI is failing for some reasons not due to my commit, perhaps missing secrets on external forks.

@kalbasit

kalbasit commented Sep 23, 2025 •

Copy link
Copy Markdown
Owner

I'll just print out a warning, ok?

It's not really necessary. None of my upstream caches needs a .netrc so warning would not help but confuse someone like me. We could however, if we get a 401 mention the netrc feature.

After having done this I just update the README and the PR is ready?

That's correct, at that point make it ready for review and I will test it/review it before I merge it.

I saw that the NixOS module lives in nixpkgs (I may open a PR there too). Do we want to add a test?

That's also correct but know that I won't merge the PR upstream until I release a new version of ncps, so make it a draft pr upstream and tag me. You don't need to add a test for this case as it's going to probably be difficult to do so.

However the CI is failing for some reasons not due to my commit, perhaps missing secrets on external forks.

Yes that's correct. You're the first PR to ncps from a fork and it seems to be failing. I will use your PR to fix it :)

Thanks again for working on this @aciceri

@aciceri
aciceri force-pushed the netrc-support branch 2 times, most recently from 6c1c500 to 4b5cf3a Compare September 23, 2025 20:40
@aciceri
aciceri marked this pull request as ready for review September 23, 2025 20:50
@aciceri

aciceri commented Sep 23, 2025 •

Copy link
Copy Markdown
Contributor Author

At the end I came up with this message:

2025-09-23T22:51:42+02:00 WRN failed to parse netrc file, proceeding without netrc authentication error="open netr: no such file or directory" cmd=serve

To me it's clear enough, honestly I would prefer knowing if the netrc is being used or not, it would help me a lot if for some reason I see that one of my authenticated cached doesn't seem to work.

Otherwise the message is something like:

2025-09-23T22:53:20+02:00 INF loaded netrc authentication credentials cmd=serve hostname=mycache.tld username=

The netrc file is parsed only once and then the credentials are injected into the various cache objects, I hope it's good as approach.
I've also added a mention to the new CLI argument in the README.

@kalbasit kalbasit left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Looking great so far, few comments to address. In the meantime I will work on fixing the tests!

Comment thread cmd/serve.go Resolved
Comment thread pkg/cache/upstream/cache_test.go Resolved
@kalbasit

Copy link
Copy Markdown
Owner

@aciceri also no need to maintain a good commit history or force push since I will squash commit anyways so the commit history (although preserved in this PR) is lost on the main branch.

@kalbasit

Copy link
Copy Markdown
Owner

@aciceri alright I fixed the tests in #316 and I merged main into your pr to test it.

Although, fyi you have a legitimate failing test to fix, just a missed New() change

ncps> # github.com/kalbasit/ncps/pkg/cache/healthcheck_test [github.com/kalbasit/ncps/pkg/cache/healthcheck.test]
ncps> pkg/cache/healthcheck/healthcheck_test.go:34:76: not enough arguments in call to upstream.New
ncps>   have (context.Context, *url.URL, []string)
ncps>   want (context.Context, *url.URL, []string, *upstream.NetrcCredentials)

@aciceri

aciceri commented Sep 24, 2025

Copy link
Copy Markdown
Contributor Author

As soon as you'll merge #315 I'll rebase onto main and I'll be able to build the checks locally. However in theory it should pass now.

@kalbasit

Copy link
Copy Markdown
Owner

Thank you, @aciceri this looks amazing! I pushed a nitpick to keep the flags ordered alphabetically but otherwise it's ready to merge. I will let GH do the honors.

@kalbasit kalbasit changed the title feat: add netrc support feat(cache/upstream): add netrc support Sep 24, 2025
@kalbasit kalbasit changed the title feat(cache/upstream): add netrc support feat(auth): Add netrc support for upstream authentication Sep 24, 2025
@kalbasit
kalbasit enabled auto-merge (squash) September 24, 2025 18:46
@aciceri

aciceri commented Sep 24, 2025 •

Copy link
Copy Markdown
Contributor Author

Thank you for this great piece of software!
I'll let you chase all the missing tests at this point, meanwhile I'll try to adjust the NixOS module and actually use this version on my system, indeed for now I've only tried it playing with nix copy and attic push.

@kalbasit
kalbasit merged commit 9de11fe into kalbasit:main Sep 24, 2025
kalbasit added a commit that referenced this pull request Sep 24, 2025
* origin/main:
  chore: Update nixpkgs and Go dependencies (#320)
  fix: Fix another flaky test (#319)
  feat(auth): Add netrc support for upstream authentication (#310)
  fix(CI): Do not run the docker-manifest if push is disabled (#318)
  fix(pkg/cache): Increase the wait time around asynchronous logic (#315)
  fix(CI): Do not attempt to access secrets for PRs from forks (#316)
  feat(Docker): Add /storage as Volume in the Docker image (#313)
  fix(Docker): Create /tmp in the Docker image (#312)
  feat(run): Accept extra arguments to ncps serve (#311)
  fix(GitHub): The Docker build on aarch64-linux (#308)
  chore(deps): update determinatesystems/nix-installer-action action to v20 (#306)
  fix(deps): update module golang.org/x/term to v0.35.0 (#304)
  fix(deps): update module github.com/xsam/otelsql to v0.40.0 (#303)
  fix(deps): update module golang.org/x/sync to v0.17.0 (#302)
  fix(deps): update module github.com/prometheus/client_golang to v1.23.2 (#300)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ncps netrc support

2 participants