/** * Returns a JSON string representation of this AWS access control policy, * suitable to be sent to an AWS service as part of a request to set an * access control policy. * * @return A JSON string representation of this AWS access control policy. */ public String toJson() { return new JsonPolicyWriter().writePolicyToString(this); }
/** * Returns an AWS access control policy object generated from JSON string. Allows configuring options for the JSON policy * reader (for example, to disable the stripping of dashes in the principal ID). * * @param jsonString * The JSON string representation of this AWS access control policy. * @param options * Configuration for the JSON policy reader that affects the way in which it converts the JSON configuration. * * @return An AWS access control policy object. * * @throws IllegalArgumentException * If the specified JSON string is null or invalid and cannot be * converted to an AWS policy object. */ public static Policy fromJson(String jsonString, PolicyReaderOptions options) { return new JsonPolicyReader(options).createPolicyFromJsonString(jsonString); }
/** * Writes an array along with its values to the JSONGenerator. * * @param arrayName * name of the JSON array. * @param values * values of the JSON array. */ private void writeJsonArray(String arrayName, List<String> values) throws JsonGenerationException, IOException { writeJsonArrayStart(arrayName); for (String value : values) generator.writeString(value); writeJsonArrayEnd(); }
generator.writeStartObject(); writeJsonKeyValue(JsonDocumentFields.VERSION, policy.getVersion()); if (isNotNull(policy.getId())) writeJsonKeyValue(JsonDocumentFields.POLICY_ID, policy.getId()); writeJsonArrayStart(JsonDocumentFields.STATEMENT); if (isNotNull(statement.getId())) { writeJsonKeyValue(JsonDocumentFields.STATEMENT_ID, statement.getId()); writeJsonKeyValue(JsonDocumentFields.STATEMENT_EFFECT, statement .getEffect().toString()); if (isNotNull(principals) && !principals.isEmpty()) writePrincipals(principals); if (isNotNull(actions) && !actions.isEmpty()) writeActions(actions); if (isNotNull(resources) && !resources.isEmpty()) writeResources(resources); if (isNotNull(conditions) && !conditions.isEmpty()) writeConditions(conditions); writeJsonArrayEnd();
/** * Writes the list of conditions to the JSONGenerator. * * @param conditions * the conditions to be written. */ private void writeConditions(List<Condition> conditions) throws JsonGenerationException, IOException { Map<String, ConditionsByKey> conditionsByType = groupConditionsByTypeAndKey(conditions); writeJsonObjectStart(JsonDocumentFields.CONDITION); ConditionsByKey conditionsByKey; for (Map.Entry<String, ConditionsByKey> entry : conditionsByType .entrySet()) { conditionsByKey = conditionsByType.get(entry.getKey()); writeJsonObjectStart(entry.getKey()); for (String key : conditionsByKey.keySet()) { writeJsonArray(key, conditionsByKey.getConditionsByKey(key)); } writeJsonObjectEnd(); } writeJsonObjectEnd(); }
/** * Writes the list of <code>Principal</code>s to the JSONGenerator. * * @param principals * the list of principals to be written. */ private void writePrincipals(List<Principal> principals) throws JsonGenerationException, IOException { if (principals.size() == 1 && principals.get(0).equals(Principal.All)) { writeJsonKeyValue(JsonDocumentFields.PRINCIPAL, Principal.All.getId()); } else { writeJsonObjectStart(JsonDocumentFields.PRINCIPAL); Map<String, List<String>> principalsByScheme = groupPrincipalByScheme(principals); List<String> principalValues; for (Map.Entry<String, List<String>> entry : principalsByScheme.entrySet()) { principalValues = principalsByScheme.get(entry.getKey()); if (principalValues.size() == 1) { writeJsonKeyValue(entry.getKey(), principalValues.get(0)); } else { writeJsonArray(entry.getKey(), principalValues); } } writeJsonObjectEnd(); } }
final Effect effect = isNotNull(effectNode) ? Effect.valueOf(effectNode.asText()) : Effect.Deny ; if (isNotNull(id)) { statement.setId(id.asText()); if (isNotNull(actionNodes)) statement.setActions(actionsOf(actionNodes)); if (isNotNull(resourceNodes)) statement.setResources(resourcesOf(resourceNodes)); if (isNotNull(conditionNodes)) statement.setConditions(conditionsOf(conditionNodes)); if (isNotNull(principalNodes)) statement.setPrincipals(principalOf(principalNodes));
/** * 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(!isNotNull(policy)) throw new IllegalArgumentException("Policy cannot be null"); try { return jsonStringOf(policy); } 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) { } } }
/** * Groups the list of <code>Condition</code>s by the condition type and * condition key. * * @param conditions * the list of conditions to be grouped * @return a map of conditions grouped by type and then key. */ private Map<String, ConditionsByKey> groupConditionsByTypeAndKey( List<Condition> conditions) { Map<String, ConditionsByKey> conditionsByType = new LinkedHashMap<String, ConditionsByKey>(); String type; String key; ConditionsByKey conditionsByKey; for (Condition condition : conditions) { type = condition.getType(); key = condition.getConditionKey(); if (!(conditionsByType.containsKey(type))) { conditionsByType.put(type, new ConditionsByKey()); } conditionsByKey = conditionsByType.get(type); conditionsByKey.addValuesToKey(key, condition.getValues()); } return conditionsByType; }
if (isNotNull(idNode)) { policy.setId(idNode.asText()); if (isNotNull(statementsNode)) { if (statementsNode.isObject()) { statements.add(statementOf(statementsNode)); } else if (statementsNode.isArray()) { for (JsonNode statementNode : statementsNode) { statements.add(statementOf(statementNode));
/** * Writes the list of <code>Action</code>s to the JSONGenerator. * * @param actions * the list of the actions to be written. */ private void writeActions(List<Action> actions) throws JsonGenerationException, IOException { List<String> actionStrings = new ArrayList<String>(); for (Action action : actions) { actionStrings.add(action.getActionName()); } writeJsonArray(JsonDocumentFields.ACTION, actionStrings); }
public void addValuesToKey(String key, List<String> values) { List<String> conditionValues = getConditionsByKey(key); if (conditionValues == null) conditionsByKey.put(key, new ArrayList<String>(values)); else conditionValues.addAll(values); } }
/** * Generates a list of actions from the Action Json Node. * * @param actionNodes * the action Json node to be parsed. * @return the list of actions. */ private List<Action> actionsOf(JsonNode actionNodes) { List<Action> actions = new LinkedList<Action>(); if (actionNodes.isArray()) { for (JsonNode action : actionNodes) { actions.add(new NamedAction(action.asText())); } } else { actions.add(new NamedAction(actionNodes.asText())); } return actions; }
/** * Generates a list of condition from the Json node. * * @param conditionNodes * the condition Json node to be parsed. * @return the list of conditions. */ private List<Condition> conditionsOf(JsonNode conditionNodes) { List<Condition> conditionList = new LinkedList<Condition>(); Iterator<Map.Entry<String, JsonNode>> mapOfConditions = conditionNodes .fields(); Entry<String, JsonNode> condition; while (mapOfConditions.hasNext()) { condition = mapOfConditions.next(); convertConditionRecord(conditionList, condition.getKey(), condition.getValue()); } return conditionList; }
elements = principalNode.elements(); while (elements.hasNext()) { principals.add(createPrincipal(schema, elements.next())); principals.add(createPrincipal(schema, principalNode));
/** * Returns a JSON string representation of this AWS access control policy, * suitable to be sent to an AWS service as part of a request to set an * access control policy. * * @return A JSON string representation of this AWS access control policy. */ public String toJson() { return new JsonPolicyWriter().writePolicyToString(this); }
/** * Returns an AWS access control policy object generated from JSON string. * * @param jsonString The JSON string representation of this AWS access * control policy. * @return An AWS access control policy object. * @throws IllegalArgumentException If the specified JSON string is null or * invalid and cannot be converted to an AWS policy object. */ public static Policy fromJson(String jsonString) { return new JsonPolicyReader().createPolicyFromJsonString(jsonString); }
/** * Writes the list of <code>Resource</code>s to the JSONGenerator. * * @param resources * the list of resources to be written. */ private void writeResources(List<Resource> resources) throws JsonGenerationException, IOException { PolicyUtils.validateResourceList(resources); List<String> resourceStrings = new ArrayList<String>(); for (Resource resource : resources) { resourceStrings.add(resource.getId()); } // all resources are validated to be of the same type, so it is safe to take the type of the first one if (resources.get(0).isNotType()) { writeJsonArray(JsonDocumentFields.NOT_RESOURCE, resourceStrings); } else { writeJsonArray(JsonDocumentFields.RESOURCE, resourceStrings); } }
/** * Returns a JSON string representation of this AWS access control policy, * suitable to be sent to an AWS service as part of a request to set an * access control policy. * * @return A JSON string representation of this AWS access control policy. */ public String toJson() { return new JsonPolicyWriter().writePolicyToString(this); }
/** * Returns an AWS access control policy object generated from JSON string. Allows configuring options for the JSON policy * reader (for example, to disable the stripping of dashes in the principal ID). * * @param jsonString * The JSON string representation of this AWS access control policy. * @param options * Configuration for the JSON policy reader that affects the way in which it converts the JSON configuration. * * @return An AWS access control policy object. * * @throws IllegalArgumentException * If the specified JSON string is null or invalid and cannot be * converted to an AWS policy object. */ public static Policy fromJson(String jsonString, PolicyReaderOptions options) { return new JsonPolicyReader(options).createPolicyFromJsonString(jsonString); }