-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use exported envman function for step input expansion. (#721)
* 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
Showing
13 changed files
with
744 additions
and
401 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
Oops, something went wrong.