/
settings.go
134 lines (116 loc) · 3.22 KB
/
settings.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package awsds
import (
"bytes"
"encoding/json"
"fmt"
"github.com/grafana/grafana-plugin-sdk-go/backend"
)
const defaultRegion = "default"
type AuthType int
const (
AuthTypeDefault AuthType = iota
AuthTypeSharedCreds
AuthTypeKeys
AuthTypeEC2IAMRole
AuthTypeGrafanaAssumeRole //cloud only
)
func (at AuthType) String() string {
switch at {
case AuthTypeDefault:
return "default"
case AuthTypeSharedCreds:
return "credentials"
case AuthTypeKeys:
return "keys"
case AuthTypeEC2IAMRole:
return "ec2_iam_role"
case AuthTypeGrafanaAssumeRole:
return "grafana_assume_role"
default:
panic(fmt.Sprintf("Unrecognized auth type %d", at))
}
}
func ToAuthType(authType string) (AuthType, error) {
switch authType {
case "credentials", "sharedCreds":
return AuthTypeSharedCreds, nil
case "keys":
return AuthTypeKeys, nil
case "default":
return AuthTypeDefault, nil
case "ec2_iam_role":
return AuthTypeEC2IAMRole, nil
case "arn":
return AuthTypeDefault, nil
case "grafana_assume_role":
return AuthTypeGrafanaAssumeRole, nil
default:
return AuthTypeDefault, fmt.Errorf("invalid auth type: %s", authType)
}
}
// MarshalJSON marshals the enum as a quoted json string
func (at *AuthType) MarshalJSON() ([]byte, error) {
buffer := bytes.NewBufferString(`"`)
buffer.WriteString(at.String())
buffer.WriteString(`"`)
return buffer.Bytes(), nil
}
// UnmarshalJSON unmashals a quoted json string to the enum value
func (at *AuthType) UnmarshalJSON(b []byte) error {
var j string
err := json.Unmarshal(b, &j)
if err != nil {
return err
}
switch j {
case "credentials": // Old name
fallthrough
case "sharedCreds":
*at = AuthTypeSharedCreds
case "keys":
*at = AuthTypeKeys
case "ec2_iam_role":
*at = AuthTypeEC2IAMRole
case "grafana_assume_role":
*at = AuthTypeGrafanaAssumeRole
case "default":
fallthrough
default:
*at = AuthTypeDefault // For old 'arn' option
}
return nil
}
// DatasourceSettings holds basic connection info
type AWSDatasourceSettings struct {
Profile string `json:"profile"`
Region string `json:"region"`
AuthType AuthType `json:"authType"`
AssumeRoleARN string `json:"assumeRoleARN"`
ExternalID string `json:"externalId"`
// Override the client endpoint
Endpoint string `json:"endpoint"`
//go:deprecated Use Region instead
DefaultRegion string `json:"defaultRegion"`
// Loaded from DecryptedSecureJSONData (not the json object)
AccessKey string `json:"-"`
SecretKey string `json:"-"`
SessionToken string `json:"-"`
}
// LoadSettings will read and validate Settings from the DataSourceConfg
func (s *AWSDatasourceSettings) Load(config backend.DataSourceInstanceSettings) error {
if config.JSONData != nil && len(config.JSONData) > 1 {
if err := json.Unmarshal(config.JSONData, s); err != nil {
return fmt.Errorf("could not unmarshal DatasourceSettings json: %w", err)
}
}
if s.Region == defaultRegion || s.Region == "" {
s.Region = s.DefaultRegion
}
if s.Profile == "" {
s.Profile = config.Database // legacy support (only for cloudwatch?)
}
s.AccessKey = config.DecryptedSecureJSONData["accessKey"]
s.SecretKey = config.DecryptedSecureJSONData["secretKey"]
s.SessionToken = config.DecryptedSecureJSONData["sessionToken"]
return nil
}