scala-steward icon indicating copy to clipboard operation
scala-steward copied to clipboard

Collect HTTP Headers from Coursier for authentication

Open markvandertol opened this issue 3 years ago • 17 comments

This PR lets Scala Steward also pick up custom headers used for authentication. This is for example needed for Gitlab. I think looking at csrConfiguration.value.authenticationByRepositoryId is the cleanest and most generic way to achieve this.

Example snippet of a .sbt file from a project that sets a custom HTTP-header for authentication with Coursier:

resolvers += "gitlab-repo" at "https://gitlab.example.com/api/v4/groups/1/-/packages/maven",
csrConfiguration ~= _.addRepositoryAuthentication("gitlab-repo", Authentication(Seq( ("private-token", "xxx") )))

It works for SBT and Mill, but doesn't work for Maven

markvandertol avatar Feb 20 '22 17:02 markvandertol

Codecov Report

Merging #2533 (ffd36e5) into main (fcd5ea3) will decrease coverage by 0.67%. The diff coverage is 34.09%.

@@            Coverage Diff             @@
##             main    #2533      +/-   ##
==========================================
- Coverage   81.32%   80.65%   -0.68%     
==========================================
  Files         146      146              
  Lines        2592     2621      +29     
  Branches       43       45       +2     
==========================================
+ Hits         2108     2114       +6     
- Misses        484      507      +23     
Impacted Files Coverage Δ
.../scala/org/scalasteward/core/application/Cli.scala 100.00% <ø> (ø)
...a/org/scalasteward/mill/plugin/StewardPlugin.scala 0.00% <0.00%> (ø)
...la/org/scalasteward/sbt/plugin/StewardPlugin.scala 0.00% <0.00%> (ø)
...a/org/scalasteward/core/coursier/CoursierAlg.scala 71.11% <35.71%> (-14.19%) :arrow_down:
.../org/scalasteward/core/buildtool/mill/parser.scala 66.66% <50.00%> (-1.52%) :arrow_down:
...org/scalasteward/core/buildtool/maven/parser.scala 100.00% <100.00%> (ø)
...in/scala/org/scalasteward/core/data/Resolver.scala 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

codecov[bot] avatar Feb 21 '22 08:02 codecov[bot]

Thanks for having a look. I replaced the Option[List[Header]] with List[Header]. That indeed simplifies the code. I feel that encoding { "key": "foo", "value": "bar" } as object instead of array is clearer, the same as the credentials object is encoded as { "user": "foo", "pass", "bar" }. Also, because you could have more than one header. If you want I can change this.

markvandertol avatar Mar 05 '22 08:03 markvandertol

May I kindly request a review from the maintainers?

julienrf avatar Mar 11 '22 09:03 julienrf

@markvandertol, could u also update the gitlab docs on how we can use this new feature?

cipriansofronia avatar Mar 18 '22 07:03 cipriansofronia

@markvandertol, could u also update the gitlab docs on how we can use this new feature?

@cipriansofronia I've added a few lines explaining how to authenticate against a Gitlab repository. Does this clarify enough how it works? No specific code for Scala Steward is needed.

markvandertol avatar Apr 03 '22 17:04 markvandertol

Looking forward to this one being released, thanks a lot @markvandertol

jchapuis avatar Apr 05 '22 14:04 jchapuis

@fthomas can this PR get some ❤️ from you? 😇

cipriansofronia avatar Apr 07 '22 08:04 cipriansofronia

Is there anything preventing this PR from being merged? 🙂

markvandertol avatar Apr 13 '22 07:04 markvandertol

@fthomas Do you have a time to test this with your test Steward instance ??

exoego avatar Apr 15 '22 11:04 exoego

👋 is there anything holding up the merge at this point?

jchapuis avatar May 09 '22 06:05 jchapuis

wave is there anything holding up the merge at this point?

I am wondering that to. This would really be a good thing to have when managing dependencies in private gilab repos

kaffepanna avatar May 12 '22 12:05 kaffepanna

wave is there anything holding up the merge at this point?

I am wondering that to. This would really be a good thing to have when managing dependencies in private gilab repos

It is not only gitlb. Also Google Cloud Storage or Artifactregistry or managing packages in AWS need this feature.

987Nabil avatar May 12 '22 16:05 987Nabil

@markvandertol Sorry for waiting so long. Could you resolve the conflict? 🙇

exoego avatar Jun 15 '22 01:06 exoego

No worries. I resolved the merge conflict. 🙂

markvandertol avatar Jun 15 '22 07:06 markvandertol

Thanks Mark for this PR! It looks promising.

Any chance to have it merged some time soon?

michaltomanski avatar Jul 06 '22 08:07 michaltomanski

Thanks for having a look. I replaced the Option[List[Header]] with List[Header]. That indeed simplifies the code.

Having the fields optional allows to read in persisted RepoCaches of previous Scala Steward runs. If the header fields are non-optional, Scala Steward fails to read these files:

[info] 2022-07-14 14:48:58,983 INFO  ──────────── Steward scala-steward-org/scala-steward ────────────
[info] 2022-07-14 14:48:58,983 INFO  Check cache of scala-steward-org/scala-steward
[info] 2022-07-14 14:48:59,080 ERROR Failed to parse or decode JSON from /home/frank/data/code/scala-steward/core/workspace/store/repo_cache/v1/github/scala-steward-org/scala-steward/repo_cache.json
[info] io.circe.DecodingFailure$$anon$2: Attempt to decode value on failed cursor: DownField(headers),DownField(MavenRepository),DownArray,DownField(resolvers),DownArray,DownField(dependencyInfos)

Another alternative would be to bump the version of the repoCacheStore here. But for this small change I would prefer making the fields optional.

Other than that, this PR looks good to me.

fthomas avatar Jul 14 '22 13:07 fthomas

Thanks @fthomas for having a look at the PR.

I fixed the issue by using a default value of Nil for the headers, and using deriveConfiguredCodec as shown below.

  @annotation.nowarn("cat=unused-locals")
  implicit val resolverCodec: Codec[Resolver] = {
    implicit val customConfig: Configuration = Configuration.default.withDefaults
    deriveConfiguredCodec
  }

I had to suppress the warning, as the implicit is only used by a macro. I could also have used the compiler flag -Ywarn-macros:after, but that would be inconsistent with other @annotation.nowarn usages.

markvandertol avatar Aug 08 '22 21:08 markvandertol

Sorry for the late response.

exoego avatar Aug 11 '22 11:08 exoego

Any chance this can get shipped in a new release? 😃 🙏 thanks!

jchapuis avatar Oct 07 '22 08:10 jchapuis

Just noticed that this change broke StewardPlugin.scala for old sbt versions. See #2847 for details.

fthomas avatar Dec 21 '22 19:12 fthomas