Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Refactor AT services #461

Merged
merged 5 commits into from
Nov 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,14 @@ help:
## Update protobuf dependencies and regenerate .pb.go from .proto,
## use this target only when it is really needed, not for usual builds
build_protobuf:
go get -u github.com/golang/protobuf/proto github.com/golang/protobuf/protoc-gen-go
go get -u google.golang.org/grpc
protoc --go_out=plugins=grpc:. cmd/acra-translator/grpc_api/*.proto
@go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.26
@go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.1
@protoc --go_out=`pwd` --go-grpc_out=`pwd` \
--go_opt=module=github.com/cossacklabs/acra \
--go-grpc_opt=module=github.com/cossacklabs/acra \
-Icmd/acra-translator/grpc_api \
cmd/acra-translator/grpc_api/*.proto
@python3 -m grpc_tools.protoc -Icmd/acra-translator/grpc_api --proto_path=. --python_out=tests/ --grpc_python_out=tests/ cmd/acra-translator/grpc_api/*.proto

## Build the application in the subdirectory (default)
build:
Expand Down
26 changes: 23 additions & 3 deletions cmd/acra-translator/acra-translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
filesystemBackendV2CE "github.com/cossacklabs/acra/keystore/v2/keystore/filesystem/backend"
"github.com/cossacklabs/acra/logging"
"github.com/cossacklabs/acra/network"
"github.com/cossacklabs/acra/poison"
"github.com/cossacklabs/acra/pseudonymization"
common2 "github.com/cossacklabs/acra/pseudonymization/common"
"github.com/cossacklabs/acra/pseudonymization/storage"
Expand Down Expand Up @@ -377,15 +378,34 @@ func realMain() error {
return err
}
config.SetTokenizer(tokenizer)
serviceFactory, err := grpc_api.NewgRPCServerFactory(tokenizer, keyStore)
poisonCallbacks := poison.NewCallbackStorage()
if config.DetectPoisonRecords() {
// used to turn off poison record detection which rely on HasCallbacks
poisonCallbacks.AddCallback(poison.EmptyCallback{})
if config.ScriptOnPoison() != "" {
poisonCallbacks.AddCallback(poison.NewExecuteScriptCallback(config.ScriptOnPoison()))
}
// should setup "stopOnPoison" as last poison record callback"
if config.StopOnPoison() {
poisonCallbacks.AddCallback(&poison.StopCallback{})
}
}
translatorData := &common.TranslatorData{
Tokenizer: tokenizer,
Config: config,
Keystorage: transportKeystore,
PoisonRecordCallbacks: poisonCallbacks,
UseConnectionClientID: config.GetUseClientIDFromConnection(),
TLSClientIDExtractor: config.GetTLSClientIDExtractor(),
}
grpcServer, err := grpc_api.NewServer(translatorData, config.GRPCConnectionWrapper)
if err != nil {
log.WithError(err).WithField(logging.FieldKeyEventCode, logging.EventCodeErrorGeneral).Errorln("Can't initialize gRPC service factory")
return err
}

var readerServer *server.ReaderServer
waitTimeout := time.Duration(*closeConnectionTimeout) * time.Second
readerServer, err = server.NewReaderServer(config, transportKeystore, serviceFactory, waitTimeout)
readerServer, err := server.NewReaderServer(translatorData, grpcServer, waitTimeout)
if err != nil {
log.WithError(err).WithField(logging.FieldKeyEventCode, logging.EventCodeErrorCantStartService).
Errorf("System error: can't start %s", ServiceName)
Expand Down
3 changes: 3 additions & 0 deletions cmd/acra-translator/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ import (
"github.com/cossacklabs/acra/decryptor/base"
"github.com/cossacklabs/acra/keystore"
"github.com/cossacklabs/acra/network"
tokenCommon "github.com/cossacklabs/acra/pseudonymization/common"
)

// TranslatorData connects KeyStorage and Poison records settings for HTTP and gRPC decryptors.
type TranslatorData struct {
Tokenizer tokenCommon.Pseudoanonymizer
Config *AcraTranslatorConfig
PoisonRecordCallbacks base.PoisonRecordCallbackStorage
Keystorage keystore.TranslationKeyStore
UseConnectionClientID bool
Expand Down
26 changes: 0 additions & 26 deletions cmd/acra-translator/common/factory.go

This file was deleted.

9 changes: 4 additions & 5 deletions cmd/acra-translator/common/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ type ITranslatorService interface {
// TranslatorService service that implements all Acra-Translator functions
type TranslatorService struct {
data *TranslatorData
tokenizer tokenCommon.Pseudoanonymizer
handler crypto.RegistryHandler
poisonDetector *crypto.EnvelopeDetector
}

// NewTranslatorService return new initialized TranslatorService
func NewTranslatorService(translatorData *TranslatorData, tokenizer tokenCommon.Pseudoanonymizer) (*TranslatorService, error) {
func NewTranslatorService(translatorData *TranslatorData) (*TranslatorService, error) {
registryHandler := crypto.NewRegistryHandler(translatorData.Keystorage)
poisonEnvelopeDetector := crypto.NewEnvelopeDetector()
if translatorData.PoisonRecordCallbacks != nil && translatorData.PoisonRecordCallbacks.HasCallbacks() {
Expand All @@ -44,7 +43,7 @@ func NewTranslatorService(translatorData *TranslatorData, tokenizer tokenCommon.
poisonDetector.SetPoisonRecordCallbacks(translatorData.PoisonRecordCallbacks)
poisonEnvelopeDetector.AddCallback(poisonDetector)
}
return &TranslatorService{data: translatorData, tokenizer: tokenizer, handler: registryHandler, poisonDetector: poisonEnvelopeDetector}, nil
return &TranslatorService{data: translatorData, handler: registryHandler, poisonDetector: poisonEnvelopeDetector}, nil
}

// Errors possible during decrypting AcraStructs.
Expand Down Expand Up @@ -257,7 +256,7 @@ func (service *TranslatorService) Tokenize(ctx context.Context, data interface{}
if len(zoneID) > 0 {
tokenContext = tokenCommon.TokenContext{ZoneID: zoneID}
}
response, err := service.tokenizer.AnonymizeConsistently(data, tokenContext, dataType)
response, err := service.data.Tokenizer.AnonymizeConsistently(data, tokenContext, dataType)
if err != nil {
logger.WithError(err).Errorln("Can't tokenize")
return nil, ErrTokenize
Expand All @@ -277,7 +276,7 @@ func (service *TranslatorService) Detokenize(ctx context.Context, data interface
}
switch dataType {
case tokenCommon.TokenType_Bytes, tokenCommon.TokenType_Email, tokenCommon.TokenType_Int32, tokenCommon.TokenType_Int64, tokenCommon.TokenType_String:
sourceData, err := service.tokenizer.Deanonymize(data, tokenContext, dataType)
sourceData, err := service.data.Tokenizer.Deanonymize(data, tokenContext, dataType)
if err != nil {
logger.WithField("type", dataType).WithError(err).Errorln("Can't tokenize data")
return nil, ErrDetokenize
Expand Down
Loading