Launcher, app/base, sentry, update service

This commit is contained in:
James Houlahan
2020-11-23 11:56:57 +01:00
parent 6fffb460b8
commit dc3f61acee
164 changed files with 5368 additions and 4039 deletions

View File

@ -0,0 +1,38 @@
// Copyright (c) 2020 Proton Technologies AG
//
// This file is part of ProtonMail Bridge.
//
// ProtonMail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ProtonMail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
package versioner
import (
"compress/gzip"
"io"
"path/filepath"
"github.com/Masterminds/semver/v3"
"github.com/ProtonMail/proton-bridge/pkg/tar"
)
// InstallNewVersion installs a tgz update package of the given version.
func (v *Versioner) InstallNewVersion(version *semver.Version, r io.Reader) error {
gr, err := gzip.NewReader(r)
if err != nil {
return err
}
defer func() { _ = gr.Close() }()
return tar.UntarToDir(gr, filepath.Join(v.root, version.Original()))
}

View File

@ -0,0 +1,24 @@
// Copyright (c) 2021 Proton Technologies AG
//
// This file is part of ProtonMail Bridge.
//
// ProtonMail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ProtonMail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
// +build !windows
package versioner
func getExeName(name string) string {
return name
}

View File

@ -0,0 +1,22 @@
// Copyright (c) 2021 Proton Technologies AG
//
// This file is part of ProtonMail Bridge.
//
// ProtonMail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ProtonMail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
package versioner
func getExeName(name string) string {
return name + ".exe"
}

View File

@ -0,0 +1,24 @@
// Copyright (c) 2020 Proton Technologies AG
//
// This file is part of ProtonMail Bridge.
//
// ProtonMail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ProtonMail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
package versioner
// RemoveOldVersions removes all but the latest app version.
func (v *Versioner) RemoveOldVersions() error {
// darwin does not use the versioner; removal is a noop.
return nil
}

View File

@ -0,0 +1,47 @@
// Copyright (c) 2021 Proton Technologies AG
//
// This file is part of ProtonMail Bridge.
//
// ProtonMail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ProtonMail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
// +build !darwin
package versioner
import (
"os"
"github.com/sirupsen/logrus"
)
// RemoveOldVersions removes all but the latest app version.
func (v *Versioner) RemoveOldVersions() error {
versions, err := v.ListVersions()
if err != nil {
return err
}
// darwin does not currently use the versioner.
if len(versions) == 0 {
return nil
}
for _, version := range versions[1:] {
if err := os.RemoveAll(version.path); err != nil {
logrus.WithError(err).Error("Failed to remove old app version")
}
}
return nil
}

View File

@ -0,0 +1,43 @@
// Copyright (c) 2020 Proton Technologies AG
//
// This file is part of ProtonMail Bridge.
//
// ProtonMail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ProtonMail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
package versioner
import (
"os"
"runtime"
)
// fileExists returns whether the given file exists.
func fileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
// fileIsExecutable returns the given filepath and true if it exists.
func fileIsExecutable(path string) bool {
if runtime.GOOS == "windows" {
return true
}
info, err := os.Stat(path)
if err != nil {
return false
}
return info.Mode()&0111 != 0
}

View File

@ -0,0 +1,87 @@
// Copyright (c) 2020 Proton Technologies AG
//
// This file is part of ProtonMail Bridge.
//
// ProtonMail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ProtonMail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
package versioner
import (
"io/ioutil"
"os"
"path/filepath"
"github.com/Masterminds/semver/v3"
"github.com/ProtonMail/gopenpgp/v2/crypto"
)
type Version struct {
version *semver.Version
path string
}
type Versions []*Version
func (v Versions) Len() int {
return len(v)
}
func (v Versions) Less(i, j int) bool {
return v[i].version.LessThan(v[j].version)
}
func (v Versions) Swap(i, j int) {
v[i], v[j] = v[j], v[i]
}
// VerifyFiles verifies all files in the version directory.
func (v *Version) VerifyFiles(kr *crypto.KeyRing) error {
return filepath.Walk(v.path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if filepath.Ext(path) == ".sig" || info.IsDir() {
return nil
}
fileBytes, err := ioutil.ReadFile(path) // nolint[gosec]
if err != nil {
return err
}
sigBytes, err := ioutil.ReadFile(path + ".sig") // nolint[gosec]
if err != nil {
return err
}
return kr.VerifyDetached(
crypto.NewPlainMessage(fileBytes),
crypto.NewPGPSignature(sigBytes),
crypto.GetUnixTime(),
)
})
}
// GetExecutable returns the full path to the executable of the given version.
// It returns an error if the executable is missing or does not have executable permissions set.
func (v *Version) GetExecutable(name string) (string, error) {
exe := filepath.Join(v.path, getExeName(name))
if !fileExists(exe) || !fileIsExecutable(exe) {
return "", ErrNoExecutable
}
return exe, nil
}

View File

@ -0,0 +1,142 @@
// Copyright (c) 2020 Proton Technologies AG
//
// This file is part of ProtonMail Bridge.
//
// ProtonMail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ProtonMail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
package versioner
import (
"crypto/rand"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/Masterminds/semver/v3"
"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestVerifyFiles(t *testing.T) {
tempDir, err := ioutil.TempDir("", "verify-test")
require.NoError(t, err)
version := &Version{
version: semver.MustParse("1.2.3"),
path: tempDir,
}
kr := createSignedFiles(t,
filepath.Join(tempDir, "f1.txt"),
filepath.Join(tempDir, "f2.png"),
filepath.Join(tempDir, "f3.dat"),
filepath.Join(tempDir, "sub", "f4.tar"),
filepath.Join(tempDir, "sub", "f5.tgz"),
)
assert.NoError(t, version.VerifyFiles(kr))
}
func TestVerifyWithBadFile(t *testing.T) {
tempDir, err := ioutil.TempDir("", "verify-test")
require.NoError(t, err)
version := &Version{
version: semver.MustParse("1.2.3"),
path: tempDir,
}
kr := createSignedFiles(t,
filepath.Join(tempDir, "f1.txt"),
filepath.Join(tempDir, "f2.png"),
filepath.Join(tempDir, "f3.bad"),
filepath.Join(tempDir, "sub", "f4.tar"),
filepath.Join(tempDir, "sub", "f5.tgz"),
)
badKeyRing := makeKeyRing(t)
signFile(t, filepath.Join(tempDir, "f3.bad"), badKeyRing)
assert.Error(t, version.VerifyFiles(kr))
}
func TestVerifyWithBadSubFile(t *testing.T) {
tempDir, err := ioutil.TempDir("", "verify-test")
require.NoError(t, err)
version := &Version{
version: semver.MustParse("1.2.3"),
path: tempDir,
}
kr := createSignedFiles(t,
filepath.Join(tempDir, "f1.txt"),
filepath.Join(tempDir, "f2.png"),
filepath.Join(tempDir, "f3.dat"),
filepath.Join(tempDir, "sub", "f4.tar"),
filepath.Join(tempDir, "sub", "f5.bad"),
)
badKeyRing := makeKeyRing(t)
signFile(t, filepath.Join(tempDir, "sub", "f5.bad"), badKeyRing)
assert.Error(t, version.VerifyFiles(kr))
}
func createSignedFiles(t *testing.T, paths ...string) *crypto.KeyRing {
kr := makeKeyRing(t)
for _, path := range paths {
require.NoError(t, os.MkdirAll(filepath.Dir(path), 0700))
makeFile(t, path)
signFile(t, path, kr)
}
return kr
}
func makeKeyRing(t *testing.T) *crypto.KeyRing {
key, err := crypto.GenerateKey("name", "email", "rsa", 2048)
require.NoError(t, err)
kr, err := crypto.NewKeyRing(key)
require.NoError(t, err)
return kr
}
func makeFile(t *testing.T, path string) {
f, err := os.Create(path)
require.NoError(t, err)
data := make([]byte, 64)
_, err = rand.Read(data)
require.NoError(t, err)
_, err = f.Write(data)
require.NoError(t, err)
require.NoError(t, f.Close())
}
func signFile(t *testing.T, path string, kr *crypto.KeyRing) {
file, err := ioutil.ReadFile(path)
require.NoError(t, err)
sig, err := kr.SignDetached(crypto.NewPlainMessage(file))
require.NoError(t, err)
require.NoError(t, ioutil.WriteFile(path+".sig", sig.GetBinary(), 0700))
}

View File

@ -0,0 +1,81 @@
// Copyright (c) 2020 Proton Technologies AG
//
// This file is part of ProtonMail Bridge.
//
// ProtonMail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ProtonMail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
package versioner
import (
"errors"
"io/ioutil"
"path/filepath"
"sort"
"github.com/Masterminds/semver/v3"
)
var (
ErrNoVersions = errors.New("no available versions")
ErrNoExecutable = errors.New("no executable found")
)
// Versioner manages a directory of versioned app directories.
type Versioner struct {
root string
}
func New(root string) *Versioner {
return &Versioner{root: root}
}
// ListVersions returns a collection of all available version numbers, sorted from newest to oldest.
func (v *Versioner) ListVersions() (Versions, error) {
dirs, err := ioutil.ReadDir(v.root)
if err != nil {
return nil, err
}
var versions Versions
for _, dir := range dirs {
version, err := semver.StrictNewVersion(dir.Name())
if err != nil {
continue
}
// NOTE: If it's a bad directory, maybe delete it?
versions = append(versions, &Version{
version: version,
path: filepath.Join(v.root, dir.Name()),
})
}
sort.Sort(sort.Reverse(versions))
return versions, nil
}
// GetExecutableInDirectory returns the full path to the executable in the given directory, if present.
// It returns an error if the executable is missing or does not have executable permissions set.
func (v *Versioner) GetExecutableInDirectory(name, directory string) (string, error) {
exe := filepath.Join(directory, getExeName(name))
if !fileExists(exe) || !fileIsExecutable(exe) {
return "", ErrNoExecutable
}
return exe, nil
}

View File

@ -0,0 +1,95 @@
// Copyright (c) 2020 Proton Technologies AG
//
// This file is part of ProtonMail Bridge.
//
// ProtonMail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ProtonMail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
package versioner
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/Masterminds/semver/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestListVersions(t *testing.T) {
updates, err := ioutil.TempDir("", "updates")
require.NoError(t, err)
v := newTestVersioner(t, "myCoolApp", updates, "2.3.4-beta", "2.3.4", "2.3.5", "2.4.0")
versions, err := v.ListVersions()
require.NoError(t, err)
assert.Equal(t, semver.MustParse("2.4.0"), versions[0].version)
assert.Equal(t, filepath.Join(updates, "2.4.0"), versions[0].path)
assert.Equal(t, semver.MustParse("2.3.5"), versions[1].version)
assert.Equal(t, filepath.Join(updates, "2.3.5"), versions[1].path)
assert.Equal(t, semver.MustParse("2.3.4"), versions[2].version)
assert.Equal(t, filepath.Join(updates, "2.3.4"), versions[2].path)
assert.Equal(t, semver.MustParse("2.3.4-beta"), versions[3].version)
assert.Equal(t, filepath.Join(updates, "2.3.4-beta"), versions[3].path)
}
func TestRemoveOldVersions(t *testing.T) {
updates, err := ioutil.TempDir("", "updates")
require.NoError(t, err)
v := newTestVersioner(t, "myCoolApp", updates, "2.3.4-beta", "2.3.4", "2.3.5", "2.4.0")
allVersions, err := v.ListVersions()
require.NoError(t, err)
require.Len(t, allVersions, 4)
assert.NoError(t, v.RemoveOldVersions())
cleanedVersions, err := v.ListVersions()
assert.NoError(t, err)
assert.Len(t, cleanedVersions, 1)
assert.Equal(t, semver.MustParse("2.4.0"), cleanedVersions[0].version)
assert.Equal(t, filepath.Join(updates, "2.4.0"), cleanedVersions[0].path)
}
func newTestVersioner(t *testing.T, exeName, updates string, versions ...string) *Versioner {
for _, version := range versions {
makeDummyVersionDirectory(t, exeName, updates, version)
}
return New(updates)
}
func makeDummyVersionDirectory(t *testing.T, exeName, updates, version string) string {
target := filepath.Join(updates, version)
require.NoError(t, os.Mkdir(target, 0700))
exe, err := os.Create(filepath.Join(target, getExeName(exeName)))
require.NoError(t, err)
require.NotNil(t, exe)
require.NoError(t, os.Chmod(exe.Name(), 0700))
sig, err := os.Create(filepath.Join(target, getExeName(exeName)+".sig"))
require.NoError(t, err)
require.NotNil(t, sig)
return target
}