authorizer.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package oracle
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/opencost/opencost/pkg/cloud"
  6. "github.com/oracle/oci-go-sdk/v65/common"
  7. )
  8. const RawConfigProviderAuthorizerType = "OCIRawConfigProvider"
  9. // Authorizer provides which is used in when creating clients in the OCI SDK
  10. type Authorizer interface {
  11. cloud.Authorizer
  12. CreateOCIConfig() (common.ConfigurationProvider, error)
  13. }
  14. // SelectAuthorizerByType is an implementation of AuthorizerSelectorFn and acts as a register for Authorizer types
  15. func SelectAuthorizerByType(typeStr string) (Authorizer, error) {
  16. switch typeStr {
  17. case RawConfigProviderAuthorizerType:
  18. return &RawConfigProvider{}, nil
  19. default:
  20. return nil, fmt.Errorf("OCI: provider authorizer type '%s' is not valid", typeStr)
  21. }
  22. }
  23. // RawConfigProvider holds OCI credentials and fulfils the common.ConfigurationProvider interface
  24. type RawConfigProvider struct {
  25. TenancyID string `json:"tenancyID"`
  26. UserID string `json:"userID"`
  27. Region string `json:"region"`
  28. Fingerprint string `json:"fingerprint"`
  29. PrivateKey string `json:"privateKey"`
  30. PrivateKeyPassphrase *string `json:"privateKeyPassphrase"`
  31. }
  32. // MarshalJSON custom json marshalling functions, sets properties as tagged in struct and sets the authorizer type property
  33. func (ak *RawConfigProvider) MarshalJSON() ([]byte, error) {
  34. fmap := make(map[string]any, 6)
  35. fmap[cloud.AuthorizerTypeProperty] = RawConfigProviderAuthorizerType
  36. fmap["tenancyId"] = ak.TenancyID
  37. fmap["userId"] = ak.UserID
  38. fmap["region"] = ak.Region
  39. fmap["fingerprint"] = ak.Fingerprint
  40. fmap["privateKey"] = ak.PrivateKey
  41. fmap["privateKeyPassphrase"] = ak.PrivateKeyPassphrase
  42. return json.Marshal(fmap)
  43. }
  44. func (ak *RawConfigProvider) Validate() error {
  45. if ak.TenancyID == "" {
  46. return fmt.Errorf("RawConfigProvider: missing tenancy ID")
  47. }
  48. if ak.UserID == "" {
  49. return fmt.Errorf("RawConfigProvider: missing user ID")
  50. }
  51. if ak.Fingerprint == "" {
  52. return fmt.Errorf("RawConfigProvider: missing key fingerprint")
  53. }
  54. if ak.Region == "" {
  55. return fmt.Errorf("RawConfigProvider: missing region")
  56. }
  57. if ak.PrivateKey == "" {
  58. return fmt.Errorf("RawConfigProvider: missing private key")
  59. }
  60. if ak.PrivateKeyPassphrase != nil {
  61. if *ak.PrivateKeyPassphrase == "" {
  62. return fmt.Errorf("RawConfigProvider: missing private key passphrase")
  63. }
  64. }
  65. return nil
  66. }
  67. func (ak *RawConfigProvider) Equals(config cloud.Config) bool {
  68. if config == nil {
  69. return false
  70. }
  71. thatConfig, ok := config.(*RawConfigProvider)
  72. if !ok {
  73. return false
  74. }
  75. if ak.TenancyID != thatConfig.TenancyID {
  76. return false
  77. }
  78. if ak.UserID != thatConfig.UserID {
  79. return false
  80. }
  81. if ak.Fingerprint != thatConfig.Fingerprint {
  82. return false
  83. }
  84. if ak.Region != thatConfig.Region {
  85. return false
  86. }
  87. if ak.PrivateKey != thatConfig.PrivateKey {
  88. return false
  89. }
  90. if ak.PrivateKeyPassphrase == nil && thatConfig.PrivateKeyPassphrase != nil {
  91. return false
  92. }
  93. if ak.PrivateKeyPassphrase != nil && thatConfig.PrivateKeyPassphrase == nil {
  94. return false
  95. }
  96. if ak.PrivateKeyPassphrase != nil && thatConfig.PrivateKeyPassphrase != nil {
  97. if *ak.PrivateKeyPassphrase != *thatConfig.PrivateKeyPassphrase {
  98. return false
  99. }
  100. }
  101. return true
  102. }
  103. func (ak *RawConfigProvider) Sanitize() cloud.Config {
  104. redacted := cloud.Redacted
  105. return &RawConfigProvider{
  106. TenancyID: ak.TenancyID,
  107. UserID: ak.UserID,
  108. Fingerprint: ak.Fingerprint,
  109. Region: ak.Region,
  110. PrivateKey: cloud.Redacted,
  111. PrivateKeyPassphrase: &redacted,
  112. }
  113. }
  114. func (ak *RawConfigProvider) CreateOCIConfig() (common.ConfigurationProvider, error) {
  115. return common.NewRawConfigurationProvider(ak.TenancyID, ak.UserID, ak.Region, ak.Fingerprint, ak.PrivateKey, ak.PrivateKeyPassphrase), nil
  116. }