forked from DataDog/datadog-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
secret.go
76 lines (63 loc) · 1.99 KB
/
secret.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-2019 Datadog, Inc.
package app
import (
"encoding/json"
"fmt"
"os"
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/ninnemana/datadog-agent/cmd/agent/common"
"github.com/ninnemana/datadog-agent/pkg/api/util"
"github.com/ninnemana/datadog-agent/pkg/config"
"github.com/ninnemana/datadog-agent/pkg/secrets"
)
func init() {
AgentCmd.AddCommand(secretInfoCommand)
}
var secretInfoCommand = &cobra.Command{
Use: "secret",
Short: "Print information about decrypted secrets in configuration.",
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error {
if err := common.SetupConfigWithoutSecrets(confFilePath); err != nil {
fmt.Printf("unable to set up global agent configuration: %v", err)
return nil
}
if err := util.SetAuthToken(); err != nil {
fmt.Printf("%s", err)
return nil
}
if flagNoColor {
color.NoColor = true
}
if err := showSecretInfo(); err != nil {
fmt.Printf("%s", err)
return nil
}
return nil
},
}
func showSecretInfo() error {
c := util.GetClient(false)
apiConfigURL := fmt.Sprintf("https://localhost:%v/agent/secrets", config.Datadog.GetInt("cmd_port"))
r, err := util.DoGet(c, apiConfigURL)
if err != nil {
var errMap = make(map[string]string)
json.Unmarshal(r, errMap)
// If the error has been marshalled into a json object, check it and return it properly
if e, found := errMap["error"]; found {
return fmt.Errorf("%s", e)
}
return fmt.Errorf("Could not reach agent: %v\nMake sure the agent is running before requesting the runtime configuration and contact support if you continue having issues", err)
}
info := &secrets.SecretInfo{}
err = json.Unmarshal(r, info)
if err != nil {
return fmt.Errorf("Could not Unmarshal agent answer: %s", r)
}
info.Print(os.Stdout)
return nil
}