cli: Add option to whitelist hooks (#302)
This commit is contained in:
parent
9433afd70c
commit
f1fe5e2603
|
@ -110,6 +110,8 @@ Usage of tusd:
|
||||||
Use Google Cloud Storage with this bucket as storage backend (requires the GCS_SERVICE_ACCOUNT_FILE environment variable to be set)
|
Use Google Cloud Storage with this bucket as storage backend (requires the GCS_SERVICE_ACCOUNT_FILE environment variable to be set)
|
||||||
-hooks-dir string
|
-hooks-dir string
|
||||||
Directory to search for available hooks scripts
|
Directory to search for available hooks scripts
|
||||||
|
-hooks-enabled-events string
|
||||||
|
Comma separated list of enabled hook events (e.g. post-create,post-finish). Leave empty to enable all events
|
||||||
-hooks-http string
|
-hooks-http string
|
||||||
An HTTP endpoint to which hook events will be sent to
|
An HTTP endpoint to which hook events will be sent to
|
||||||
-hooks-http-backoff int
|
-hooks-http-backoff int
|
||||||
|
|
|
@ -4,6 +4,8 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/tus/tusd/cmd/tusd/cli/hooks"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Flags struct {
|
var Flags struct {
|
||||||
|
@ -20,12 +22,14 @@ var Flags struct {
|
||||||
S3Endpoint string
|
S3Endpoint string
|
||||||
GCSBucket string
|
GCSBucket string
|
||||||
GCSObjectPrefix string
|
GCSObjectPrefix string
|
||||||
|
EnabledHooksString string
|
||||||
FileHooksDir string
|
FileHooksDir string
|
||||||
HttpHooksEndpoint string
|
HttpHooksEndpoint string
|
||||||
HttpHooksRetry int
|
HttpHooksRetry int
|
||||||
HttpHooksBackoff int
|
HttpHooksBackoff int
|
||||||
HooksStopUploadCode int
|
HooksStopUploadCode int
|
||||||
PluginHookPath string
|
PluginHookPath string
|
||||||
|
EnabledHooks []hooks.HookType
|
||||||
ShowVersion bool
|
ShowVersion bool
|
||||||
ExposeMetrics bool
|
ExposeMetrics bool
|
||||||
MetricsPath string
|
MetricsPath string
|
||||||
|
@ -50,6 +54,7 @@ func ParseFlags() {
|
||||||
flag.StringVar(&Flags.S3Endpoint, "s3-endpoint", "", "Endpoint to use S3 compatible implementations like minio (requires s3-bucket to be pass)")
|
flag.StringVar(&Flags.S3Endpoint, "s3-endpoint", "", "Endpoint to use S3 compatible implementations like minio (requires s3-bucket to be pass)")
|
||||||
flag.StringVar(&Flags.GCSBucket, "gcs-bucket", "", "Use Google Cloud Storage with this bucket as storage backend (requires the GCS_SERVICE_ACCOUNT_FILE environment variable to be set)")
|
flag.StringVar(&Flags.GCSBucket, "gcs-bucket", "", "Use Google Cloud Storage with this bucket as storage backend (requires the GCS_SERVICE_ACCOUNT_FILE environment variable to be set)")
|
||||||
flag.StringVar(&Flags.GCSObjectPrefix, "gcs-object-prefix", "", "Prefix for GCS object names (can't contain underscore character)")
|
flag.StringVar(&Flags.GCSObjectPrefix, "gcs-object-prefix", "", "Prefix for GCS object names (can't contain underscore character)")
|
||||||
|
flag.StringVar(&Flags.EnabledHooksString, "hooks-enabled-events", "", "Comma separated list of enabled hook events (e.g. post-create,post-finish). Leave empty to enable all events")
|
||||||
flag.StringVar(&Flags.FileHooksDir, "hooks-dir", "", "Directory to search for available hooks scripts")
|
flag.StringVar(&Flags.FileHooksDir, "hooks-dir", "", "Directory to search for available hooks scripts")
|
||||||
flag.StringVar(&Flags.HttpHooksEndpoint, "hooks-http", "", "An HTTP endpoint to which hook events will be sent to")
|
flag.StringVar(&Flags.HttpHooksEndpoint, "hooks-http", "", "An HTTP endpoint to which hook events will be sent to")
|
||||||
flag.IntVar(&Flags.HttpHooksRetry, "hooks-http-retry", 3, "Number of times to retry on a 500 or network timeout")
|
flag.IntVar(&Flags.HttpHooksRetry, "hooks-http-retry", 3, "Number of times to retry on a 500 or network timeout")
|
||||||
|
@ -61,9 +66,10 @@ func ParseFlags() {
|
||||||
flag.StringVar(&Flags.MetricsPath, "metrics-path", "/metrics", "Path under which the metrics endpoint will be accessible")
|
flag.StringVar(&Flags.MetricsPath, "metrics-path", "/metrics", "Path under which the metrics endpoint will be accessible")
|
||||||
flag.BoolVar(&Flags.BehindProxy, "behind-proxy", false, "Respect X-Forwarded-* and similar headers which may be set by proxies")
|
flag.BoolVar(&Flags.BehindProxy, "behind-proxy", false, "Respect X-Forwarded-* and similar headers which may be set by proxies")
|
||||||
flag.BoolVar(&Flags.VerboseOutput, "verbose", true, "Enable verbose logging output")
|
flag.BoolVar(&Flags.VerboseOutput, "verbose", true, "Enable verbose logging output")
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
SetEnabledHooks()
|
||||||
|
|
||||||
if Flags.FileHooksDir != "" {
|
if Flags.FileHooksDir != "" {
|
||||||
Flags.FileHooksDir, _ = filepath.Abs(Flags.FileHooksDir)
|
Flags.FileHooksDir, _ = filepath.Abs(Flags.FileHooksDir)
|
||||||
Flags.FileHooksInstalled = true
|
Flags.FileHooksInstalled = true
|
||||||
|
@ -89,3 +95,30 @@ func ParseFlags() {
|
||||||
"Please remove underscore from the value", Flags.GCSObjectPrefix)
|
"Please remove underscore from the value", Flags.GCSObjectPrefix)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SetEnabledHooks() {
|
||||||
|
if Flags.EnabledHooksString != "" {
|
||||||
|
slc := strings.Split(Flags.EnabledHooksString, ",")
|
||||||
|
|
||||||
|
for i, h := range slc {
|
||||||
|
slc[i] = strings.TrimSpace(h)
|
||||||
|
|
||||||
|
if !hookTypeInSlice(hooks.HookType(h), hooks.AvailableHooks) {
|
||||||
|
stderr.Fatalf("Unknown hook event type in -hooks-enabled-events flag: %s", h)
|
||||||
|
}
|
||||||
|
|
||||||
|
Flags.EnabledHooks = append(Flags.EnabledHooks, hooks.HookType(h))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(Flags.EnabledHooks) == 0 {
|
||||||
|
Flags.EnabledHooks = hooks.AvailableHooks
|
||||||
|
}
|
||||||
|
|
||||||
|
var enabledHooksString []string
|
||||||
|
for _, h := range Flags.EnabledHooks {
|
||||||
|
enabledHooksString = append(enabledHooksString, string(h))
|
||||||
|
}
|
||||||
|
|
||||||
|
stdout.Printf("Enabled hook events: %s", strings.Join(enabledHooksString, ", "))
|
||||||
|
}
|
||||||
|
|
|
@ -10,6 +10,15 @@ import (
|
||||||
|
|
||||||
var hookHandler hooks.HookHandler = nil
|
var hookHandler hooks.HookHandler = nil
|
||||||
|
|
||||||
|
func hookTypeInSlice(a hooks.HookType, list []hooks.HookType) bool {
|
||||||
|
for _, b := range list {
|
||||||
|
if b == a {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
type hookDataStore struct {
|
type hookDataStore struct {
|
||||||
tusd.DataStore
|
tusd.DataStore
|
||||||
}
|
}
|
||||||
|
@ -62,7 +71,6 @@ func SetupPreHooks(composer *tusd.StoreComposer) error {
|
||||||
composer.UseCore(hookDataStore{
|
composer.UseCore(hookDataStore{
|
||||||
DataStore: composer.Core,
|
DataStore: composer.Core,
|
||||||
})
|
})
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,6 +99,10 @@ func invokeHookAsync(typ hooks.HookType, info tusd.FileInfo) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func invokeHookSync(typ hooks.HookType, info tusd.FileInfo, captureOutput bool) ([]byte, error) {
|
func invokeHookSync(typ hooks.HookType, info tusd.FileInfo, captureOutput bool) ([]byte, error) {
|
||||||
|
if !hookTypeInSlice(typ, Flags.EnabledHooks) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
switch typ {
|
switch typ {
|
||||||
case hooks.HookPostFinish:
|
case hooks.HookPostFinish:
|
||||||
logEv(stdout, "UploadFinished", "id", info.ID, "size", strconv.FormatInt(info.Size, 10))
|
logEv(stdout, "UploadFinished", "id", info.ID, "size", strconv.FormatInt(info.Size, 10))
|
||||||
|
|
|
@ -19,6 +19,8 @@ const (
|
||||||
HookPreCreate HookType = "pre-create"
|
HookPreCreate HookType = "pre-create"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var AvailableHooks []HookType = []HookType{HookPreCreate, HookPostCreate, HookPostReceive, HookPostTerminate, HookPostFinish}
|
||||||
|
|
||||||
type hookDataStore struct {
|
type hookDataStore struct {
|
||||||
tusd.DataStore
|
tusd.DataStore
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,6 +45,9 @@ This event will be triggered after an upload has been terminated, meaning that t
|
||||||
|
|
||||||
This event will be triggered for every running upload to indicate its current progress. It will be emitted whenever the server has received more data from the client but at most every second. The offset property will be set to the number of bytes which have been transfered to the server, at the time in total. Please be aware that this number may be higher than the number of bytes which have been stored by the data store!
|
This event will be triggered for every running upload to indicate its current progress. It will be emitted whenever the server has received more data from the client but at most every second. The offset property will be set to the number of bytes which have been transfered to the server, at the time in total. Please be aware that this number may be higher than the number of bytes which have been stored by the data store!
|
||||||
|
|
||||||
|
## Whitelisting Hook Events
|
||||||
|
|
||||||
|
The `--hooks-enabled-events` option for the tusd binary works as a whitelist for hook events and takes a comma separated list of hook events (for instance: `pre-create,post-create`). This can be useful to limit the number of hook executions and save resources if you are only interested in some events. If the `--hooks-enabled-events` option is omitted, all hook events are enabled.
|
||||||
|
|
||||||
## File Hooks
|
## File Hooks
|
||||||
### The Hook Directory
|
### The Hook Directory
|
||||||
|
|
Loading…
Reference in New Issue