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