Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

Commit

Permalink
Merge branch 'master' into cacert
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel committed May 4, 2016
2 parents 19d6b37 + 1111990 commit 8ff2d80
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 83 deletions.
1 change: 0 additions & 1 deletion keybase/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ func (c config) updaterOptions() updater.UpdateOptions {
Channel: "test",
DestinationPath: c.destinationPath(),
Env: "prod",
InstallID: c.GetInstallID(),
OSVersion: osVersion,
UpdaterVersion: updater.Version,
}
Expand Down
1 change: 0 additions & 1 deletion keybase/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ func TestConfig(t *testing.T) {
DestinationPath: "",
Channel: "test",
Env: "prod",
InstallID: "deadbeef",
Arch: "amd64",
Force: false,
OSVersion: cfg.osVersion(),
Expand Down
14 changes: 8 additions & 6 deletions keybase/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,17 @@ type context struct {

// endpoints define all the url locations for reporting, etc
type endpoints struct {
update string
action string
err string
update string
action string
success string
err string
}

var defaultEndpoints = endpoints{
update: "https://api.keybase.io/_/api/1.0/pkg/update.json",
action: "https://api.keybase.io/_/api/1.0/pkg/act.json",
err: "https://api.keybase.io/_/api/1.0/pkg/error.json",
update: "https://api.keybase.io/_/api/1.0/pkg/update.json",
action: "https://api.keybase.io/_/api/1.0/pkg/act.json",
success: "https://api.keybase.io/_/api/1.0/pkg/success.json",
err: "https://api.keybase.io/_/api/1.0/pkg/error.json",
}

func newContext(cfg *config, log logging.Logger) *context {
Expand Down
5 changes: 0 additions & 5 deletions keybase/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,3 @@ func TestContextVerifyBadSignature(t *testing.T) {
err := ctx.Verify(testContextUpdate(testMessagePath, "BEGIN KEYBASE SALTPACK DETACHED SIGNATURE. END KEYBASE SALTPACK DETACHED SIGNATURE."))
require.Error(t, err)
}

func TestContextReportError(t *testing.T) {
ctx := testContext(t)
ctx.ReportError(updater.NewError(updater.UnknownError, nil), updater.UpdateOptions{})
}
44 changes: 28 additions & 16 deletions keybase/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import (
)

// ReportError notifies the API server of a client updater error
func (c context) ReportError(err error, options updater.UpdateOptions) {
if err := c.reportError(err, options, defaultEndpoints.err, time.Minute); err != nil {
func (c context) ReportError(err error, update *updater.Update, options updater.UpdateOptions) {
if err := c.reportError(err, update, options, defaultEndpoints.err, time.Minute); err != nil {
c.log.Warningf("Error notifying about an error: %s", err)
}
}

func (c context) reportError(err error, options updater.UpdateOptions, uri string, timeout time.Duration) error {
func (c context) reportError(err error, update *updater.Update, options updater.UpdateOptions, uri string, timeout time.Duration) error {
var errorType string
switch uerr := err.(type) {
case updater.Error:
Expand All @@ -31,33 +31,45 @@ func (c context) reportError(err error, options updater.UpdateOptions, uri strin
}

data := url.Values{}
data.Add("install_id", options.InstallID)
data.Add("version", options.Version)
data.Add("upd_version", options.UpdaterVersion)
data.Add("error_type", errorType)
data.Add("description", err.Error())
return c.report(data, uri, timeout)
return c.report(data, update, options, uri, timeout)
}

// ReportAction notifies the API server of a client updater action
func (c context) ReportAction(action updater.UpdateAction, options updater.UpdateOptions) {
if err := c.reportAction(action, options, defaultEndpoints.action, time.Minute); err != nil {
func (c context) ReportAction(action updater.UpdateAction, update *updater.Update, options updater.UpdateOptions) {
if err := c.reportAction(action, update, options, defaultEndpoints.action, time.Minute); err != nil {
c.log.Warningf("Error notifying about an action (%s): %s", action, err)
}
}

func (c context) reportAction(action updater.UpdateAction, options updater.UpdateOptions, uri string, timeout time.Duration) error {
func (c context) reportAction(action updater.UpdateAction, update *updater.Update, options updater.UpdateOptions, uri string, timeout time.Duration) error {
data := url.Values{}
data.Add("install_id", options.InstallID)
data.Add("version", options.Version)
data.Add("upd_version", options.UpdaterVersion)
data.Add("action", action.String())
autoUpdate, _ := c.config.GetUpdateAuto()
data.Add("auto_update", util.URLValueForBool(autoUpdate))
return c.report(data, uri, timeout)
return c.report(data, update, options, uri, timeout)
}

func (c context) ReportSuccess(update *updater.Update, options updater.UpdateOptions) {
if err := c.reportSuccess(update, options, defaultEndpoints.success, time.Minute); err != nil {
c.log.Warningf("Error notifying about success: %s", err)
}
}

func (c context) report(data url.Values, uri string, timeout time.Duration) error {
func (c context) reportSuccess(update *updater.Update, options updater.UpdateOptions, uri string, timeout time.Duration) error {
data := url.Values{}
return c.report(data, update, options, uri, timeout)
}

func (c context) report(data url.Values, update *updater.Update, options updater.UpdateOptions, uri string, timeout time.Duration) error {
if update != nil {
data.Add("install_id", update.InstallID)
data.Add("request_id", update.RequestID)
}
data.Add("version", options.Version)
data.Add("upd_version", options.UpdaterVersion)

req, err := http.NewRequest("POST", uri, bytes.NewBufferString(data.Encode()))
if err != nil {
return err
Expand All @@ -66,7 +78,7 @@ func (c context) report(data url.Values, uri string, timeout time.Duration) erro
if err != nil {
return err
}
c.log.Infof("Reporting error: %s %v", uri, data)
c.log.Infof("Reporting: %s %v", uri, data)
resp, err := client.Do(req)
defer util.DiscardAndCloseBodyIgnoreError(resp)
if err != nil {
Expand Down
34 changes: 26 additions & 8 deletions keybase/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ import (
"github.com/stretchr/testify/require"
)

var testOptions = updater.UpdateOptions{
var testUpdate = updater.Update{
InstallID: "deadbeef",
Version: "1.2.3-400+abcdef",
RequestID: "cafedead",
Version: "1.2.2+fedcba",
}

var testOptions = updater.UpdateOptions{
Version: "1.2.3-400+abcdef",
}

func TestReportError(t *testing.T) {
Expand All @@ -26,7 +31,7 @@ func TestReportError(t *testing.T) {

updateErr := updater.NewError(updater.PromptError, fmt.Errorf("Test error"))
ctx := testContext(t)
err := ctx.reportError(updateErr, testOptions, server.URL, 5*time.Millisecond)
err := ctx.reportError(updateErr, &testUpdate, testOptions, server.URL, 5*time.Millisecond)
assert.NoError(t, err)
}

Expand All @@ -37,7 +42,7 @@ func TestReportErrorEmpty(t *testing.T) {
updateErr := updater.NewError(updater.UnknownError, nil)
emptyOptions := updater.UpdateOptions{}
ctx := testContext(t)
err := ctx.reportError(updateErr, emptyOptions, server.URL, 5*time.Millisecond)
err := ctx.reportError(updateErr, nil, emptyOptions, server.URL, 5*time.Millisecond)
assert.NoError(t, err)
}

Expand All @@ -46,7 +51,7 @@ func TestReportBadResponse(t *testing.T) {
defer server.Close()

ctx := testContext(t)
err := ctx.report(url.Values{}, server.URL, 5*time.Millisecond)
err := ctx.report(url.Values{}, &testUpdate, testOptions, server.URL, 5*time.Millisecond)
assert.EqualError(t, err, "Notify error returned bad HTTP status 500 Internal Server Error")
}

Expand All @@ -55,7 +60,7 @@ func TestReportTimeout(t *testing.T) {
defer server.Close()

ctx := testContext(t)
err := ctx.report(url.Values{}, server.URL, 2*time.Millisecond)
err := ctx.report(url.Values{}, &testUpdate, testOptions, server.URL, 2*time.Millisecond)
require.Error(t, err)
assert.True(t, strings.Contains(err.Error(), "net/http: request canceled"))
}
Expand All @@ -65,7 +70,7 @@ func TestReportActionApply(t *testing.T) {
defer server.Close()

ctx := testContext(t)
err := ctx.reportAction(updater.UpdateActionApply, testOptions, server.URL, 5*time.Millisecond)
err := ctx.reportAction(updater.UpdateActionApply, &testUpdate, testOptions, server.URL, 5*time.Millisecond)
assert.NoError(t, err)
}

Expand All @@ -74,6 +79,19 @@ func TestReportActionEmpty(t *testing.T) {
defer server.Close()

ctx := testContext(t)
err := ctx.reportAction("", updater.UpdateOptions{}, server.URL, 5*time.Millisecond)
err := ctx.reportAction("", &testUpdate, testOptions, server.URL, 5*time.Millisecond)
assert.NoError(t, err)
}

func TestReportSuccess(t *testing.T) {
server := newServer("{}")
defer server.Close()

tmp := defaultEndpoints.success
defaultEndpoints.success = server.URL
defer func() { defaultEndpoints.success = tmp }()

ctx := testContext(t)
err := ctx.reportSuccess(&testUpdate, testOptions, server.URL, 5*time.Millisecond)
assert.NoError(t, err)
}
2 changes: 1 addition & 1 deletion keybase/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (k UpdateSource) findUpdate(options updater.UpdateOptions, timeout time.Dur
}

urlValues := url.Values{}
urlValues.Add("install_id", options.InstallID)
urlValues.Add("install_id", k.cfg.GetInstallID())
urlValues.Add("version", options.Version)
urlValues.Add("platform", options.Platform)
urlValues.Add("run_mode", options.Env)
Expand Down
28 changes: 2 additions & 26 deletions protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

package updater

import "time"

// Asset describes a downloadable file
type Asset struct {
Name string `codec:"name" json:"name"`
Expand Down Expand Up @@ -33,8 +31,9 @@ type Update struct {
Name string `json:"name"`
Description string `json:"description"`
InstallID string `json:"installId"`
RequestID string `json:"requestId"`
Type UpdateType `json:"type"`
PublishedAt Time `json:"publishedAt"`
PublishedAt int64 `json:"publishedAt"`
Asset *Asset `json:"asset,omitempty"`
}

Expand All @@ -52,8 +51,6 @@ type UpdateOptions struct {
Channel string `json:"channel"`
// Env is an environment or run mode (prod, staging, devel)
Env string `json:"env"`
// InstallID is an identifier that the client can send with requests
InstallID string `json:"installId"`
// Arch is an architecure description (x64, i386, arm)
Arch string `json:"arch"`
// Force is whether to apply the update, even if older or same version
Expand Down Expand Up @@ -101,24 +98,3 @@ type UpdateUI interface {
// UpdatePrompt prompts for an update
UpdatePrompt(Update, UpdateOptions, UpdatePromptOptions) (*UpdatePromptResponse, error)
}

// Time is milliseconds since epoch
type Time int64

// FromTime converts protocol time to golang Time
func FromTime(t Time) time.Time {
if t == 0 {
return time.Time{}
}
return time.Unix(0, int64(t)*1000000)
}

// ToTime converts golang Time to protocol Time
func ToTime(t time.Time) Time {
// the result of calling UnixNano on the zero Time is undefined.
// https://golang.org/pkg/time/#Time.UnixNano
if t.IsZero() {
return 0
}
return Time(t.UnixNano() / 1000000)
}
6 changes: 4 additions & 2 deletions update_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ func (u testUpdateCheckUI) UpdateOptions() UpdateOptions {
return newDefaultTestUpdateOptions()
}

func (u testUpdateCheckUI) ReportAction(_ UpdateAction, _ UpdateOptions) {}
func (u testUpdateCheckUI) ReportAction(_ UpdateAction, _ *Update, _ UpdateOptions) {}

func (u testUpdateCheckUI) ReportError(_ error, _ UpdateOptions) {}
func (u testUpdateCheckUI) ReportError(_ error, _ *Update, _ UpdateOptions) {}

func (u testUpdateCheckUI) ReportSuccess(_ *Update, _ UpdateOptions) {}

func TestUpdateCheckerError(t *testing.T) {
testServer := testServerForUpdateFile(t, testZipPath)
Expand Down
25 changes: 12 additions & 13 deletions updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ type Context interface {
BeforeApply(update Update) error
AfterApply(update Update) error
Restart() error
ReportError(err error, options UpdateOptions)
ReportAction(action UpdateAction, options UpdateOptions)
ReportError(err error, update *Update, options UpdateOptions)
ReportAction(action UpdateAction, update *Update, options UpdateOptions)
ReportSuccess(update *Update, options UpdateOptions)
}

// Config defines configuration for the Updater
Expand All @@ -62,7 +63,9 @@ func (u *Updater) Update(ctx Context) (*Update, error) {
options := ctx.UpdateOptions()
update, err := u.update(ctx, options)
if err != nil {
ctx.ReportError(err, options)
ctx.ReportError(err, update, options)
} else {
ctx.ReportSuccess(update, options)
}
return update, err
}
Expand All @@ -84,14 +87,14 @@ func (u *Updater) update(ctx Context, options UpdateOptions) (*Update, error) {
}
switch updateAction {
case UpdateActionApply:
ctx.ReportAction(UpdateActionApply, options)
ctx.ReportAction(UpdateActionApply, update, options)
case UpdateActionAuto:
ctx.ReportAction(UpdateActionAuto, options)
ctx.ReportAction(UpdateActionAuto, update, options)
case UpdateActionSnooze:
ctx.ReportAction(UpdateActionSnooze, options)
ctx.ReportAction(UpdateActionSnooze, update, options)
return update, cancelErr(fmt.Errorf("Snoozed update"))
case UpdateActionCancel:
ctx.ReportAction(UpdateActionCancel, options)
ctx.ReportAction(UpdateActionCancel, update, options)
return update, cancelErr(fmt.Errorf("Canceled"))
case UpdateActionError:
return update, promptErr(fmt.Errorf("Unknown prompt error"))
Expand Down Expand Up @@ -129,10 +132,6 @@ func (u *Updater) apply(ctx Context, update Update, options UpdateOptions) error
return applyErr(err)
}

if err := ctx.BeforeApply(update); err != nil {
return applyErr(err)
}

if err := u.platformApplyUpdate(update, options); err != nil {
return applyErr(err)
}
Expand Down Expand Up @@ -185,7 +184,7 @@ func (u *Updater) checkForUpdate(ctx Context, options UpdateOptions) (*Update, e
if update.InstallID != "" {
if err := u.config.SetInstallID(update.InstallID); err != nil {
u.log.Warningf("Error saving install ID: %s", err)
ctx.ReportError(configErr(fmt.Errorf("Error saving install ID: %s", err)), options)
ctx.ReportError(configErr(fmt.Errorf("Error saving install ID: %s", err)), update, options)
}
}

Expand Down Expand Up @@ -219,7 +218,7 @@ func (u *Updater) promptForUpdateAction(ctx Context, update Update, options Upda
u.log.Debugf("Update prompt response: %#v", updatePromptResponse)
if err := u.config.SetUpdateAuto(updatePromptResponse.AutoUpdate); err != nil {
u.log.Warningf("Error setting auto preference: %s", err)
ctx.ReportError(configErr(fmt.Errorf("Error setting auto preference: %s", err)), options)
ctx.ReportError(configErr(fmt.Errorf("Error setting auto preference: %s", err)), &update, options)
}

return updatePromptResponse.Action, nil
Expand Down
Loading

0 comments on commit 8ff2d80

Please sign in to comment.