Skip to content

Commit

Permalink
Use exported envman function for step input expansion. (#721)
Browse files Browse the repository at this point in the history
* Use exported envman function for step input expansion.

* Adding comment for exported function

* dep update

* dep update

* Dep update

* dep update vendors

* dep update

* Fill missing defaults for expanded vars

* Moving step input template expansion to a seperate function, updating error messages.

* Adding explanation and test case for not checking if expanded input is in the result environment map.

* Check if input key is present in evironment map

* normalize error messages

* Use common env var expansion during step input template parsing.

* removing unused field

* Normalize inputs:
  • Loading branch information
lpusok authored May 12, 2020
1 parent 6c26984 commit 5fdea37
Show file tree
Hide file tree
Showing 13 changed files with 744 additions and 401 deletions.
6 changes: 4 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ required = ["github.com/bitrise-io/go-utils/envutil",
"github.com/bitrise-io/stepman"]

[[constraint]]
name = "github.com/sirupsen/logrus"
version = "1.4.2"
name = "github.com/Sirupsen/logrus"

[[constraint]]
branch = "master"
Expand Down
45 changes: 45 additions & 0 deletions cli/analytics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cli

import (
"bytes"
"fmt"
"io"

"github.com/bitrise-io/bitrise/tools/filterwriter"
"github.com/bitrise-io/envman/models"
)

func redactStepInputs(environment map[string]string, inputs []models.EnvironmentItemModel, secrets []string) (map[string]string, error) {
redactedStepInputs := make(map[string]string)

// Filter inputs from enviroments
for _, input := range inputs {
inputKey, _, err := input.GetKeyValuePair()
if err != nil {
return map[string]string{}, fmt.Errorf("failed to get input key: %s", err)
}

// If input key may not be present in the result environment.
// This can happen if the input has the "skip_if_empty" property set to true, and it is empty.
inputValue, ok := environment[inputKey]
if !ok {
redactedStepInputs[inputKey] = ""
continue
}

src := bytes.NewReader([]byte(inputValue))
dstBuf := new(bytes.Buffer)
secretFilterDst := filterwriter.New(secrets, dstBuf)

if _, err := io.Copy(secretFilterDst, src); err != nil {
return map[string]string{}, fmt.Errorf("failed to redact secrets, stream copy failed: %s", err)
}
if _, err := secretFilterDst.Flush(); err != nil {
return map[string]string{}, fmt.Errorf("failed to redact secrets, stream flush failed: %s", err)
}

redactedStepInputs[inputKey] = dstBuf.String()
}

return redactedStepInputs, nil
}
55 changes: 55 additions & 0 deletions cli/analytics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package cli

import (
"testing"

"github.com/bitrise-io/envman/models"
envmanModels "github.com/bitrise-io/envman/models"
"github.com/stretchr/testify/require"
)

func Test_expandStepInputsForAnalytics(t *testing.T) {
type args struct {
environments map[string]string
inputs []envmanModels.EnvironmentItemModel
secretValues []string
}
tests := []struct {
name string
args args
want map[string]string
}{
{
name: "Secret filtering",
args: args{
environments: map[string]string{"secret_simulator_device": "secret_a_secret_b_secret_c"},
inputs: []models.EnvironmentItemModel{
{"secret_simulator_device": "secret_a_secret_b_secret_c"},
},
secretValues: []string{"secret_a_secret_b_secret_c"},
},
want: map[string]string{
"secret_simulator_device": "[REDACTED]",
},
},
{
name: "Input is empty, and skip_if_empty is true",
args: args{
environments: map[string]string{},
inputs: []models.EnvironmentItemModel{
{"myinput": ""},
},
},
want: map[string]string{
"myinput": "",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := redactStepInputs(tt.args.environments, tt.args.inputs, tt.args.secretValues)
require.NoError(t, err, "expandStepInputsForAnalytics")
require.Equal(t, tt.want, got)
})
}
}
Loading

0 comments on commit 5fdea37

Please sign in to comment.