/
env.go
146 lines (124 loc) · 4.08 KB
/
env.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//
// Copyright 2022 The Sigstore Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cli
import (
"fmt"
"os"
"sort"
"strings"
"github.com/spf13/cobra"
"github.com/sigstore/cosign/cmd/cosign/cli/options"
"github.com/sigstore/cosign/pkg/cosign/env"
)
func Env() *cobra.Command {
o := &options.EnvOptions{}
cmd := &cobra.Command{
Use: "env",
Short: "Prints Cosign environment variables",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
envVars := env.EnvironmentVariables()
printEnv(envVars, getEnv(), getEnviron(), o.ShowDescriptions, o.ShowSensitiveValues)
return nil
},
}
o.AddFlags(cmd)
return cmd
}
// NB: the purpose of those types and functions is to make it possible to swap function for testing purposes
type envGetter func(env.Variable) string
type environGetter func() []string
func getEnv() envGetter {
return env.Getenv
}
func getEnviron() environGetter {
return os.Environ
}
// NB: printEnv intentionally takes map of env vars to make it easier to unit test it
func printEnv(envVars map[env.Variable]env.VariableOpts,
envGet envGetter,
environGet environGetter,
showDescription, showSensitive bool) {
// Sort keys to print them in a predictable order
keys := sortEnvKeys(envVars)
// Print known/registered environment variables
for _, e := range keys {
opts := envVars[e]
// Get value of environment variable
val := envGet(e)
// If showDescription is set, print description for that variable
if showDescription {
fmt.Printf("# %s %s\n", e.String(), opts.Description)
fmt.Printf("# Expects: %s\n", opts.Expects)
}
// If variable is sensitive, and we don't want to show sensitive values,
// print environment variable name and some asterisk symbols.
// If sensitive variable isn't set or doesn't have any value, we'll just
// print like non-sensitive variable
if opts.Sensitive && !showSensitive && val != "" {
fmt.Printf("%s=******\n", e.String())
} else {
fmt.Printf("%s=%s\n", e.String(), val)
}
}
// Print not registered environment variables
nonRegEnv := map[string]string{}
for _, e := range environGet() {
// Prefixes to look for. err on the side of showing too much rather
// than too little. We'll only output things that have values set.
for _, prefix := range []string{
// We want to print eventually non-registered cosign variables (even if this shouldn't happen)
"COSIGN_",
// Can modify Sigstore/TUF client behavior - https://github.com/sigstore/sigstore/blob/35d6a82c15183f7fe7a07eca45e17e378aa32126/pkg/tuf/client.go#L52
"SIGSTORE_",
"TUF_",
} {
if strings.HasPrefix(e, prefix) {
// os.Environ returns key=value pairs, so we split by =
envSplit := strings.Split(e, "=")
key := envSplit[0]
// Skip registered environment variables (those are already printed above)
if _, ok := envVars[env.Variable(key)]; ok {
continue
}
val := ""
if len(envSplit) == 2 {
val = envSplit[1]
}
nonRegEnv[key] = val
}
}
}
if len(nonRegEnv) > 0 && showDescription {
fmt.Println("# Environment variables below are not registered with cosign,\n# but might still influence cosign's behavior.")
}
for key, val := range nonRegEnv {
if !showSensitive && val != "" {
fmt.Printf("%s=******\n", key)
} else {
fmt.Printf("%s=%s\n", key, val)
}
}
}
func sortEnvKeys(envVars map[env.Variable]env.VariableOpts) []env.Variable {
keys := []env.Variable{}
for k := range envVars {
keys = append(keys, k)
}
sort.Slice(keys, func(i, j int) bool {
return strings.Compare(keys[i].String(), keys[j].String()) < 0
})
return keys
}