Skip to content

Commit

Permalink
Merge pull request #317 from pixlise/feature/impersonate-user
Browse files Browse the repository at this point in the history
Feature/impersonate user
  • Loading branch information
pnemere authored Sep 16, 2024
2 parents adc4ff9 + 4b376c3 commit 75f6d6d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 24 deletions.
33 changes: 32 additions & 1 deletion api/ws/handlers/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/mongodb/mongo-tools/mongodump"
"github.com/pixlise/core/v4/api/services"
"github.com/pixlise/core/v4/api/ws/wsHelpers"
"github.com/pixlise/core/v4/core/fileaccess"
"github.com/pixlise/core/v4/core/logger"
"github.com/pixlise/core/v4/core/mongoDBConnection"
protos "github.com/pixlise/core/v4/generated-protos"
)
Expand Down Expand Up @@ -50,13 +52,42 @@ func HandleBackupDBReq(req *protos.BackupDBReq, hctx wsHelpers.HandlerContext) (
return &protos.BackupDBResp{}, nil
}

func clearBucket(bucket string, fs fileaccess.FileAccess, logger logger.ILogger) error {
files, err := fs.ListObjects(bucket, "")
if err != nil {
return err
}

logger.Infof("Clearing %v files from bucket: %v", len(files), bucket)

for c, file := range files {
logger.Infof("Clearing file %v of %v...", c, len(files))

err = fs.DeleteObject(bucket, file)
if err != nil {
return err
}
}

logger.Infof("Bucket cleared: %v", bucket)
return nil
}

func runBackup(dump *mongodump.MongoDump, startTimestamp int64, svcs *services.APIServices) {
var wg sync.WaitGroup
var errDBDump error
var errScanSync error
var errImageSync error
var errQuantSync error

svcs.Log.Infof("Clearing PIXLISE backup bucket: %v", svcs.Config.DataBackupBucket)

err := clearBucket(svcs.Config.DataBackupBucket, svcs.FS, svcs.Log)
if err != nil {
svcs.Log.Errorf("PIXLISE Backup bucket clear failed: %v", err)
return
}

wg.Add(1)
go func() {
defer wg.Done()
Expand Down Expand Up @@ -96,7 +127,6 @@ func runBackup(dump *mongodump.MongoDump, startTimestamp int64, svcs *services.A
// Wait for all sync tasks
wg.Wait()

var err error
if errDBDump != nil {
err = fmt.Errorf("PIXLISE Backup DB dump failed: %v", errDBDump)
}
Expand All @@ -114,6 +144,7 @@ func runBackup(dump *mongodump.MongoDump, startTimestamp int64, svcs *services.A
}

if err != nil {
svcs.Log.Errorf("%v", err)
return
}

Expand Down
12 changes: 1 addition & 11 deletions api/ws/wsHelpers/sync-mongo-dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,15 @@ func MakeMongoDumpInstance(mongoDetails mongoDBConnection.MongoConnectionDetails
UseSSL: true,
SSLCAFile: "./global-bundle.pem",
SSLPEMKeyFile: "./global-bundle.pem",
//SSLAllowInvalidCert: true,
//SSLAllowInvalidHost: true,
// SSLFipsMode bool `long:"sslFIPSMode" description:"use FIPS mode of the installed openssl library"`
//TLSInsecure: true,
}

auth := options.Auth{
Username: mongoDetails.User,
Password: mongoDetails.Password,
//Mechanism: "DEFAULT",
}

connection := &options.Connection{
Host: mongoDetails.Host,
//Port: db.DefaultTestPort,
}

// Trim excess
Expand All @@ -48,11 +42,7 @@ func MakeMongoDumpInstance(mongoDetails mongoDBConnection.MongoConnectionDetails

connectionURI := fmt.Sprintf("mongodb://%s/%s", connection.Host, "")

passSeg := ""
if len(auth.Password) > 5 {
passSeg = auth.Password[0:5]
}
logger.Infof("MongoDump connecting to: %v, user %v, pass %v...", connection.Host, auth.Username, passSeg)
logger.Infof("MongoDump connecting to: %v, user %v...", connection.Host, auth.Username)

uri, err := options.NewURI(connectionURI)
if err != nil {
Expand Down
27 changes: 15 additions & 12 deletions api/ws/wsHelpers/sync-mongo-restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,36 @@ import (
func MakeMongoRestoreInstance(mongoDetails mongoDBConnection.MongoConnectionDetails, logger logger.ILogger, restoreToDBName string, restoreFromDBName string) (*mongorestore.MongoRestore, error) {
var toolOptions *options.ToolOptions

ssl := options.SSL{}
ssl := options.SSL{
UseSSL: true,
SSLCAFile: "./global-bundle.pem",
SSLPEMKeyFile: "./global-bundle.pem",
}

auth := options.Auth{
Username: mongoDetails.User,
Password: mongoDetails.Password,
}

connection := &options.Connection{
Host: mongoDetails.Host,
//Port: db.DefaultTestPort,
}

// Trim excess
protocolPrefix := "mongodb://"
connection.Host = strings.TrimPrefix(connection.Host, protocolPrefix)

passSeg := ""
if len(auth.Password) > 5 {
passSeg = auth.Password[0:5]
}
logger.Infof("MongoRestore connecting to: %v, user %v, pass %v...", connection.Host, auth.Username, passSeg)
logger.Infof("MongoRestore connecting to: %v, user %v...", connection.Host, auth.Username)

retryWrites := false

toolOptions = &options.ToolOptions{
SSL: &ssl,
Connection: connection,
Auth: &auth,
Verbosity: &options.Verbosity{},
URI: &options.URI{},
RetryWrites: &retryWrites,
SSL: &ssl,
Connection: connection,
Auth: &auth,
Verbosity: &options.Verbosity{},
URI: &options.URI{},
}

toolOptions.Namespace = &options.Namespace{DB: restoreToDBName}
Expand Down

0 comments on commit 75f6d6d

Please sign in to comment.