Skip to content
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

feat(eol): added eol dates #148

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
c8be602
feat(alpine): added parser for alpine eol dates
DmitriyLewen Apr 15, 2022
a5b853e
fix(lint): unused constants has been removed
DmitriyLewen Apr 15, 2022
fcbfcc6
refactor(eol): move eol for alpine in different package
DmitriyLewen Apr 15, 2022
3bed40b
refactor(eol): refactor eof, used update func for tests
DmitriyLewen Apr 18, 2022
bae8be7
test(eol): removed wrong test
DmitriyLewen Apr 18, 2022
44e805c
test(eol): added comparison of error texts
DmitriyLewen Apr 18, 2022
b17ca43
refactor(eol): changed error texts
DmitriyLewen Apr 18, 2022
8a6c7ab
refactor(eol): use dist array for update EOL dates
DmitriyLewen Apr 20, 2022
ddfe3e5
refactor(eol): use dist array for update EOL dates
DmitriyLewen Apr 20, 2022
eae3324
refactor(eol): Alpine
DmitriyLewen Apr 20, 2022
08c9a73
feat(eol): alma
DmitriyLewen Apr 20, 2022
d389406
feat(eol): rocky
DmitriyLewen Apr 20, 2022
d343aeb
feat(eol): added debian EOL dates
DmitriyLewen Apr 20, 2022
b07810a
feat(eol): added openSUSE EOL dates
DmitriyLewen Apr 20, 2022
1dad173
feat(eol): added RHEL EOL dates
DmitriyLewen Apr 21, 2022
c1156b7
refactor(eol): changed Support and Eol types from string to interface
DmitriyLewen Apr 21, 2022
a95fab8
refactor(eol): added errors for empty list of eol dates
DmitriyLewen Apr 21, 2022
36d8825
feat(eol): added centOS eol dates
DmitriyLewen Apr 21, 2022
881475e
feat(eol): added ubuntu eol dates
DmitriyLewen Apr 21, 2022
22c89d1
refactor(eol): returned defaultRepoOwner
DmitriyLewen Apr 21, 2022
edba0df
resolve conflict
DmitriyLewen Apr 21, 2022
1c01d8b
feat(eol): added support for amazon linux
DmitriyLewen Apr 26, 2022
78c47f6
fix(amazon): added tests, fix mistake in eol tests
DmitriyLewen Apr 26, 2022
41bbfdd
feat(eol): added support SLES
DmitriyLewen Apr 26, 2022
07b1a9d
feat(eol): added support for oracle linux and photon os
DmitriyLewen Apr 26, 2022
ded4f19
fix(eol): fixed ubuntu mistakes, changed trigger for update
DmitriyLewen Apr 26, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/update.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ jobs:
name: Go Vulnerability Database
run: ./vuln-list-update -target go-vulndb

- if: always()
name: EOL Dates
run: ./vuln-list-update -target eol

# Red Hat Security Data API is unstable.
# It should be split into small pieces to reduce the impact of failure.
- if: always()
Expand Down
101 changes: 101 additions & 0 deletions eol/alma/alma.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package alma

import (
"fmt"
"path/filepath"
"strings"
"time"

eolutils "github.com/aquasecurity/vuln-list-update/eol/utils"
"github.com/aquasecurity/vuln-list-update/utils"
"github.com/spf13/afero"
"golang.org/x/xerrors"
)

const (
baseUrl = "https://endoflife.date/api/%s.json"
distName = "AlmaLinux"
dirPath = "eol/alma"
fileName = "alma.json"
retry = 3
)

type options struct {
url string
vulnListDir string
retry int
appFs afero.Fs
}

type option func(*options)

func WithURL(url string) option {
return func(opts *options) { opts.url = url }
}

func WithVulnListDir(vulnListDir string) option {
return func(opts *options) { opts.vulnListDir = vulnListDir }
}

func WithRetry(retry int) option {
return func(opts *options) { opts.retry = retry }
}

type Config struct {
*options
}

func NewConfig(opts ...option) Config {
o := &options{
url: fmt.Sprintf(baseUrl, strings.ToLower(distName)),
vulnListDir: utils.VulnListDir(),
retry: retry,
appFs: afero.NewOsFs(),
}

for _, opt := range opts {
opt(o)
}

return Config{
options: o,
}
}

func (c Config) Name() string {
return distName
}

func (c Config) Update() error {
eolDates := map[string]time.Time{}

cycles, err := eolutils.GetLifeCycles(distName, c.url, c.retry)
if err != nil {
return xerrors.Errorf("unable to get EOL dates from %s, err: %w ", c.url, err)
}

for _, cycle := range cycles {
if eol, ok := cycle.Eol.(string); ok {
d, err := time.Parse("2006-01-02", eol)
if err != nil {
return xerrors.Errorf("unable to parse %q date: %w", cycle.Eol, err)
}
eolDates[cycle.Cycle] = d
}
}

if len(eolDates) == 0 {
return xerrors.Errorf("list of end-of-life dates is empty")
}

return c.save(eolDates)
}

func (c Config) save(dates map[string]time.Time) error {
dir := filepath.Join(c.vulnListDir, dirPath)

if err := utils.WriteJSON(c.appFs, dir, fileName, dates); err != nil {
return xerrors.Errorf("failed to write %s under %s: %w", fileName, dirPath, err)
}
return nil
}
77 changes: 77 additions & 0 deletions eol/alma/alma_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package alma

import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
)

func TestConfig_Update(t *testing.T) {
tests := []struct {
name string
filepath string
goldenPath string
wantErr string
}{
{
name: "happy path",
filepath: "testdata/happy.json",
goldenPath: "testdata/golden.json",
},
{
name: "sad path. 404",
wantErr: "failed to get list of end-of-life dates from url: failed to fetch URL",
},
{
name: "sad path. Empty json has been returned",
filepath: "testdata/empty.json",
wantErr: "list of end-of-life dates is empty",
},
{
name: "sad path. Bad json has been returned",
filepath: "testdata/sad.json",
wantErr: "unable to get EOL dates from",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if test.filepath != "" {
http.ServeFile(w, r, test.filepath)
} else {
w.WriteHeader(http.StatusNotFound)
}
}))
defer ts.Close()

tmpDir, _ := os.MkdirTemp("", "eol-"+distName)
filePath := filepath.Join(tmpDir, dirPath, fileName)

c := NewConfig(WithVulnListDir(tmpDir), WithURL(ts.URL), WithRetry(1))

err := c.Update()

if test.wantErr != "" {
assert.NotNil(t, err)
assert.NoFileExists(t, filePath)
assert.ErrorContains(t, err, test.wantErr)
} else {
assert.Nil(t, err)
assert.FileExists(t, filePath)

gotJson, err := os.ReadFile(filePath)
assert.NoError(t, err)

wantJson, err := os.ReadFile(test.goldenPath)
assert.NoError(t, err)

assert.JSONEq(t, string(wantJson), string(gotJson))
}
})
}
}
4 changes: 4 additions & 0 deletions eol/alma/testdata/empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
{
}
]
3 changes: 3 additions & 0 deletions eol/alma/testdata/golden.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"8": "2029-03-01T00:00:00Z"
}
10 changes: 10 additions & 0 deletions eol/alma/testdata/happy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"cycle": "8",
"release": "2019-05-01",
"support": "2024-05-01",
"eol": "2029-03-01",
"latest": "8.5",
"link": "https://mirrors.almalinux.org/isos.html"
}
]
9 changes: 9 additions & 0 deletions eol/alma/testdata/sad.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"cycle": "8",
"release": "2019-05-01",
"support": "2024-05-01",
"eol": "2029-03-01",
"latest": "8.5",
"link": "https://mirrors.almalinux.org/isos.html"
}
128 changes: 128 additions & 0 deletions eol/alpine/alpine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package alpine

import (
"bytes"
"log"
"path/filepath"
"strings"
"time"

"github.com/PuerkitoBio/goquery"
eolutils "github.com/aquasecurity/vuln-list-update/eol/utils"
"github.com/aquasecurity/vuln-list-update/utils"
"github.com/spf13/afero"
"golang.org/x/xerrors"
)

const (
distName = "alpine"
eolAlpineFolder = "eol/alpine"
eolAlpineFile = "alpine.json"
eolAlpineUrl = "https://alpinelinux.org/releases/"
retry = 5
)

type Config struct {
vulnListDir string
eolUrl string
retry int
appFs afero.Fs
}

type Option func(c *Config)

func WithVulnListDir(v string) Option {
return func(c *Config) { c.vulnListDir = v }
}

func WithEolURL(v string) Option {
return func(c *Config) { c.eolUrl = v }
}

func WithRetry(v int) Option {
return func(c *Config) { c.retry = v }
}

func NewConfig(options ...Option) *Config {
c := &Config{
vulnListDir: utils.VulnListDir(),
eolUrl: eolAlpineUrl,
retry: retry,
appFs: afero.NewOsFs(),
}
for _, option := range options {
option(c)
}
return c
}

func (c Config) Name() string {
return distName
}

func (c Config) Update() error {
dates, err := c.getEOFDates()
if err != nil {
return err
}

err = c.save(dates)
if err != nil {
return err
}

return nil
}

func (c Config) getEOFDates() (map[string]time.Time, error) {
eolDates := make(map[string]time.Time)

log.Println("Fetching Alpine end-of-life dates...")
b, err := utils.FetchURL(c.eolUrl, "", c.retry)
if err != nil {
return nil, xerrors.Errorf("failed to get list of end-of-life dates from url: %w", err)
}

doc, err := goquery.NewDocumentFromReader(bytes.NewReader(b))
if err != nil {
return nil, xerrors.Errorf("failed to read list of end-of-life dates: %w", err)
}

doc.Find("tbody tr").Each(func(_ int, tr *goquery.Selection) {
var version string
var date time.Time

tr.Find("td").Each(func(ix int, td *goquery.Selection) {
switch ix {
case 0:
version = strings.TrimPrefix(td.Text(), "v") // remove 'v' prefix
case 4:
d, err := time.Parse("2006-01-02", td.Text())
if err != nil {
return
}

date = eolutils.MoveToEndOfDay(d)
}
})
eolDates[version] = date
})

if len(eolDates) == 0 {
return nil, xerrors.Errorf("List of end-of-life dates is empty.")
}

// edge version doesn't have EOL. Add max date.
eolDates["edge"] = time.Date(9999, 1, 1, 0, 0, 0, 0, time.UTC)

return eolDates, nil
}

func (c Config) save(dates map[string]time.Time) error {
dir := filepath.Join(c.vulnListDir, eolAlpineFolder)

if err := utils.WriteJSON(c.appFs, dir, eolAlpineFile, dates); err != nil {
return xerrors.Errorf("failed to write %s under %s: %w", eolAlpineFile, eolAlpineFolder, err)
}
return nil
}
Loading