go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers-sdk/v1/vault/vault.ranger.go (about)

     1  // Code generated by protoc-gen-rangerrpc version DO NOT EDIT.
     2  // source: vault.proto
     3  
     4  package vault
     5  
     6  import (
     7  	"context"
     8  	"errors"
     9  	"net/http"
    10  	"net/url"
    11  	"strings"
    12  
    13  	ranger "go.mondoo.com/ranger-rpc"
    14  	"go.mondoo.com/ranger-rpc/metadata"
    15  	jsonpb "google.golang.org/protobuf/encoding/protojson"
    16  	pb "google.golang.org/protobuf/proto"
    17  )
    18  
    19  // service interface definition
    20  
    21  type Vault interface {
    22  	About(context.Context, *Empty) (*VaultInfo, error)
    23  	Get(context.Context, *SecretID) (*Secret, error)
    24  	Set(context.Context, *Secret) (*SecretID, error)
    25  }
    26  
    27  // client implementation
    28  
    29  type VaultClient struct {
    30  	ranger.Client
    31  	httpclient ranger.HTTPClient
    32  	prefix     string
    33  }
    34  
    35  func NewVaultClient(addr string, client ranger.HTTPClient, plugins ...ranger.ClientPlugin) (*VaultClient, error) {
    36  	base, err := url.Parse(ranger.SanitizeUrl(addr))
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  
    41  	u, err := url.Parse("./Vault")
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  
    46  	serviceClient := &VaultClient{
    47  		httpclient: client,
    48  		prefix:     base.ResolveReference(u).String(),
    49  	}
    50  	serviceClient.AddPlugins(plugins...)
    51  	return serviceClient, nil
    52  }
    53  func (c *VaultClient) About(ctx context.Context, in *Empty) (*VaultInfo, error) {
    54  	out := new(VaultInfo)
    55  	err := c.DoClientRequest(ctx, c.httpclient, strings.Join([]string{c.prefix, "/About"}, ""), in, out)
    56  	return out, err
    57  }
    58  func (c *VaultClient) Get(ctx context.Context, in *SecretID) (*Secret, error) {
    59  	out := new(Secret)
    60  	err := c.DoClientRequest(ctx, c.httpclient, strings.Join([]string{c.prefix, "/Get"}, ""), in, out)
    61  	return out, err
    62  }
    63  func (c *VaultClient) Set(ctx context.Context, in *Secret) (*SecretID, error) {
    64  	out := new(SecretID)
    65  	err := c.DoClientRequest(ctx, c.httpclient, strings.Join([]string{c.prefix, "/Set"}, ""), in, out)
    66  	return out, err
    67  }
    68  
    69  // server implementation
    70  
    71  type VaultServerOption func(s *VaultServer)
    72  
    73  func WithUnknownFieldsForVaultServer() VaultServerOption {
    74  	return func(s *VaultServer) {
    75  		s.allowUnknownFields = true
    76  	}
    77  }
    78  
    79  func NewVaultServer(handler Vault, opts ...VaultServerOption) http.Handler {
    80  	srv := &VaultServer{
    81  		handler: handler,
    82  	}
    83  
    84  	for i := range opts {
    85  		opts[i](srv)
    86  	}
    87  
    88  	service := ranger.Service{
    89  		Name: "Vault",
    90  		Methods: map[string]ranger.Method{
    91  			"About": srv.About,
    92  			"Get":   srv.Get,
    93  			"Set":   srv.Set,
    94  		},
    95  	}
    96  	return ranger.NewRPCServer(&service)
    97  }
    98  
    99  type VaultServer struct {
   100  	handler            Vault
   101  	allowUnknownFields bool
   102  }
   103  
   104  func (p *VaultServer) About(ctx context.Context, reqBytes *[]byte) (pb.Message, error) {
   105  	var req Empty
   106  	var err error
   107  
   108  	md, ok := metadata.FromIncomingContext(ctx)
   109  	if !ok {
   110  		return nil, errors.New("could not access header")
   111  	}
   112  
   113  	switch md.First("Content-Type") {
   114  	case "application/protobuf", "application/octet-stream", "application/grpc+proto":
   115  		err = pb.Unmarshal(*reqBytes, &req)
   116  	default:
   117  		// handle case of empty object
   118  		if len(*reqBytes) > 0 {
   119  			err = jsonpb.UnmarshalOptions{DiscardUnknown: true}.Unmarshal(*reqBytes, &req)
   120  		}
   121  	}
   122  
   123  	if err != nil {
   124  		return nil, err
   125  	}
   126  	return p.handler.About(ctx, &req)
   127  }
   128  func (p *VaultServer) Get(ctx context.Context, reqBytes *[]byte) (pb.Message, error) {
   129  	var req SecretID
   130  	var err error
   131  
   132  	md, ok := metadata.FromIncomingContext(ctx)
   133  	if !ok {
   134  		return nil, errors.New("could not access header")
   135  	}
   136  
   137  	switch md.First("Content-Type") {
   138  	case "application/protobuf", "application/octet-stream", "application/grpc+proto":
   139  		err = pb.Unmarshal(*reqBytes, &req)
   140  	default:
   141  		// handle case of empty object
   142  		if len(*reqBytes) > 0 {
   143  			err = jsonpb.UnmarshalOptions{DiscardUnknown: true}.Unmarshal(*reqBytes, &req)
   144  		}
   145  	}
   146  
   147  	if err != nil {
   148  		return nil, err
   149  	}
   150  	return p.handler.Get(ctx, &req)
   151  }
   152  func (p *VaultServer) Set(ctx context.Context, reqBytes *[]byte) (pb.Message, error) {
   153  	var req Secret
   154  	var err error
   155  
   156  	md, ok := metadata.FromIncomingContext(ctx)
   157  	if !ok {
   158  		return nil, errors.New("could not access header")
   159  	}
   160  
   161  	switch md.First("Content-Type") {
   162  	case "application/protobuf", "application/octet-stream", "application/grpc+proto":
   163  		err = pb.Unmarshal(*reqBytes, &req)
   164  	default:
   165  		// handle case of empty object
   166  		if len(*reqBytes) > 0 {
   167  			err = jsonpb.UnmarshalOptions{DiscardUnknown: true}.Unmarshal(*reqBytes, &req)
   168  		}
   169  	}
   170  
   171  	if err != nil {
   172  		return nil, err
   173  	}
   174  	return p.handler.Set(ctx, &req)
   175  }