mirror of
				https://github.com/optim-enterprises-bv/vault.git
				synced 2025-11-03 20:17:59 +00:00 
			
		
		
		
	* Simplify Run(): the function that was being sent over a channel doesn't need to close over anything except latestToken, and we don't need to create a new one each iteration. Instead just pass the relevant items, namely the token and sink to work on. * Disallow the following config combinations: 1. auto_auth.method.wrap_ttl > 0 and multiple file sinks 2. auto_auth.method.wrap_ttl > 0 and single file sink with wrap_ttl > 0 3. auto_auth.method.wrap_ttl > 0 and cache.use_auto_auth_token = true * Expose errors that occur when APIProxy is forwarding request to Vault. * Fix merge issues.
		
			
				
	
	
		
			62 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			62 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package cache
 | 
						|
 | 
						|
import (
 | 
						|
	"context"
 | 
						|
	"fmt"
 | 
						|
 | 
						|
	hclog "github.com/hashicorp/go-hclog"
 | 
						|
	"github.com/hashicorp/vault/api"
 | 
						|
)
 | 
						|
 | 
						|
// APIProxy is an implementation of the proxier interface that is used to
 | 
						|
// forward the request to Vault and get the response.
 | 
						|
type APIProxy struct {
 | 
						|
	client *api.Client
 | 
						|
	logger hclog.Logger
 | 
						|
}
 | 
						|
 | 
						|
type APIProxyConfig struct {
 | 
						|
	Client *api.Client
 | 
						|
	Logger hclog.Logger
 | 
						|
}
 | 
						|
 | 
						|
func NewAPIProxy(config *APIProxyConfig) (Proxier, error) {
 | 
						|
	if config.Client == nil {
 | 
						|
		return nil, fmt.Errorf("nil API client")
 | 
						|
	}
 | 
						|
	return &APIProxy{
 | 
						|
		client: config.Client,
 | 
						|
		logger: config.Logger,
 | 
						|
	}, nil
 | 
						|
}
 | 
						|
 | 
						|
func (ap *APIProxy) Send(ctx context.Context, req *SendRequest) (*SendResponse, error) {
 | 
						|
	client, err := ap.client.Clone()
 | 
						|
	if err != nil {
 | 
						|
		return nil, err
 | 
						|
	}
 | 
						|
	client.SetToken(req.Token)
 | 
						|
	client.SetHeaders(req.Request.Header)
 | 
						|
 | 
						|
	fwReq := client.NewRequest(req.Request.Method, req.Request.URL.Path)
 | 
						|
	fwReq.BodyBytes = req.RequestBody
 | 
						|
 | 
						|
	// Make the request to Vault and get the response
 | 
						|
	ap.logger.Info("forwarding request", "path", req.Request.URL.Path, "method", req.Request.Method)
 | 
						|
 | 
						|
	resp, err := client.RawRequestWithContext(ctx, fwReq)
 | 
						|
	if resp == nil && err != nil {
 | 
						|
		return nil, err
 | 
						|
	}
 | 
						|
 | 
						|
	// Before error checking from the request call, we'd want to initialize a SendResponse to
 | 
						|
	// potentially return
 | 
						|
	sendResponse, newErr := NewSendResponse(resp, nil)
 | 
						|
	if newErr != nil {
 | 
						|
		return nil, newErr
 | 
						|
	}
 | 
						|
 | 
						|
	// Bubble back the api.Response as well for error checking/handling at the handler layer.
 | 
						|
	return sendResponse, err
 | 
						|
}
 |