Skip to content

Commit

Permalink
objstore: add experimental encryption wrapper
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Hoffmann <[email protected]>
  • Loading branch information
MichaHoffmann committed Feb 18, 2023
1 parent 11ffbc4 commit dde5f47
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ NOTE: As semantic versioning states all 0.y.z releases can contain breaking chan
We use *breaking :warning:* to mark changes that are not backward compatible (relates only to v0.y.z releases.)

## Unreleased
- [#46](https://github.com/thanos-io/objstore/pull/46) Objstore: Add experimental encryption wrapper

### Fixed
- [#33](https://github.com/thanos-io/objstore/pull/33) Tracing: Add `ContextWithTracer()` to inject the tracer into the context.
Expand Down
24 changes: 21 additions & 3 deletions client/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/go-kit/log/level"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/crypto/scrypt"
"gopkg.in/yaml.v2"

"github.com/thanos-io/objstore"
Expand Down Expand Up @@ -41,9 +42,14 @@ const (
)

type BucketConfig struct {
Type ObjProvider `yaml:"type"`
Config interface{} `yaml:"config"`
Prefix string `yaml:"prefix" default:""`
Type ObjProvider `yaml:"type"`
Config interface{} `yaml:"config"`
Prefix string `yaml:"prefix" default:""`
Encryption *EncryptionConfig `yaml:"encryption"`
}

type EncryptionConfig struct {
SecretKey string `yaml:"secretKey"`
}

// NewBucket initializes and returns new object storage clients.
Expand Down Expand Up @@ -87,5 +93,17 @@ func NewBucket(logger log.Logger, confContentYaml []byte, reg prometheus.Registe
return nil, errors.Wrap(err, fmt.Sprintf("create %s client", bucketConf.Type))
}

if bucketConf.Encryption != nil {
// TODO: salt?
key, err := scrypt.Key([]byte(bucketConf.Encryption.SecretKey), nil, 32768, 16, 1, 32)
if err != nil {
return nil, errors.Wrap(err, "unable to create key from secret key")
}
bucket, err = objstore.BucketWithEncryption(bucket, key)
if err != nil {
return nil, errors.Wrap(err, "unable to create encrypted bucket")
}
}

return objstore.NewTracingBucket(objstore.BucketWithMetrics(bucket.Name(), objstore.NewPrefixedBucket(bucket, bucketConf.Prefix), reg)), nil
}
9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ require (
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/minio/md5-simd v1.1.2 // indirect
github.com/minio/sha256-simd v1.0.0 // indirect
github.com/minio/sio v0.3.0 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
Expand All @@ -80,9 +81,9 @@ require (
github.com/sony/gobreaker v0.5.0 // indirect
github.com/stretchr/objx v0.2.0 // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/net v0.2.0 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/text v0.5.0 // indirect
golang.org/x/net v0.6.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/time v0.0.0-20220224211638-0e9765cccd65 // indirect
golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f // indirect
google.golang.org/appengine v1.6.7 // indirect
Expand All @@ -100,5 +101,5 @@ require (
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v0.5.1
github.com/kr/text v0.2.0 // indirect
github.com/satori/go.uuid v1.2.1-0.20181028125025-b2ce2384e17b // indirect
golang.org/x/crypto v0.3.0 // indirect
golang.org/x/crypto v0.6.0 // indirect
)
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@ github.com/minio/minio-go/v7 v7.0.45 h1:g4IeM9M9pW/Lo8AGGNOjBZYlvmtlE1N5TQEYWXRW
github.com/minio/minio-go/v7 v7.0.45/go.mod h1:nCrRzjoSUQh8hgKKtu3Y708OLvRLtuASMg2/nvmbarw=
github.com/minio/sha256-simd v1.0.0 h1:v1ta+49hkWZyvaKwrQB8elexRqm6Y0aMLjCNsrYxo6g=
github.com/minio/sha256-simd v1.0.0/go.mod h1:OuYzVNI5vcoYIAmbIvHPl3N3jUzVedXbKy5RFepssQM=
github.com/minio/sio v0.3.0 h1:syEFBewzOMOYVzSTFpp1MqpSZk8rUNbz8VIIc+PNzus=
github.com/minio/sio v0.3.0/go.mod h1:8b0yPp2avGThviy/+OCJBI6OMpvxoUuiLvE6F1lebhw=
github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs=
github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down Expand Up @@ -407,11 +409,14 @@ go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.3.0 h1:a06MkbcxBrEFc0w0QIZWXrH/9cCX6KJyWbBOIwAn+7A=
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/crypto v0.6.0 h1:qfktjS5LUO+fFKeJXZ+ikTRijMmljikvG68fpMMruSc=
golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
Expand Down Expand Up @@ -492,6 +497,8 @@ golang.org/x/net v0.0.0-20220412020605-290c469a71a5/go.mod h1:CfG3xpIq0wQ8r1q4Su
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.2.0 h1:sZfSu1wtKLGlWI4ZZayP0ck9Y73K1ynO6gqzTdBVdPU=
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
golang.org/x/net v0.6.0 h1:L4ZwwTvKW9gr0ZMS1yrHD9GZhIuVjOBBnaKH+SPQK0Q=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down Expand Up @@ -594,6 +601,8 @@ golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand All @@ -607,6 +616,8 @@ golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM=
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down
77 changes: 77 additions & 0 deletions objstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/efficientgo/core/logerrcapture"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/minio/sio"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
Expand Down Expand Up @@ -395,6 +396,82 @@ func DownloadDir(ctx context.Context, logger log.Logger, bkt BucketReader, origi
// IsOpFailureExpectedFunc allows to mark certain errors as expected, so they will not increment thanos_objstore_bucket_operation_failures_total metric.
type IsOpFailureExpectedFunc func(error) bool

// BucketWithEncryption takes a bucket and transparently encrypts and decrypts its payloads. Its 'Attributes'
// method is ill-defined and will error. It should not be used if the 'Attributes' method is important.
func BucketWithEncryption(b Bucket, key []byte) (*encryptedBucket, error) {
return &encryptedBucket{Bucket: b, cfg: sio.Config{Key: key}}, nil
}

type encryptedBucket struct {
Bucket

cfg sio.Config
}

var errNotExistsSentinel = errors.New("does not exist")

func (eb *encryptedBucket) Attributes(ctx context.Context, name string) (ObjectAttributes, error) {
attrs, err := eb.Bucket.Attributes(ctx, name)
if err != nil {
return attrs, err
}

decSize, err := sio.DecryptedSize(uint64(attrs.Size))
if err != nil {
return ObjectAttributes{}, errors.Wrap(err, "unable to determine unecrypted size")
}

// TODO: check that conversion to int64 is safe?
return ObjectAttributes{Size: int64(decSize), LastModified: attrs.LastModified}, nil
}

func (eb *encryptedBucket) Upload(ctx context.Context, name string, r io.Reader) error {
er, err := sio.EncryptReader(r, eb.cfg)
if err != nil {
return errors.Wrap(err, "unable to create encryption stream")
}
return eb.Bucket.Upload(ctx, name, er)
}

func (eb *encryptedBucket) Get(ctx context.Context, name string) (io.ReadCloser, error) {
return eb.GetRange(ctx, name, 0, -1)
}

func (eb *encryptedBucket) IsObjNotFoundErr(err error) bool {
return errors.Cause(err) == errNotExistsSentinel || eb.Bucket.IsObjNotFoundErr(err)
}

func (eb *encryptedBucket) GetRange(ctx context.Context, name string, off, length int64) (io.ReadCloser, error) {
if exists, err := eb.Bucket.Exists(ctx, name); err != nil {
return nil, err
} else if !exists {
return nil, errNotExistsSentinel
}

br := &bucketReaderAt{ctx: ctx, name: name, b: eb.Bucket}
dr, err := sio.DecryptReaderAt(br, eb.cfg)
if err != nil {
return nil, errors.Wrap(err, "unable to create decryption stream")
}
return io.NopCloser(io.NewSectionReader(dr, off, length)), nil
}

type bucketReaderAt struct {
ctx context.Context
name string
b BucketReader
}

func (br *bucketReaderAt) ReadAt(p []byte, off int64) (n int, err error) {
rc, err := br.b.GetRange(br.ctx, br.name, off, int64(len(p)))
if err != nil {
return 0, err
}
defer rc.Close()

return rc.Read(p)
}

var _ InstrumentedBucket = &metricBucket{}

// BucketWithMetrics takes a bucket and registers metrics with the given registry for
Expand Down
34 changes: 34 additions & 0 deletions objstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package objstore
import (
"bytes"
"context"
"encoding/hex"
"io"
"os"
"strings"
Expand Down Expand Up @@ -204,3 +205,36 @@ func (b unreliableBucket) Get(ctx context.Context, name string) (io.ReadCloser,
}
return b.Bucket.Get(ctx, name)
}

func TestEncryptedBucket(t *testing.T) {
key, _ := hex.DecodeString("6368616e6765207468697320706173736368616e676520746869732070617373")

eb, err := BucketWithEncryption(NewInMemBucket(), key)
testutil.Ok(t, err)

testutil.Ok(t, eb.Upload(context.Background(), "dir/obj1", bytes.NewReader([]byte("foo bar baz"))))

r, err := eb.Get(context.Background(), "dir/obj1")
testutil.Ok(t, err)

content, err := io.ReadAll(r)
testutil.Ok(t, err)
testutil.Equals(t, string(content), "foo bar baz")

r, err = eb.GetRange(context.Background(), "dir/obj1", 4, 3)
testutil.Ok(t, err)

content, err = io.ReadAll(r)
testutil.Ok(t, err)
testutil.Equals(t, string(content), "bar")

r, err = eb.GetRange(context.Background(), "dir/obj1", 8, 3)
testutil.Ok(t, err)

content, err = io.ReadAll(r)
testutil.Ok(t, err)
testutil.Equals(t, string(content), "baz")

_, err = eb.GetRange(context.Background(), "dir/nonexistent", 0, -1)
testutil.Equals(t, eb.IsObjNotFoundErr(err), true)
}

0 comments on commit dde5f47

Please sign in to comment.