Skip to content

Commit 9ac6da6

Browse files
author
Jonathan Thurman
authored
Merge pull request #179 from newrelic/jthurman/appName
chore(client): Sanitize Region override
2 parents 37f2ee5 + 06b5828 commit 9ac6da6

File tree

2 files changed

+118
-10
lines changed

2 files changed

+118
-10
lines changed

internal/client/client.go

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ import (
55
"fmt"
66
"os"
77

8-
"github.com/newrelic/newrelic-cli/internal/config"
9-
"github.com/newrelic/newrelic-cli/internal/credentials"
8+
log "github.com/sirupsen/logrus"
9+
1010
"github.com/newrelic/newrelic-client-go/newrelic"
1111
"github.com/newrelic/newrelic-client-go/pkg/region"
12+
13+
"github.com/newrelic/newrelic-cli/internal/config"
14+
"github.com/newrelic/newrelic-cli/internal/credentials"
1215
)
1316

1417
var (
@@ -64,11 +67,8 @@ func applyOverrides(p *credentials.Profile) *credentials.Profile {
6467
return p
6568
}
6669

67-
var out credentials.Profile
68-
69-
if p == nil {
70-
out = credentials.Profile{}
71-
} else {
70+
out := credentials.Profile{}
71+
if p != nil {
7272
out = *p
7373
}
7474

@@ -77,9 +77,30 @@ func applyOverrides(p *credentials.Profile) *credentials.Profile {
7777
}
7878

7979
if envRegion != "" {
80-
reg, err := region.Parse(envRegion)
81-
if err == nil {
82-
out.Region = reg
80+
var err error
81+
out.Region, err = region.Parse(envRegion)
82+
83+
if err != nil {
84+
switch err.(type) {
85+
case region.UnknownError:
86+
log.Errorf("error parsing NEW_RELIC_REGION: %s", err)
87+
// Ignore the override if they have a default on the profile
88+
if p.Region != "" {
89+
var e2 error
90+
out.Region, e2 = region.Parse(p.Region.String())
91+
if e2 != nil {
92+
log.Errorf("error parsing default profile: %s", e2)
93+
out.Region = region.Default
94+
}
95+
} else {
96+
out.Region = region.Default
97+
}
98+
log.Errorf("using region %s", out.Region.String())
99+
case region.UnknownUsingDefaultError:
100+
log.Error(err)
101+
default:
102+
log.Fatalf("unknown error: %v", err)
103+
}
83104
}
84105
}
85106

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// +build integration
2+
3+
package client
4+
5+
import (
6+
"io/ioutil"
7+
"os"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
12+
"github.com/newrelic/newrelic-client-go/pkg/region"
13+
14+
"github.com/newrelic/newrelic-cli/internal/credentials"
15+
)
16+
17+
var overrideEnvVars = []string{
18+
"NEW_RELIC_API_KEY",
19+
"NEW_RELIC_REGION",
20+
}
21+
22+
func TestApplyOverrides(t *testing.T) {
23+
// Do not run this in parallel, we are messing with the environment
24+
25+
f, err := ioutil.TempDir("/tmp", "newrelic")
26+
assert.NoError(t, err)
27+
defer os.RemoveAll(f)
28+
29+
// Initialize the new configuration directory
30+
c, err := credentials.LoadCredentials(f)
31+
assert.NoError(t, err)
32+
33+
// Create an initial profile to work with
34+
err = c.AddProfile("testCase1", "us", "apiKeyGoesHere")
35+
assert.NoError(t, err)
36+
p := c.Profiles["testCase1"]
37+
assert.NotNil(t, p)
38+
39+
// Clear env vars we are going to mess with, and reset on exit
40+
for _, v := range overrideEnvVars {
41+
if val, ok := os.LookupEnv(v); ok {
42+
defer os.Setenv(v, val)
43+
os.Unsetenv(v)
44+
}
45+
}
46+
47+
// Default case (no overrides)
48+
p2 := applyOverrides(&p)
49+
assert.NotNil(t, p2)
50+
assert.Equal(t, p.APIKey, p2.APIKey)
51+
assert.Equal(t, p.Region, p2.Region)
52+
53+
// Override just the API Key
54+
os.Setenv("NEW_RELIC_API_KEY", "anotherAPIKey")
55+
p2 = applyOverrides(&p)
56+
assert.NotNil(t, p2)
57+
assert.Equal(t, "anotherAPIKey", p2.APIKey)
58+
assert.Equal(t, p.Region, p2.Region)
59+
60+
// Both
61+
os.Setenv("NEW_RELIC_REGION", "US")
62+
p2 = applyOverrides(&p)
63+
assert.NotNil(t, p2)
64+
assert.Equal(t, "anotherAPIKey", p2.APIKey)
65+
assert.Equal(t, region.US, p2.Region)
66+
67+
// Override just the REGION (valid)
68+
os.Unsetenv("NEW_RELIC_API_KEY")
69+
p2 = applyOverrides(&p)
70+
assert.NotNil(t, p2)
71+
assert.Equal(t, p.APIKey, p2.APIKey)
72+
assert.Equal(t, region.US, p2.Region)
73+
74+
// Region lowercase
75+
os.Setenv("NEW_RELIC_REGION", "eu")
76+
p2 = applyOverrides(&p)
77+
assert.NotNil(t, p2)
78+
assert.Equal(t, p.APIKey, p2.APIKey)
79+
assert.Equal(t, region.EU, p2.Region)
80+
81+
// Invalid REGION (Should be region.Default)
82+
os.Setenv("NEW_RELIC_REGION", "test")
83+
p2 = applyOverrides(&p)
84+
assert.NotNil(t, p2)
85+
assert.Equal(t, p.APIKey, p2.APIKey)
86+
assert.Equal(t, region.Default, p2.Region)
87+
}

0 commit comments

Comments
 (0)