|
|
|
@ -7,7 +7,10 @@ import ( |
|
|
|
|
"errors" |
|
|
|
|
"fmt" |
|
|
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/attribute" |
|
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log" |
|
|
|
|
"github.com/grafana/grafana/pkg/infra/tracing" |
|
|
|
|
"github.com/grafana/grafana/pkg/infra/usagestats" |
|
|
|
|
"github.com/grafana/grafana/pkg/services/encryption" |
|
|
|
|
"github.com/grafana/grafana/pkg/setting" |
|
|
|
@ -24,7 +27,8 @@ const ( |
|
|
|
|
// Service must not be used for encryption.
|
|
|
|
|
// Use secrets.Service implementing envelope encryption instead.
|
|
|
|
|
type Service struct { |
|
|
|
|
log log.Logger |
|
|
|
|
tracer tracing.Tracer |
|
|
|
|
log log.Logger |
|
|
|
|
|
|
|
|
|
cfg *setting.Cfg |
|
|
|
|
usageMetrics usagestats.Service |
|
|
|
@ -34,12 +38,14 @@ type Service struct { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func ProvideEncryptionService( |
|
|
|
|
tracer tracing.Tracer, |
|
|
|
|
provider encryption.Provider, |
|
|
|
|
usageMetrics usagestats.Service, |
|
|
|
|
cfg *setting.Cfg, |
|
|
|
|
) (*Service, error) { |
|
|
|
|
s := &Service{ |
|
|
|
|
log: log.New("encryption"), |
|
|
|
|
tracer: tracer, |
|
|
|
|
log: log.New("encryption"), |
|
|
|
|
|
|
|
|
|
ciphers: provider.ProvideCiphers(), |
|
|
|
|
deciphers: provider.ProvideDeciphers(), |
|
|
|
@ -93,6 +99,9 @@ func (s *Service) registerUsageMetrics() { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (s *Service) Decrypt(ctx context.Context, payload []byte, secret string) ([]byte, error) { |
|
|
|
|
ctx, span := s.tracer.Start(ctx, "encryption.service.Decrypt") |
|
|
|
|
defer span.End() |
|
|
|
|
|
|
|
|
|
var err error |
|
|
|
|
defer func() { |
|
|
|
|
if err != nil { |
|
|
|
@ -115,6 +124,8 @@ func (s *Service) Decrypt(ctx context.Context, payload []byte, secret string) ([ |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
span.SetAttributes(attribute.String("encryption.algorithm", algorithm)) |
|
|
|
|
|
|
|
|
|
var decrypted []byte |
|
|
|
|
decrypted, err = decipher.Decrypt(ctx, toDecrypt, secret) |
|
|
|
|
|
|
|
|
@ -163,6 +174,9 @@ func (s *Service) deriveEncryptionAlgorithm(payload []byte) (string, []byte, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (s *Service) Encrypt(ctx context.Context, payload []byte, secret string) ([]byte, error) { |
|
|
|
|
ctx, span := s.tracer.Start(ctx, "encryption.service.Encrypt") |
|
|
|
|
defer span.End() |
|
|
|
|
|
|
|
|
|
var err error |
|
|
|
|
defer func() { |
|
|
|
|
if err != nil { |
|
|
|
@ -179,6 +193,8 @@ func (s *Service) Encrypt(ctx context.Context, payload []byte, secret string) ([ |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
span.SetAttributes(attribute.String("encryption.algorithm", algorithm)) |
|
|
|
|
|
|
|
|
|
var encrypted []byte |
|
|
|
|
encrypted, err = cipher.Encrypt(ctx, payload, secret) |
|
|
|
|
|
|
|
|
|