aboutsummaryrefslogtreecommitdiff
path: root/weed/s3api/policy_conversion.go
blob: e22827e3ad65abc6bcba0b1c76c5b59f9508f74c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package s3api

import (
	"fmt"

	"github.com/seaweedfs/seaweedfs/weed/glog"
	"github.com/seaweedfs/seaweedfs/weed/iam/policy"
	"github.com/seaweedfs/seaweedfs/weed/s3api/policy_engine"
)

// ConvertPolicyDocumentToPolicyEngine converts a policy.PolicyDocument to policy_engine.PolicyDocument
// This function provides type-safe conversion with explicit field mapping and error handling.
// It handles the differences between the two types:
// - Converts []string fields to StringOrStringSlice
// - Maps Condition types with type validation
// - Converts Principal fields with support for AWS principals only
// - Handles optional fields (Id, NotPrincipal, NotAction, NotResource are ignored in policy_engine)
//
// Returns an error if the policy contains unsupported types or malformed data.
func ConvertPolicyDocumentToPolicyEngine(src *policy.PolicyDocument) (*policy_engine.PolicyDocument, error) {
	if src == nil {
		return nil, nil
	}

	// Warn if the policy document Id is being dropped
	if src.Id != "" {
		glog.Warningf("policy document Id %q is not supported and will be ignored", src.Id)
	}

	dest := &policy_engine.PolicyDocument{
		Version:   src.Version,
		Statement: make([]policy_engine.PolicyStatement, len(src.Statement)),
	}

	for i := range src.Statement {
		stmt, err := convertStatement(&src.Statement[i])
		if err != nil {
			return nil, fmt.Errorf("failed to convert statement %d: %w", i, err)
		}
		dest.Statement[i] = stmt
	}

	return dest, nil
}

// convertStatement converts a policy.Statement to policy_engine.PolicyStatement
func convertStatement(src *policy.Statement) (policy_engine.PolicyStatement, error) {
	// Check for unsupported fields that would fundamentally change policy semantics
	// These fields invert the logic and ignoring them could create security holes
	if len(src.NotAction) > 0 {
		return policy_engine.PolicyStatement{}, fmt.Errorf("statement %q: NotAction is not supported (would invert action logic, creating potential security risk)", src.Sid)
	}
	if len(src.NotResource) > 0 {
		return policy_engine.PolicyStatement{}, fmt.Errorf("statement %q: NotResource is not supported (would invert resource logic, creating potential security risk)", src.Sid)
	}
	if src.NotPrincipal != nil {
		return policy_engine.PolicyStatement{}, fmt.Errorf("statement %q: NotPrincipal is not supported (would invert principal logic, creating potential security risk)", src.Sid)
	}

	stmt := policy_engine.PolicyStatement{
		Sid:    src.Sid,
		Effect: policy_engine.PolicyEffect(src.Effect),
	}

	// Convert Action ([]string to StringOrStringSlice)
	if len(src.Action) > 0 {
		stmt.Action = policy_engine.NewStringOrStringSlice(src.Action...)
	}

	// Convert Resource ([]string to StringOrStringSlice)
	if len(src.Resource) > 0 {
		stmt.Resource = policy_engine.NewStringOrStringSlice(src.Resource...)
	}

	// Convert Principal (interface{} to *StringOrStringSlice)
	if src.Principal != nil {
		principal, err := convertPrincipal(src.Principal)
		if err != nil {
			return policy_engine.PolicyStatement{}, fmt.Errorf("statement %q: failed to convert principal: %w", src.Sid, err)
		}
		stmt.Principal = principal
	}

	// Convert Condition (map[string]map[string]interface{} to PolicyConditions)
	if len(src.Condition) > 0 {
		condition, err := convertCondition(src.Condition)
		if err != nil {
			return policy_engine.PolicyStatement{}, fmt.Errorf("statement %q: failed to convert condition: %w", src.Sid, err)
		}
		stmt.Condition = condition
	}

	return stmt, nil
}

// convertPrincipal converts a Principal field to *StringOrStringSlice
func convertPrincipal(principal interface{}) (*policy_engine.StringOrStringSlice, error) {
	if principal == nil {
		return nil, nil
	}

	switch p := principal.(type) {
	case string:
		if p == "" {
			return nil, fmt.Errorf("principal string cannot be empty")
		}
		result := policy_engine.NewStringOrStringSlice(p)
		return &result, nil
	case []string:
		if len(p) == 0 {
			return nil, nil
		}
		for _, s := range p {
			if s == "" {
				return nil, fmt.Errorf("principal string in slice cannot be empty")
			}
		}
		result := policy_engine.NewStringOrStringSlice(p...)
		return &result, nil
	case []interface{}:
		strs := make([]string, 0, len(p))
		for _, v := range p {
			if v != nil {
				str, err := convertToString(v)
				if err != nil {
					return nil, fmt.Errorf("failed to convert principal array item: %w", err)
				}
				if str == "" {
					return nil, fmt.Errorf("principal string in slice cannot be empty")
				}
				strs = append(strs, str)
			}
		}
		if len(strs) == 0 {
			return nil, nil
		}
		result := policy_engine.NewStringOrStringSlice(strs...)
		return &result, nil
	case map[string]interface{}:
		// Handle AWS-style principal with service/user keys
		// Example: {"AWS": "arn:aws:iam::123456789012:user/Alice"}
		// Only AWS principals are supported for now. Other types like Service or Federated need special handling.

		awsPrincipals, ok := p["AWS"]
		if !ok || len(p) != 1 {
			glog.Warningf("unsupported principal map, only a single 'AWS' key is supported: %v", p)
			return nil, fmt.Errorf("unsupported principal map, only a single 'AWS' key is supported, got keys: %v", getMapKeys(p))
		}

		// Recursively convert the AWS principal value
		res, err := convertPrincipal(awsPrincipals)
		if err != nil {
			return nil, fmt.Errorf("invalid 'AWS' principal value: %w", err)
		}
		return res, nil
	default:
		return nil, fmt.Errorf("unsupported principal type: %T", p)
	}
}

// convertCondition converts policy conditions to PolicyConditions
func convertCondition(src map[string]map[string]interface{}) (policy_engine.PolicyConditions, error) {
	if len(src) == 0 {
		return nil, nil
	}

	dest := make(policy_engine.PolicyConditions)
	for condType, condBlock := range src {
		destBlock := make(map[string]policy_engine.StringOrStringSlice)
		for key, value := range condBlock {
			condValue, err := convertConditionValue(value)
			if err != nil {
				return nil, fmt.Errorf("failed to convert condition %s[%s]: %w", condType, key, err)
			}
			destBlock[key] = condValue
		}
		dest[condType] = destBlock
	}

	return dest, nil
}

// convertConditionValue converts a condition value to StringOrStringSlice
func convertConditionValue(value interface{}) (policy_engine.StringOrStringSlice, error) {
	switch v := value.(type) {
	case string:
		return policy_engine.NewStringOrStringSlice(v), nil
	case []string:
		return policy_engine.NewStringOrStringSlice(v...), nil
	case []interface{}:
		strs := make([]string, 0, len(v))
		for _, item := range v {
			if item != nil {
				str, err := convertToString(item)
				if err != nil {
					return policy_engine.StringOrStringSlice{}, fmt.Errorf("failed to convert condition array item: %w", err)
				}
				strs = append(strs, str)
			}
		}
		return policy_engine.NewStringOrStringSlice(strs...), nil
	default:
		// For non-string types, convert to string
		// This handles numbers, booleans, etc.
		str, err := convertToString(v)
		if err != nil {
			return policy_engine.StringOrStringSlice{}, err
		}
		return policy_engine.NewStringOrStringSlice(str), nil
	}
}

// convertToString converts any value to string representation
// Returns an error for unsupported types to prevent silent data corruption
func convertToString(value interface{}) (string, error) {
	switch v := value.(type) {
	case string:
		return v, nil
	case bool,
		int, int8, int16, int32, int64,
		uint, uint8, uint16, uint32, uint64,
		float32, float64:
		// Use fmt.Sprint for supported primitive types
		return fmt.Sprint(v), nil
	default:
		glog.Warningf("unsupported type in policy conversion: %T", v)
		return "", fmt.Errorf("unsupported type in policy conversion: %T", v)
	}
}

// getMapKeys returns the keys of a map as a slice (helper for error messages)
func getMapKeys(m map[string]interface{}) []string {
	keys := make([]string, 0, len(m))
	for k := range m {
		keys = append(keys, k)
	}
	return keys
}