-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bb4423e
commit 762205c
Showing
29 changed files
with
7,132 additions
and
1,876 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
// Copyright 2024 The Bucketeer Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package api | ||
|
||
import ( | ||
"context" | ||
|
||
"go.uber.org/zap" | ||
"google.golang.org/genproto/googleapis/rpc/errdetails" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
|
||
v2 "github.com/bucketeer-io/bucketeer/pkg/coderef/storage/v2" | ||
"github.com/bucketeer-io/bucketeer/pkg/locale" | ||
"github.com/bucketeer-io/bucketeer/pkg/log" | ||
"github.com/bucketeer-io/bucketeer/pkg/pubsub/publisher" | ||
"github.com/bucketeer-io/bucketeer/pkg/role" | ||
"github.com/bucketeer-io/bucketeer/pkg/storage/v2/mysql" | ||
accountproto "github.com/bucketeer-io/bucketeer/proto/account" | ||
proto "github.com/bucketeer-io/bucketeer/proto/coderef" | ||
eventproto "github.com/bucketeer-io/bucketeer/proto/event/domain" | ||
) | ||
|
||
type options struct { | ||
logger *zap.Logger | ||
} | ||
|
||
var defaultOptions = options{ | ||
logger: zap.NewNop(), | ||
} | ||
|
||
type Option func(*options) | ||
|
||
func WithLogger(logger *zap.Logger) Option { | ||
return func(opts *options) { | ||
opts.logger = logger | ||
} | ||
} | ||
|
||
type CodeReferenceService struct { | ||
codeRefStorage v2.CodeReferenceStorage | ||
publisher publisher.Publisher | ||
opts *options | ||
logger *zap.Logger | ||
} | ||
|
||
func NewCodeReferenceService( | ||
mysqlClient mysql.Client, | ||
publisher publisher.Publisher, | ||
opts ...Option, | ||
) *CodeReferenceService { | ||
options := defaultOptions | ||
for _, opt := range opts { | ||
opt(&options) | ||
} | ||
return &CodeReferenceService{ | ||
codeRefStorage: v2.NewCodeReferenceStorage(mysqlClient), | ||
publisher: publisher, | ||
opts: &options, | ||
logger: options.logger.Named("api"), | ||
} | ||
} | ||
|
||
func (s *CodeReferenceService) Register(server *grpc.Server) { | ||
proto.RegisterCodeReferenceServiceServer(server, s) | ||
} | ||
|
||
func (s *CodeReferenceService) checkEnvironmentRole( | ||
ctx context.Context, | ||
requiredRole accountproto.AccountV2_Role_Environment, | ||
environmentID string, | ||
localizer locale.Localizer, | ||
) (*eventproto.Editor, error) { | ||
editor, err := role.CheckEnvironmentRole( | ||
ctx, | ||
requiredRole, | ||
environmentID, | ||
nil, // TODO: implement account lookup function | ||
) | ||
if err != nil { | ||
switch status.Code(err) { | ||
case codes.Unauthenticated: | ||
s.logger.Info( | ||
"Unauthenticated", | ||
log.FieldsFromImcomingContext(ctx).AddFields( | ||
zap.Error(err), | ||
zap.String("environmentId", environmentID), | ||
)..., | ||
) | ||
dt, err := statusUnauthenticated.WithDetails(&errdetails.LocalizedMessage{ | ||
Locale: localizer.GetLocale(), | ||
Message: localizer.MustLocalize(locale.UnauthenticatedError), | ||
}) | ||
if err != nil { | ||
return nil, statusInternal.Err() | ||
} | ||
return nil, dt.Err() | ||
case codes.PermissionDenied: | ||
s.logger.Info( | ||
"Permission denied", | ||
log.FieldsFromImcomingContext(ctx).AddFields( | ||
zap.Error(err), | ||
zap.String("environmentId", environmentID), | ||
)..., | ||
) | ||
dt, err := statusPermissionDenied.WithDetails(&errdetails.LocalizedMessage{ | ||
Locale: localizer.GetLocale(), | ||
Message: localizer.MustLocalize(locale.PermissionDenied), | ||
}) | ||
if err != nil { | ||
return nil, statusInternal.Err() | ||
} | ||
return nil, dt.Err() | ||
default: | ||
s.logger.Error( | ||
"Failed to check role", | ||
log.FieldsFromImcomingContext(ctx).AddFields( | ||
zap.Error(err), | ||
zap.String("environmentId", environmentID), | ||
)..., | ||
) | ||
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{ | ||
Locale: localizer.GetLocale(), | ||
Message: localizer.MustLocalize(locale.InternalServerError), | ||
}) | ||
if err != nil { | ||
return nil, statusInternal.Err() | ||
} | ||
return nil, dt.Err() | ||
} | ||
} | ||
return editor, nil | ||
} |
Oops, something went wrong.