authorizer_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package aws
  2. import (
  3. "testing"
  4. "github.com/opencost/opencost/core/pkg/util/json"
  5. "github.com/opencost/opencost/pkg/cloud"
  6. )
  7. func TestAuthorizerJSON_Sanitize(t *testing.T) {
  8. testCases := map[string]struct {
  9. input Authorizer
  10. expected Authorizer
  11. }{
  12. "Access Key": {
  13. input: &AccessKey{
  14. ID: "ID",
  15. Secret: "Secret",
  16. },
  17. expected: &AccessKey{
  18. ID: "ID",
  19. Secret: cloud.Redacted,
  20. },
  21. },
  22. "Service Account": {
  23. input: &ServiceAccount{},
  24. expected: &ServiceAccount{},
  25. },
  26. "Master Payer Access Key": {
  27. input: &AssumeRole{
  28. Authorizer: &AccessKey{
  29. ID: "ID",
  30. Secret: "Secret",
  31. },
  32. RoleARN: "role arn",
  33. },
  34. expected: &AssumeRole{
  35. Authorizer: &AccessKey{
  36. ID: "ID",
  37. Secret: cloud.Redacted,
  38. },
  39. RoleARN: "role arn",
  40. },
  41. },
  42. "Master Payer Service Account": {
  43. input: &AssumeRole{
  44. Authorizer: &ServiceAccount{},
  45. RoleARN: "role arn",
  46. },
  47. expected: &AssumeRole{
  48. Authorizer: &ServiceAccount{},
  49. RoleARN: "role arn",
  50. },
  51. },
  52. "Google Web Identity": {
  53. input: &WebIdentity{
  54. RoleARN: "role arn",
  55. IdentityProvider: "Google",
  56. TokenRetriever: &GoogleIDTokenRetriever{
  57. Aud: "aud",
  58. },
  59. },
  60. expected: &WebIdentity{
  61. RoleARN: "role arn",
  62. IdentityProvider: "Google",
  63. TokenRetriever: &GoogleIDTokenRetriever{
  64. Aud: "aud",
  65. },
  66. },
  67. },
  68. }
  69. for name, tc := range testCases {
  70. t.Run(name, func(t *testing.T) {
  71. sanitizedAuthorizer := tc.input.Sanitize()
  72. if !tc.expected.Equals(sanitizedAuthorizer) {
  73. t.Error("Authorizer was not as expected after Sanitization")
  74. }
  75. })
  76. }
  77. }
  78. func TestAuthorizerJSON_Encode(t *testing.T) {
  79. testCases := map[string]struct {
  80. authorizer Authorizer
  81. }{
  82. "Access Key": {
  83. authorizer: &AccessKey{
  84. ID: "ID",
  85. Secret: "Secret",
  86. },
  87. },
  88. "Service Account": {
  89. authorizer: &ServiceAccount{},
  90. },
  91. "Master Payer Access Key": {
  92. authorizer: &AssumeRole{
  93. Authorizer: &AccessKey{
  94. ID: "ID",
  95. Secret: "Secret",
  96. },
  97. RoleARN: "role arn",
  98. },
  99. },
  100. "Master Payer Service Account": {
  101. authorizer: &AssumeRole{
  102. Authorizer: &ServiceAccount{},
  103. RoleARN: "role arn",
  104. },
  105. },
  106. "Google Web Identity": {
  107. authorizer: &WebIdentity{
  108. RoleARN: "role arn",
  109. IdentityProvider: "Google",
  110. TokenRetriever: &GoogleIDTokenRetriever{
  111. Aud: "aud",
  112. },
  113. },
  114. },
  115. }
  116. for name, tc := range testCases {
  117. t.Run(name, func(t *testing.T) {
  118. b, err := tc.authorizer.MarshalJSON()
  119. if err != nil {
  120. t.Errorf("Failed to Marshal Authorizer: %s", err)
  121. }
  122. var f interface{}
  123. err = json.Unmarshal(b, &f)
  124. if err != nil {
  125. t.Errorf("Failed to Unmarshal Authorizer: %s", err)
  126. }
  127. authorizer, err := cloud.AuthorizerFromInterface(f, SelectAuthorizerByType)
  128. if err != nil {
  129. t.Errorf("Failed to Unmarshal Authorizer: %s", err)
  130. }
  131. if !tc.authorizer.Equals(authorizer) {
  132. t.Error("Authorizer was not as expected after Sanitization")
  133. }
  134. })
  135. }
  136. }