$98 GRAYBYTE WORDPRESS FILE MANAGER $99

SERVER : in-mum-web1330.main-hosting.eu #1 SMP Mon Feb 10 22:45:17 UTC 2025
SERVER IP : 147.79.69.169 | ADMIN IP 216.73.216.215
OPTIONS : CRL = ON | WGT = ON | SDO = OFF | PKEX = OFF
DEACTIVATED : NONE

/opt/go/pkg/mod/go.mongodb.org/mongo-driver@v1.11.3/mongo/

HOME
Current File : /opt/go/pkg/mod/go.mongodb.org/mongo-driver@v1.11.3/mongo//mongocryptd.go
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

package mongo

import (
	"context"
	"os/exec"
	"strings"
	"time"

	"go.mongodb.org/mongo-driver/mongo/options"
	"go.mongodb.org/mongo-driver/mongo/readconcern"
	"go.mongodb.org/mongo-driver/mongo/readpref"
	"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
)

const (
	defaultServerSelectionTimeout = 10 * time.Second
	defaultURI                    = "mongodb://localhost:27020"
	defaultPath                   = "mongocryptd"
	serverSelectionTimeoutStr     = "server selection error"
)

var defaultTimeoutArgs = []string{"--idleShutdownTimeoutSecs=60"}
var databaseOpts = options.Database().SetReadConcern(readconcern.New()).SetReadPreference(readpref.Primary())

type mongocryptdClient struct {
	bypassSpawn bool
	client      *Client
	path        string
	spawnArgs   []string
}

func newMongocryptdClient(cryptSharedLibAvailable bool, opts *options.AutoEncryptionOptions) (*mongocryptdClient, error) {
	// create mcryptClient instance and spawn process if necessary
	var bypassSpawn bool
	var bypassAutoEncryption bool

	if bypass, ok := opts.ExtraOptions["mongocryptdBypassSpawn"]; ok {
		bypassSpawn = bypass.(bool)
	}
	if opts.BypassAutoEncryption != nil {
		bypassAutoEncryption = *opts.BypassAutoEncryption
	}

	bypassQueryAnalysis := opts.BypassQueryAnalysis != nil && *opts.BypassQueryAnalysis

	mc := &mongocryptdClient{
		// mongocryptd should not be spawned if any of these conditions are true:
		// - mongocryptdBypassSpawn is passed
		// - bypassAutoEncryption is true because mongocryptd is not used during decryption
		// - bypassQueryAnalysis is true because mongocryptd is not used during decryption
		// - the crypt_shared library is available because it replaces all mongocryptd functionality.
		bypassSpawn: bypassSpawn || bypassAutoEncryption || bypassQueryAnalysis || cryptSharedLibAvailable,
	}

	if !mc.bypassSpawn {
		mc.path, mc.spawnArgs = createSpawnArgs(opts.ExtraOptions)
		if err := mc.spawnProcess(); err != nil {
			return nil, err
		}
	}

	// get connection string
	uri := defaultURI
	if u, ok := opts.ExtraOptions["mongocryptdURI"]; ok {
		uri = u.(string)
	}

	// create client
	client, err := NewClient(options.Client().ApplyURI(uri).SetServerSelectionTimeout(defaultServerSelectionTimeout))
	if err != nil {
		return nil, err
	}
	mc.client = client

	return mc, nil
}

// markCommand executes the given command on mongocryptd.
func (mc *mongocryptdClient) markCommand(ctx context.Context, dbName string, cmd bsoncore.Document) (bsoncore.Document, error) {
	// Remove the explicit session from the context if one is set.
	// The explicit session will be from a different client.
	// If an explicit session is set, it is applied after automatic encryption.
	ctx = NewSessionContext(ctx, nil)
	db := mc.client.Database(dbName, databaseOpts)

	res, err := db.RunCommand(ctx, cmd).DecodeBytes()
	// propagate original result
	if err == nil {
		return bsoncore.Document(res), nil
	}
	// wrap original error
	if mc.bypassSpawn || !strings.Contains(err.Error(), serverSelectionTimeoutStr) {
		return nil, MongocryptdError{Wrapped: err}
	}

	// re-spawn and retry
	if err = mc.spawnProcess(); err != nil {
		return nil, err
	}
	res, err = db.RunCommand(ctx, cmd).DecodeBytes()
	if err != nil {
		return nil, MongocryptdError{Wrapped: err}
	}
	return bsoncore.Document(res), nil
}

// connect connects the underlying Client instance. This must be called before performing any mark operations.
func (mc *mongocryptdClient) connect(ctx context.Context) error {
	return mc.client.Connect(ctx)
}

// disconnect disconnects the underlying Client instance. This should be called after all operations have completed.
func (mc *mongocryptdClient) disconnect(ctx context.Context) error {
	return mc.client.Disconnect(ctx)
}

func (mc *mongocryptdClient) spawnProcess() error {
	// Ignore gosec warning about subprocess launched with externally-provided path variable.
	/* #nosec G204 */
	cmd := exec.Command(mc.path, mc.spawnArgs...)
	cmd.Stdout = nil
	cmd.Stderr = nil
	return cmd.Start()
}

// createSpawnArgs creates arguments to spawn mcryptClient. It returns the path and a slice of arguments.
func createSpawnArgs(opts map[string]interface{}) (string, []string) {
	var spawnArgs []string

	// get command path
	path := defaultPath
	if p, ok := opts["mongocryptdPath"]; ok {
		path = p.(string)
	}

	// add specified options
	if sa, ok := opts["mongocryptdSpawnArgs"]; ok {
		spawnArgs = append(spawnArgs, sa.([]string)...)
	}

	// add timeout options if necessary
	var foundTimeout bool
	for _, arg := range spawnArgs {
		// need to use HasPrefix instead of doing an exact equality check because both
		// mongocryptd supports both [--idleShutdownTimeoutSecs, 0] and [--idleShutdownTimeoutSecs=0]
		if strings.HasPrefix(arg, "--idleShutdownTimeoutSecs") {
			foundTimeout = true
			break
		}
	}
	if !foundTimeout {
		spawnArgs = append(spawnArgs, defaultTimeoutArgs...)
	}

	return path, spawnArgs
}

Current_dir [ NOT WRITEABLE ] Document_root [ WRITEABLE ]


[ Back ]
NAME
SIZE
LAST TOUCH
USER
CAN-I?
FUNCTIONS
..
--
1 Jan 1970 12.00 AM
root / root
0
address
--
25 Jan 2024 4.43 PM
root / root
0555
description
--
25 Jan 2024 4.43 PM
root / root
0555
gridfs
--
25 Jan 2024 4.43 PM
root / root
0555
integration
--
25 Jan 2024 4.43 PM
root / root
0555
options
--
25 Jan 2024 4.43 PM
root / root
0555
readconcern
--
25 Jan 2024 4.43 PM
root / root
0555
readpref
--
25 Jan 2024 4.43 PM
root / root
0555
writeconcern
--
25 Jan 2024 4.43 PM
root / root
0555
batch_cursor.go
1.56 KB
25 Jan 2024 4.43 PM
root / root
0444
bson_helpers_test.go
2.208 KB
25 Jan 2024 4.43 PM
root / root
0444
bulk_write.go
16.054 KB
25 Jan 2024 4.43 PM
root / root
0444
bulk_write_models.go
12.305 KB
25 Jan 2024 4.43 PM
root / root
0444
change_stream.go
22.284 KB
25 Jan 2024 4.43 PM
root / root
0444
change_stream_deployment.go
1.351 KB
25 Jan 2024 4.43 PM
root / root
0444
change_stream_test.go
0.857 KB
25 Jan 2024 4.43 PM
root / root
0444
client.go
28.838 KB
25 Jan 2024 4.43 PM
root / root
0444
client_encryption.go
12.148 KB
25 Jan 2024 4.43 PM
root / root
0444
client_examples_test.go
15.06 KB
25 Jan 2024 4.43 PM
root / root
0444
client_side_encryption_examples_test.go
11.118 KB
25 Jan 2024 4.43 PM
root / root
0444
client_test.go
18.347 KB
25 Jan 2024 4.43 PM
root / root
0444
collection.go
58.865 KB
25 Jan 2024 4.43 PM
root / root
0444
collection_test.go
8.775 KB
25 Jan 2024 4.43 PM
root / root
0444
crud_examples_test.go
30.029 KB
25 Jan 2024 4.43 PM
root / root
0444
crypt_retrievers.go
1.786 KB
25 Jan 2024 4.43 PM
root / root
0444
cursor.go
10.557 KB
25 Jan 2024 4.43 PM
root / root
0444
cursor_test.go
7.092 KB
25 Jan 2024 4.43 PM
root / root
0444
database.go
27.19 KB
25 Jan 2024 4.43 PM
root / root
0444
database_test.go
4.198 KB
25 Jan 2024 4.43 PM
root / root
0444
doc.go
5.937 KB
25 Jan 2024 4.43 PM
root / root
0444
errors.go
19.745 KB
25 Jan 2024 4.43 PM
root / root
0444
errors_test.go
1.866 KB
25 Jan 2024 4.43 PM
root / root
0444
index_options_builder.go
6.528 KB
25 Jan 2024 4.43 PM
root / root
0444
index_view.go
15.269 KB
25 Jan 2024 4.43 PM
root / root
0444
main_test.go
1.496 KB
25 Jan 2024 4.43 PM
root / root
0444
mongo.go
12.521 KB
25 Jan 2024 4.43 PM
root / root
0444
mongo_test.go
12.931 KB
25 Jan 2024 4.43 PM
root / root
0444
mongocryptd.go
5.07 KB
25 Jan 2024 4.43 PM
root / root
0444
ocsp_test.go
2.348 KB
25 Jan 2024 4.43 PM
root / root
0444
read_write_concern_spec_test.go
8.521 KB
25 Jan 2024 4.43 PM
root / root
0444
results.go
9.236 KB
25 Jan 2024 4.43 PM
root / root
0444
results_test.go
2.571 KB
25 Jan 2024 4.43 PM
root / root
0444
session.go
13.816 KB
25 Jan 2024 4.43 PM
root / root
0444
single_result.go
3.84 KB
25 Jan 2024 4.43 PM
root / root
0444
single_result_test.go
4.047 KB
25 Jan 2024 4.43 PM
root / root
0444
util.go
0.277 KB
25 Jan 2024 4.43 PM
root / root
0444
with_transactions_test.go
23.404 KB
25 Jan 2024 4.43 PM
root / root
0444

GRAYBYTE WORDPRESS FILE MANAGER @ 2026 CONTACT ME
Static GIF