1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package edu.internet2.middleware.shibboleth.wayf.plugins.provider;
18
19 import java.util.HashSet;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Set;
23
24 import org.opensaml.saml2.metadata.EntitiesDescriptor;
25 import org.opensaml.saml2.metadata.EntityDescriptor;
26 import org.opensaml.saml2.metadata.provider.FilterException;
27 import org.opensaml.saml2.metadata.provider.MetadataFilter;
28 import org.opensaml.xml.XMLObject;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.w3c.dom.Element;
32 import org.w3c.dom.NodeList;
33
34 import edu.internet2.middleware.shibboleth.wayf.HandlerConfig;
35 import edu.internet2.middleware.shibboleth.wayf.XMLConstants;
36
37
38
39
40
41
42
43 public class ListFilter implements MetadataFilter {
44
45
46
47
48 private static final Logger LOG = LoggerFactory.getLogger(ListFilter.class.getName());
49
50
51
52
53 private boolean excludeEntries;
54
55
56
57
58 private final Set<String> filterEntities;
59
60
61
62
63 private final String filterName;
64
65
66
67
68 private ListFilter() {
69 this.excludeEntries = false;
70 this.filterEntities = new HashSet<String>(0);
71 this.filterName = "anonymous";
72 }
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 public ListFilter(Element config) {
88 String excludeEntriesValue;
89 this.filterEntities = new HashSet<String>(10);
90 this.filterName = config.getAttribute("identifier");
91 excludeEntriesValue = config.getAttribute("excludeEntries");
92
93 if (null == excludeEntriesValue || 0 == excludeEntriesValue.length()) {
94 this.excludeEntries = true;
95 } else {
96 this.excludeEntries = Boolean.getBoolean(excludeEntriesValue);
97 }
98
99 NodeList itemElements = config.getElementsByTagNameNS(XMLConstants.CONFIG_NS, "EntityId");
100
101 if (excludeEntries) {
102 LOG.debug("Populating blacklist " + filterName);
103 } else {
104 LOG.debug("Populating whitelist " + filterName);
105 }
106
107 for (int i = 0; i < itemElements.getLength(); i++) {
108 Element element = (Element) itemElements.item(i);
109 String entityId = element.getTextContent();
110
111 LOG.debug("\t" + entityId);
112 this.filterEntities.add(entityId);
113 }
114 }
115
116
117
118
119
120
121
122 public void doFilter(XMLObject metadata) throws FilterException {
123
124 if (metadata instanceof EntitiesDescriptor) {
125 filterEntities((EntitiesDescriptor)metadata);
126 } else if (metadata instanceof EntityDescriptor) {
127 EntityDescriptor entity = (EntityDescriptor) metadata;
128 String entityName = entity.getEntityID();
129
130 if (excludeEntries) {
131 if (filterEntities.contains(entityName)) {
132 LOG.error("Metadata provider contains a single <EntityDescriptor> (" + entityName +
133 ") which is in exclude list");
134 }
135 } else if (!filterEntities.contains(entity.getEntityID())) {
136 LOG.error("Metadata provider contains a single <EntityDescriptor> (" + entityName +
137 ") which is not on include list");
138 }
139 }
140 }
141
142
143
144
145
146
147
148 private void filterEntities(EntitiesDescriptor entities) {
149 String entitiesName = entities.getName();
150 List<EntitiesDescriptor> childEntities = entities.getEntitiesDescriptors();
151 List<EntityDescriptor> children = entities.getEntityDescriptors();
152
153
154
155
156
157 if (children != null) {
158 Iterator<EntityDescriptor> itr;
159 EntityDescriptor entity;
160 itr = children.iterator();
161
162 while (itr.hasNext()) {
163 entity = itr.next();
164 String entityName = entity.getEntityID();
165 if (excludeEntries) {
166
167 if (filterEntities.contains(entityName)) {
168 LOG.debug("Filter " + filterName + ": Removing blacklisted " + entityName + " from " + entitiesName);
169 itr.remove();
170 }
171 } else if (!filterEntities.contains(entityName)) {
172 LOG.debug("Filter " + filterName + ": Removing non-whitelisted " + entityName + " from " + entitiesName);
173 itr.remove();
174 }
175 }
176 }
177
178 if (childEntities != null) {
179 for (EntitiesDescriptor descriptor : childEntities) {
180 filterEntities(descriptor);
181 }
182 }
183 }
184 }