github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/vendor_skip/go.mongodb.org/mongo-driver/mongo/crypt_retrievers.go (about)

     1  // Copyright (C) MongoDB, Inc. 2017-present.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"); you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
     6  
     7  package mongo
     8  
     9  import (
    10  	"context"
    11  
    12  	"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
    13  )
    14  
    15  // keyRetriever gets keys from the key vault collection.
    16  type keyRetriever struct {
    17  	coll *Collection
    18  }
    19  
    20  func (kr *keyRetriever) cryptKeys(ctx context.Context, filter bsoncore.Document) ([]bsoncore.Document, error) {
    21  	// Remove the explicit session from the context if one is set.
    22  	// The explicit session may be from a different client.
    23  	ctx = NewSessionContext(ctx, nil)
    24  	cursor, err := kr.coll.Find(ctx, filter)
    25  	if err != nil {
    26  		return nil, EncryptionKeyVaultError{Wrapped: err}
    27  	}
    28  	defer cursor.Close(ctx)
    29  
    30  	var results []bsoncore.Document
    31  	for cursor.Next(ctx) {
    32  		cur := make([]byte, len(cursor.Current))
    33  		copy(cur, cursor.Current)
    34  		results = append(results, cur)
    35  	}
    36  	if err = cursor.Err(); err != nil {
    37  		return nil, EncryptionKeyVaultError{Wrapped: err}
    38  	}
    39  
    40  	return results, nil
    41  }
    42  
    43  // collInfoRetriever gets info for collections from a database.
    44  type collInfoRetriever struct {
    45  	client *Client
    46  }
    47  
    48  func (cir *collInfoRetriever) cryptCollInfo(ctx context.Context, db string, filter bsoncore.Document) (bsoncore.Document, error) {
    49  	// Remove the explicit session from the context if one is set.
    50  	// The explicit session may be from a different client.
    51  	ctx = NewSessionContext(ctx, nil)
    52  	cursor, err := cir.client.Database(db).ListCollections(ctx, filter)
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  	defer cursor.Close(ctx)
    57  
    58  	if !cursor.Next(ctx) {
    59  		return nil, cursor.Err()
    60  	}
    61  
    62  	res := make([]byte, len(cursor.Current))
    63  	copy(res, cursor.Current)
    64  	return res, nil
    65  }