go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/auth_service/impl/servers/allowlists/server.go (about)

     1  // Copyright 2021 The LUCI Authors.
     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  // Package allowlists contains Allowlists server implementation.
    16  package allowlists
    17  
    18  import (
    19  	"context"
    20  	"errors"
    21  
    22  	"google.golang.org/grpc/codes"
    23  	"google.golang.org/grpc/status"
    24  	"google.golang.org/protobuf/types/known/emptypb"
    25  
    26  	"go.chromium.org/luci/gae/service/datastore"
    27  
    28  	"go.chromium.org/luci/auth_service/api/rpcpb"
    29  	"go.chromium.org/luci/auth_service/impl/model"
    30  )
    31  
    32  // Server implements Allowlists server.
    33  type Server struct {
    34  	rpcpb.UnimplementedAllowlistsServer
    35  }
    36  
    37  // ListAllowlists implements the corresponding RPC method.
    38  func (*Server) ListAllowlists(ctx context.Context, _ *emptypb.Empty) (*rpcpb.ListAllowlistsResponse, error) {
    39  	// Get allowlists from datastore.
    40  	allowlists, err := model.GetAllAuthIPAllowlists(ctx)
    41  	if err != nil {
    42  		return nil, status.Errorf(codes.Internal, "failed to fetch allowlists: %s", err)
    43  	}
    44  
    45  	allowlistList := make([]*rpcpb.Allowlist, len(allowlists))
    46  	for idx, entity := range allowlists {
    47  		allowlistList[idx] = entity.ToProto()
    48  	}
    49  
    50  	return &rpcpb.ListAllowlistsResponse{
    51  		Allowlists: allowlistList,
    52  	}, nil
    53  }
    54  
    55  // GetAllowlist implements the corresponding RPC method.
    56  func (*Server) GetAllowlist(ctx context.Context, request *rpcpb.GetAllowlistRequest) (*rpcpb.Allowlist, error) {
    57  	switch allowlist, err := model.GetAuthIPAllowlist(ctx, request.Name); {
    58  	case err == nil:
    59  		return allowlist.ToProto(), nil
    60  	case errors.Is(err, datastore.ErrNoSuchEntity):
    61  		return nil, status.Errorf(codes.NotFound, "no such allowlist %q", request.Name)
    62  	default:
    63  		return nil, status.Errorf(codes.Internal, "failed to fetch allowlist %q: %s", request.Name, err)
    64  	}
    65  }