Skip to content

SeaweedFS is a distributed storage system for object storage (S3), file systems, Iceberg table buckets, and Lance vector buckets, designed to handle billions of files with O(1) disk access and effortless horizontal scaling.

1.0 KB
Raw Blame
package sts

import (
	"encoding/json"
	"fmt"
	"strings"

	"github.com/seaweedfs/seaweedfs/weed/iam/policy"
)

// NormalizeSessionPolicy validates and normalizes inline session policy JSON.
// It returns an empty string if the input is empty or whitespace.
func NormalizeSessionPolicy(policyJSON string) (string, error) {
	trimmed := strings.TrimSpace(policyJSON)
	if trimmed == "" {
		return "", nil
	}
	const maxSessionPolicySize = 2048
	if len(trimmed) > maxSessionPolicySize {
		return "", fmt.Errorf("session policy exceeds maximum size of %d characters", maxSessionPolicySize)
	}

	var policyDoc policy.PolicyDocument
	if err := json.Unmarshal([]byte(trimmed), &policyDoc); err != nil {
		return "", fmt.Errorf("invalid session policy JSON: %w", err)
	}
	if err := policy.ValidatePolicyDocument(&policyDoc); err != nil {
		return "", fmt.Errorf("invalid session policy document: %w", err)
	}
	normalized, err := json.Marshal(&policyDoc)
	if err != nil {
		return "", fmt.Errorf("failed to normalize session policy: %w", err)
	}
	return string(normalized), nil
}