-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathEntity.java
More file actions
165 lines (144 loc) · 5.26 KB
/
Entity.java
File metadata and controls
165 lines (144 loc) · 5.26 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
/*
* 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 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 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);
}
/**
* An InnerEntity is a data wrapper for an Entity that holds its entity uid, attributes, and ancestors
*/
public static class InnerEntity {
public final EntityUID euid;
public final Map<String, Value> attrs;
public final Set<EntityUID> ancestors;
protected InnerEntity(Entity entity) {
this.euid = entity.getEUID();
this.attrs = entity.attrs;
this.ancestors = entity.getParents();
}
public EntityUID getEntityUid() {
return this.euid;
}
public Map<String, Value> getAttributes() {
return attrs;
}
public Set<EntityUID> getAncestors() {
return ancestors;
}
}
public InnerEntity intoInner() {
return new InnerEntity(this);
}
/**
* 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 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;
}
}