Commit 83c37f5c authored by Kevin Daudt's avatar Kevin Daudt 💻
Browse files

secdb: add go implementation

Lua has just a single structure, namely a table, which is used for both
lists and maps. This poses a problem when a list is empty, because it's
not able to distinguish between the 2, and will default to a map.

This implementation:

* Defines the exact structure
* Writes out json and yaml at the same time
* Fetches release, instead of statically defining it in-line

And because it's statically built, has no dependencies except musl.

See: #2
parent 826dbc0b
module secdb
go 1.16
require (
gitlab.alpinelinux.org/alpine/go v0.2.1
gopkg.in/yaml.v2 v2.4.0
)
github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gitlab.alpinelinux.org/alpine/go v0.2.1 h1:seYzsntFNE/v0H4pd+r+kNnOICYFnIhjGlBjNbkLg84=
gitlab.alpinelinux.org/alpine/go v0.2.1/go.mod h1:auOw3SnxDQBo1vzPh8q6gjvKsYgKxYet03lgSKtA3Q4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"strings"
"gitlab.alpinelinux.org/alpine/go/pkg/apkbuild"
"gitlab.alpinelinux.org/alpine/go/pkg/releases"
"gopkg.in/yaml.v2"
)
var (
repo string
release string
debug bool
outJson string
outYaml string
)
func errOut(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
}
func main() {
flag.StringVar(&repo, "repo", "", "The repository to process (required)")
flag.StringVar(&release, "release", "", "The release that's being generated (required)")
flag.BoolVar(&debug, "debug", false, "Turn on debug output")
flag.StringVar(&outJson, "outJson", "", "File to write json output to")
flag.StringVar(&outYaml, "outYaml", "", "File to write yaml output to")
flag.Parse()
if release == "" {
errOut("-release argument is required")
flag.Usage()
os.Exit(1)
}
if repo == "" {
errOut("-repo argument is required")
flag.Usage()
os.Exit(1)
}
releases, err := releases.Fetch()
if err != nil {
panic(err)
}
releaseBranch := releases.GetRelBranch(release)
if releaseBranch == nil {
errOut("No release branch found called: %s\n", release)
os.Exit(1)
}
secfixDB := SecfixDB{}
secfixDB.Urlprefix = "https://dl-cdn.alpinelinux.org/alpine"
secfixDB.Apkurl = `{{urlprefix}}/{{distroversion}}/{{reponame}}/{{arch}}/{{pkg.name}}-{{pkg.ver}}.apk`
secfixDB.Distroversion = release
secfixDB.Reponame = repo
secfixDB.Archs = releaseBranch.Arches
secfixDB.Packages = []Pkg{}
for _, pkg := range flag.Args() {
pkgComponents := strings.Split(pkg, "/")
pkgName := pkgComponents[len(pkgComponents)-2]
if debug {
fmt.Printf("Checking package %s\n", pkg)
}
file, err := os.Open(pkg)
if err != nil {
errOut("%s\n", err)
continue
}
secfixes, err := apkbuild.ParseSecfixes(file)
file.Close()
if err != nil {
errOut("%s\n", err)
continue
}
if secfixes == nil {
continue
}
secfixDB.Packages = append(secfixDB.Packages, Pkg{
Pkg: Package{
Name: pkgName,
Secfixes: secfixes,
}})
if debug {
secfixesJson, _ := json.MarshalIndent(secfixes, "", " ")
fmt.Println(string(secfixesJson))
}
}
if outYaml != "" {
secfixDBYaml, err := yaml.Marshal(secfixDB)
if err != nil {
panic(err)
}
err = writeToFile(outYaml, secfixDBYaml)
if err != nil {
panic(err)
}
}
if outJson != "" {
secfixDBJson, err := json.Marshal(secfixDB)
if err != nil {
panic(err)
}
err = writeToFile(outJson, secfixDBJson)
if err != nil {
panic(err)
}
}
}
func writeToFile(filename string, output []byte) error {
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()
_, err = file.Write(output)
if err != nil {
return err
}
return nil
}
package main
import (
"gitlab.alpinelinux.org/alpine/go/pkg/apkbuild"
)
type (
Package struct {
Name string `json:"name"`
Secfixes apkbuild.Secfixes `json:"secfixes"`
}
Pkg struct {
Pkg Package `json:"pkg"`
}
SecfixDB struct {
Apkurl string `json:"apkurl"`
Archs []string `json:"archs"`
Reponame string `json:"reponame"`
Urlprefix string `json:"urlprefix"`
Distroversion string `json:"distroversion"`
Packages []Pkg `json:"packages"`
}
)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment