Skip to content

Commit

Permalink
feat(login): introduce VKV_LOGIN_COMMAND env var
Browse files Browse the repository at this point in the history
  • Loading branch information
FalcoSuessgott committed Nov 18, 2023
1 parent a6ed0f6 commit faff5fd
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 22 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ name: Test and coverage

on:
push:
paths:
- '**.go'
branches:
- '*'
- '!master'
Expand Down
3 changes: 3 additions & 0 deletions Dockerfile.goreleaser
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ COPY vkv /usr/bin/vkv
# required for vkv server
EXPOSE 8080

# preinstall vault for VKV_LOGIN_COMMAND
RUN apk add --no-cache vault

ENTRYPOINT ["/usr/bin/vkv"]
11 changes: 6 additions & 5 deletions cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package server

import (
"bytes"
"fmt"
"io"
"log"
"path"
"fmt"

printer "github.com/FalcoSuessgott/vkv/pkg/printer/secret"
"github.com/FalcoSuessgott/vkv/pkg/utils"
Expand All @@ -18,10 +18,11 @@ import (
const envVarExportPrefix = "VKV_SERVER_"

type serverOptions struct {
Port string `env:"PORT" envDefault:"8080"`
Path string `env:"PATH"`
EnginePath string `env:"ENGINE_PATH"`
SkipErrors bool `env:"SKIP_ERRORS" envDefault:"false"`
Port string `env:"PORT" envDefault:"8080"`
Path string `env:"PATH"`
EnginePath string `env:"ENGINE_PATH"`
SkipErrors bool `env:"SKIP_ERRORS" envDefault:"false"`
LoginCommand string `env:"LoginCommand"`
}

// NewServerCmd export subcommand.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.19
require (
github.com/disiqueira/gotree/v3 v3.0.2
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
github.com/gin-gonic/gin v1.9.1
github.com/gotesttools/gotestfmt/v2 v2.5.0
github.com/hashicorp/vault/api v1.10.0
github.com/juju/ansiterm v1.0.0
Expand Down Expand Up @@ -74,7 +75,6 @@ require (
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/ghostiam/protogetter v0.2.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.9.1 // indirect
github.com/go-critic/go-critic v0.9.0 // indirect
github.com/go-jose/go-jose/v3 v3.0.0 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
Expand Down
13 changes: 1 addition & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE=
github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
Expand Down Expand Up @@ -494,8 +495,6 @@ github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNx
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
Expand Down Expand Up @@ -568,8 +567,6 @@ github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT9
github.com/otiai10/mint v1.3.1/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc=
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pelletier/go-toml/v2 v2.0.5 h1:ipoSadvV8oGUjnUbMub59IDPPwfxF694nG/jwbMiyQg=
github.com/pelletier/go-toml/v2 v2.0.5/go.mod h1:OMHamSCAODeSsVrwwvcJOaoN0LIUIaFVNZzmWyNfXas=
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down Expand Up @@ -790,8 +787,6 @@ golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/crypto v0.15.0 h1:frVn1TEaCEaZcn3Tmd7Y2b5KKPaZ+I32Q2OA3kYp5TA=
golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand Down Expand Up @@ -885,8 +880,6 @@ golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg=
golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand Down Expand Up @@ -986,8 +979,6 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand All @@ -1007,8 +998,6 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down
25 changes: 25 additions & 0 deletions pkg/exec/exec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package exec

import (
"bytes"
"errors"
"os/exec"
"strings"
)

// Run runs the given command and returns the output.
//nolint: gosec
func Run(cmd []string) ([]byte, error) {
var stdout, stderr bytes.Buffer

c := exec.Command("bash", "-c", strings.Join(cmd, " "))

c.Stdout = &stdout
c.Stderr = &stderr

if c.Run() != nil {
return nil, errors.New(stderr.String())
}

return stdout.Bytes(), nil
}
45 changes: 45 additions & 0 deletions pkg/exec/exec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package exec

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestParseConfig(t *testing.T) {
testCases := []struct {
name string
cmd []string
exp string
err bool
}{
{
name: "simple command",
cmd: []string{"echo", "hallo"},
exp: "hallo\n",
err: false,
},
{
name: "pipe command",
cmd: []string{"echo", "hallo world", "|", "cut", "-d", "\" \"", "-f2"},
exp: "world\n",
err: false,
},
{
name: "error command",
cmd: []string{"cat invalid_file.txt"},
err: true,
},
}

for _, tc := range testCases {
out, err := Run(tc.cmd)

if tc.err {
require.Error(t, err, tc.name)
}

assert.Equal(t, tc.exp, string(out), tc.name)
}
}
24 changes: 22 additions & 2 deletions pkg/vault/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package vault
import (
"fmt"
"os"
"strings"

"github.com/FalcoSuessgott/vkv/pkg/exec"
"github.com/hashicorp/vault/api"
)

Expand All @@ -19,8 +21,21 @@ func NewDefaultClient() (*Vault, error) {
return nil, fmt.Errorf("VAULT_ADDR required but not set")
}

vaultToken, ok := os.LookupEnv("VAULT_TOKEN")
if !ok {
vaultToken, tokenExported := os.LookupEnv("VAULT_TOKEN")

cmd, ok := os.LookupEnv("VKV_LOGIN_COMMAND")
if !tokenExported && ok {
cmdParts := strings.Split(cmd, " ")

token, err := exec.Run(cmdParts)
if err != nil {
return nil, fmt.Errorf("error running VKV_LOGIN_CMD (%s): %w", cmd, err)
}

vaultToken = strings.TrimSpace(string(token))
}

if vaultToken == "" {
return nil, fmt.Errorf("VAULT_TOKEN required but not set")
}

Expand All @@ -41,6 +56,11 @@ func NewDefaultClient() (*Vault, error) {
c.SetNamespace(vaultNamespace)
}

_, err = c.Auth().Token().Lookup(vaultToken)
if err != nil {
return nil, fmt.Errorf("not authenticated. Perhaps not a valid token")
}

return &Vault{Client: c}, nil
}

Expand Down

0 comments on commit faff5fd

Please sign in to comment.