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

NR-281256 [ NEW_RELIC_LICENSE_KEY Priority check ] #219

Merged
merged 4 commits into from
Jun 20, 2024
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
14 changes: 12 additions & 2 deletions checks/sanity_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,24 @@ func sanityCheck(ctx context.Context, conf *config.Configuration, res *api.Regis

envKeyExists := util.EnvVarExists("NEW_RELIC_LICENSE_KEY")
var timeout = 1 * time.Second

//Secret Manager
ctxSecret, cancelSecret := context.WithTimeout(ctx, timeout)
defer cancelSecret()
isSecretConfigured := credentials.IsSecretConfigured(ctxSecret, conf)

isSecretConfigured := false
if conf.LicenseKeySecretId != "" {
isSecretConfigured = credentials.IsSecretConfigured(ctxSecret, conf)
}

// SSM Parameter
ctxSSMParameter, cancelSSMParameter := context.WithTimeout(ctx, timeout)
defer cancelSSMParameter()

isSSMParameterConfigured := false
if conf.LicenseKeySSMParameterName != "" {
isSSMParameterConfigured = credentials.IsSSMParameterConfigured(ctxSSMParameter, conf)
}


if isSecretConfigured && envKeyExists {
return fmt.Errorf("There is both a AWS Secrets Manager secret and a NEW_RELIC_LICENSE_KEY environment variable set. Recommend removing the NEW_RELIC_LICENSE_KEY environment variable and using the AWS Secrets Manager secret.")
Expand All @@ -55,5 +61,9 @@ func sanityCheck(ctx context.Context, conf *config.Configuration, res *api.Regis
return fmt.Errorf("There is both a AWS Secrets Manager secret and a AWS Parameter Store parameter set. Recommend using just one.")
}

if !envKeyExists && !isSecretConfigured && !isSSMParameterConfigured {
util.Debugln("No configured license key found, attempting fallback to default AWS Secrets Manager secret with NEW_RELIC_LICENSE_KEY.")
}

return nil
}
86 changes: 42 additions & 44 deletions checks/sanity_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (m mockSecretManager) GetSecretValueWithContext(_ context.Context, input *s

type mockSSM struct {
ssmiface.SSMAPI
validParameters []string
validParameters []string
IsParameterCalled bool
}

Expand Down Expand Up @@ -197,48 +197,46 @@ func TestSanityCheck(t *testing.T) {
}
}


func TestSanityCheckSSMParameter(t *testing.T) {
ctx := context.Background()

tests := []struct {
name string
ssmParameterName string
validParameters []string
expectParamCalled bool
expectedErr error
}{
{
name: "SSM Parameter configured",
ssmParameterName: "parameter",
validParameters: []string{"parameter"},
expectParamCalled: true,
expectedErr: nil,
},
{
name: "SSM Parameter not configured",
expectParamCalled: false,
expectedErr: nil,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
conf := config.Configuration{
LicenseKeySSMParameterName: tc.ssmParameterName,
}

mSSM := &mockSSM{
validParameters: tc.validParameters,
}

credentials.OverrideSSM(mSSM)

err := sanityCheck(ctx, &conf, &api.RegistrationResponse{}, runtimeConfig{})

assert.Equal(t, tc.expectedErr, err, "Error from sanityCheck")
assert.Equal(t, tc.expectParamCalled, mSSM.IsParameterCalled, "Error in expected SSM parameter check")
})
}
}
ctx := context.Background()

tests := []struct {
name string
ssmParameterName string
validParameters []string
expectParamCalled bool
expectedErr error
}{
{
name: "SSM Parameter configured",
ssmParameterName: "parameter",
validParameters: []string{"parameter"},
expectParamCalled: true,
expectedErr: nil,
},
{
name: "SSM Parameter not configured",
expectParamCalled: false,
expectedErr: nil,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
conf := config.Configuration{
LicenseKeySSMParameterName: tc.ssmParameterName,
}

mSSM := &mockSSM{
validParameters: tc.validParameters,
}

credentials.OverrideSSM(mSSM)

err := sanityCheck(ctx, &conf, &api.RegistrationResponse{}, runtimeConfig{})

assert.Equal(t, tc.expectedErr, err, "Error from sanityCheck")
assert.Equal(t, tc.expectParamCalled, mSSM.IsParameterCalled, "Error in expected SSM parameter check")
})
}
}
2 changes: 1 addition & 1 deletion credentials/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func GetNewRelicLicenseKey(ctx context.Context, conf *config.Configuration) (str
return envLicenseKey, nil
}

util.Debugln("No configured license key found, attempting fallbacks")
util.Debugln("No configured license key found, attempting fallbacks to default")

licenseKey, err := tryLicenseKeyFromSecret(ctx, defaultSecretId)
if err == nil {
Expand Down
Loading