diff --git a/e2etests/bats-tests.sh b/e2etests/bats-tests.sh index cbf54700d..808f1ca44 100755 --- a/e2etests/bats-tests.sh +++ b/e2etests/bats-tests.sh @@ -21,6 +21,21 @@ get_value_from() { [[ "${BASH_VERSION:0:1}" -ge '4' ]] || false } +@test "access-to-create-pods from stdin" { + tmp="tests/checks/access-to-create-pods.yml" + cmd="cat ${tmp} | ${KUBE_LINTER_BIN} lint --include access-to-create-pods --do-not-auto-add-defaults --format json -" + run ${cmd} + + print_info "${status}" "${output}" "${cmd}" "${tmp}" + [ "$status" -eq 1 ] + + message1=$(get_value_from "${lines[0]}" '.Reports[0].Object.K8sObject.GroupVersionKind.Kind + ": " + .Reports[0].Diagnostic.Message') + count=$(get_value_from "${lines[0]}" '.Reports | length') + + [[ "${message1}" == "RoleBinding: binding to \"role1\" role that has [create] access to [pods]" ]] + [[ "${count}" == "1" ]] +} + @test "access-to-create-pods" { tmp="tests/checks/access-to-create-pods.yml" cmd="${KUBE_LINTER_BIN} lint --include access-to-create-pods --do-not-auto-add-defaults --format json ${tmp}" diff --git a/pkg/command/lint/command.go b/pkg/command/lint/command.go index 56c79694d..634b79cd6 100644 --- a/pkg/command/lint/command.go +++ b/pkg/command/lint/command.go @@ -87,8 +87,12 @@ func Command() *cobra.Command { return err } - absArgs := []string{} + absArgs := make([]string, 0, len(args)) for _, arg := range args { + if arg == lintcontext.ReadFromStdin { + absArgs = append(absArgs, lintcontext.ReadFromStdin) + continue + } absArg, err := pathutil.GetAbsolutPath(arg) if err != nil { return err diff --git a/pkg/lintcontext/create_contexts.go b/pkg/lintcontext/create_contexts.go index 64b9b32d9..6dd888f21 100644 --- a/pkg/lintcontext/create_contexts.go +++ b/pkg/lintcontext/create_contexts.go @@ -16,6 +16,9 @@ import ( "k8s.io/apimachinery/pkg/runtime" ) +// ReadFromStdin is a path used to indicate reading from os.Stdin +const ReadFromStdin = "-" + var ( knownYAMLExtensions = set.NewFrozenStringSet(".yaml", ".yml") ) @@ -41,16 +44,15 @@ func CreateContextsWithOptions(options Options, ignorePaths []string, filesOrDir contextsByDir := make(map[string]*lintContextImpl) fileOrDirsLoop: for _, fileOrDir := range filesOrDirs { - // Stdin - if fileOrDir == "-" { - if _, alreadyExists := contextsByDir["-"]; alreadyExists { + if fileOrDir == ReadFromStdin { + if _, alreadyExists := contextsByDir[ReadFromStdin]; alreadyExists { continue } ctx := newCtx(options) if err := ctx.loadObjectsFromReader("", os.Stdin); err != nil { return nil, err } - contextsByDir["-"] = ctx + contextsByDir[ReadFromStdin] = ctx continue }