s3_token_differentiation_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package s3api
  2. import (
  3. "strings"
  4. "testing"
  5. "time"
  6. "github.com/seaweedfs/seaweedfs/weed/iam/integration"
  7. "github.com/seaweedfs/seaweedfs/weed/iam/sts"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestS3IAMIntegration_isSTSIssuer(t *testing.T) {
  11. // Create test STS service with configuration
  12. stsService := sts.NewSTSService()
  13. // Set up STS configuration with a specific issuer
  14. testIssuer := "https://seaweedfs-prod.company.com/sts"
  15. stsConfig := &sts.STSConfig{
  16. Issuer: testIssuer,
  17. SigningKey: []byte("test-signing-key-32-characters-long"),
  18. TokenDuration: sts.FlexibleDuration{time.Hour},
  19. MaxSessionLength: sts.FlexibleDuration{12 * time.Hour}, // Required field
  20. }
  21. // Initialize STS service with config (this sets the Config field)
  22. err := stsService.Initialize(stsConfig)
  23. assert.NoError(t, err)
  24. // Create S3IAM integration with configured STS service
  25. s3iam := &S3IAMIntegration{
  26. iamManager: &integration.IAMManager{}, // Mock
  27. stsService: stsService,
  28. filerAddress: "test-filer:8888",
  29. enabled: true,
  30. }
  31. tests := []struct {
  32. name string
  33. issuer string
  34. expected bool
  35. }{
  36. // Only exact match should return true
  37. {
  38. name: "exact match with configured issuer",
  39. issuer: testIssuer,
  40. expected: true,
  41. },
  42. // All other issuers should return false (exact matching)
  43. {
  44. name: "similar but not exact issuer",
  45. issuer: "https://seaweedfs-prod.company.com/sts2",
  46. expected: false,
  47. },
  48. {
  49. name: "substring of configured issuer",
  50. issuer: "seaweedfs-prod.company.com",
  51. expected: false,
  52. },
  53. {
  54. name: "contains configured issuer as substring",
  55. issuer: "prefix-" + testIssuer + "-suffix",
  56. expected: false,
  57. },
  58. {
  59. name: "case sensitive - different case",
  60. issuer: strings.ToUpper(testIssuer),
  61. expected: false,
  62. },
  63. {
  64. name: "Google OIDC",
  65. issuer: "https://accounts.google.com",
  66. expected: false,
  67. },
  68. {
  69. name: "Azure AD",
  70. issuer: "https://login.microsoftonline.com/tenant-id/v2.0",
  71. expected: false,
  72. },
  73. {
  74. name: "Auth0",
  75. issuer: "https://mycompany.auth0.com",
  76. expected: false,
  77. },
  78. {
  79. name: "Keycloak",
  80. issuer: "https://keycloak.mycompany.com/auth/realms/master",
  81. expected: false,
  82. },
  83. {
  84. name: "Empty string",
  85. issuer: "",
  86. expected: false,
  87. },
  88. }
  89. for _, tt := range tests {
  90. t.Run(tt.name, func(t *testing.T) {
  91. result := s3iam.isSTSIssuer(tt.issuer)
  92. assert.Equal(t, tt.expected, result, "isSTSIssuer should use exact matching against configured issuer")
  93. })
  94. }
  95. }
  96. func TestS3IAMIntegration_isSTSIssuer_NoSTSService(t *testing.T) {
  97. // Create S3IAM integration without STS service
  98. s3iam := &S3IAMIntegration{
  99. iamManager: &integration.IAMManager{},
  100. stsService: nil, // No STS service
  101. filerAddress: "test-filer:8888",
  102. enabled: true,
  103. }
  104. // Should return false when STS service is not available
  105. result := s3iam.isSTSIssuer("seaweedfs-sts")
  106. assert.False(t, result, "isSTSIssuer should return false when STS service is nil")
  107. }