-
Notifications
You must be signed in to change notification settings - Fork 200
/
context.go
454 lines (385 loc) · 12.7 KB
/
context.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package portercontext
import (
"bytes"
"context"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"get.porter.sh/porter/pkg"
"get.porter.sh/porter/pkg/tracing"
"github.com/carolynvs/aferox"
cnabclaims "github.com/cnabio/cnab-go/claim"
"github.com/mattn/go-colorable"
"github.com/mattn/go-isatty"
"github.com/pkg/errors"
"github.com/spf13/afero"
"go.opentelemetry.io/otel/attribute"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const (
// MixinOutputsDir represents the directory where mixin output files are written/read
MixinOutputsDir = "/cnab/app/porter/outputs"
)
type CommandBuilder func(name string, arg ...string) *exec.Cmd
type Context struct {
Debug bool
DebugPlugins bool
verbose bool
environ map[string]string
FileSystem aferox.Aferox
In io.Reader
Out io.Writer
Err io.Writer
NewCommand CommandBuilder
PlugInDebugContext *PluginDebugContext
//
// Logging and Tracing configuration
//
// a consistent id that is set on the context and emitted in the logs
// Helps correlate logs with a workflow.
correlationId string
// logLevel filters the messages written to the console and logfile
logLevel zapcore.Level
logFile afero.File
// indicates if log timestamps should be printed to the console
timestampLogs bool
// handles sending tracing data to an otel collector
tracer trace.Tracer
// handles send log data to the console/logfile
logger *zap.Logger
// cleans up resources associated with the tracer when porter completes
traceCloser *sdktrace.TracerProvider
// the service name sent to the otel collector when we send tracing data
traceServiceName string
}
// New creates a new context in the specified directory.
func New() *Context {
// Ignore any error getting the working directory and report errors
// when we attempt to access files in the current directory. This
// allows us to use the current directory as a default, and allow
// tests to override it.
pwd, _ := os.Getwd()
c := &Context{
environ: getEnviron(),
FileSystem: aferox.NewAferox(pwd, afero.NewOsFs()),
In: os.Stdin,
Out: NewCensoredWriter(os.Stdout),
Err: NewCensoredWriter(os.Stderr),
correlationId: cnabclaims.MustNewULID(), // not using cnab package because that creates a cycle
timestampLogs: true,
}
c.ConfigureLogging(context.Background(), LogConfiguration{})
c.defaultNewCommand()
c.PlugInDebugContext = NewPluginDebugContext(c)
return c
}
// StartRootSpan creates the root tracing span for the porter application.
// This should only be done once.
func (c *Context) StartRootSpan(ctx context.Context, op string, attrs ...attribute.KeyValue) (context.Context, tracing.TraceLogger) {
childCtx, span := c.tracer.Start(ctx, op)
span.SetAttributes(attrs...)
return tracing.NewRootLogger(childCtx, span, c.logger, c.tracer)
}
func (c *Context) makeLogEncoding() zapcore.EncoderConfig {
enc := zap.NewProductionEncoderConfig()
if c.timestampLogs {
enc.EncodeTime = zapcore.ISO8601TimeEncoder
} else { // used for testing, so we don't have unique timestamps in the logs
enc.EncodeTime = func(time time.Time, encoder zapcore.PrimitiveArrayEncoder) {
encoder.AppendString("")
}
}
return enc
}
type LogConfiguration struct {
LogToFile bool
LogDirectory string
LogLevel zapcore.Level
LogCorrelationID string
StructuredLogs bool
TelemetryEnabled bool
TelemetryEndpoint string
TelemetryProtocol string
TelemetryInsecure bool
TelemetryCertificate string
TelemetryCompression string
TelemetryTimeout string
TelemetryHeaders map[string]string
}
// ConfigureLogging applies different configuration to our logging and tracing.
func (c *Context) ConfigureLogging(ctx context.Context, cfg LogConfiguration) {
// Cleanup in case logging has been configured before
c.logLevel = cfg.LogLevel
if len(cfg.LogCorrelationID) > 0 {
c.correlationId = cfg.LogCorrelationID
}
encoding := c.makeLogEncoding()
consoleLogger := c.makeConsoleLogger(encoding, cfg.StructuredLogs)
// make a temporary logger that we can use until we've completely initialized the full logger
tmpLog := zap.New(consoleLogger)
var err error
fileLogger := zapcore.NewNopCore()
if cfg.LogToFile {
fileLogger, err = c.configureFileLog(encoding, cfg.LogDirectory)
if err != nil {
tmpLog.Error(errors.Wrap(err, "could not configure a file logger").Error())
} else {
tmpLog.Debug("Writing logs to " + c.logFile.Name())
}
}
tmpLog = zap.New(zapcore.NewTee(consoleLogger, fileLogger))
if cfg.TelemetryEnabled {
// Only initialize the tracer once per command
if c.traceCloser == nil {
err = c.configureTelemetry(ctx, tmpLog, cfg)
if err != nil {
tmpLog.Error(errors.Wrap(err, "could not configure a tracer").Error())
}
}
} else {
c.tracer = trace.NewNoopTracerProvider().Tracer("noop")
}
c.logger = tmpLog
}
func (c *Context) makeConsoleLogger(encoding zapcore.EncoderConfig, structuredLogs bool) zapcore.Core {
stderr := c.Err
if f, ok := stderr.(*os.File); ok {
if isatty.IsTerminal(f.Fd()) {
stderr = colorable.NewColorable(f)
encoding.EncodeLevel = zapcore.LowercaseColorLevelEncoder
}
}
// if structured-logs feature isn't enabled, keep the logs looking like they do now, with just the message printed
if !structuredLogs {
encoding.TimeKey = ""
encoding.LevelKey = ""
}
consoleEncoder := zapcore.NewConsoleEncoder(encoding)
return zapcore.NewCore(consoleEncoder, zapcore.AddSync(stderr), c.logLevel)
}
func (c *Context) configureFileLog(encoding zapcore.EncoderConfig, dir string) (zapcore.Core, error) {
if err := c.FileSystem.MkdirAll(dir, pkg.FileModeDirectory); err != nil {
return nil, err
}
// Write the logs to a file
logfile := filepath.Join(dir, c.correlationId+".json")
if c.logFile == nil { // We may have already opened this logfile, and we are just changing the log level
f, err := c.FileSystem.OpenFile(logfile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, pkg.FileModeWritable)
if err != nil {
return zapcore.NewNopCore(), errors.Wrapf(err, "could not start log file at %s", logfile)
}
c.logFile = f
}
// Split logs to the console and file
fileEncoder := zapcore.NewJSONEncoder(encoding)
return zapcore.NewCore(fileEncoder, zapcore.AddSync(c.logFile), c.logLevel), nil
}
func (c *Context) Close() error {
c.closeLogger()
if c.traceCloser != nil {
c.traceCloser.Shutdown(context.TODO())
}
return nil
}
func (c *Context) closeLogger() {
if c.logFile != nil {
c.logFile.Close()
c.logFile = nil
}
}
func (c *Context) defaultNewCommand() {
c.NewCommand = func(name string, arg ...string) *exec.Cmd {
return c.Command(name, arg...)
}
}
// Command creates a new exec.Cmd using the context's current directory.
func (c *Context) Command(name string, arg ...string) *exec.Cmd {
cmd := &exec.Cmd{
Dir: c.Getwd(),
Path: name,
Args: append([]string{name}, arg...),
Env: c.Environ(),
}
if filepath.Base(name) == name {
if lp, ok := c.LookPath(name); ok {
cmd.Path = lp
}
}
return cmd
}
func getEnviron() map[string]string {
environ := map[string]string{}
for _, env := range os.Environ() {
envParts := strings.SplitN(env, "=", 2)
key := envParts[0]
value := ""
if len(envParts) > 1 {
value = envParts[1]
}
environ[key] = value
}
return environ
}
func (c *Context) SetVerbose(value bool) {
c.verbose = value
}
func (c *Context) IsVerbose() bool {
return c.Debug || c.verbose
}
// Environ returns a copy of strings representing the environment,
// in the form "key=value".
func (c *Context) Environ() []string {
e := make([]string, 0, len(c.environ))
for k, v := range c.environ {
e = append(e, fmt.Sprintf("%s=%s", k, v))
}
return e
}
// EnvironMap returns a map of the current environment variables.
func (c *Context) EnvironMap() map[string]string {
env := make(map[string]string, len(c.environ))
for k, v := range c.environ {
env[k] = v
}
return env
}
// ExpandEnv replaces ${var} or $var in the string according to the values
// of the current environment variables. References to undefined
// variables are replaced by the empty string.
func (c *Context) ExpandEnv(s string) string {
return os.Expand(s, func(key string) string { return c.Getenv(key) })
}
// Getenv retrieves the value of the environment variable named by the key.
// It returns the value, which will be empty if the variable is not present.
// To distinguish between an empty value and an unset value, use LookupEnv.
func (c *Context) Getenv(key string) string {
return c.environ[key]
}
// This is a simplified exec.LookPath that checks if command is accessible given
// a PATH environment variable.
func (c *Context) LookPath(file string) (string, bool) {
return c.FileSystem.LookPath(file, c.Getenv("PATH"), c.Getenv("PATHEXT"))
}
// LookupEnv retrieves the value of the environment variable named
// by the key. If the variable is present in the environment the
// value (which may be empty) is returned and the boolean is true.
// Otherwise the returned value will be empty and the boolean will
// be false.
func (c *Context) LookupEnv(key string) (string, bool) {
value, ok := c.environ[key]
return value, ok
}
// Setenv sets the value of the environment variable named by the key.
// It returns an error, if any.
func (c *Context) Setenv(key string, value string) {
if c.environ == nil {
c.environ = make(map[string]string, 1)
}
c.environ[key] = value
}
// Unsetenv unsets a single environment variable.
func (c *Context) Unsetenv(key string) {
delete(c.environ, key)
}
// Clearenv deletes all environment variables.
func (c *Context) Clearenv() {
c.environ = make(map[string]string, 0)
}
// Getwd returns a rooted path name corresponding to the current directory.
func (c *Context) Getwd() string {
return c.FileSystem.Getwd()
}
// Chdir changes the current working directory to the named directory.
func (c *Context) Chdir(dir string) {
c.FileSystem.Chdir(dir)
}
// CensoredWriter is a writer wrapping the provided io.Writer with logic to censor certain values
type CensoredWriter struct {
writer io.Writer
sensitiveValues []string
}
// NewCensoredWriter returns a new CensoredWriter
func NewCensoredWriter(writer io.Writer) *CensoredWriter {
return &CensoredWriter{writer: writer, sensitiveValues: []string{}}
}
// SetSensitiveValues sets values needing masking for an CensoredWriter
func (cw *CensoredWriter) SetSensitiveValues(vals []string) {
cw.sensitiveValues = vals
}
// Write implements io.Writer's Write method, performing necessary auditing while doing so
func (cw *CensoredWriter) Write(b []byte) (int, error) {
auditedBytes := b
for _, val := range cw.sensitiveValues {
if strings.TrimSpace(val) != "" {
auditedBytes = bytes.Replace(auditedBytes, []byte(val), []byte("*******"), -1)
}
}
_, err := cw.writer.Write(auditedBytes)
return len(b), err
}
func (c *Context) CopyDirectory(srcDir, destDir string, includeBaseDir bool) error {
var stripPrefix string
if includeBaseDir {
stripPrefix = filepath.Dir(srcDir)
} else {
stripPrefix = srcDir
}
return c.FileSystem.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return errors.WithStack(err)
}
// Translate the path from the src to the final destination
dest := filepath.Join(destDir, strings.TrimPrefix(path, stripPrefix))
if dest == "" {
return nil
}
if info.IsDir() {
return errors.WithStack(c.FileSystem.MkdirAll(dest, info.Mode()))
}
return c.CopyFile(path, dest)
})
}
func (c *Context) CopyFile(src, dest string) error {
info, err := c.FileSystem.Stat(src)
if err != nil {
return errors.WithStack(err)
}
data, err := c.FileSystem.ReadFile(src)
if err != nil {
return errors.WithStack(err)
}
err = c.FileSystem.WriteFile(dest, data, info.Mode())
return errors.WithStack(err)
}
// WriteMixinOutputToFile writes the provided bytes (representing a mixin output)
// to a file named by the provided filename in Porter's mixin outputs directory
func (c *Context) WriteMixinOutputToFile(filename string, bytes []byte) error {
exists, err := c.FileSystem.DirExists(MixinOutputsDir)
if err != nil {
return err
}
if !exists {
if err := c.FileSystem.MkdirAll(MixinOutputsDir, pkg.FileModeDirectory); err != nil {
return errors.Wrap(err, "couldn't make output directory")
}
}
return c.FileSystem.WriteFile(filepath.Join(MixinOutputsDir, filename), bytes, pkg.FileModeWritable)
}
// SetSensitiveValues sets the sensitive values needing masking on output/err streams
func (c *Context) SetSensitiveValues(vals []string) {
if len(vals) > 0 {
out := NewCensoredWriter(c.Out)
out.SetSensitiveValues(vals)
c.Out = out
err := NewCensoredWriter(c.Err)
err.SetSensitiveValues(vals)
c.Err = err
}
}