Skip to content

Commit 72963db

Browse files
author
Brian McCallister
committed
WIP on pluggable auth stuff
1 parent 5154604 commit 72963db

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

pkg/plugin/plug.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
3+
out="$(echo $1 | base64 -D)"
4+
5+
echo "meow $out" >&2

pkg/plugin/plugin.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package plugin
2+
3+
import (
4+
"bytes"
5+
"encoding/base64"
6+
"os"
7+
"os/exec"
8+
)
9+
10+
// Plugins work by passing in values on the command line, serialized using base64
11+
// and reading responses out on stderr.
12+
// This leaves stdin/stdout available for interaction by the user, to
13+
// enter credentials or such
14+
15+
// Run runs the plugin
16+
func Run(input []byte, plugin ...string) ([]byte, error) {
17+
c := plugin[0]
18+
args := plugin[1:]
19+
20+
in := base64.StdEncoding.EncodeToString(input)
21+
22+
args = append(args, in)
23+
24+
cmd := exec.Command(c, args...)
25+
var stderr bytes.Buffer
26+
cmd.Stderr = &stderr
27+
28+
cmd.Stdout = os.Stdout
29+
cmd.Stdin = os.Stdin
30+
31+
err := cmd.Run()
32+
if err != nil {
33+
return nil, err
34+
}
35+
return stderr.Bytes(), nil
36+
}

pkg/plugin/plugin_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package plugin_test
2+
3+
import (
4+
"math/rand"
5+
"testing"
6+
7+
"github.com/brianm/epithet/pkg/plugin"
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestPlugin_BigInput(t *testing.T) {
13+
in := randStringBytes(8192)
14+
out, err := plugin.Run([]byte(in), "./plug.sh")
15+
require.NoError(t, err)
16+
17+
assert.Contains(t, string(out), in)
18+
assert.Contains(t, string(out), "meow")
19+
}
20+
21+
const letterBytes = "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"
22+
23+
func randStringBytes(n int) string {
24+
b := make([]byte, n)
25+
for i := range b {
26+
b[i] = letterBytes[rand.Intn(len(letterBytes))]
27+
}
28+
return string(b)
29+
}

0 commit comments

Comments
 (0)