goselfupdatetest/main.go
Lukas Bachschwell 74bf044c74
Add sig validation in binary
Signed-off-by: Lukas Bachschwell <lukas@lbsfilm.at>
2021-01-09 17:45:42 +01:00

86 lines
2.0 KiB
Go

package main
import (
"errors"
"fmt"
"log"
"os"
"runtime"
selfupdate "github.com/creativeprojects/go-selfupdate"
"github.com/kenshaw/pemutil"
)
//go:generate sh injectGitVars.sh
var version = "v0.1.0"
func main() {
if len(os.Args) < 2 {
fmt.Println("provide a command")
os.Exit(1)
} else if os.Args[1] == "upgrade" || os.Args[1] == "up" {
fmt.Println("Trying Upgrade... ")
err := update(gitTag)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Success")
}
} else if os.Args[1] == "version" {
fmt.Print("Version ")
fmt.Println(gitTag)
} else {
fmt.Println("provide a valid command (version or upgrade)")
}
}
func update(version string) error {
source, _ := selfupdate.NewGiteaSource(selfupdate.GiteaConfig{BaseURL: "https://git.lbsfilm.at/"})
store := make(pemutil.Store)
err := pemutil.Decode(store, []byte(`-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE0+99Oxlx+P6F9Cd5pUIw6oGY2oFL
qCf//kV/S27OpD6skuEveQG+M1k6eT/o8oVDJ0sj3aIyaF+vruZaBB9HeA==
-----END PUBLIC KEY-----`))
if err != nil {
return err
}
pubkey, ok := store.ECPublicKey()
if !ok {
return fmt.Errorf("no pubkey")
}
updater, err := selfupdate.NewUpdater(selfupdate.Config{
Source: source,
Validator: &selfupdate.ECDSAValidator{
PublicKey: pubkey,
},
OS: runtime.GOOS,
Arch: runtime.GOARCH,
Arm: 0,
})
latest, found, err := updater.DetectLatest("lbsadmin/goselfupdatetest")
if err != nil {
return fmt.Errorf("error occurred while detecting version: %v", err)
}
if !found {
return fmt.Errorf("latest version for %s/%s could not be found from github repository", runtime.GOOS, runtime.GOARCH)
}
if latest.LessOrEqual(version) {
log.Printf("Current version (%s) is the latest", version)
return nil
}
exe, err := os.Executable()
if err != nil {
return errors.New("could not locate executable path")
}
if err := updater.UpdateTo(latest, exe); err != nil {
return fmt.Errorf("error occurred while updating binary: %v", err)
}
log.Printf("Successfully updated to version %s", latest.Version())
return nil
}