github.com/cs3org/reva/v2@v2.27.7/pkg/user/rpc_user.go (about)

     1  // Copyright 2018-2021 CERN
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package user
    20  
    21  import (
    22  	"context"
    23  	"encoding/gob"
    24  	"net/rpc"
    25  
    26  	userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
    27  	"github.com/cs3org/reva/v2/pkg/appctx"
    28  	"github.com/cs3org/reva/v2/pkg/plugin"
    29  	hcplugin "github.com/hashicorp/go-plugin"
    30  )
    31  
    32  func init() {
    33  	gob.Register(&userpb.User{})
    34  	plugin.Register("userprovider", &ProviderPlugin{})
    35  }
    36  
    37  // ProviderPlugin is the implementation of plugin.Plugin so we can serve/consume this.
    38  type ProviderPlugin struct {
    39  	Impl Manager
    40  }
    41  
    42  // Server returns the RPC Server which serves the methods that the Client calls over net/rpc
    43  func (p *ProviderPlugin) Server(*hcplugin.MuxBroker) (interface{}, error) {
    44  	return &RPCServer{Impl: p.Impl}, nil
    45  }
    46  
    47  // Client returns interface implementation for the plugin that communicates to the server end of the plugin
    48  func (p *ProviderPlugin) Client(b *hcplugin.MuxBroker, c *rpc.Client) (interface{}, error) {
    49  	return &RPCClient{Client: c}, nil
    50  }
    51  
    52  // RPCClient is an implementation of Manager that talks over RPC.
    53  type RPCClient struct{ Client *rpc.Client }
    54  
    55  // ConfigureArg for RPC
    56  type ConfigureArg struct {
    57  	Ml map[string]interface{}
    58  }
    59  
    60  // ConfigureReply for RPC
    61  type ConfigureReply struct {
    62  	Err error
    63  }
    64  
    65  // Configure RPCClient configure method
    66  func (m *RPCClient) Configure(ml map[string]interface{}) error {
    67  	args := ConfigureArg{Ml: ml}
    68  	resp := ConfigureReply{}
    69  	err := m.Client.Call("Plugin.Configure", args, &resp)
    70  	if err != nil {
    71  		return err
    72  	}
    73  	return resp.Err
    74  }
    75  
    76  // GetUserArg for RPC
    77  type GetUserArg struct {
    78  	Ctx                map[interface{}]interface{}
    79  	UID                *userpb.UserId
    80  	SkipFetchingGroups bool
    81  }
    82  
    83  // GetUserReply for RPC
    84  type GetUserReply struct {
    85  	User *userpb.User
    86  	Err  error
    87  }
    88  
    89  // GetUser RPCClient GetUser method
    90  func (m *RPCClient) GetUser(ctx context.Context, uid *userpb.UserId, skipFetchingGroups bool) (*userpb.User, error) {
    91  	ctxVal := appctx.GetKeyValuesFromCtx(ctx)
    92  	args := GetUserArg{Ctx: ctxVal, UID: uid, SkipFetchingGroups: skipFetchingGroups}
    93  	resp := GetUserReply{}
    94  	err := m.Client.Call("Plugin.GetUser", args, &resp)
    95  	if err != nil {
    96  		return nil, err
    97  	}
    98  	return resp.User, resp.Err
    99  }
   100  
   101  // GetUserByClaimArg for RPC
   102  type GetUserByClaimArg struct {
   103  	Ctx                map[interface{}]interface{}
   104  	Claim              string
   105  	Value              string
   106  	SkipFetchingGroups bool
   107  }
   108  
   109  // GetUserByClaimReply for RPC
   110  type GetUserByClaimReply struct {
   111  	User *userpb.User
   112  	Err  error
   113  }
   114  
   115  // GetUserByClaim RPCClient GetUserByClaim method
   116  func (m *RPCClient) GetUserByClaim(ctx context.Context, claim, value string, skipFetchingGroups bool) (*userpb.User, error) {
   117  	ctxVal := appctx.GetKeyValuesFromCtx(ctx)
   118  	args := GetUserByClaimArg{Ctx: ctxVal, Claim: claim, Value: value, SkipFetchingGroups: skipFetchingGroups}
   119  	resp := GetUserByClaimReply{}
   120  	err := m.Client.Call("Plugin.GetUserByClaim", args, &resp)
   121  	if err != nil {
   122  		return nil, err
   123  	}
   124  	return resp.User, resp.Err
   125  }
   126  
   127  // GetUserGroupsArg for RPC
   128  type GetUserGroupsArg struct {
   129  	Ctx  map[interface{}]interface{}
   130  	User *userpb.UserId
   131  }
   132  
   133  // GetUserGroupsReply for RPC
   134  type GetUserGroupsReply struct {
   135  	Group []string
   136  	Err   error
   137  }
   138  
   139  // GetUserGroups RPCClient GetUserGroups method
   140  func (m *RPCClient) GetUserGroups(ctx context.Context, user *userpb.UserId) ([]string, error) {
   141  	ctxVal := appctx.GetKeyValuesFromCtx(ctx)
   142  	args := GetUserGroupsArg{Ctx: ctxVal, User: user}
   143  	resp := GetUserGroupsReply{}
   144  	err := m.Client.Call("Plugin.GetUserGroups", args, &resp)
   145  	if err != nil {
   146  		return nil, err
   147  	}
   148  	return resp.Group, resp.Err
   149  }
   150  
   151  // FindUsersArg for RPC
   152  type FindUsersArg struct {
   153  	Ctx                map[interface{}]interface{}
   154  	Query              string
   155  	SkipFetchingGroups bool
   156  }
   157  
   158  // FindUsersReply for RPC
   159  type FindUsersReply struct {
   160  	User []*userpb.User
   161  	Err  error
   162  }
   163  
   164  // FindUsers RPCClient FindUsers method
   165  func (m *RPCClient) FindUsers(ctx context.Context, query string, skipFetchingGroups bool) ([]*userpb.User, error) {
   166  	ctxVal := appctx.GetKeyValuesFromCtx(ctx)
   167  	args := FindUsersArg{Ctx: ctxVal, Query: query, SkipFetchingGroups: skipFetchingGroups}
   168  	resp := FindUsersReply{}
   169  	err := m.Client.Call("Plugin.FindUsers", args, &resp)
   170  	if err != nil {
   171  		return nil, err
   172  	}
   173  	return resp.User, resp.Err
   174  }
   175  
   176  // RPCServer is the server that RPCClient talks to, conforming to the requirements of net/rpc
   177  type RPCServer struct {
   178  	// This is the real implementation
   179  	Impl Manager
   180  }
   181  
   182  // Configure RPCServer Configure method
   183  func (m *RPCServer) Configure(args ConfigureArg, resp *ConfigureReply) error {
   184  	resp.Err = m.Impl.Configure(args.Ml)
   185  	return nil
   186  }
   187  
   188  // GetUser RPCServer GetUser method
   189  func (m *RPCServer) GetUser(args GetUserArg, resp *GetUserReply) error {
   190  	ctx := appctx.PutKeyValuesToCtx(args.Ctx)
   191  	resp.User, resp.Err = m.Impl.GetUser(ctx, args.UID, args.SkipFetchingGroups)
   192  	return nil
   193  }
   194  
   195  // GetUserByClaim RPCServer GetUserByClaim method
   196  func (m *RPCServer) GetUserByClaim(args GetUserByClaimArg, resp *GetUserByClaimReply) error {
   197  	ctx := appctx.PutKeyValuesToCtx(args.Ctx)
   198  	resp.User, resp.Err = m.Impl.GetUserByClaim(ctx, args.Claim, args.Value, args.SkipFetchingGroups)
   199  	return nil
   200  }
   201  
   202  // GetUserGroups RPCServer GetUserGroups method
   203  func (m *RPCServer) GetUserGroups(args GetUserGroupsArg, resp *GetUserGroupsReply) error {
   204  	ctx := appctx.PutKeyValuesToCtx(args.Ctx)
   205  	resp.Group, resp.Err = m.Impl.GetUserGroups(ctx, args.User)
   206  	return nil
   207  }
   208  
   209  // FindUsers RPCServer FindUsers method
   210  func (m *RPCServer) FindUsers(args FindUsersArg, resp *FindUsersReply) error {
   211  	ctx := appctx.PutKeyValuesToCtx(args.Ctx)
   212  	resp.User, resp.Err = m.Impl.FindUsers(ctx, args.Query, args.SkipFetchingGroups)
   213  	return nil
   214  }