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

Stop tracking registry accesses #197

Merged
merged 1 commit into from
Apr 20, 2022
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
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 @@ -909,38 +909,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