...

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

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

     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  //go:build cse
     8  // +build cse
     9  
    10  package mongocrypt
    11  
    12  // #include <mongocrypt.h>
    13  import "C"
    14  import (
    15  	"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
    16  )
    17  
    18  // Context represents a mongocrypt_ctx_t handle
    19  type Context struct {
    20  	wrapped *C.mongocrypt_ctx_t
    21  }
    22  
    23  // newContext creates a Context wrapper around the given C type.
    24  func newContext(wrapped *C.mongocrypt_ctx_t) *Context {
    25  	return &Context{
    26  		wrapped: wrapped,
    27  	}
    28  }
    29  
    30  // State returns the current State of the Context.
    31  func (c *Context) State() State {
    32  	return State(int(C.mongocrypt_ctx_state(c.wrapped)))
    33  }
    34  
    35  // NextOperation gets the document for the next database operation to run.
    36  func (c *Context) NextOperation() (bsoncore.Document, error) {
    37  	opDocBinary := newBinary() // out param for mongocrypt_ctx_mongo_op to fill in operation
    38  	defer opDocBinary.close()
    39  
    40  	if ok := C.mongocrypt_ctx_mongo_op(c.wrapped, opDocBinary.wrapped); !ok {
    41  		return nil, c.createErrorFromStatus()
    42  	}
    43  	return opDocBinary.toBytes(), nil
    44  }
    45  
    46  // AddOperationResult feeds the result of a database operation to mongocrypt.
    47  func (c *Context) AddOperationResult(result bsoncore.Document) error {
    48  	resultBinary := newBinaryFromBytes(result)
    49  	defer resultBinary.close()
    50  
    51  	if ok := C.mongocrypt_ctx_mongo_feed(c.wrapped, resultBinary.wrapped); !ok {
    52  		return c.createErrorFromStatus()
    53  	}
    54  	return nil
    55  }
    56  
    57  // CompleteOperation signals a database operation has been completed.
    58  func (c *Context) CompleteOperation() error {
    59  	if ok := C.mongocrypt_ctx_mongo_done(c.wrapped); !ok {
    60  		return c.createErrorFromStatus()
    61  	}
    62  	return nil
    63  }
    64  
    65  // NextKmsContext returns the next KmsContext, or nil if there are no more.
    66  func (c *Context) NextKmsContext() *KmsContext {
    67  	ctx := C.mongocrypt_ctx_next_kms_ctx(c.wrapped)
    68  	if ctx == nil {
    69  		return nil
    70  	}
    71  	return newKmsContext(ctx)
    72  }
    73  
    74  // FinishKmsContexts signals that all KMS contexts have been completed.
    75  func (c *Context) FinishKmsContexts() error {
    76  	if ok := C.mongocrypt_ctx_kms_done(c.wrapped); !ok {
    77  		return c.createErrorFromStatus()
    78  	}
    79  	return nil
    80  }
    81  
    82  // Finish performs the final operations for the context and returns the resulting document.
    83  func (c *Context) Finish() (bsoncore.Document, error) {
    84  	docBinary := newBinary() // out param for mongocrypt_ctx_finalize to fill in resulting document
    85  	defer docBinary.close()
    86  
    87  	if ok := C.mongocrypt_ctx_finalize(c.wrapped, docBinary.wrapped); !ok {
    88  		return nil, c.createErrorFromStatus()
    89  	}
    90  	return docBinary.toBytes(), nil
    91  }
    92  
    93  // Close cleans up any resources associated with the given Context instance.
    94  func (c *Context) Close() {
    95  	C.mongocrypt_ctx_destroy(c.wrapped)
    96  }
    97  
    98  // createErrorFromStatus creates a new Error based on the status of the MongoCrypt instance.
    99  func (c *Context) createErrorFromStatus() error {
   100  	status := C.mongocrypt_status_new()
   101  	defer C.mongocrypt_status_destroy(status)
   102  	C.mongocrypt_ctx_status(c.wrapped, status)
   103  	return errorFromStatus(status)
   104  }
   105  
   106  // ProvideKmsProviders provides the KMS providers when in the NeedKmsCredentials state.
   107  func (c *Context) ProvideKmsProviders(kmsProviders bsoncore.Document) error {
   108  	kmsProvidersBinary := newBinaryFromBytes(kmsProviders)
   109  	defer kmsProvidersBinary.close()
   110  
   111  	if ok := C.mongocrypt_ctx_provide_kms_providers(c.wrapped, kmsProvidersBinary.wrapped); !ok {
   112  		return c.createErrorFromStatus()
   113  	}
   114  	return nil
   115  }
   116  

View as plain text