1package awsds 2 3import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 8 "github.com/grafana/grafana-plugin-sdk-go/backend" 9) 10 11const defaultRegion = "default" 12 13type AuthType int 14 15const ( 16 AuthTypeDefault AuthType = iota 17 AuthTypeSharedCreds 18 AuthTypeKeys 19 AuthTypeEC2IAMRole 20) 21 22func (at AuthType) String() string { 23 switch at { 24 case AuthTypeDefault: 25 return "default" 26 case AuthTypeSharedCreds: 27 return "credentials" 28 case AuthTypeKeys: 29 return "keys" 30 case AuthTypeEC2IAMRole: 31 return "ec2_iam_role" 32 default: 33 panic(fmt.Sprintf("Unrecognized auth type %d", at)) 34 } 35} 36 37func ToAuthType(authType string) AuthType { 38 switch authType { 39 case "credentials", "sharedCreds": 40 return AuthTypeSharedCreds 41 case "keys": 42 return AuthTypeKeys 43 case "default": 44 return AuthTypeDefault 45 case "ec2_iam_role": 46 return AuthTypeEC2IAMRole 47 case "arn": 48 return AuthTypeDefault 49 default: 50 panic(fmt.Sprintf("Unrecognized auth type %s", authType)) 51 } 52} 53 54// MarshalJSON marshals the enum as a quoted json string 55func (at *AuthType) MarshalJSON() ([]byte, error) { 56 buffer := bytes.NewBufferString(`"`) 57 buffer.WriteString(at.String()) 58 buffer.WriteString(`"`) 59 return buffer.Bytes(), nil 60} 61 62// UnmarshalJSON unmashals a quoted json string to the enum value 63func (at *AuthType) UnmarshalJSON(b []byte) error { 64 var j string 65 err := json.Unmarshal(b, &j) 66 if err != nil { 67 return err 68 } 69 switch j { 70 case "credentials": // Old name 71 fallthrough 72 case "sharedCreds": 73 *at = AuthTypeSharedCreds 74 case "keys": 75 *at = AuthTypeKeys 76 case "ec2_iam_role": 77 *at = AuthTypeEC2IAMRole 78 case "default": 79 fallthrough 80 default: 81 *at = AuthTypeDefault // For old 'arn' option 82 } 83 return nil 84} 85 86// DatasourceSettings holds basic connection info 87type AWSDatasourceSettings struct { 88 Profile string `json:"profile"` 89 Region string `json:"region"` 90 AuthType AuthType `json:"authType"` 91 AssumeRoleARN string `json:"assumeRoleARN"` 92 ExternalID string `json:"externalId"` 93 94 // Override the client endpoint 95 Endpoint string `json:"endpoint"` 96 97 //go:deprecated Use Region instead 98 DefaultRegion string `json:"defaultRegion"` 99 100 // Loaded from DecryptedSecureJSONData (not the json object) 101 AccessKey string `json:"-"` 102 SecretKey string `json:"-"` 103} 104 105// LoadSettings will read and validate Settings from the DataSourceConfg 106func (s *AWSDatasourceSettings) Load(config backend.DataSourceInstanceSettings) error { 107 if config.JSONData != nil && len(config.JSONData) > 1 { 108 if err := json.Unmarshal(config.JSONData, s); err != nil { 109 return fmt.Errorf("could not unmarshal DatasourceSettings json: %w", err) 110 } 111 } 112 113 if s.Region == defaultRegion || s.Region == "" { 114 s.Region = s.DefaultRegion 115 } 116 117 if s.Profile == "" { 118 s.Profile = config.Database // legacy support (only for cloudwatch?) 119 } 120 121 s.AccessKey = config.DecryptedSecureJSONData["accessKey"] 122 s.SecretKey = config.DecryptedSecureJSONData["secretKey"] 123 124 return nil 125} 126