athenaconfiguration.go 5.5 KB

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