athenaconfiguration.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package aws
  2. import (
  3. "fmt"
  4. "github.com/aws/aws-sdk-go-v2/service/athena"
  5. "github.com/opencost/opencost/pkg/cloud/config"
  6. "github.com/opencost/opencost/pkg/util/json"
  7. )
  8. // AthenaConfiguration
  9. type AthenaConfiguration struct {
  10. Bucket string `json:"bucket"`
  11. Region string `json:"region"`
  12. Database string `json:"database"`
  13. Table string `json:"table"`
  14. Workgroup string `json:"workgroup"`
  15. Account string `json:"account"`
  16. Authorizer Authorizer `json:"authorizer"`
  17. }
  18. func (ac *AthenaConfiguration) Validate() error {
  19. // Validate Authorizer
  20. if ac.Authorizer == nil {
  21. return fmt.Errorf("AthenaConfiguration: missing Authorizer")
  22. }
  23. err := ac.Authorizer.Validate()
  24. if err != nil {
  25. return fmt.Errorf("AthenaConfiguration: %s", err)
  26. }
  27. // Validate base properties
  28. if ac.Bucket == "" {
  29. return fmt.Errorf("AthenaConfiguration: missing bucket")
  30. }
  31. if ac.Region == "" {
  32. return fmt.Errorf("AthenaConfiguration: missing region")
  33. }
  34. if ac.Database == "" {
  35. return fmt.Errorf("AthenaConfiguration: missing database")
  36. }
  37. if ac.Table == "" {
  38. return fmt.Errorf("AthenaConfiguration: missing table")
  39. }
  40. if ac.Account == "" {
  41. return fmt.Errorf("AthenaConfiguration: missing account")
  42. }
  43. return nil
  44. }
  45. func (ac *AthenaConfiguration) Equals(config config.Config) bool {
  46. if config == nil {
  47. return false
  48. }
  49. thatConfig, ok := config.(*AthenaConfiguration)
  50. if !ok {
  51. return false
  52. }
  53. if ac.Authorizer != nil {
  54. if !ac.Authorizer.Equals(thatConfig.Authorizer) {
  55. return false
  56. }
  57. } else {
  58. if thatConfig.Authorizer != nil {
  59. return false
  60. }
  61. }
  62. if ac.Bucket != thatConfig.Bucket {
  63. return false
  64. }
  65. if ac.Region != thatConfig.Region {
  66. return false
  67. }
  68. if ac.Database != thatConfig.Database {
  69. return false
  70. }
  71. if ac.Table != thatConfig.Table {
  72. return false
  73. }
  74. if ac.Workgroup != thatConfig.Workgroup {
  75. return false
  76. }
  77. if ac.Account != thatConfig.Account {
  78. return false
  79. }
  80. return true
  81. }
  82. func (ac *AthenaConfiguration) Sanitize() config.Config {
  83. return &AthenaConfiguration{
  84. Bucket: ac.Bucket,
  85. Region: ac.Region,
  86. Database: ac.Database,
  87. Table: ac.Table,
  88. Workgroup: ac.Workgroup,
  89. Account: ac.Account,
  90. Authorizer: ac.Authorizer.Sanitize().(Authorizer),
  91. }
  92. }
  93. func (ac *AthenaConfiguration) Key() string {
  94. return fmt.Sprintf("%s/%s", ac.Account, ac.Bucket)
  95. }
  96. func (ac *AthenaConfiguration) UnmarshalJSON(b []byte) error {
  97. var f interface{}
  98. err := json.Unmarshal(b, &f)
  99. if err != nil {
  100. return err
  101. }
  102. fmap := f.(map[string]interface{})
  103. bucket, err := config.GetInterfaceValue[string](fmap, "bucket")
  104. if err != nil {
  105. return fmt.Errorf("AthenaConfiguration: UnmarshalJSON: %s", err.Error())
  106. }
  107. ac.Bucket = bucket
  108. region, err := config.GetInterfaceValue[string](fmap, "region")
  109. if err != nil {
  110. return fmt.Errorf("AthenaConfiguration: UnmarshalJSON: %s", err.Error())
  111. }
  112. ac.Region = region
  113. database, err := config.GetInterfaceValue[string](fmap, "database")
  114. if err != nil {
  115. return fmt.Errorf("AthenaConfiguration: UnmarshalJSON: %s", err.Error())
  116. }
  117. ac.Database = database
  118. table, err := config.GetInterfaceValue[string](fmap, "table")
  119. if err != nil {
  120. return fmt.Errorf("AthenaConfiguration: UnmarshalJSON: %s", err.Error())
  121. }
  122. ac.Table = table
  123. workgroup, err := config.GetInterfaceValue[string](fmap, "workgroup")
  124. if err != nil {
  125. return fmt.Errorf("AthenaConfiguration: UnmarshalJSON: %s", err.Error())
  126. }
  127. ac.Workgroup = workgroup
  128. account, err := config.GetInterfaceValue[string](fmap, "account")
  129. if err != nil {
  130. return fmt.Errorf("AthenaConfiguration: UnmarshalJSON: %s", err.Error())
  131. }
  132. ac.Account = account
  133. authAny, ok := fmap["authorizer"]
  134. if !ok {
  135. return fmt.Errorf("AthenaConfiguration: UnmarshalJSON: missing authorizer")
  136. }
  137. authorizer, err := config.AuthorizerFromInterface(authAny, SelectAuthorizerByType)
  138. if err != nil {
  139. return fmt.Errorf("AthenaConfiguration: UnmarshalJSON: %s", err.Error())
  140. }
  141. ac.Authorizer = authorizer
  142. return nil
  143. }
  144. func (ac *AthenaConfiguration) GetAthenaClient() (*athena.Client, error) {
  145. cfg, err := ac.Authorizer.CreateAWSConfig(ac.Region)
  146. if err != nil {
  147. return nil, err
  148. }
  149. cli := athena.NewFromConfig(cfg)
  150. return cli, nil
  151. }
  152. // ConvertAwsAthenaInfoToConfig takes a legacy config and generates a Config based on the presence of properties to match
  153. // legacy behavior
  154. func ConvertAwsAthenaInfoToConfig(aai AwsAthenaInfo) config.KeyedConfig {
  155. if aai.IsEmpty() {
  156. return nil
  157. }
  158. var authorizer Authorizer
  159. if aai.ServiceKeyName == "" && aai.ServiceKeySecret == "" {
  160. authorizer = &ServiceAccount{}
  161. } else {
  162. authorizer = &AccessKey{
  163. ID: aai.ServiceKeyName,
  164. Secret: aai.ServiceKeySecret,
  165. }
  166. }
  167. // Wrap Authorizer with AssumeRole if MasterPayerArn is set
  168. if aai.MasterPayerARN != "" {
  169. authorizer = &AssumeRole{
  170. Authorizer: authorizer,
  171. RoleARN: aai.MasterPayerARN,
  172. }
  173. }
  174. var config config.KeyedConfig
  175. if aai.AthenaTable != "" || aai.AthenaDatabase != "" {
  176. config = &AthenaConfiguration{
  177. Bucket: aai.AthenaBucketName,
  178. Region: aai.AthenaRegion,
  179. Database: aai.AthenaDatabase,
  180. Table: aai.AthenaTable,
  181. Workgroup: aai.AthenaWorkgroup,
  182. Account: aai.AccountID,
  183. Authorizer: authorizer,
  184. }
  185. } else {
  186. config = &S3Configuration{
  187. Bucket: aai.AthenaBucketName,
  188. Region: aai.AthenaRegion,
  189. Account: aai.AccountID,
  190. Authorizer: authorizer,
  191. }
  192. }
  193. return config
  194. }