Skip to content

Commit

Permalink
Merge pull request #56 from newrelic/jthurman/key-cleanup
Browse files Browse the repository at this point in the history
Remove Admin API Key
  • Loading branch information
Jonathan Thurman authored Feb 27, 2020
2 parents 896db7b + 64442bd commit 22a800c
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 40 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/llorllale/go-gitlint v0.0.0-20190914155841-58c0b8cef0e5
github.com/mattn/go-runewidth v0.0.8 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/newrelic/newrelic-client-go v0.10.1
github.com/newrelic/newrelic-client-go v0.11.0
github.com/remyoudompheng/go-misc v0.0.0-20190427085024-2d6ac652a50e
github.com/sirupsen/logrus v1.4.2
github.com/spf13/cobra v0.0.5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ github.com/mozilla/tls-observatory v0.0.0-20190404164649-a3c1b6cfecfd/go.mod h1:
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/nbutton23/zxcvbn-go v0.0.0-20180912185939-ae427f1e4c1d h1:AREM5mwr4u1ORQBMvzfzBgpsctsbQikCVpvC+tX285E=
github.com/nbutton23/zxcvbn-go v0.0.0-20180912185939-ae427f1e4c1d/go.mod h1:o96djdrsSGy3AWPyBgZMAGfxZNfgntdJG+11KU4QvbU=
github.com/newrelic/newrelic-client-go v0.10.1 h1:jCcyxydCmyG+vXjvDQvYU3fgtGx5t35SHvSs0V8ZzBo=
github.com/newrelic/newrelic-client-go v0.10.1/go.mod h1:cy6GDp11OfIj8Wfg0jfzDycso91NcojANZUqnjU9+QM=
github.com/newrelic/newrelic-client-go v0.11.0 h1:zPd9U+LCff+oK2BkrjXor7JzAz0+Z1XUt/dlsmSZVqI=
github.com/newrelic/newrelic-client-go v0.11.0/go.mod h1:cy6GDp11OfIj8Wfg0jfzDycso91NcojANZUqnjU9+QM=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.10.1 h1:q/mM8GF/n0shIN8SaAZ0V+jnLPzen6WIVZdiwrRlMlo=
Expand Down
11 changes: 2 additions & 9 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ func CreateNRClient(cfg *config.Config, creds *credentials.Credentials) (*newrel
var (
err error
personalAPIKey string
adminAPIKey string
region string
)

Expand All @@ -24,15 +23,13 @@ func CreateNRClient(cfg *config.Config, creds *credentials.Credentials) (*newrel
defProfile = applyOverrides(defProfile)

if defProfile != nil {
adminAPIKey = defProfile.AdminAPIKey
personalAPIKey = defProfile.PersonalAPIKey
region = defProfile.Region
} else {
return nil, fmt.Errorf("invalid profile name: '%s'", creds.DefaultProfile)
}

nrClient, err := newrelic.New(
newrelic.ConfigAPIKey(adminAPIKey),
newrelic.ConfigPersonalAPIKey(personalAPIKey),
newrelic.ConfigLogLevel(cfg.LogLevel),
newrelic.ConfigRegion(region),
Expand All @@ -44,12 +41,12 @@ func CreateNRClient(cfg *config.Config, creds *credentials.Credentials) (*newrel
return nrClient, nil
}

// applyOverrides reads Profile info out of the Environment to override config
func applyOverrides(p *credentials.Profile) *credentials.Profile {
envAPIKey := os.Getenv("NEWRELIC_PERSONAL_API_KEY")
envAdminAPIKey := os.Getenv("NEWRELIC_ADMIN_API_KEY")
envRegion := os.Getenv("NEWRELIC_REGION")

if envAPIKey == "" && envAdminAPIKey == "" && envRegion == "" {
if envAPIKey == "" && envRegion == "" {
return p
}

Expand All @@ -65,10 +62,6 @@ func applyOverrides(p *credentials.Profile) *credentials.Profile {
out.PersonalAPIKey = envAPIKey
}

if envAdminAPIKey != "" {
out.AdminAPIKey = envAdminAPIKey
}

if envRegion != "" {
out.Region = envRegion
}
Expand Down
9 changes: 3 additions & 6 deletions internal/credentials/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ var (
profileName string
region string
apiKey string
adminAPIKey string
)

// Command is the base command for managing profiles
Expand All @@ -29,10 +28,10 @@ var credentialsAdd = &cobra.Command{
The describe-deployments command performs a search for New Relic APM
deployments.
`,
Example: "newrelic credentials add -n <profileName> -r <region> --apiKey <apiKey> --adminAPIKey <adminAPIKey>",
Example: "newrelic credentials add -n <profileName> -r <region> --apiKey <apiKey>",
Run: func(cmd *cobra.Command, args []string) {
WithCredentials(func(creds *Credentials) {
err := creds.AddProfile(profileName, region, apiKey, adminAPIKey)
err := creds.AddProfile(profileName, region, apiKey)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -117,11 +116,9 @@ func init() {
credentialsAdd.Flags().StringVarP(&profileName, "profileName", "n", "", "The profile name to add")
credentialsAdd.Flags().StringVarP(&region, "region", "r", "", "us or eu region")
credentialsAdd.Flags().StringVarP(&apiKey, "apiKey", "", "", "Personal API key")
credentialsAdd.Flags().StringVarP(&adminAPIKey, "adminAPIKey", "", "", "Admin API key for REST v2")
credentialsAdd.MarkFlagRequired("profileName")
credentialsAdd.MarkFlagRequired("region")
// credentialsAdd.MarkFlagRequired("apiKey")
// credentialsAdd.MarkFlagRequired("adminAPIKey")
credentialsAdd.MarkFlagRequired("apiKey")

// Default
Command.AddCommand(credentialsDefault)
Expand Down
22 changes: 8 additions & 14 deletions internal/credentials/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (c *Credentials) profileExists(profileName string) bool {
}

// AddProfile adds a new profile to the credentials file.
func (c *Credentials) AddProfile(profileName, region, apiKey, adminAPIKey string) error {
func (c *Credentials) AddProfile(profileName, region, apiKey string) error {

if c.profileExists(profileName) {
return fmt.Errorf("profile with name %s already exists", profileName)
Expand All @@ -78,7 +78,6 @@ func (c *Credentials) AddProfile(profileName, region, apiKey, adminAPIKey string
p := Profile{
Region: region,
PersonalAPIKey: apiKey,
AdminAPIKey: adminAPIKey,
}

c.Profiles[profileName] = p
Expand Down Expand Up @@ -150,10 +149,9 @@ func (c *Credentials) List() {
defer color.Unset()
colorMuted := color.New(color.FgHiBlack).SprintFunc()

nameLen := 4 // Name
keyLen := 8 // <hidden>
adminKeyLen := 8 // <hidden>
regionLen := 6 // Region
nameLen := 4 // Name
keyLen := 8 // <hidden>
regionLen := 6 // Region

// Find lengths for pretty printing
for k, v := range c.Profiles {
Expand All @@ -177,9 +175,9 @@ func (c *Credentials) List() {

nameLen += len(defaultProfileString)

format := fmt.Sprintf("%%-%ds %%-%ds %%-%ds %%-%ds\n", nameLen, regionLen, keyLen, adminKeyLen)
fmt.Printf(format, "Name", "Region", "API key", "Admin API key")
fmt.Printf(format, strings.Repeat("-", nameLen), strings.Repeat("-", regionLen), strings.Repeat("-", keyLen), strings.Repeat("-", adminKeyLen))
format := fmt.Sprintf("%%-%ds %%-%ds %%-%ds\n", nameLen, regionLen, keyLen)
fmt.Printf(format, "Name", "Region", "API key")
fmt.Printf(format, strings.Repeat("-", nameLen), strings.Repeat("-", regionLen), strings.Repeat("-", keyLen))
// Print them out
for k, v := range c.Profiles {
name := k
Expand All @@ -191,11 +189,7 @@ func (c *Credentials) List() {
key = v.PersonalAPIKey
}

adminKey := colorMuted(hiddenKeyString)
if showKeys {
adminKey = v.AdminAPIKey
}
fmt.Printf(format, name, v.Region, key, adminKey)
fmt.Printf(format, name, v.Region, key)
}
fmt.Println("")
}
5 changes: 2 additions & 3 deletions internal/credentials/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ const DefaultProfileFile = "default-profile"

// Profile contains data of a single profile
type Profile struct {
AdminAPIKey string `mapstructure:"adminAPIKey" json:"adminAPIKey,omitempty"` // For accessing New Relic (Rest v2)
PersonalAPIKey string `mapstructure:"apiKey" json:"apiKey,omitempty"` // For accessing New Relic GraphQL resources
Region string `mapstructure:"region" json:"region,omitempty"` // Region to use for New Relic resources
PersonalAPIKey string `mapstructure:"apiKey" json:"apiKey,omitempty"` // For accessing New Relic GraphQL resources
Region string `mapstructure:"region" json:"region,omitempty"` // Region to use for New Relic resources
}

// LoadProfiles reads the credential profiles from the default path.
Expand Down
8 changes: 3 additions & 5 deletions internal/credentials/profiles_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@ func TestCredentials(t *testing.T) {
assert.Equal(t, c.ConfigDirectory, f)

// Create an initial profile to work with
err = c.AddProfile("testCase1", "us", "apiKeyGoesHere", "anotherApiKeyGoesHere")
err = c.AddProfile("testCase1", "us", "apiKeyGoesHere")
assert.NoError(t, err)
assert.Equal(t, len(c.Profiles), 1)
assert.Equal(t, c.Profiles["testCase1"].Region, "us")
assert.Equal(t, c.Profiles["testCase1"].PersonalAPIKey, "apiKeyGoesHere")
assert.Equal(t, c.Profiles["testCase1"].AdminAPIKey, "anotherApiKeyGoesHere")
assert.Equal(t, c.DefaultProfile, "")

// Set the default profile to the only one we've got
Expand All @@ -58,18 +57,17 @@ func TestCredentials(t *testing.T) {
assert.Equal(t, c.DefaultProfile, "testCase1")

// Adding a profile with the same name should result in an error
err = c.AddProfile("testCase1", "us", "foot", "hand")
err = c.AddProfile("testCase1", "us", "foot")
assert.Error(t, err)
assert.Equal(t, len(c.Profiles), 1)
assert.True(t, c.profileExists("testCase1"))

// Create a second profile to work with
err = c.AddProfile("testCase2", "us", "apiKeyGoesHere", "anotherApiKeyGoesHere")
err = c.AddProfile("testCase2", "us", "apiKeyGoesHere")
assert.NoError(t, err)
assert.Equal(t, len(c.Profiles), 2)
assert.Equal(t, c.Profiles["testCase2"].Region, "us")
assert.Equal(t, c.Profiles["testCase2"].PersonalAPIKey, "apiKeyGoesHere")
assert.Equal(t, c.Profiles["testCase2"].AdminAPIKey, "anotherApiKeyGoesHere")

// Set the default profile to the new one
err = c.SetDefaultProfile("testCase2")
Expand Down

0 comments on commit 22a800c

Please sign in to comment.