Skip to content

Commit

Permalink
refactor(profile): Remove Admin API key from Profile
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Thurman committed Feb 27, 2020
1 parent f56f91f commit 64442bd
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 37 deletions.
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 64442bd

Please sign in to comment.