forked from aws/aws-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonPolicyWriter.java
More file actions
248 lines (203 loc) · 8.51 KB
/
JsonPolicyWriter.java
File metadata and controls
248 lines (203 loc) · 8.51 KB
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
239
240
241
242
243
244
245
246
247
248
/*
* Copyright 2010-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amazonaws.auth.policy.internal;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.amazonaws.auth.policy.Action;
import com.amazonaws.auth.policy.Condition;
import com.amazonaws.auth.policy.Policy;
import com.amazonaws.auth.policy.Principal;
import com.amazonaws.auth.policy.Resource;
import com.amazonaws.auth.policy.Statement;
import com.amazonaws.util.json.JSONException;
import com.amazonaws.util.json.JSONWriter;
/**
* Serializes an AWS policy object to a JSON string, suitable for sending to an
* AWS service.
*/
public class JsonPolicyWriter {
/**
* Converts the specified AWS policy object to a JSON string, suitable for
* passing to an AWS service.
*
* @param policy
* The AWS policy object to convert to a JSON string.
*
* @return The JSON string representation of the specified policy object.
*
* @throws IllegalArgumentException
* If the specified policy is null or invalid and cannot be
* serialized to a JSON string.
*/
public String writePolicyToString(Policy policy) {
if (policy == null) {
throw new IllegalArgumentException("Policy cannot be null");
}
StringWriter writer = new StringWriter();
try {
JSONWriter generator = new JSONWriter(writer);
writePolicy(policy, generator);
return writer.toString();
} catch (Exception e) {
String message = "Unable to serialize policy to JSON string: " + e.getMessage();
throw new IllegalArgumentException(message, e);
} finally {
try {writer.close();} catch (Exception e) {}
}
}
private void writePolicy(Policy policy, JSONWriter generator)
throws JSONException, IOException {
generator.object();
generator.key("Version").value(policy.getVersion());
if (policy.getId() != null) {
generator.key("Id").value(policy.getId());
}
generator.key("Statement").array();
for (Statement statement : policy.getStatements()) {
generator.object();
if (statement.getId() != null) {
generator.key("Sid").value(statement.getId());
}
generator.key("Effect").value(statement.getEffect().toString());
writePrincipals(statement, generator);
writeActions(statement, generator);
writeResources(statement, generator);
writeConditions(statement, generator);
generator.endObject();
}
generator.endArray();
generator.endObject();
}
private void writeConditions(Statement statement, JSONWriter generator)
throws IOException, JSONException {
List<Condition> conditions = statement.getConditions();
if (conditions == null || conditions.isEmpty()) return;
/*
* We could have multiple conditions that are the same condition type
* but use different condition keys. In JSON and XML, those conditions
* must be siblings, otherwise data can be lost when they get parsed.
*/
Map<String, List<Condition>> conditionsByType =
sortConditionsByType(conditions);
generator.key("Condition").object();
for (Map.Entry<String, List<Condition>> entry
: conditionsByType.entrySet()) {
String conditionType = entry.getKey();
generator.key(conditionType).object();
writeConditions(entry.getValue(), generator);
generator.endObject();
}
generator.endObject();
}
private void writeConditions(final List<Condition> conditions,
final JSONWriter generator)
throws IOException, JSONException {
/*
* We could also have multiple conditions that use the same type and
* same key, in which case, we need to push them together as one entry
* when we send the JSON representation.
*/
Map<String, List<String>> conditionValuesByKey =
sortConditionsByKey(conditions);
for (Map.Entry<String, List<String>> entry
: conditionValuesByKey.entrySet()) {
String conditionKey = entry.getKey();
generator.key(conditionKey).array();
for (String value : entry.getValue()) {
generator.value(value);
}
generator.endArray();
}
}
private Map<String, List<String>> sortConditionsByKey(List<Condition> conditions) {
Map<String, List<String>> conditionValuesByConditionKey = new HashMap<String, List<String>>();
for (Condition condition : conditions) {
String key = condition.getConditionKey();
List<String> values = condition.getValues();
if (conditionValuesByConditionKey.containsKey(key) == false) {
conditionValuesByConditionKey.put(key, new ArrayList<String>());
}
conditionValuesByConditionKey.get(key).addAll(values);
}
return conditionValuesByConditionKey;
}
private Map<String, List<Condition>> sortConditionsByType(List<Condition> conditions) {
Map<String, List<Condition>> conditionsByType = new HashMap<String, List<Condition>>();
for (Condition condition : conditions) {
String conditionType = condition.getType();
if (conditionsByType.get(conditionType) == null) {
conditionsByType.put(conditionType, new ArrayList<Condition>());
}
conditionsByType.get(conditionType).add(condition);
}
return conditionsByType;
}
private void writeResources(Statement statement, JSONWriter generator)
throws IOException, JSONException {
List<Resource> resources = statement.getResources();
if (resources == null || resources.isEmpty()) return;
generator.key("Resource").array();
for (Resource resource : resources) {
generator.value(resource.getId());
}
generator.endArray();
}
private void writeActions(Statement statement, JSONWriter generator)
throws IOException, JSONException {
List<Action> actions = statement.getActions();
if (actions == null || actions.isEmpty()) return;
generator.key("Action").array();
for (Action action : actions) {
generator.value(action.getActionName());
}
generator.endArray();
}
/**
* Uses the specified generator to write the JSON data for the principals in
* the specified policy statement.
*/
private void writePrincipals(Statement statement, JSONWriter generator)
throws IOException, JSONException {
List<Principal> principals = statement.getPrincipals();
if (principals == null || principals.isEmpty()) return;
generator.key("Principal").object();
Map<String, List<String>> principalContentsByScheme =
new HashMap<String, List<String>>();
for (Principal p : principals) {
List<String> principalValues =
principalContentsByScheme.get(p.getProvider());
if (principalValues == null) {
principalValues = new ArrayList<String>();
principalContentsByScheme.put(p.getProvider(),
principalValues);
}
principalValues.add(p.getId());
}
for (Map.Entry<String, List<String>> entry
: principalContentsByScheme.entrySet()) {
String scheme = entry.getKey();
generator.key(scheme).array();
for (String principalId : entry.getValue()) {
generator.value(principalId);
}
generator.endArray();
}
generator.endObject();
}
}