authorizer.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package azure
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/Azure/azure-storage-blob-go/azblob"
  6. "github.com/opencost/opencost/pkg/cloud/config"
  7. )
  8. const AccessKeyAuthorizerType = "AzureAccessKey"
  9. type Authorizer interface {
  10. config.Authorizer
  11. GetBlobCredentials() (azblob.Credential, error)
  12. }
  13. // SelectAuthorizerByType is an implementation of AuthorizerSelectorFn and acts as a register for Authorizer types
  14. func SelectAuthorizerByType(typeStr string) (Authorizer, error) {
  15. switch typeStr {
  16. case AccessKeyAuthorizerType:
  17. return &AccessKey{}, nil
  18. default:
  19. return nil, fmt.Errorf("azure: provider authorizer type '%s' is not valid", typeStr)
  20. }
  21. }
  22. type AccessKey struct {
  23. AccessKey string `json:"accessKey"`
  24. Account string `json:"account"`
  25. }
  26. func (ak *AccessKey) MarshalJSON() ([]byte, error) {
  27. fmap := make(map[string]any, 3)
  28. fmap[config.AuthorizerTypeProperty] = AccessKeyAuthorizerType
  29. fmap["accessKey"] = ak.AccessKey
  30. fmap["account"] = ak.Account
  31. return json.Marshal(fmap)
  32. }
  33. func (ak *AccessKey) Validate() error {
  34. if ak.AccessKey == "" {
  35. return fmt.Errorf("AccessKey: missing access key")
  36. }
  37. if ak.Account == "" {
  38. return fmt.Errorf("AccessKey: missing account")
  39. }
  40. return nil
  41. }
  42. func (ak *AccessKey) Equals(config config.Config) bool {
  43. if config == nil {
  44. return false
  45. }
  46. thatConfig, ok := config.(*AccessKey)
  47. if !ok {
  48. return false
  49. }
  50. if ak.AccessKey != thatConfig.AccessKey {
  51. return false
  52. }
  53. if ak.Account != thatConfig.Account {
  54. return false
  55. }
  56. return true
  57. }
  58. func (ak *AccessKey) Sanitize() config.Config {
  59. return &AccessKey{
  60. AccessKey: config.Redacted,
  61. Account: ak.Account,
  62. }
  63. }
  64. func (ak *AccessKey) GetBlobCredentials() (azblob.Credential, error) {
  65. // Create a default request pipeline using your storage account name and account key.
  66. return azblob.NewSharedKeyCredential(ak.Account, ak.AccessKey)
  67. }