TUN-528: Move cloudflared into a separate repo

This commit is contained in:
Areg Harutyunyan
2018-05-01 18:45:06 -05:00
parent e8c621a648
commit d06fc520c7
4726 changed files with 1763680 additions and 0 deletions

20
vendor/github.com/mholt/caddy/onevent/hook/config.go generated vendored Normal file
View File

@@ -0,0 +1,20 @@
package hook
import (
"github.com/mholt/caddy"
)
// Config describes how Hook should be configured and used.
type Config struct {
ID string
Event caddy.EventName
Command string
Args []string
}
// SupportedEvents is a map of supported events.
var SupportedEvents = map[string]caddy.EventName{
"startup": caddy.InstanceStartupEvent,
"shutdown": caddy.ShutdownEvent,
"certrenew": caddy.CertRenewEvent,
}

41
vendor/github.com/mholt/caddy/onevent/hook/hook.go generated vendored Normal file
View File

@@ -0,0 +1,41 @@
package hook
import (
"log"
"os"
"os/exec"
"strings"
"github.com/mholt/caddy"
)
// Hook executes a command.
func (cfg *Config) Hook(event caddy.EventName, info interface{}) error {
if event != cfg.Event {
return nil
}
nonblock := false
if len(cfg.Args) >= 1 && cfg.Args[len(cfg.Args)-1] == "&" {
// Run command in background; non-blocking
nonblock = true
cfg.Args = cfg.Args[:len(cfg.Args)-1]
}
// Execute command.
cmd := exec.Command(cfg.Command, cfg.Args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if nonblock {
log.Printf("[INFO] Nonblocking Command \"%s %s\" with ID %s", cfg.Command, strings.Join(cfg.Args, " "), cfg.ID)
return cmd.Start()
}
log.Printf("[INFO] Blocking Command \"%s %s\" with ID %s", cfg.Command, strings.Join(cfg.Args, " "), cfg.ID)
err := cmd.Run()
if err != nil {
return err
}
return nil
}

View File

@@ -0,0 +1,61 @@
package hook
import (
"os"
"path/filepath"
"strconv"
"testing"
"time"
"github.com/google/uuid"
"github.com/mholt/caddy"
)
func TestHook(t *testing.T) {
tempDirPath := os.TempDir()
testDir := filepath.Join(tempDirPath, "temp_dir_for_testing_command")
defer func() {
// clean up after non-blocking startup function quits
time.Sleep(500 * time.Millisecond)
os.RemoveAll(testDir)
}()
osSenitiveTestDir := filepath.FromSlash(testDir)
os.RemoveAll(osSenitiveTestDir) // start with a clean slate
tests := []struct {
name string
event caddy.EventName
command string
args []string
shouldErr bool
shouldRemoveErr bool
}{
{name: "blocking", event: caddy.InstanceStartupEvent, command: "mkdir", args: []string{osSenitiveTestDir}, shouldErr: false, shouldRemoveErr: false},
{name: "nonBlocking", event: caddy.ShutdownEvent, command: "mkdir", args: []string{osSenitiveTestDir, "&"}, shouldErr: false, shouldRemoveErr: true},
{name: "nonBlocking2", event: caddy.ShutdownEvent, command: "echo", args: []string{"&"}, shouldErr: false, shouldRemoveErr: true},
{name: "nonExistent", event: caddy.CertRenewEvent, command: strconv.Itoa(int(time.Now().UnixNano())), shouldErr: true, shouldRemoveErr: true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
cfg := new(Config)
cfg.ID = uuid.New().String()
cfg.Event = test.event
cfg.Command = test.command
cfg.Args = test.args
err := cfg.Hook(test.event, nil)
if err == nil && test.shouldErr {
t.Error("Test didn't error, but it should have")
} else if err != nil && !test.shouldErr {
t.Errorf("Test errored, but it shouldn't have; got '%v'", err)
}
err = os.Remove(osSenitiveTestDir)
if err != nil && !test.shouldRemoveErr {
t.Errorf("Test received an error of:\n%v", err)
}
})
}
}

68
vendor/github.com/mholt/caddy/onevent/on.go generated vendored Normal file
View File

@@ -0,0 +1,68 @@
package onevent
import (
"strings"
"github.com/google/uuid"
"github.com/mholt/caddy"
"github.com/mholt/caddy/onevent/hook"
)
func init() {
// Register Directive.
caddy.RegisterPlugin("on", caddy.Plugin{Action: setup})
}
func setup(c *caddy.Controller) error {
config, err := onParse(c)
if err != nil {
return err
}
// Register Event Hooks.
c.OncePerServerBlock(func() error {
for _, cfg := range config {
caddy.RegisterEventHook("on-"+cfg.ID, cfg.Hook)
}
return nil
})
return nil
}
func onParse(c *caddy.Controller) ([]*hook.Config, error) {
var config []*hook.Config
for c.Next() {
cfg := new(hook.Config)
if !c.NextArg() {
return config, c.ArgErr()
}
// Configure Event.
event, ok := hook.SupportedEvents[strings.ToLower(c.Val())]
if !ok {
return config, c.Errf("Wrong event name or event not supported: '%s'", c.Val())
}
cfg.Event = event
// Assign an unique ID.
cfg.ID = uuid.New().String()
args := c.RemainingArgs()
// Extract command and arguments.
command, args, err := caddy.SplitCommandAndArgs(strings.Join(args, " "))
if err != nil {
return config, c.Err(err.Error())
}
cfg.Command = command
cfg.Args = args
config = append(config, cfg)
}
return config, nil
}

81
vendor/github.com/mholt/caddy/onevent/on_test.go generated vendored Normal file
View File

@@ -0,0 +1,81 @@
package onevent
import (
"testing"
"github.com/mholt/caddy"
"github.com/mholt/caddy/onevent/hook"
)
func TestSetup(t *testing.T) {
tests := []struct {
name string
input string
shouldErr bool
}{
{name: "noInput", input: "on", shouldErr: true},
{name: "nonExistent", input: "on xyz cmd arg", shouldErr: true},
{name: "startup", input: "on startup cmd arg", shouldErr: false},
{name: "shutdown", input: "on shutdown cmd arg &", shouldErr: false},
{name: "certrenew", input: "on certrenew cmd arg", shouldErr: false},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
c := caddy.NewTestController("http", test.input)
c.Key = test.name
err := setup(c)
if err == nil && test.shouldErr {
t.Error("Test didn't error, but it should have")
} else if err != nil && !test.shouldErr {
t.Errorf("Test errored, but it shouldn't have; got '%v'", err)
}
})
}
}
func TestCommandParse(t *testing.T) {
tests := []struct {
name string
input string
shouldErr bool
config hook.Config
}{
{name: "noInput", input: `on`, shouldErr: true},
{name: "nonExistent", input: "on xyz cmd arg", shouldErr: true},
{name: "startup", input: `on startup cmd arg1 arg2`, shouldErr: false, config: hook.Config{Event: caddy.InstanceStartupEvent, Command: "cmd", Args: []string{"arg1", "arg2"}}},
{name: "shutdown", input: `on shutdown cmd arg1 arg2 &`, shouldErr: false, config: hook.Config{Event: caddy.ShutdownEvent, Command: "cmd", Args: []string{"arg1", "arg2", "&"}}},
{name: "certrenew", input: `on certrenew cmd arg1 arg2`, shouldErr: false, config: hook.Config{Event: caddy.CertRenewEvent, Command: "cmd", Args: []string{"arg1", "arg2"}}},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
config, err := onParse(caddy.NewTestController("http", test.input))
if err == nil && test.shouldErr {
t.Error("Test didn't error, but it should have")
} else if err != nil && !test.shouldErr {
t.Errorf("Test errored, but it shouldn't have; got '%v'", err)
}
for _, cfg := range config {
if cfg.Event != test.config.Event {
t.Errorf("Expected event %s; got %s", test.config.Event, cfg.Event)
}
if cfg.Command != test.config.Command {
t.Errorf("Expected command %s; got %s", test.config.Command, cfg.Command)
}
for i, arg := range cfg.Args {
if arg != test.config.Args[i] {
t.Errorf("Expected arg in position %d to be %s, got %s", i, test.config.Args[i], arg)
}
}
}
})
}
}