-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathEntity.java
More file actions
193 lines (171 loc) · 6.66 KB
/
Entity.java
File metadata and controls
193 lines (171 loc) · 6.66 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
/*
* Copyright Cedar Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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.cedarpolicy.model.entity;
import com.cedarpolicy.value.EntityUID;
import com.cedarpolicy.value.Value;
import com.cedarpolicy.model.exception.InternalException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.core.JsonProcessingException;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
/**
* An entity is the kind of object about which authorization decisions are made; principals,
* actions, and resources are all a kind of entity. Each entity is defined by its entity type, a
* unique identifier (UID), zero or more attributes mapped to values, zero or more parent
* entities, and zero or more tags.
*/
public class Entity {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private final EntityUID euid;
/** Key/Value attribute map. */
public final Map<String, Value> attrs;
/** Set of entity EUIDs that are parents to this entity. */
public final Set<EntityUID> parentsEUIDs;
/** Tags on this entity (RFC 82) */
public final Map<String, Value> tags;
/**
* Create an entity from an EntityUIDs, a map of attributes, and a set of parent EntityUIDs.
*
* @param uid EUID of the Entity.
* @param attributes Key/Value map of attributes.
* @param parentsEUIDs Set of parent entities' EUIDs.
*/
public Entity(EntityUID uid, Map<String, Value> attributes, Set<EntityUID> parentsEUIDs) {
this(uid, attributes, parentsEUIDs, new HashMap<>());
}
/**
* Create an entity from an EntityUIDs, a map of attributes, a set of parent EntityUIDs, and a map of tags.
*
* @param uid EUID of the Entity.
* @param attributes Key/Value map of attributes.
* @param parentsEUIDs Set of parent entities' EUIDs.
* @param tags Key/Value map of tags.
*/
public Entity(EntityUID uid, Map<String, Value> attributes, Set<EntityUID> parentsEUIDs, Map<String, Value> tags) {
this.attrs = new HashMap<>(attributes);
this.euid = uid;
this.parentsEUIDs = parentsEUIDs;
this.tags = new HashMap<>(tags);
}
/**
* Get the Entity's JSON string value.
*
* @return the Entity's JSON string value
* @throws NullPointerException if the Entity is null
* @throws InternalException if the Entity is unable to be parsed
*/
public String toJsonString() throws NullPointerException, InternalException {
return toJsonEntityJni(this);
}
/**
* Get the Entity's JSON value.
*
* @return the Entity's JSON value
* @throws NullPointerException if the Entity is null
* @throws InternalException if the Entity is unable to be parsed
* @throws JsonProcessingException if the Entity JSON is unable to be processed
*/
public JsonNode toJsonValue() throws NullPointerException, InternalException, JsonProcessingException {
String entityJsonStr = this.toJsonString();
return OBJECT_MAPPER.readTree(entityJsonStr);
}
/**
* Dump the Entity into an entity JSON file.
*
* @param file the file to dump the Entity into
* @throws NullPointerException if the Entity is null
* @throws InternalException if the Entity is unable to be parsed
* @throws JsonProcessingException if the Entity JSON is unable to be processed
* @throws IOException if the Entity is unable to be written to the file
*/
public void writeToJson(File file) throws NullPointerException, InternalException, JsonProcessingException, IOException {
ObjectWriter writer = OBJECT_MAPPER.writer();
JsonNode entityJson = this.toJsonValue();
writer.writeValue(file, entityJson);
}
/**
* Get the value for the given attribute, or null if not present.
*
* @param attribute Attribute key
* @return Attribute value for the given key or null if not present
* @throws IllegalArgumentException if attribute is null
*/
public Value getAttr(String attribute) {
if (attribute == null) {
throw new IllegalArgumentException("Attribute key cannot be null");
}
return this.attrs.getOrDefault(attribute, null);
}
@Override
public String toString() {
String parentStr = "";
if (!parentsEUIDs.isEmpty()) {
List<String> parentStrs = new ArrayList<String>(parentsEUIDs.stream()
.map(euid -> euid.toString()).collect(Collectors.toList()));
parentStr = "\n\tparents:\n\t\t" + String.join("\n\t\t", parentStrs);
}
String attributeStr = "";
if (!attrs.isEmpty()) {
attributeStr =
"\n\tattrs:\n\t\t"
+ attrs.entrySet().stream()
.map(e -> e.getKey() + ": " + e.getValue())
.collect(Collectors.joining("\n\t\t"));
}
String tagsStr = "";
if (!tags.isEmpty()) {
tagsStr =
"\n\ttags:\n\t\t"
+ tags.entrySet().stream()
.map(e -> e.getKey() + ": " + e.getValue())
.collect(Collectors.joining("\n\t\t"));
}
return euid.toString() + parentStr + attributeStr + tagsStr;
}
/**
* Get the entity uid
* @return Entity UID
*/
public EntityUID getEUID() {
return euid;
}
/**
* Get the Entity's attributes
* @return the attribute map
*/
private Map<String, Value> getAttributes() {
return attrs;
}
/**
* Get this Entity's parents
* @return the set of parent EntityUIDs
*/
public Set<EntityUID> getParents() {
return parentsEUIDs;
}
/**
* Get this Entity's tags
* @return the map of tags
*/
public Map<String, Value> getTags() {
return tags;
}
private static native String toJsonEntityJni(Entity entity) throws NullPointerException, InternalException;
}