2
0

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