Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide license key in logs #21

Merged
merged 4 commits into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/nri-prometheus/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func loadConfig() (*scraper.Config, error) {
}

if scraperCfg.MetricAPIURL == "" {
scraperCfg.MetricAPIURL = determineMetricAPIURL(scraperCfg.LicenseKey)
scraperCfg.MetricAPIURL = determineMetricAPIURL(string(scraperCfg.LicenseKey))
}

return &scraperCfg, nil
Expand Down
21 changes: 18 additions & 3 deletions internal/cmd/scraper/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
type Config struct {
ConfigFile string
MetricAPIURL string `mapstructure:"metric_api_url"`
LicenseKey string `mapstructure:"license_key"`
LicenseKey LicenseKey `mapstructure:"license_key"`
ClusterName string `mapstructure:"cluster_name"`
Debug bool `mapstructure:"debug"`
Verbose bool `mapstructure:"verbose"`
Expand All @@ -48,6 +48,21 @@ type Config struct {
EmitterInsecureSkipVerify bool `mapstructure:"emitter_insecure_skip_verify" default:"false"`
}

const maskedLicenseKey = "****"

// LicenseKey is a New Relic license key that will be masked when printed using standard formatters
type LicenseKey string

// String ensures that the LicenseKey will be masked in functions like fmt.Println(licenseKey)
func (l LicenseKey) String() string {
return maskedLicenseKey
}

// GoString ensures that the LicenseKey will be masked in functions like fmt.Printf("%#v", licenseKey)
func (l LicenseKey) GoString() string {
return maskedLicenseKey
}

// Number of /metrics targets that can be fetched in parallel
const maxTargetConnections = 4

Expand Down Expand Up @@ -186,7 +201,7 @@ func Run(cfg *Config) error {
}

harvesterOpts := []func(*telemetry.Config){
telemetry.ConfigAPIKey(cfg.LicenseKey),
telemetry.ConfigAPIKey(string(cfg.LicenseKey)),
telemetry.ConfigBasicErrorLogger(os.Stdout),
integration.TelemetryHarvesterWithMetricsURL(cfg.MetricAPIURL),
integration.TelemetryHarvesterWithHarvestPeriod(hTime),
Expand Down Expand Up @@ -218,7 +233,7 @@ func Run(cfg *Config) error {
// Transport to `integration.licenseKeyRoundTripper`.
harvesterOpts = append(
harvesterOpts,
integration.TelemetryHarvesterWithLicenseKeyRoundTripper(cfg.LicenseKey),
integration.TelemetryHarvesterWithLicenseKeyRoundTripper(string(cfg.LicenseKey)),
)

if cfg.Verbose {
Expand Down
88 changes: 88 additions & 0 deletions internal/cmd/scraper/scraper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Package scraper ...
// Copyright 2019 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package scraper

import (
"bytes"
"fmt"
"strings"
"testing"

"github.com/stretchr/testify/require"

"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)

func TestLicenseKeyMasking(t *testing.T) {

const licenseKeyString = "secret"
licenseKey := LicenseKey(licenseKeyString)

t.Run("Masks licenseKey in fmt.Sprintf (which uses same logic as Printf)", func(t *testing.T) {
masked := fmt.Sprintf("%s", licenseKey)
assert.Equal(t, masked, maskedLicenseKey)
})

t.Run("Masks licenseKey in fmt.Sprint (which uses same logic as Print)", func(t *testing.T) {
masked := fmt.Sprint(licenseKey)
assert.Equal(t, masked, maskedLicenseKey)
})

t.Run("Masks licenseKey in %#v formatting", func(t *testing.T) {
masked := fmt.Sprintf("%#v", licenseKey)
if strings.Contains(masked, licenseKeyString) {
t.Error("found licenseKey in formatted string")
}
if !strings.Contains(masked, maskedLicenseKey) {
t.Error("could not find masked password in formatted string")
}
})

t.Run("Able to convert licenseKey back to string", func(t *testing.T) {
unmasked := string(licenseKey)
assert.Equal(t, licenseKeyString, unmasked)
})
}

func TestLogrusDebugPrintMasksLicenseKey(t *testing.T) {

const licenseKey = "SECRET_LICENSE_KEY"

cfg := Config{
LicenseKey: licenseKey,
}

var b bytes.Buffer

logrus.SetOutput(&b)
logrus.SetLevel(logrus.DebugLevel)
logrus.Debugf("Config: %#v", cfg)

msg := b.String()
if strings.Contains(msg, licenseKey) {
t.Error("Log output contains the license key")
}
if !strings.Contains(msg, maskedLicenseKey) {
t.Error("Log output does not contain the masked licenseKey")
}
}

func TestConfigParseWithCustomType(t *testing.T) {

const licenseKey = "MY_LICENSE_KEY"
cfgStr := []byte(fmt.Sprintf(`LICENSE_KEY: %s`, licenseKey))

vip := viper.New()
vip.SetConfigType("yaml")
err := vip.ReadConfig(bytes.NewBuffer(cfgStr))
require.NoError(t, err)

var cfg Config
err = vip.Unmarshal(&cfg)
require.NoError(t, err)

assert.Equal(t, licenseKey, string(cfg.LicenseKey))
}
1 change: 0 additions & 1 deletion internal/integration/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@ const (
metricType_GAUGE metricType = "gauge"
metricType_SUMMARY metricType = "summary"
metricType_HISTOGRAM metricType = "histogram"
metricType_UNTYPED metricType = "untyped"
)

// Metric represents a Prometheus metric.
Expand Down