Skip to content

Commit

Permalink
Merge pull request #197 from SkynetLabs/ivo/stop_tracking_registry
Browse files Browse the repository at this point in the history
Stop tracking registry accesses
  • Loading branch information
ro-tex authored Apr 20, 2022
2 parents 3b6e97a + 68ea849 commit 348c073
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 57 deletions.
16 changes: 4 additions & 12 deletions api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -1200,22 +1200,14 @@ func (api *API) trackDownloadPOST(u *database.User, w http.ResponseWriter, req *
}

// trackRegistryReadPOST registers a new registry read in the system.
func (api *API) trackRegistryReadPOST(u *database.User, w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
_, err := api.staticDB.RegistryReadCreate(req.Context(), *u)
if err != nil {
api.WriteError(w, err, http.StatusInternalServerError)
return
}
func (api *API) trackRegistryReadPOST(_ *database.User, w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
// Tracking registry reads is disabled.
api.WriteSuccess(w)
}

// trackRegistryWritePOST registers a new registry write in the system.
func (api *API) trackRegistryWritePOST(u *database.User, w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
_, err := api.staticDB.RegistryWriteCreate(req.Context(), *u)
if err != nil {
api.WriteError(w, err, http.StatusInternalServerError)
return
}
func (api *API) trackRegistryWritePOST(_ *database.User, w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
// Tracking registry writes is disabled.
api.WriteSuccess(w)
}

Expand Down
18 changes: 17 additions & 1 deletion database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"context"
"fmt"
"net/url"
"strings"

"github.com/SkynetLabs/skynet-accounts/lib"

"github.com/sirupsen/logrus"
"gitlab.com/NebulousLabs/errors"
"go.mongodb.org/mongo-driver/bson"
Expand Down Expand Up @@ -188,6 +188,22 @@ func connectionString(creds DBCredentials) string {
// See https://docs.mongodb.com/manual/indexes/
// See https://docs.mongodb.com/manual/core/index-unique/
func ensureDBSchema(ctx context.Context, db *mongo.Database, schema map[string][]mongo.IndexModel, log *logrus.Logger) error {
// Drop collections we no longer need.
err := db.Collection(collRegistryReads).Drop(ctx)
if err != nil {
return err
}
err = db.Collection(collRegistryWrites).Drop(ctx)
if err != nil {
return err
}
// Drop indexes we no longer need.
_, err = db.Collection(collUsers).Indexes().DropOne(ctx, "email_unique")
// Ignore namespace not found errors.
if err != nil && !strings.Contains(err.Error(), "NamespaceNotFound") {
return err
}
// Ensure current schema.
for collName, models := range schema {
coll, err := ensureCollection(ctx, db, collName)
if err != nil {
Expand Down
12 changes: 0 additions & 12 deletions database/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,6 @@ var (
Options: options.Index().SetName("skylink_id"),
},
},
collRegistryReads: {
{
Keys: bson.D{{"user_id", 1}},
Options: options.Index().SetName("user_id"),
},
},
collRegistryWrites: {
{
Keys: bson.D{{"user_id", 1}},
Options: options.Index().SetName("user_id"),
},
},
collEmails: {
{
Keys: bson.D{{"failed_attempts", 1}},
Expand Down
32 changes: 0 additions & 32 deletions test/api/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -914,38 +914,6 @@ func testTrackingAndStats(t *testing.T, at *test.AccountsTester) {
expectedStats.BandwidthDownloads += skynet.BandwidthDownloadCost(200)
expectedStats.TotalDownloadsSize += 200

// Call trackRegistryRead without a cookie.
at.ClearCredentials()
_, err = at.TrackRegistryRead()
if err == nil || !strings.Contains(err.Error(), unauthorized) {
t.Fatalf("Expected error '%s', got '%v'", unauthorized, err)
}
at.SetCookie(c)
// Call trackRegistryRead.
_, err = at.TrackRegistryRead()
if err != nil {
t.Fatal(err)
}
// Adjust the expectations.
expectedStats.NumRegReads++
expectedStats.BandwidthRegReads += skynet.CostBandwidthRegistryRead

// Call trackRegistryWrite without a cookie.
at.ClearCredentials()
_, err = at.TrackRegistryWrite()
if err == nil || !strings.Contains(err.Error(), unauthorized) {
t.Fatalf("Expected error '%s', got '%v'", unauthorized, err)
}
at.SetCookie(c)
// Call trackRegistryWrite.
_, err = at.TrackRegistryWrite()
if err != nil {
t.Fatal(err)
}
// Adjust the expectations.
expectedStats.NumRegWrites++
expectedStats.BandwidthRegWrites += skynet.CostBandwidthRegistryWrite

// Call userStats without a cookie.
at.ClearCredentials()
_, _, err = at.UserStats("", nil)
Expand Down

0 comments on commit 348c073

Please sign in to comment.