test_utils.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package sts
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "github.com/seaweedfs/seaweedfs/weed/iam/providers"
  7. )
  8. // MockTrustPolicyValidator is a simple mock for testing STS functionality
  9. type MockTrustPolicyValidator struct{}
  10. // ValidateTrustPolicyForWebIdentity allows valid JWT test tokens for STS testing
  11. func (m *MockTrustPolicyValidator) ValidateTrustPolicyForWebIdentity(ctx context.Context, roleArn string, webIdentityToken string) error {
  12. // Reject non-existent roles for testing
  13. if strings.Contains(roleArn, "NonExistentRole") {
  14. return fmt.Errorf("trust policy validation failed: role does not exist")
  15. }
  16. // For STS unit tests, allow JWT tokens that look valid (contain dots for JWT structure)
  17. // In real implementation, this would validate against actual trust policies
  18. if len(webIdentityToken) > 20 && strings.Count(webIdentityToken, ".") >= 2 {
  19. // This appears to be a JWT token - allow it for testing
  20. return nil
  21. }
  22. // Legacy support for specific test tokens during migration
  23. if webIdentityToken == "valid_test_token" || webIdentityToken == "valid-oidc-token" {
  24. return nil
  25. }
  26. // Reject invalid tokens
  27. if webIdentityToken == "invalid_token" || webIdentityToken == "expired_token" || webIdentityToken == "invalid-token" {
  28. return fmt.Errorf("trust policy denies token")
  29. }
  30. return nil
  31. }
  32. // ValidateTrustPolicyForCredentials allows valid test identities for STS testing
  33. func (m *MockTrustPolicyValidator) ValidateTrustPolicyForCredentials(ctx context.Context, roleArn string, identity *providers.ExternalIdentity) error {
  34. // Reject non-existent roles for testing
  35. if strings.Contains(roleArn, "NonExistentRole") {
  36. return fmt.Errorf("trust policy validation failed: role does not exist")
  37. }
  38. // For STS unit tests, allow test identities
  39. if identity != nil && identity.UserID != "" {
  40. return nil
  41. }
  42. return fmt.Errorf("invalid identity for role assumption")
  43. }