Integrated client-side with dump requests, haven't written any integration tests yet

This commit is contained in:
David Dworken 2022-04-28 11:46:14 -07:00
parent 46d7e9e013
commit cbc4e70605
4 changed files with 41 additions and 15 deletions

View File

@ -46,12 +46,6 @@ func updateUsageData(userId, deviceId string) {
} }
} }
type DumpRequest struct {
UserId string `json:"user_id"`
RequestingDeviceId string `json:"requesting_device_id"`
RequestTime time.Time `json:"request_time"`
}
func apiSubmitHandler(w http.ResponseWriter, r *http.Request) { func apiSubmitHandler(w http.ResponseWriter, r *http.Request) {
data, err := ioutil.ReadAll(r.Body) data, err := ioutil.ReadAll(r.Body)
if err != nil { if err != nil {
@ -128,14 +122,16 @@ func apiRegisterHandler(w http.ResponseWriter, r *http.Request) {
userId := r.URL.Query().Get("user_id") userId := r.URL.Query().Get("user_id")
deviceId := r.URL.Query().Get("device_id") deviceId := r.URL.Query().Get("device_id")
GLOBAL_DB.Create(&shared.Device{UserId: userId, DeviceId: deviceId, RegistrationIp: r.RemoteAddr, RegistrationDate: time.Now()}) GLOBAL_DB.Create(&shared.Device{UserId: userId, DeviceId: deviceId, RegistrationIp: r.RemoteAddr, RegistrationDate: time.Now()})
GLOBAL_DB.Create(&DumpRequest{UserId: userId, RequestingDeviceId: deviceId, RequestTime: time.Now()}) GLOBAL_DB.Create(&shared.DumpRequest{UserId: userId, RequestingDeviceId: deviceId, RequestTime: time.Now()})
updateUsageData(userId, deviceId) updateUsageData(userId, deviceId)
} }
func apiGetPendingDumpRequestsHandler(w http.ResponseWriter, r *http.Request) { func apiGetPendingDumpRequestsHandler(w http.ResponseWriter, r *http.Request) {
userId := r.URL.Query().Get("user_id") userId := r.URL.Query().Get("user_id")
var dumpRequests []*DumpRequest deviceId := r.URL.Query().Get("device_id")
result := GLOBAL_DB.Where("user_id = ?", userId).Find(&dumpRequests) var dumpRequests []*shared.DumpRequest
// Filter out ones requested by the hishtory instance that sent this request
result := GLOBAL_DB.Where("user_id = ? AND requesting_device_id != ?", userId, deviceId).Find(&dumpRequests)
if result.Error != nil { if result.Error != nil {
panic(fmt.Errorf("DB query error: %v", result.Error)) panic(fmt.Errorf("DB query error: %v", result.Error))
} }
@ -175,7 +171,7 @@ func apiSubmitDumpHandler(w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
panic(fmt.Errorf("failed to execute transaction to add dumped DB: %v", err)) panic(fmt.Errorf("failed to execute transaction to add dumped DB: %v", err))
} }
result := GLOBAL_DB.Delete(&DumpRequest{}, "user_id = ? AND requesting_device_id = ?", userId, requestingDeviceId) result := GLOBAL_DB.Delete(&shared.DumpRequest{}, "user_id = ? AND requesting_device_id = ?", userId, requestingDeviceId)
if result.Error != nil { if result.Error != nil {
panic(fmt.Errorf("failed to clear the dump request: %v", err)) panic(fmt.Errorf("failed to clear the dump request: %v", err))
} }
@ -206,7 +202,7 @@ func OpenDB() (*gorm.DB, error) {
db.AutoMigrate(&shared.EncHistoryEntry{}) db.AutoMigrate(&shared.EncHistoryEntry{})
db.AutoMigrate(&shared.Device{}) db.AutoMigrate(&shared.Device{})
db.AutoMigrate(&UsageData{}) db.AutoMigrate(&UsageData{})
db.AutoMigrate(&DumpRequest{}) db.AutoMigrate(&shared.DumpRequest{})
db.Exec("PRAGMA journal_mode = WAL") db.Exec("PRAGMA journal_mode = WAL")
return db, nil return db, nil
} }
@ -218,7 +214,7 @@ func OpenDB() (*gorm.DB, error) {
db.AutoMigrate(&shared.EncHistoryEntry{}) db.AutoMigrate(&shared.EncHistoryEntry{})
db.AutoMigrate(&shared.Device{}) db.AutoMigrate(&shared.Device{})
db.AutoMigrate(&UsageData{}) db.AutoMigrate(&UsageData{})
db.AutoMigrate(&DumpRequest{}) db.AutoMigrate(&shared.DumpRequest{})
return db, nil return db, nil
} }

View File

@ -134,7 +134,7 @@ func TestDumpRequestAndResponse(t *testing.T) {
defer res.Body.Close() defer res.Body.Close()
respBody, err := ioutil.ReadAll(res.Body) respBody, err := ioutil.ReadAll(res.Body)
shared.Check(t, err) shared.Check(t, err)
var dumpRequests []*DumpRequest var dumpRequests []*shared.DumpRequest
shared.Check(t, json.Unmarshal(respBody, &dumpRequests)) shared.Check(t, json.Unmarshal(respBody, &dumpRequests))
if len(dumpRequests) != 1 { if len(dumpRequests) != 1 {
t.Fatalf("expected one pending dump request, got %#v", dumpRequests) t.Fatalf("expected one pending dump request, got %#v", dumpRequests)
@ -154,7 +154,7 @@ func TestDumpRequestAndResponse(t *testing.T) {
defer res.Body.Close() defer res.Body.Close()
respBody, err = ioutil.ReadAll(res.Body) respBody, err = ioutil.ReadAll(res.Body)
shared.Check(t, err) shared.Check(t, err)
dumpRequests = make([]*DumpRequest, 0) dumpRequests = make([]*shared.DumpRequest, 0)
shared.Check(t, json.Unmarshal(respBody, &dumpRequests)) shared.Check(t, json.Unmarshal(respBody, &dumpRequests))
if len(dumpRequests) != 1 { if len(dumpRequests) != 1 {
t.Fatalf("expected one pending dump request, got %#v", dumpRequests) t.Fatalf("expected one pending dump request, got %#v", dumpRequests)
@ -219,7 +219,7 @@ func TestDumpRequestAndResponse(t *testing.T) {
defer res.Body.Close() defer res.Body.Close()
respBody, err = ioutil.ReadAll(res.Body) respBody, err = ioutil.ReadAll(res.Body)
shared.Check(t, err) shared.Check(t, err)
dumpRequests = make([]*DumpRequest, 0) dumpRequests = make([]*shared.DumpRequest, 0)
shared.Check(t, json.Unmarshal(respBody, &dumpRequests)) shared.Check(t, json.Unmarshal(respBody, &dumpRequests))
if len(dumpRequests) != 1 { if len(dumpRequests) != 1 {
t.Fatalf("expected one pending dump request, got %#v", dumpRequests) t.Fatalf("expected one pending dump request, got %#v", dumpRequests)

View File

@ -140,6 +140,30 @@ func saveHistoryEntry() {
lib.CheckFatalError(err) lib.CheckFatalError(err)
} }
} }
// Check if there is a pending dump request and reply to it if so
resp, err := lib.ApiGet("/api/v1/get-dump-requests?user_id=" + data.UserId(config.UserSecret) + "&device_id=" + config.DeviceId)
lib.CheckFatalError(err)
var dumpRequests []*shared.DumpRequest
err = json.Unmarshal(resp, &dumpRequests)
lib.CheckFatalError(err)
if len(dumpRequests) > 0 {
lib.CheckFatalError(retrieveAdditionalEntriesFromRemote(db))
entries, err := data.Search(db, "", 0)
lib.CheckFatalError(err)
var encEntries []*shared.EncHistoryEntry
for _, entry := range entries {
enc, err := data.EncryptHistoryEntry(config.UserSecret, *entry)
lib.CheckFatalError(err)
encEntries = append(encEntries, &enc)
}
reqBody, err := json.Marshal(encEntries)
lib.CheckFatalError(err)
for _, dumpRequest := range dumpRequests {
_, err := lib.ApiPost("/api/v1/submit-dump?user_id="+dumpRequest.UserId+"&requesting_device_id="+dumpRequest.RequestingDeviceId, "application/json", reqBody)
lib.CheckFatalError(err)
}
}
} }
func export() { func export() {

View File

@ -25,6 +25,12 @@ type Device struct {
RegistrationDate time.Time `json:"registration_date"` RegistrationDate time.Time `json:"registration_date"`
} }
type DumpRequest struct {
UserId string `json:"user_id"`
RequestingDeviceId string `json:"requesting_device_id"`
RequestTime time.Time `json:"request_time"`
}
type UpdateInfo struct { type UpdateInfo struct {
LinuxAmd64Url string `json:"linux_amd_64_url"` LinuxAmd64Url string `json:"linux_amd_64_url"`
LinuxAmd64AttestationUrl string `json:"linux_amd_64_attestation_url"` LinuxAmd64AttestationUrl string `json:"linux_amd_64_attestation_url"`