-
Notifications
You must be signed in to change notification settings - Fork 313
feat: Add proper version parsing mapping for TuxCare advisories #5296
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
Korulag
wants to merge
4
commits into
google:master
Choose a base branch
from
Korulag:fix-tuxcare-version-parsing
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
4 commits
Select commit
Hold shift + click to select a range
401f000
feat: Add proper version parsing mapping for TuxCare advisories
vasilykleschov edcd140
Addressed review comments:
vasilykleschov 6a9536e
Merge branch 'master' into fix-tuxcare-version-parsing
Korulag b1b5450
Make TuxCare ecosystem behave similarly to Debian one
vasilykleschov 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package ecosystem | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/ossf/osv-schema/bindings/go/osvconstants" | ||
| ) | ||
|
|
||
| // tuxcareEcosystem represents "TuxCare:<ecosystem>" advisories. It delegates | ||
| // version handling to the inner ecosystem, resolved lazily via the Provider | ||
| // so that this factory can be registered in the ecosystems map without | ||
| // creating a package-init cycle. | ||
| type tuxcareEcosystem struct { | ||
| p *Provider | ||
| suffix string | ||
| } | ||
|
|
||
| var _ Ecosystem = tuxcareEcosystem{} | ||
|
|
||
| func tuxcareFactory(p *Provider, suffix string) Ecosystem { | ||
| innerName, _, _ := strings.Cut(suffix, ":") | ||
| if suffix == "" || innerName == string(osvconstants.EcosystemTuxCare) { | ||
| // Bare "TuxCare" or nested "TuxCare:TuxCare:..." is malformed. | ||
| return nil | ||
| } | ||
|
|
||
| return tuxcareEcosystem{p: p, suffix: suffix} | ||
| } | ||
|
|
||
| // resolve looks up the inner ecosystem on demand. Inner is unwrapped to avoid | ||
| // double-wrapping the resulting Version (which would fail to compare against | ||
| // a singly-wrapped Version from the same inner ecosystem). | ||
| func (e tuxcareEcosystem) resolve() (Ecosystem, error) { | ||
| inner, ok := e.p.Get(e.suffix) | ||
| if !ok { | ||
| return nil, fmt.Errorf("TuxCare: unknown inner ecosystem %q", e.suffix) | ||
| } | ||
|
|
||
| return unwrap(inner), nil | ||
| } | ||
|
|
||
| func (e tuxcareEcosystem) Parse(version string) (Version, error) { | ||
| inner, err := e.resolve() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return inner.Parse(version) | ||
| } | ||
|
|
||
| func (e tuxcareEcosystem) Coarse(version string) (string, error) { | ||
| inner, err := e.resolve() | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| return inner.Coarse(version) | ||
| } | ||
|
|
||
| func (e tuxcareEcosystem) IsSemver() bool { | ||
| inner, err := e.resolve() | ||
| if err != nil { | ||
| return false | ||
| } | ||
|
|
||
| return inner.IsSemver() | ||
| } | ||
|
|
||
| // unwrap strips the wrapper added by Provider.Get, so callers that wrap us | ||
| // again don't produce a doubly-wrapped Version. | ||
| func unwrap(e Ecosystem) Ecosystem { | ||
| switch w := e.(type) { | ||
| case *ecosystemWrapper: | ||
| return w.Ecosystem | ||
| case *enumerableWrapper: | ||
| return w.Enumerable | ||
| } | ||
|
|
||
| return e | ||
| } | ||
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,132 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package ecosystem | ||
|
|
||
| import ( | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestTuxCareEcosystem_DelegatesToInner(t *testing.T) { | ||
| p := NewProvider(nil) | ||
|
|
||
| cases := []struct { | ||
| name string | ||
| ecosystem string | ||
| }{ | ||
| {"RedHat", "TuxCare:Red Hat"}, | ||
| {"AlmaLinux", "TuxCare:AlmaLinux"}, | ||
| {"Debian", "TuxCare:Debian:12"}, | ||
| {"NPM", "TuxCare:npm"}, | ||
| {"AlpineWithSuffix", "TuxCare:Alpine:v3.16"}, | ||
| {"UbuntuMultiSegment", "TuxCare:Ubuntu:22.04:LTS"}, | ||
| } | ||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| if _, ok := p.Get(tc.ecosystem); !ok { | ||
| t.Fatalf("Provider.Get(%q) = ok=false, want true", tc.ecosystem) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestTuxCareEcosystem_Malformed(t *testing.T) { | ||
| p := NewProvider(nil) | ||
| cases := []string{ | ||
| // Bare TuxCare with no suffix. | ||
| "TuxCare", | ||
| "TuxCare:", | ||
| // Nested TuxCare. | ||
| "TuxCare:TuxCare", | ||
| "TuxCare:TuxCare:Red Hat", | ||
| } | ||
| for _, ecosystem := range cases { | ||
| t.Run(ecosystem, func(t *testing.T) { | ||
| if e, ok := p.Get(ecosystem); ok { | ||
| t.Errorf("Provider.Get(%q) = (%v, true), want (_, false)", ecosystem, e) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // Unknown inner ecosystems are accepted by Get (the inner is resolved | ||
| // lazily, mirroring debianFactory which accepts any release suffix); the | ||
| // failure surfaces at Parse time. | ||
| func TestTuxCareEcosystem_UnknownInnerFailsAtParse(t *testing.T) { | ||
| p := NewProvider(nil) | ||
| e, ok := p.Get("TuxCare:NotARealEcosystem") | ||
| if !ok { | ||
| t.Fatalf("Provider.Get(TuxCare:NotARealEcosystem) = ok=false, want true") | ||
| } | ||
| if _, err := e.Parse("1.0.0"); err == nil { | ||
| t.Errorf("Parse on unknown inner ecosystem returned nil error, want non-nil") | ||
| } | ||
| } | ||
|
|
||
| func TestTuxCareEcosystem_SortMatchesInner(t *testing.T) { | ||
| p := NewProvider(nil) | ||
|
|
||
| tuxRPM, ok := p.Get("TuxCare:Red Hat") | ||
| if !ok { | ||
| t.Fatalf("TuxCare:Red Hat not found") | ||
| } | ||
| plainRPM, ok := p.Get("Red Hat") | ||
| if !ok { | ||
| t.Fatalf("Red Hat not found") | ||
| } | ||
|
|
||
| v1, err := tuxRPM.Parse("1.0.0-1") | ||
| if err != nil { | ||
| t.Fatalf("tuxRPM.Parse: %v", err) | ||
| } | ||
| v2, err := tuxRPM.Parse("1.0.1-1") | ||
| if err != nil { | ||
| t.Fatalf("tuxRPM.Parse: %v", err) | ||
| } | ||
| if c, err := v1.Compare(v2); err != nil || c != -1 { | ||
| t.Errorf("Compare(1.0.0-1, 1.0.1-1) = (%d, %v), want (-1, nil)", c, err) | ||
| } | ||
|
|
||
| // Sort behaviour matches the underlying RPM parser. | ||
| tv, err := tuxRPM.Parse("1.2.3-1.el8") | ||
| if err != nil { | ||
| t.Fatalf("tuxRPM.Parse: %v", err) | ||
| } | ||
| pv, err := plainRPM.Parse("1.2.3-1.el8") | ||
| if err != nil { | ||
| t.Fatalf("plainRPM.Parse: %v", err) | ||
| } | ||
| if c, err := tv.Compare(pv); err != nil || c != 0 { | ||
| t.Errorf("Compare(tuxRPM, plainRPM) = (%d, %v), want (0, nil)", c, err) | ||
| } | ||
| } | ||
|
|
||
| func TestTuxCareEcosystem_ZeroVersion(t *testing.T) { | ||
| p := NewProvider(nil) | ||
| e, ok := p.Get("TuxCare:Red Hat") | ||
| if !ok { | ||
| t.Fatalf("TuxCare:Red Hat not found") | ||
| } | ||
| zero, err := e.Parse("0") | ||
| if err != nil { | ||
| t.Fatalf("Parse(0): %v", err) | ||
| } | ||
| v, err := e.Parse("1.0.0-1") | ||
| if err != nil { | ||
| t.Fatalf("Parse(1.0.0-1): %v", err) | ||
| } | ||
| if c, err := zero.Compare(v); err != nil || c != -1 { | ||
| t.Errorf("Compare(0, 1.0.0-1) = (%d, %v), want (-1, nil)", c, err) | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
One small thing:
I'd prefer this always return
falseIsSemver()is mostly only to be used to convertaffected[].ranges[].typefromECOSYSTEMtoSEMVERfor those ecosystems, which I don't think we want to do with TuxCare.