-
Notifications
You must be signed in to change notification settings - Fork 574
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Redact token information from debug logs [HEAD-1184] (#4983)
* test: add failing token redact test * feat: adopt log scrubbing * fix: debuglog test * use common func to get scrubdict * use scrub logger in Extensible CLI * chore: run formatter * fix: acceptance test through gaf upgrade * refactor: cleaning up logger initialization * fix: toolchain * refactor: move debug logic in separate file to reduce main * fix: init order and redact tests * chore: run formatter * chore: use final gaf commit hash * chore: undo unneccessary change * fix: add missing fips_enable import --------- Co-authored-by: Luke Watts <[email protected]>
- Loading branch information
1 parent
0226e20
commit 9dd5ad4
Showing
7 changed files
with
135 additions
and
61 deletions.
There are no files selected for viewing
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,61 @@ | ||
package main | ||
|
||
// !!! This import needs to be the first import, please do not change this !!! | ||
import _ "github.com/snyk/go-application-framework/pkg/networking/fips_enable" | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
"time" | ||
|
||
"github.com/rs/zerolog" | ||
"github.com/snyk/go-application-framework/pkg/configuration" | ||
"github.com/snyk/go-application-framework/pkg/logging" | ||
) | ||
|
||
func getDebugLevel(config configuration.Configuration, logger *zerolog.Logger) zerolog.Level { | ||
loglevel := zerolog.DebugLevel | ||
if loglevelString := config.GetString("snyk_log_level"); loglevelString != "" { | ||
var err error | ||
loglevel, err = zerolog.ParseLevel(loglevelString) | ||
if err == nil { | ||
logger.Log().Msgf("Setting log level to %s", loglevelString) | ||
} else { | ||
logger.Log().Msgf("%v", err) | ||
loglevel = zerolog.DebugLevel | ||
} | ||
} | ||
return loglevel | ||
} | ||
|
||
func initDebugLogger(config configuration.Configuration) *zerolog.Logger { | ||
debug := config.GetBool(configuration.DEBUG) | ||
if !debug { | ||
return &noopLogger | ||
} else { | ||
var consoleWriter = zerolog.ConsoleWriter{ | ||
Out: os.Stderr, | ||
TimeFormat: time.RFC3339, | ||
NoColor: true, | ||
PartsOrder: []string{ | ||
zerolog.TimestampFieldName, | ||
"ext", | ||
"separator", | ||
zerolog.CallerFieldName, | ||
zerolog.MessageFieldName, | ||
}, | ||
FieldsExclude: []string{"ext", "separator"}, | ||
FormatTimestamp: func(i interface{}) string { | ||
t, _ := time.Parse(time.RFC3339, i.(string)) | ||
return strings.ToUpper(fmt.Sprintf("%s", t.UTC().Format(time.RFC3339))) | ||
}, | ||
} | ||
|
||
scrubLogger := logging.NewScrubbingWriter(zerolog.MultiLevelWriter(consoleWriter), logging.GetScrubDictFromConfig(config)) | ||
localLogger := zerolog.New(scrubLogger).With().Str("ext", "main").Str("separator", "-").Timestamp().Logger() | ||
loglevel := getDebugLevel(config, &localLogger) | ||
debugLogger := localLogger.Level(loglevel) | ||
return &debugLogger | ||
} | ||
} |
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
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
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,44 @@ | ||
import { runSnykCLI } from '../util/runSnykCLI'; | ||
import { createProjectFromWorkspace } from '../util/createProject'; | ||
|
||
jest.setTimeout(1000 * 60); | ||
|
||
describe('debug log', () => { | ||
it('redacts token from env var', async () => { | ||
const project = await createProjectFromWorkspace('cocoapods-app'); | ||
const token = 'mytoken'; | ||
|
||
const { stderr } = await runSnykCLI('test -d', { | ||
cwd: project.path(), | ||
env: { | ||
...process.env, | ||
SNYK_DISABLE_ANALYTICS: '1', | ||
DEBUG: '*', | ||
SNYK_LOG_LEVEL: 'trace', | ||
SNYK_TOKEN: token, | ||
}, | ||
}); | ||
|
||
expect(stderr).not.toContain(token); | ||
}); | ||
|
||
it('redacts token from config file', async () => { | ||
const project = await createProjectFromWorkspace('cocoapods-app'); | ||
|
||
const config = await runSnykCLI('config get api'); | ||
const expectedToken = config.stdout.trim(); | ||
|
||
const { stderr } = await runSnykCLI('test -d', { | ||
cwd: project.path(), | ||
env: { | ||
...process.env, | ||
SNYK_DISABLE_ANALYTICS: '1', | ||
DEBUG: '*', | ||
SNYK_LOG_LEVEL: 'trace', | ||
}, | ||
}); | ||
|
||
expect(expectedToken).not.toBeFalsy(); | ||
expect(stderr).not.toContain(expectedToken); | ||
}); | ||
}); |