github.com/cs3org/reva/v2@v2.27.7/internal/grpc/services/ocmproviderauthorizer/ocmproviderauthorizer.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 ocmproviderauthorizer
    20  
    21  import (
    22  	"context"
    23  
    24  	ocmprovider "github.com/cs3org/go-cs3apis/cs3/ocm/provider/v1beta1"
    25  	"github.com/cs3org/reva/v2/pkg/errtypes"
    26  	"github.com/cs3org/reva/v2/pkg/ocm/provider"
    27  	"github.com/cs3org/reva/v2/pkg/ocm/provider/authorizer/registry"
    28  	"github.com/cs3org/reva/v2/pkg/rgrpc"
    29  	"github.com/cs3org/reva/v2/pkg/rgrpc/status"
    30  	"github.com/mitchellh/mapstructure"
    31  	"github.com/pkg/errors"
    32  	"github.com/rs/zerolog"
    33  	"google.golang.org/grpc"
    34  )
    35  
    36  func init() {
    37  	rgrpc.Register("ocmproviderauthorizer", New)
    38  }
    39  
    40  type config struct {
    41  	Driver  string                            `mapstructure:"driver"`
    42  	Drivers map[string]map[string]interface{} `mapstructure:"drivers"`
    43  }
    44  
    45  type service struct {
    46  	conf *config
    47  	pa   provider.Authorizer
    48  }
    49  
    50  func (c *config) init() {
    51  	if c.Driver == "" {
    52  		c.Driver = "json"
    53  	}
    54  }
    55  
    56  func (s *service) Register(ss *grpc.Server) {
    57  	ocmprovider.RegisterProviderAPIServer(ss, s)
    58  }
    59  
    60  func getProviderAuthorizer(c *config) (provider.Authorizer, error) {
    61  	if f, ok := registry.NewFuncs[c.Driver]; ok {
    62  		return f(c.Drivers[c.Driver])
    63  	}
    64  	return nil, errtypes.NotFound("driver not found: " + c.Driver)
    65  }
    66  
    67  func parseConfig(m map[string]interface{}) (*config, error) {
    68  	c := &config{}
    69  	if err := mapstructure.Decode(m, c); err != nil {
    70  		err = errors.Wrap(err, "error decoding conf")
    71  		return nil, err
    72  	}
    73  	return c, nil
    74  }
    75  
    76  // New creates a new OCM provider authorizer svc
    77  func New(m map[string]interface{}, ss *grpc.Server, _ *zerolog.Logger) (rgrpc.Service, error) {
    78  
    79  	c, err := parseConfig(m)
    80  	if err != nil {
    81  		return nil, err
    82  	}
    83  	c.init()
    84  
    85  	pa, err := getProviderAuthorizer(c)
    86  	if err != nil {
    87  		return nil, err
    88  	}
    89  
    90  	service := &service{
    91  		conf: c,
    92  		pa:   pa,
    93  	}
    94  	return service, nil
    95  }
    96  
    97  func (s *service) Close() error {
    98  	return nil
    99  }
   100  
   101  func (s *service) UnprotectedEndpoints() []string {
   102  	return []string{
   103  		"/cs3.ocm.provider.v1beta1.ProviderAPI/IsProviderAllowed",
   104  		"/cs3.ocm.provider.v1beta1.ProviderAPI/ListAllProviders",
   105  	}
   106  }
   107  
   108  func (s *service) GetInfoByDomain(ctx context.Context, req *ocmprovider.GetInfoByDomainRequest) (*ocmprovider.GetInfoByDomainResponse, error) {
   109  	domainInfo, err := s.pa.GetInfoByDomain(ctx, req.Domain)
   110  	if err != nil {
   111  		return &ocmprovider.GetInfoByDomainResponse{
   112  			Status: status.NewInternal(ctx, "error getting provider info: "+err.Error()),
   113  		}, nil
   114  	}
   115  
   116  	return &ocmprovider.GetInfoByDomainResponse{
   117  		Status:       status.NewOK(ctx),
   118  		ProviderInfo: domainInfo,
   119  	}, nil
   120  }
   121  
   122  func (s *service) IsProviderAllowed(ctx context.Context, req *ocmprovider.IsProviderAllowedRequest) (*ocmprovider.IsProviderAllowedResponse, error) {
   123  	err := s.pa.IsProviderAllowed(ctx, req.Provider)
   124  	if err != nil {
   125  		return &ocmprovider.IsProviderAllowedResponse{
   126  			Status: status.NewInternal(ctx, "error verifying mesh provider"),
   127  		}, nil
   128  	}
   129  
   130  	return &ocmprovider.IsProviderAllowedResponse{
   131  		Status: status.NewOK(ctx),
   132  	}, nil
   133  }
   134  
   135  func (s *service) ListAllProviders(ctx context.Context, req *ocmprovider.ListAllProvidersRequest) (*ocmprovider.ListAllProvidersResponse, error) {
   136  	providers, err := s.pa.ListAllProviders(ctx)
   137  	if err != nil {
   138  		return &ocmprovider.ListAllProvidersResponse{
   139  			Status: status.NewInternal(ctx, "error retrieving mesh providers"),
   140  		}, nil
   141  	}
   142  
   143  	return &ocmprovider.ListAllProvidersResponse{
   144  		Status:    status.NewOK(ctx),
   145  		Providers: providers,
   146  	}, nil
   147  }