Skip to content

Commit

Permalink
GR: Parse paths in npmrc auth fields correctly (#901)
Browse files Browse the repository at this point in the history
Should fix #899 
Changed the npmrc parsing logic when using `--data-source=native` so
that specifying registries with paths (e.g.
`//my.registry/package/path:_authToken`) will now correctly add the
authorization headers.

Used
[npm-registry-fetch](https://github.com/npm/npm-registry-fetch/tree/main)
as reference.
  • Loading branch information
michaelkedar committed Apr 10, 2024
1 parent a8c3536 commit 0280abf
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 118 deletions.
40 changes: 20 additions & 20 deletions internal/resolution/datasource/__snapshots__/npmrc_test.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

[TestNpmrcNoRegistries - 1]
https://registry.npmjs.org/@test%2Fpackage/1.2.3
https://registry.npmjs.org/@test%2fpackage/1.2.3
---

[TestNpmrcNoRegistries - 2]
Expand All @@ -18,7 +18,7 @@ https://registry1.test.com/foo
---

[TestNpmrcRegistryAuth - 3]
https://registry1.test.com/@test0%2Fbar
https://registry1.test.com/@test0%2fbar
---

[TestNpmrcRegistryAuth - 4]
Expand All @@ -28,7 +28,7 @@ https://registry1.test.com/@test0%2Fbar
---

[TestNpmrcRegistryAuth - 5]
https://registry2.test.com/@test1%2Fbaz
https://registry2.test.com/@test1%2fbaz
---

[TestNpmrcRegistryAuth - 6]
Expand All @@ -38,7 +38,7 @@ https://registry2.test.com/@test1%2Fbaz
---

[TestNpmrcRegistryAuth - 7]
https://sub.registry2.test.com/@test2%2Ftest
https://sub.registry2.test.com/@test2%2ftest
---

[TestNpmrcRegistryAuth - 8]
Expand All @@ -56,31 +56,31 @@ null
---

[TestNpmrcRegistryOverriding - 3]
https://general.global.registry.com/@general%2Fpkg
https://general.global.registry.com/@general%2fpkg
---

[TestNpmrcRegistryOverriding - 4]
null
---

[TestNpmrcRegistryOverriding - 5]
https://global.registry.com/@global%2Fpkg
https://global.registry.com/@global%2fpkg
---

[TestNpmrcRegistryOverriding - 6]
null
---

[TestNpmrcRegistryOverriding - 7]
https://user.registry.com/@user%2Fpkg
https://user.registry.com/@user%2fpkg
---

[TestNpmrcRegistryOverriding - 8]
null
---

[TestNpmrcRegistryOverriding - 9]
https://project.registry.com/@project%2Fpkg
https://project.registry.com/@project%2fpkg
---

[TestNpmrcRegistryOverriding - 10]
Expand All @@ -96,31 +96,31 @@ null
---

[TestNpmrcRegistryOverriding - 13]
https://general.user.registry.com/@general%2Fpkg
https://general.user.registry.com/@general%2fpkg
---

[TestNpmrcRegistryOverriding - 14]
null
---

[TestNpmrcRegistryOverriding - 15]
https://global.registry.com/@global%2Fpkg
https://global.registry.com/@global%2fpkg
---

[TestNpmrcRegistryOverriding - 16]
null
---

[TestNpmrcRegistryOverriding - 17]
https://user.registry.com/@user%2Fpkg
https://user.registry.com/@user%2fpkg
---

[TestNpmrcRegistryOverriding - 18]
null
---

[TestNpmrcRegistryOverriding - 19]
https://project.registry.com/@project%2Fpkg
https://project.registry.com/@project%2fpkg
---

[TestNpmrcRegistryOverriding - 20]
Expand All @@ -136,31 +136,31 @@ null
---

[TestNpmrcRegistryOverriding - 23]
https://general.project.registry.com/@general%2Fpkg
https://general.project.registry.com/@general%2fpkg
---

[TestNpmrcRegistryOverriding - 24]
null
---

[TestNpmrcRegistryOverriding - 25]
https://global.registry.com/@global%2Fpkg
https://global.registry.com/@global%2fpkg
---

[TestNpmrcRegistryOverriding - 26]
null
---

[TestNpmrcRegistryOverriding - 27]
https://user.registry.com/@user%2Fpkg
https://user.registry.com/@user%2fpkg
---

[TestNpmrcRegistryOverriding - 28]
null
---

[TestNpmrcRegistryOverriding - 29]
https://project.registry.com/@project%2Fpkg
https://project.registry.com/@project%2fpkg
---

[TestNpmrcRegistryOverriding - 30]
Expand All @@ -176,31 +176,31 @@ null
---

[TestNpmrcRegistryOverriding - 33]
https://general.project.registry.com/@general%2Fpkg
https://general.project.registry.com/@general%2fpkg
---

[TestNpmrcRegistryOverriding - 34]
null
---

[TestNpmrcRegistryOverriding - 35]
https://global.registry.com/@global%2Fpkg
https://global.registry.com/@global%2fpkg
---

[TestNpmrcRegistryOverriding - 36]
null
---

[TestNpmrcRegistryOverriding - 37]
https://user.registry.com/@user%2Fpkg
https://user.registry.com/@user%2fpkg
---

[TestNpmrcRegistryOverriding - 38]
null
---

[TestNpmrcRegistryOverriding - 39]
https://project.registry.com/@project%2Fpkg
https://project.registry.com/@project%2fpkg
---

[TestNpmrcRegistryOverriding - 40]
Expand Down
18 changes: 8 additions & 10 deletions internal/resolution/datasource/npm_registry_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
)

type npmRegistryCache struct {
Timestamp *time.Time
Details map[string]npmRegistryPackageDetails
RegistryURLs map[string]string
Timestamp *time.Time // Timestamp of when this cache was made
Details map[string]npmRegistryPackageDetails // For a package name, the versions & their dependencies, and the list of tags
ScopeURLs map[string]string // The URL of the registry used for a given package @scope. Used to invalidate cache if registry has changed.
}

func (c *NpmRegistryAPIClient) GobEncode() ([]byte, error) {
Expand All @@ -23,15 +23,13 @@ func (c *NpmRegistryAPIClient) GobEncode() ([]byte, error) {
}

cache := npmRegistryCache{
Timestamp: c.cacheTimestamp,
Details: c.details,
RegistryURLs: make(map[string]string),
Timestamp: c.cacheTimestamp,
Details: c.details,
ScopeURLs: make(map[string]string),
}

// store the registry URL for each scope (but not the auth info)
for scope, reg := range c.registries {
cache.RegistryURLs[scope] = reg.URL
}
cache.ScopeURLs = c.registries.ScopeURLs

return gobMarshal(&cache)
}
Expand All @@ -58,7 +56,7 @@ func (c *NpmRegistryAPIClient) GobDecode(b []byte) error {
scope, _, _ = strings.Cut(pkg, "/")
}

return cache.RegistryURLs[scope] != c.registries[scope].URL
return cache.ScopeURLs[scope] != c.registries.ScopeURLs[scope]
})

c.cacheTimestamp = cache.Timestamp
Expand Down
2 changes: 1 addition & 1 deletion internal/resolution/datasource/npm_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestNpmRegistryClient(t *testing.T) {

srv2 := testutility.NewMockHTTPServer(t)
srv2.SetAuthorization(t, "Bearer "+authToken)
srv2.SetResponseFromFile(t, "/@fake-registry%2Fa", "./fixtures/npm_registry/@fake-registry-a.json")
srv2.SetResponseFromFile(t, "/@fake-registry%2fa", "./fixtures/npm_registry/@fake-registry-a.json")

npmrcFile := createTempNpmrc(t, ".npmrc")
writeToNpmrc(t, npmrcFile,
Expand Down
Loading

0 comments on commit 0280abf

Please sign in to comment.