Add reupload command + include source_device_id query param when submitting entries

This commit is contained in:
David Dworken 2022-10-09 12:13:05 -07:00
parent f7a1e14451
commit 52c3b13cb6
2 changed files with 18 additions and 3 deletions

View File

@ -245,12 +245,15 @@ func testIntegrationWithNewDevice(t *testing.T, tester shellTester) {
t.Fatalf("hishtory query doesn't contain cmd run on another machine! out=%#v", out)
}
// Run a reupload just to test that flow
tester.RunInteractiveShell(t, "hishtory reupload")
// Finally, test the export command
out = tester.RunInteractiveShell(t, `hishtory export | grep -v pipefail | grep -v '/tmp/client install'`)
if strings.Contains(out, "thisisnotrecorded") {
t.Fatalf("hishtory export contains a command that should not have been recorded, out=%#v", out)
}
expectedOutputWithoutKey := "hishtory status\nhishtory query\nls /a\nls /bar\nls /foo\necho foo\necho bar\nhishtory enable\necho thisisrecorded\nhishtory query\nhishtory query foo\necho hello | grep complex | sed s/h/i/g; echo baz && echo \"fo 'o\"\nhishtory query complex\nhishtory query\necho mynewcommand\nhishtory query\nyes | hishtory init %s\nhishtory query\necho mynewercommand\nhishtory query\nothercomputer\nhishtory query\n"
expectedOutputWithoutKey := "hishtory status\nhishtory query\nls /a\nls /bar\nls /foo\necho foo\necho bar\nhishtory enable\necho thisisrecorded\nhishtory query\nhishtory query foo\necho hello | grep complex | sed s/h/i/g; echo baz && echo \"fo 'o\"\nhishtory query complex\nhishtory query\necho mynewcommand\nhishtory query\nyes | hishtory init %s\nhishtory query\necho mynewercommand\nhishtory query\nothercomputer\nhishtory query\nhishtory reupload\n"
expectedOutput := fmt.Sprintf(expectedOutputWithoutKey, userSecret)
if diff := cmp.Diff(expectedOutput, out); diff != "" {
t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out)

View File

@ -107,6 +107,18 @@ func main() {
if err != nil {
log.Fatalf("Failed to update hishtory: %v", err)
}
case "reupload":
// Purposefully undocumented since this command is generally not necessary to run
ctx := hctx.MakeContext()
config := hctx.GetConf(ctx)
entries, err := data.Search(hctx.GetDb(ctx), "", 0)
lib.CheckFatalError(err)
for _, entry := range entries {
jsonValue, err := lib.EncryptAndMarshal(config, entry)
lib.CheckFatalError(err)
_, err = lib.ApiPost("/api/v1/submit?source_device_id="+config.DeviceId, "application/json", jsonValue)
lib.CheckFatalError(err)
}
case "-h":
fallthrough
case "help":
@ -265,7 +277,7 @@ func maybeUploadSkippedHistoryEntries(ctx *context.Context) error {
if err != nil {
return err
}
_, err = lib.ApiPost("/api/v1/submit", "application/json", jsonValue)
_, err = lib.ApiPost("/api/v1/submit?source_device_id="+config.DeviceId, "application/json", jsonValue)
if err != nil {
// Failed to upload the history entry, so we must still be offline. So just return nil and we'll try again later.
return nil
@ -303,7 +315,7 @@ func saveHistoryEntry(ctx *context.Context) {
// Persist it remotely
jsonValue, err := lib.EncryptAndMarshal(config, entry)
lib.CheckFatalError(err)
_, err = lib.ApiPost("/api/v1/submit", "application/json", jsonValue)
_, err = lib.ApiPost("/api/v1/submit?source_device_id="+config.DeviceId, "application/json", jsonValue)
if err != nil {
if lib.IsOfflineError(err) {
hctx.GetLogger().Printf("Failed to remotely persist hishtory entry because the device is offline!")