Skip to content

Commit fe12c36

Browse files
[AGENTCFG-1] The diagnose command should error if an API key is not configured (#46942)
### What does this PR do? When running the `diagnose` command without an API key configured the command exit successfully saying that no check where run. We want to fail gracefully with an error message. ### Motivation ### Describe how you validated your changes Added a unit test, and ran the agent with/without an api key set. The error appears when running the `diagnose` command without an api key. ### Additional Notes Co-authored-by: rahul.kaukuntla <rahul.kaukuntla@datadoghq.com>
1 parent dafa3b7 commit fe12c36

3 files changed

Lines changed: 33 additions & 0 deletions

File tree

cmd/agent/subcommands/diagnose/command.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package diagnose
99
import (
1010
"bytes"
1111
"encoding/json"
12+
"errors"
1213
"fmt"
1314
"os"
1415
"sort"
@@ -417,6 +418,11 @@ func cmdDiagnose(cliParams *cliParams,
417418
return nil
418419
}
419420

421+
// API key is required to run diagnose; without it no checks can run and the command would exit successfully with no useful output
422+
if !config.IsConfigured("api_key") {
423+
return errors.New("no API key configured: diagnose requires an API key to run checks. Set the API key in datadog.yaml or use the DD_API_KEY environment variable")
424+
}
425+
420426
// Run command
421427
var err error
422428
var result *diagnose.Result

cmd/agent/subcommands/diagnose/command_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
package diagnose
77

88
import (
9+
"os"
10+
"path/filepath"
911
"testing"
1012

13+
"github.com/spf13/cobra"
1114
"github.com/stretchr/testify/require"
1215

1316
"github.com/DataDog/datadog-agent/cmd/agent/command"
@@ -23,6 +26,25 @@ func TestDiagnoseCommand(t *testing.T) {
2326
func(_ *cliParams, _ core.BundleParams) {})
2427
}
2528

29+
func TestDiagnoseCommandFailsWithoutAPIKey(t *testing.T) {
30+
t.Setenv("DD_API_KEY", "")
31+
32+
dir := t.TempDir()
33+
configPath := filepath.Join(dir, "datadog.yaml")
34+
require.NoError(t, os.WriteFile(configPath, []byte("hostname: test\n"), 0o600))
35+
36+
root := &cobra.Command{Use: "agent"}
37+
for _, c := range Commands(&command.GlobalParams{ConfFilePath: dir}) {
38+
root.AddCommand(c)
39+
}
40+
root.SetArgs([]string{"diagnose"})
41+
42+
err := root.Execute()
43+
require.Error(t, err)
44+
require.Contains(t, err.Error(), "no API key configured")
45+
require.Contains(t, err.Error(), "DD_API_KEY")
46+
}
47+
2648
func TestShowMetadataV5Command(t *testing.T) {
2749
fxutil.TestOneShotSubcommand(t,
2850
Commands(&command.GlobalParams{}),
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
The diagnose command now returns an error if an API key is not configured.
5+

0 commit comments

Comments
 (0)