authorizer_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. b, err := tc.input.MarshalJSON()
  72. if err != nil {
  73. t.Errorf("Failed to Marshal Authorizer: %s", err)
  74. }
  75. var f interface{}
  76. err = json.Unmarshal(b, &f)
  77. if err != nil {
  78. t.Errorf("Failed to Unmarshal Authorizer: %s", err)
  79. }
  80. authorizer, err := cloud.AuthorizerFromInterface(f, SelectAuthorizerByType)
  81. if err != nil {
  82. t.Errorf("Failed to Unmarshal Authorizer: %s", err)
  83. }
  84. // Convert to AuthorizerJSON for sanitization
  85. if authorizer != nil {
  86. sanitizedAuthorizer := authorizer.Sanitize()
  87. if !tc.expected.Equals(sanitizedAuthorizer) {
  88. t.Error("Authorizer was not as expected after Sanitization")
  89. }
  90. }
  91. })
  92. }
  93. }
  94. func TestAuthorizerJSON_Encode(t *testing.T) {
  95. testCases := map[string]struct {
  96. authorizer Authorizer
  97. }{
  98. "Access Key": {
  99. authorizer: &AccessKey{
  100. ID: "ID",
  101. Secret: "Secret",
  102. },
  103. },
  104. "Service Account": {
  105. authorizer: &ServiceAccount{},
  106. },
  107. "Master Payer Access Key": {
  108. authorizer: &AssumeRole{
  109. Authorizer: &AccessKey{
  110. ID: "ID",
  111. Secret: "Secret",
  112. },
  113. RoleARN: "role arn",
  114. },
  115. },
  116. "Master Payer Service Account": {
  117. authorizer: &AssumeRole{
  118. Authorizer: &ServiceAccount{},
  119. RoleARN: "role arn",
  120. },
  121. },
  122. "Google Web Identity": {
  123. authorizer: &WebIdentity{
  124. RoleARN: "role arn",
  125. IdentityProvider: "Google",
  126. TokenRetriever: &GoogleIDTokenRetriever{
  127. Aud: "aud",
  128. },
  129. },
  130. },
  131. }
  132. for name, tc := range testCases {
  133. t.Run(name, func(t *testing.T) {
  134. b, err := tc.authorizer.MarshalJSON()
  135. if err != nil {
  136. t.Errorf("Failed to Marshal Authorizer: %s", err)
  137. }
  138. var f interface{}
  139. err = json.Unmarshal(b, &f)
  140. if err != nil {
  141. t.Errorf("Failed to Unmarshal Authorizer: %s", err)
  142. }
  143. authorizer, err := cloud.AuthorizerFromInterface(f, SelectAuthorizerByType)
  144. if err != nil {
  145. t.Errorf("Failed to Unmarshal Authorizer: %s", err)
  146. }
  147. if !tc.authorizer.Equals(authorizer) {
  148. t.Error("Authorizer was not as expected after Sanitization")
  149. }
  150. })
  151. }
  152. }