2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
22 * BlacklistMetadataFilter.cpp
24 * Removes blacklisted entities from a metadata instance
28 #include "saml2/metadata/EntityMatcher.h"
29 #include "saml2/metadata/Metadata.h"
30 #include "saml2/metadata/MetadataFilter.h"
32 #include <boost/scoped_ptr.hpp>
33 #include <xmltooling/logging.h>
35 using namespace opensaml::saml2md;
36 using namespace opensaml::saml2;
37 using namespace xmltooling::logging;
38 using namespace xmltooling;
39 using namespace boost;
44 class SAML_DLLLOCAL BlacklistMetadataFilter : public MetadataFilter
47 BlacklistMetadataFilter(const DOMElement* e);
48 ~BlacklistMetadataFilter() {}
50 const char* getId() const { return BLACKLIST_METADATA_FILTER; }
51 void doFilter(XMLObject& xmlObject) const;
54 void filterGroup(EntitiesDescriptor*) const;
55 bool included(const EntityDescriptor&) const;
57 set<xstring> m_entities;
58 scoped_ptr<EntityMatcher> m_matcher;
61 MetadataFilter* SAML_DLLLOCAL BlacklistMetadataFilterFactory(const DOMElement* const & e)
63 return new BlacklistMetadataFilter(e);
66 static const XMLCh Exclude[] = UNICODE_LITERAL_7(E,x,c,l,u,d,e);
67 static const XMLCh _matcher[] = UNICODE_LITERAL_7(m,a,t,c,h,e,r);
72 BlacklistMetadataFilter::BlacklistMetadataFilter(const DOMElement* e)
74 string matcher(XMLHelper::getAttrString(e, nullptr, _matcher));
76 m_matcher.reset(SAMLConfig::getConfig().EntityMatcherManager.newPlugin(matcher.c_str(), e));
78 e = XMLHelper::getFirstChildElement(e, Exclude);
80 if (e->hasChildNodes()) {
81 const XMLCh* excl = e->getTextContent();
83 m_entities.insert(excl);
85 e = XMLHelper::getNextSiblingElement(e, Exclude);
89 void BlacklistMetadataFilter::doFilter(XMLObject& xmlObject) const
91 EntitiesDescriptor* group = dynamic_cast<EntitiesDescriptor*>(&xmlObject);
93 if (group->getName() && !m_entities.empty() && m_entities.count(group->getName()) > 0)
94 throw MetadataFilterException(BLACKLIST_METADATA_FILTER " MetadataFilter instructed to filter the root group in the metadata.");
98 EntityDescriptor* entity = dynamic_cast<EntityDescriptor*>(&xmlObject);
100 if (included(*entity))
101 throw MetadataFilterException(BLACKLIST_METADATA_FILTER " MetadataFilter instructed to filter the root/only entity in the metadata.");
104 throw MetadataFilterException(BLACKLIST_METADATA_FILTER " MetadataFilter was given an improper metadata instance to filter.");
109 void BlacklistMetadataFilter::filterGroup(EntitiesDescriptor* entities) const
111 Category& log = Category::getInstance(SAML_LOGCAT ".MetadataFilter." WHITELIST_METADATA_FILTER);
113 VectorOf(EntityDescriptor) v = entities->getEntityDescriptors();
114 for (VectorOf(EntityDescriptor)::size_type i = 0; i < v.size(); ) {
115 if (included(*v[i])) {
116 auto_ptr_char id(v[i]->getEntityID());
117 log.info("filtering out blacklisted entity (%s)", id.get());
118 v.erase(v.begin() + i);
125 VectorOf(EntitiesDescriptor) w = entities->getEntitiesDescriptors();
126 for (VectorOf(EntitiesDescriptor)::size_type j = 0; j < w.size(); ) {
127 const XMLCh* name = w[j]->getName();
128 if (name && !m_entities.empty() && m_entities.count(name) > 0) {
129 auto_ptr_char name2(name);
130 log.info("filtering out blacklisted group (%s)", name2.get());
131 w.erase(w.begin() + j);
140 bool BlacklistMetadataFilter::included(const EntityDescriptor& entity) const
142 // Check for entityID.
143 if (entity.getEntityID() && !m_entities.empty() && m_entities.count(entity.getEntityID()) > 0)
146 if (m_matcher && m_matcher->matches(entity))