Skip to content

Commit

Permalink
feat: add secret values to masked outputs (#465)
Browse files Browse the repository at this point in the history
* feat: add secret values to masked outputs

* t.setenv

---------

Co-authored-by: ecrupper <easton.crupper@target.com>
  • Loading branch information
ecrupper and ecrupper authored Mar 11, 2025
1 parent c70003d commit 79abe19
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
41 changes: 41 additions & 0 deletions cmd/secret-vault/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
package main

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/go-vela/secret-vault/vault"
"github.com/go-vela/types/raw"
"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
)
Expand Down Expand Up @@ -66,6 +69,24 @@ func (r *Read) Exec(v *vault.Client) error {
Fs: appFS,
}

var outputs map[string]string

outputsPath := os.Getenv("VELA_MASKED_OUTPUTS")

// if the masked Vela outputs is configured, create a map to store the values to write later
if len(outputsPath) > 0 {
rawOutputs, err := a.ReadFile(outputsPath)
if err != nil {
logrus.Debugf("empty masked outputs file. creating one...")
}

// godotenv has a Read, but for testing it will not read a memory map FS
outputs, err = godotenv.Parse(bytes.NewReader(rawOutputs))
if err != nil {
return err
}
}

for _, item := range r.Items {
for _, pth := range item.Path {
// remove any leading slashes from path
Expand Down Expand Up @@ -109,10 +130,30 @@ func (r *Read) Exec(v *vault.Client) error {
if err != nil {
return err
}

if len(outputsPath) > 0 {
// create key of VELA_SECRETS_<path>_<key>
envKey := strings.ReplaceAll(strings.ToUpper(strings.TrimPrefix(path, "/")), "/", "_")

outputs[envKey] = v.(string)
}
}
}
}

if len(outputsPath) > 0 {
// godotenv has a Write, but for testing it will not write to a memory map FS
content, err := godotenv.Marshal(outputs)
if err != nil {
return err
}

err = a.WriteFile(outputsPath, []byte(content), 0600)
if err != nil {
return err
}
}

return nil
}

Expand Down
33 changes: 33 additions & 0 deletions cmd/secret-vault/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
package main

import (
"bytes"
"path/filepath"
"reflect"
"testing"

"github.com/go-vela/secret-vault/vault"
"github.com/joho/godotenv"
"github.com/spf13/afero"
)

Expand Down Expand Up @@ -44,6 +47,36 @@ func TestVault_Read_Exec(t *testing.T) {
if err != nil {
t.Errorf("Exec returned err: %v", err)
}

t.Setenv("VELA_MASKED_OUTPUTS", "/vela/outputs/masked.env")

err = appFS.MkdirAll(filepath.Dir("/vela/outputs/masked.env"), 0777)
if err != nil {
t.Error(err)
}

err = r.Exec(vault)
if err != nil {
t.Errorf("Exec returned err: %v", err)
}

a := &afero.Afero{
Fs: appFS,
}

rawOutputs, err := a.ReadFile("/vela/outputs/masked.env")
if err != nil {
t.Errorf("unable to read outputs file: %v", err)
}

envMap, err := godotenv.Parse(bytes.NewReader(rawOutputs))
if err != nil {
t.Errorf("unable to parse outputs file: %v", err)
}

if envMap["VELA_SECRETS_FOOBAR_MY_SECRET"] != "bar" {
t.Errorf("Exec is %v, want %v", envMap["foobar_foobar2_secret"], "bar")
}
}

func TestVault_Read_Exec_Fail(t *testing.T) {
Expand Down

0 comments on commit 79abe19

Please sign in to comment.