Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete #10

Merged
merged 3 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"args": [
"-port",
"8088",
]
],
"env": {"CGO_ENABLED": "0"}
}
]
}
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ curl http://127.0.0.1:8080/62fb66ee6841600228945ef592c8998e097c51271f9acf1f15e72
12
```

#### Delete Value

```bash
# delete everything with this upload key
curl -s https://your-server.com/delete/1e1c7e5f220d2eee5ebbfd1428b84aaf1570ca4f88105a81feac901850b20a77
```

```plain
OK
```

### Create Key Pair

The upload key is just random data with a length of 256bit encoded in hex, the download key is derived a each upload time. The download key is just a hashed upload key with sha256.
Expand Down
2 changes: 2 additions & 0 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ if (-Not (Test-Path $outputDir)) {

# Build the application
Write-Host "Building application for Linux AMD64..."

$env:CGO_ENABLED = 0
go build -ldflags="-w -s" -o "$outputDir/$binaryName" .

# Check if the build was successful
Expand Down
27 changes: 27 additions & 0 deletions httphandler/deleteHandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package httphandler

import (
"net/http"

"github.com/dhcgn/iot-ephemeral-value-store/domain"
"github.com/gorilla/mux"
)

func (c Config) DeleteHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
uploadKey := vars["uploadKey"]

downloadKey, err := domain.DeriveDownloadKey(uploadKey)
if err != nil {
http.Error(w, "Error deriving download key", http.StatusInternalServerError)
return
}

if err := c.StorageInstance.Delete(downloadKey); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,16 @@ func createRouter(hhc httphandler.Config, mc middleware.Config) *mux.Router {
// New routes
r.HandleFunc("/u/{uploadKey}", hhc.UploadHandler).Methods("GET")
r.HandleFunc("/u/{uploadKey}/", hhc.UploadHandler).Methods("GET")

r.HandleFunc("/d/{downloadKey}/json", hhc.DownloadHandler).Methods("GET")
r.HandleFunc("/d/{downloadKey}/plain/{param:.*}", hhc.PlainDownloadHandler).Methods("GET")
// New routes with nestetd paths, eg. /u/1234/param1
r.HandleFunc("/patch/{uploadKey}/{param:.*}", hhc.UploadAndPatchHandler).Methods("GET")

// Admin
r.HandleFunc("/delete/{uploadKey}", hhc.DeleteHandler).Methods("GET")
r.HandleFunc("/delete/{uploadKey}/", hhc.DeleteHandler).Methods("GET")

r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
key := domain.GenerateRandomKey()
key_down, err := domain.DeriveDownloadKey(key)
Expand Down
43 changes: 40 additions & 3 deletions main_router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ func TestLegacyRoutes(t *testing.T) {
checkBody bool
bodyContains string
}{
{"GET /", "GET", "/" + key_up + "/" + "?value=8923423", http.StatusOK, true, "Data uploaded successfully"},
{"GET /", "GET", "/" + key_down + "/" + "plain/value", http.StatusOK, true, "8923423\n"},
{"GET /", "GET", "/" + key_down + "/" + "json", http.StatusOK, true, "\"value\":\"8923423\""},
{"Upload", "GET", "/" + key_up + "/" + "?value=8923423", http.StatusOK, true, "Data uploaded successfully"},
{"Download Plain", "GET", "/" + key_down + "/" + "plain/value", http.StatusOK, true, "8923423\n"},
{"Downlaod JSON", "GET", "/" + key_down + "/" + "json", http.StatusOK, true, "\"value\":\"8923423\""},
}

for _, tt := range tests {
Expand Down Expand Up @@ -133,6 +133,43 @@ func TestRoutesUploadDownload(t *testing.T) {
}
}

func TestRoutesUploadDownloadDelete(t *testing.T) {
httphandlerConfig, middlewareConfig := createTestEnvireonment(t)

router := createRouter(httphandlerConfig, middlewareConfig)

tests := []struct {
name string
url string
expectedStatusCode int
bodyContains string
}{
{"Upload", "/u/" + key_up + "/" + "?value=8923423", http.StatusOK, "Data uploaded successfully"},
{"Download plain", "/d/" + key_down + "/" + "plain/value", http.StatusOK, "8923423\n"},
{"Download json", "/d/" + key_down + "/" + "json", http.StatusOK, "\"value\":\"8923423\""},
{"Delete", "/delete/" + key_up + "/", http.StatusOK, "OK"},
{"Download after delete plain", "/d/" + key_down + "/" + "plain/value", http.StatusNotFound, ""},
{"Download after delete json", "/d/" + key_down + "/" + "json", http.StatusNotFound, ""},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req, err := http.NewRequest("GET", tt.url, nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
router.ServeHTTP(rr, req)

assert.Equal(t, tt.expectedStatusCode, rr.Code)
if tt.bodyContains != "" {
assert.Contains(t, rr.Body.String(), tt.bodyContains)
}
})
}
}

func TestLegacyRoutesWithDifferentPathEndings(t *testing.T) {
httphandlerConfig, middlewareConfig := createTestEnvireonment(t)

Expand Down
4 changes: 4 additions & 0 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,15 @@ <h1>Welcome to the IoT Ephemeral Value Store Server</h1>
<h2>Getting Started</h2>
<p>
Just use this url generated just for you: <br>
<h4>Upload data</h4>
<a class="dynamic-link" data-path="/u/{{.UploadKey}}/?name=value">/u/{{.UploadKey}}/?name=value</a><br>
<h4>Download data</h4>
and access it with<br>
<a class="dynamic-link" data-path="/d/{{.DownloadKey}}/plain/name">/d/{{.DownloadKey}}/plain/name</a><br>
or in json with<br>
<a class="dynamic-link" data-path="/d/{{.DownloadKey}}/json">/d/{{.DownloadKey}}/json</a>
<h4>Delete data (or wait for retention time)</h4>
<a class="dynamic-link" data-path="/delete/{{.UploadKey}}">/u/{{.UploadKey}}/?name=value</a><br>
</p>

<h2>Server Settings</h2>
Expand Down
7 changes: 7 additions & 0 deletions storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type StorageInstance struct {

type Storage interface {
GetJSON(downloadKey string) ([]byte, error)
Delete(downloadKey string) error
Store(downloadKey string, dataToStore map[string]interface{}) error
Retrieve(downloadKey string) (map[string]interface{}, error)
}
Expand Down Expand Up @@ -103,3 +104,9 @@ func (c StorageInstance) Retrieve(downloadKey string) (map[string]interface{}, e
})
return existingData, err
}

func (c StorageInstance) Delete(downloadKey string) error {
return c.Db.Update(func(txn *badger.Txn) error {
return txn.Delete([]byte(downloadKey))
})
}
Loading