...

Source file src/go.mongodb.org/mongo-driver/x/mongo/driver/mongocrypt/options/mongocrypt_context_options.go

Documentation: go.mongodb.org/mongo-driver/x/mongo/driver/mongocrypt/options

     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 options
     8  
     9  import (
    10  	"go.mongodb.org/mongo-driver/bson/primitive"
    11  	"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
    12  )
    13  
    14  // DataKeyOptions specifies options for creating a new data key.
    15  type DataKeyOptions struct {
    16  	KeyAltNames []string
    17  	KeyMaterial []byte
    18  	MasterKey   bsoncore.Document
    19  }
    20  
    21  // DataKey creates a new DataKeyOptions instance.
    22  func DataKey() *DataKeyOptions {
    23  	return &DataKeyOptions{}
    24  }
    25  
    26  // SetKeyAltNames specifies alternate key names.
    27  func (dko *DataKeyOptions) SetKeyAltNames(names []string) *DataKeyOptions {
    28  	dko.KeyAltNames = names
    29  	return dko
    30  }
    31  
    32  // SetMasterKey specifies the master key.
    33  func (dko *DataKeyOptions) SetMasterKey(key bsoncore.Document) *DataKeyOptions {
    34  	dko.MasterKey = key
    35  	return dko
    36  }
    37  
    38  // SetKeyMaterial specifies the key material.
    39  func (dko *DataKeyOptions) SetKeyMaterial(keyMaterial []byte) *DataKeyOptions {
    40  	dko.KeyMaterial = keyMaterial
    41  	return dko
    42  }
    43  
    44  // QueryType describes the type of query the result of Encrypt is used for.
    45  type QueryType int
    46  
    47  // These constants specify valid values for QueryType
    48  const (
    49  	QueryTypeEquality QueryType = 1
    50  )
    51  
    52  // ExplicitEncryptionOptions specifies options for configuring an explicit encryption context.
    53  type ExplicitEncryptionOptions struct {
    54  	KeyID            *primitive.Binary
    55  	KeyAltName       *string
    56  	Algorithm        string
    57  	QueryType        string
    58  	ContentionFactor *int64
    59  	RangeOptions     *ExplicitRangeOptions
    60  }
    61  
    62  // ExplicitRangeOptions specifies options for the range index.
    63  type ExplicitRangeOptions struct {
    64  	Min       *bsoncore.Value
    65  	Max       *bsoncore.Value
    66  	Sparsity  int64
    67  	Precision *int32
    68  }
    69  
    70  // ExplicitEncryption creates a new ExplicitEncryptionOptions instance.
    71  func ExplicitEncryption() *ExplicitEncryptionOptions {
    72  	return &ExplicitEncryptionOptions{}
    73  }
    74  
    75  // SetKeyID sets the key identifier.
    76  func (eeo *ExplicitEncryptionOptions) SetKeyID(keyID primitive.Binary) *ExplicitEncryptionOptions {
    77  	eeo.KeyID = &keyID
    78  	return eeo
    79  }
    80  
    81  // SetKeyAltName sets the key alternative name.
    82  func (eeo *ExplicitEncryptionOptions) SetKeyAltName(keyAltName string) *ExplicitEncryptionOptions {
    83  	eeo.KeyAltName = &keyAltName
    84  	return eeo
    85  }
    86  
    87  // SetAlgorithm specifies an encryption algorithm.
    88  func (eeo *ExplicitEncryptionOptions) SetAlgorithm(algorithm string) *ExplicitEncryptionOptions {
    89  	eeo.Algorithm = algorithm
    90  	return eeo
    91  }
    92  
    93  // SetQueryType specifies the query type.
    94  func (eeo *ExplicitEncryptionOptions) SetQueryType(queryType string) *ExplicitEncryptionOptions {
    95  	eeo.QueryType = queryType
    96  	return eeo
    97  }
    98  
    99  // SetContentionFactor specifies the contention factor.
   100  func (eeo *ExplicitEncryptionOptions) SetContentionFactor(contentionFactor int64) *ExplicitEncryptionOptions {
   101  	eeo.ContentionFactor = &contentionFactor
   102  	return eeo
   103  }
   104  
   105  // SetRangeOptions specifies the range options.
   106  func (eeo *ExplicitEncryptionOptions) SetRangeOptions(ro ExplicitRangeOptions) *ExplicitEncryptionOptions {
   107  	eeo.RangeOptions = &ro
   108  	return eeo
   109  }
   110  
   111  // RewrapManyDataKeyOptions represents all possible options used to decrypt and encrypt all matching data keys with a
   112  // possibly new masterKey.
   113  type RewrapManyDataKeyOptions struct {
   114  	// Provider identifies the new KMS provider. If omitted, encrypting uses the current KMS provider.
   115  	Provider *string
   116  
   117  	// MasterKey identifies the new masterKey. If omitted, rewraps with the current masterKey.
   118  	MasterKey bsoncore.Document
   119  }
   120  
   121  // RewrapManyDataKey creates a new RewrapManyDataKeyOptions instance.
   122  func RewrapManyDataKey() *RewrapManyDataKeyOptions {
   123  	return new(RewrapManyDataKeyOptions)
   124  }
   125  
   126  // SetProvider sets the value for the Provider field.
   127  func (rmdko *RewrapManyDataKeyOptions) SetProvider(provider string) *RewrapManyDataKeyOptions {
   128  	rmdko.Provider = &provider
   129  	return rmdko
   130  }
   131  
   132  // SetMasterKey sets the value for the MasterKey field.
   133  func (rmdko *RewrapManyDataKeyOptions) SetMasterKey(masterKey bsoncore.Document) *RewrapManyDataKeyOptions {
   134  	rmdko.MasterKey = masterKey
   135  	return rmdko
   136  }
   137  
   138  // MergeRewrapManyDataKeyOptions combines the given RewrapManyDataKeyOptions instances into a single
   139  // RewrapManyDataKeyOptions in a last one wins fashion.
   140  //
   141  // Deprecated: Merging options structs will not be supported in Go Driver 2.0. Users should create a
   142  // single options struct instead.
   143  func MergeRewrapManyDataKeyOptions(opts ...*RewrapManyDataKeyOptions) *RewrapManyDataKeyOptions {
   144  	rmdkOpts := RewrapManyDataKey()
   145  	for _, rmdko := range opts {
   146  		if rmdko == nil {
   147  			continue
   148  		}
   149  		if provider := rmdko.Provider; provider != nil {
   150  			rmdkOpts.Provider = provider
   151  		}
   152  		if masterKey := rmdko.MasterKey; masterKey != nil {
   153  			rmdkOpts.MasterKey = masterKey
   154  		}
   155  	}
   156  	return rmdkOpts
   157  }
   158  

View as plain text