fix: add option to disable asset storage (#1878)

* fix: add no storage option

* improve error handling
This commit is contained in:
Livio Amstutz 2021-07-02 13:55:52 +02:00 committed by GitHub
parent 992b598100
commit 91f1c88d4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 8 deletions

View File

@ -5,13 +5,9 @@ import (
"io" "io"
"github.com/caos/zitadel/internal/domain" "github.com/caos/zitadel/internal/domain"
caos_errors "github.com/caos/zitadel/internal/errors"
) )
func (c *Commands) UploadAsset(ctx context.Context, bucketName, objectName, contentType string, file io.Reader, size int64) (*domain.AssetInfo, error) { func (c *Commands) UploadAsset(ctx context.Context, bucketName, objectName, contentType string, file io.Reader, size int64) (*domain.AssetInfo, error) {
if c.static == nil {
return nil, caos_errors.ThrowPreconditionFailed(nil, "STATIC-Fm92f", "Errors.Assets.Store.NotConfigured")
}
return c.static.PutObject(ctx, return c.static.PutObject(ctx,
bucketName, bucketName,
objectName, objectName,

View File

@ -129,9 +129,6 @@ func (m *LabelPolicy) OnSuccess() error {
} }
func (p *LabelPolicy) CleanUpBucket(policy *iam_model.LabelPolicyView) { func (p *LabelPolicy) CleanUpBucket(policy *iam_model.LabelPolicyView) {
if p.static == nil {
return
}
ctx := context.Background() ctx := context.Background()
objects, err := p.static.ListObjectInfos(ctx, policy.AggregateID, domain.LabelPolicyPrefix+"/", false) objects, err := p.static.ListObjectInfos(ctx, policy.AggregateID, domain.LabelPolicyPrefix+"/", false)
if err != nil { if err != nil {

View File

@ -1,8 +1,13 @@
package config package config
import ( import (
"context"
"encoding/json" "encoding/json"
"io"
"net/url"
"time"
"github.com/caos/zitadel/internal/domain"
"github.com/caos/zitadel/internal/errors" "github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/static" "github.com/caos/zitadel/internal/static"
"github.com/caos/zitadel/internal/static/s3" "github.com/caos/zitadel/internal/static/s3"
@ -14,7 +19,9 @@ type AssetStorageConfig struct {
} }
var storage = map[string]func() static.Config{ var storage = map[string]func() static.Config{
"s3": func() static.Config { return &s3.Config{} }, "s3": func() static.Config { return &s3.Config{} },
"none": func() static.Config { return &NoStorage{} },
"": func() static.Config { return &NoStorage{} },
} }
func (c *AssetStorageConfig) UnmarshalJSON(data []byte) error { func (c *AssetStorageConfig) UnmarshalJSON(data []byte) error {
@ -55,3 +62,53 @@ func newStorageConfig(storageType string, configData []byte) (static.Config, err
return staticConfig, nil return staticConfig, nil
} }
var (
errNoStorage = errors.ThrowInternal(nil, "STATIC-ashg4", "Errors.Assets.Store.NotConfigured")
)
type NoStorage struct{}
func (_ *NoStorage) NewStorage() (static.Storage, error) {
return &NoStorage{}, nil
}
func (_ *NoStorage) CreateBucket(ctx context.Context, name, location string) error {
return errNoStorage
}
func (_ *NoStorage) RemoveBucket(ctx context.Context, name string) error {
return errNoStorage
}
func (_ *NoStorage) ListBuckets(ctx context.Context) ([]*domain.BucketInfo, error) {
return nil, errNoStorage
}
func (_ *NoStorage) PutObject(ctx context.Context, bucketName, objectName, contentType string, object io.Reader, objectSize int64, createBucketIfNotExisting bool) (*domain.AssetInfo, error) {
return nil, errNoStorage
}
func (_ *NoStorage) GetObjectInfo(ctx context.Context, bucketName, objectName string) (*domain.AssetInfo, error) {
return nil, errNoStorage
}
func (_ *NoStorage) GetObject(ctx context.Context, bucketName, objectName string) (io.Reader, func() (*domain.AssetInfo, error), error) {
return nil, nil, errNoStorage
}
func (_ *NoStorage) ListObjectInfos(ctx context.Context, bucketName, prefix string, recursive bool) ([]*domain.AssetInfo, error) {
return nil, errNoStorage
}
func (_ *NoStorage) GetObjectPresignedURL(ctx context.Context, bucketName, objectName string, expiration time.Duration) (*url.URL, error) {
return nil, errNoStorage
}
func (_ *NoStorage) RemoveObject(ctx context.Context, bucketName, objectName string) error {
return errNoStorage
}
func (_ *NoStorage) RemoveObjects(ctx context.Context, bucketName, path string, recursive bool) error {
return errNoStorage
}