athenaconfiguration.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. package aws
  2. import (
  3. "fmt"
  4. "github.com/opencost/opencost/core/pkg/opencost"
  5. "github.com/opencost/opencost/core/pkg/util/json"
  6. "github.com/opencost/opencost/pkg/cloud"
  7. )
  8. // AthenaConfiguration
  9. type AthenaConfiguration struct {
  10. Bucket string `json:"bucket"`
  11. Region string `json:"region"`
  12. Database string `json:"database"`
  13. Catalog string `json:"catalog"`
  14. Table string `json:"table"`
  15. Workgroup string `json:"workgroup"`
  16. Account string `json:"account"`
  17. Authorizer Authorizer `json:"authorizer"`
  18. // ResultReuseMaxAgeMinutes enables Athena query result reuse for this many minutes (0 = disabled).
  19. // When set, identical queries within the TTL window return cached results instead of re-scanning,
  20. // reducing cost for multi-cluster deployments where many clusters run the same CUR query.
  21. ResultReuseMaxAgeMinutes int32 `json:"resultReuseMaxAgeMinutes,omitempty"`
  22. }
  23. func (ac *AthenaConfiguration) Validate() error {
  24. // Validate Authorizer
  25. if ac.Authorizer == nil {
  26. return fmt.Errorf("AthenaConfiguration: missing Authorizer")
  27. }
  28. err := ac.Authorizer.Validate()
  29. if err != nil {
  30. return fmt.Errorf("AthenaConfiguration: %s", err)
  31. }
  32. // Validate base properties
  33. if ac.Bucket == "" {
  34. return fmt.Errorf("AthenaConfiguration: missing bucket")
  35. }
  36. if ac.Region == "" {
  37. return fmt.Errorf("AthenaConfiguration: missing region")
  38. }
  39. if ac.Database == "" {
  40. return fmt.Errorf("AthenaConfiguration: missing database")
  41. }
  42. if ac.Table == "" {
  43. return fmt.Errorf("AthenaConfiguration: missing table")
  44. }
  45. if ac.Account == "" {
  46. return fmt.Errorf("AthenaConfiguration: missing account")
  47. }
  48. return nil
  49. }
  50. func (ac *AthenaConfiguration) Equals(config cloud.Config) bool {
  51. if config == nil {
  52. return false
  53. }
  54. thatConfig, ok := config.(*AthenaConfiguration)
  55. if !ok {
  56. return false
  57. }
  58. if ac.Authorizer != nil {
  59. if !ac.Authorizer.Equals(thatConfig.Authorizer) {
  60. return false
  61. }
  62. } else {
  63. if thatConfig.Authorizer != nil {
  64. return false
  65. }
  66. }
  67. if ac.Bucket != thatConfig.Bucket {
  68. return false
  69. }
  70. if ac.Region != thatConfig.Region {
  71. return false
  72. }
  73. if ac.Database != thatConfig.Database {
  74. return false
  75. }
  76. if ac.Catalog != thatConfig.Catalog {
  77. return false
  78. }
  79. if ac.Table != thatConfig.Table {
  80. return false
  81. }
  82. if ac.Workgroup != thatConfig.Workgroup {
  83. return false
  84. }
  85. if ac.Account != thatConfig.Account {
  86. return false
  87. }
  88. if ac.ResultReuseMaxAgeMinutes != thatConfig.ResultReuseMaxAgeMinutes {
  89. return false
  90. }
  91. return true
  92. }
  93. func (ac *AthenaConfiguration) Sanitize() cloud.Config {
  94. return &AthenaConfiguration{
  95. Bucket: ac.Bucket,
  96. Region: ac.Region,
  97. Database: ac.Database,
  98. Catalog: ac.Catalog,
  99. Table: ac.Table,
  100. Workgroup: ac.Workgroup,
  101. Account: ac.Account,
  102. Authorizer: ac.Authorizer.Sanitize().(Authorizer),
  103. ResultReuseMaxAgeMinutes: ac.ResultReuseMaxAgeMinutes,
  104. }
  105. }
  106. func (ac *AthenaConfiguration) Key() string {
  107. return fmt.Sprintf("%s/%s", ac.Account, ac.Bucket)
  108. }
  109. func (ac *AthenaConfiguration) Provider() string {
  110. return opencost.AWSProvider
  111. }
  112. func (ac *AthenaConfiguration) UnmarshalJSON(b []byte) error {
  113. var f interface{}
  114. err := json.Unmarshal(b, &f)
  115. if err != nil {
  116. return err
  117. }
  118. fmap := f.(map[string]interface{})
  119. bucket, err := cloud.GetInterfaceValue[string](fmap, "bucket")
  120. if err != nil {
  121. return fmt.Errorf("AthenaConfiguration: UnmarshalJSON: %w", err)
  122. }
  123. ac.Bucket = bucket
  124. region, err := cloud.GetInterfaceValue[string](fmap, "region")
  125. if err != nil {
  126. return fmt.Errorf("AthenaConfiguration: UnmarshalJSON: %w", err)
  127. }
  128. ac.Region = region
  129. database, err := cloud.GetInterfaceValue[string](fmap, "database")
  130. if err != nil {
  131. return fmt.Errorf("AthenaConfiguration: UnmarshalJSON: %w", err)
  132. }
  133. ac.Database = database
  134. if _, ok := fmap["catalog"]; ok {
  135. catalog, err := cloud.GetInterfaceValue[string](fmap, "catalog")
  136. if err != nil {
  137. return fmt.Errorf("AthenaConfiguration: UnmarshalJSON: %w", err)
  138. }
  139. ac.Catalog = catalog
  140. }
  141. table, err := cloud.GetInterfaceValue[string](fmap, "table")
  142. if err != nil {
  143. return fmt.Errorf("AthenaConfiguration: UnmarshalJSON: %w", err)
  144. }
  145. ac.Table = table
  146. workgroup, err := cloud.GetInterfaceValue[string](fmap, "workgroup")
  147. if err != nil {
  148. return fmt.Errorf("AthenaConfiguration: UnmarshalJSON: %w", err)
  149. }
  150. ac.Workgroup = workgroup
  151. account, err := cloud.GetInterfaceValue[string](fmap, "account")
  152. if err != nil {
  153. return fmt.Errorf("AthenaConfiguration: UnmarshalJSON: %w", err)
  154. }
  155. ac.Account = account
  156. authAny, ok := fmap["authorizer"]
  157. if !ok {
  158. return fmt.Errorf("AthenaConfiguration: UnmarshalJSON: missing authorizer")
  159. }
  160. authorizer, err := cloud.AuthorizerFromInterface(authAny, SelectAuthorizerByType)
  161. if err != nil {
  162. return fmt.Errorf("AthenaConfiguration: UnmarshalJSON: %w", err)
  163. }
  164. ac.Authorizer = authorizer
  165. if _, ok := fmap["resultReuseMaxAgeMinutes"]; ok {
  166. resultReuseMaxAgeMinutes, err := cloud.GetInterfaceValue[float64](fmap, "resultReuseMaxAgeMinutes")
  167. if err != nil {
  168. return fmt.Errorf("AthenaConfiguration: UnmarshalJSON: %w", err)
  169. }
  170. ac.ResultReuseMaxAgeMinutes = int32(resultReuseMaxAgeMinutes)
  171. }
  172. return nil
  173. }
  174. // ConvertAwsAthenaInfoToConfig takes a legacy config and generates a Config based on the presence of properties to match
  175. // legacy behavior
  176. func ConvertAwsAthenaInfoToConfig(aai AwsAthenaInfo) cloud.KeyedConfig {
  177. if aai.IsEmpty() {
  178. return nil
  179. }
  180. var authorizer Authorizer
  181. if aai.ServiceKeyName == "" && aai.ServiceKeySecret == "" {
  182. authorizer = &ServiceAccount{}
  183. } else {
  184. authorizer = &AccessKey{
  185. ID: aai.ServiceKeyName,
  186. Secret: aai.ServiceKeySecret,
  187. }
  188. }
  189. // Wrap Authorizer with AssumeRole if MasterPayerArn is set
  190. if aai.MasterPayerARN != "" {
  191. authorizer = &AssumeRole{
  192. Authorizer: authorizer,
  193. RoleARN: aai.MasterPayerARN,
  194. }
  195. }
  196. var config cloud.KeyedConfig
  197. if aai.AthenaTable != "" || aai.AthenaDatabase != "" {
  198. config = &AthenaConfiguration{
  199. Bucket: aai.AthenaBucketName,
  200. Region: aai.AthenaRegion,
  201. Catalog: aai.AthenaCatalog,
  202. Database: aai.AthenaDatabase,
  203. Table: aai.AthenaTable,
  204. Workgroup: aai.AthenaWorkgroup,
  205. Account: aai.AccountID,
  206. Authorizer: authorizer,
  207. }
  208. } else {
  209. config = &S3Configuration{
  210. Bucket: aai.AthenaBucketName,
  211. Region: aai.AthenaRegion,
  212. Account: aai.AccountID,
  213. Authorizer: authorizer,
  214. }
  215. }
  216. return config
  217. }