2 * Copyright 2009-2010 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * EntityRoleMetadataFilter.cpp
20 * Removes non-whitelisted roles from a metadata instance.
24 #include "saml2/metadata/Metadata.h"
25 #include "saml2/metadata/MetadataFilter.h"
27 #include <xmltooling/logging.h>
28 #include <xmltooling/util/NDC.h>
30 using namespace opensaml::saml2md;
31 using namespace xmltooling::logging;
32 using namespace xmltooling;
38 class SAML_DLLLOCAL EntityRoleMetadataFilter : public MetadataFilter
41 EntityRoleMetadataFilter(const DOMElement* e);
42 ~EntityRoleMetadataFilter() {}
44 const char* getId() const { return ENTITYROLE_METADATA_FILTER; }
45 void doFilter(XMLObject& xmlObject) const;
48 void doFilter(EntityDescriptor& entity) const;
49 void doFilter(EntitiesDescriptor& entities) const;
51 bool m_removeRolelessEntityDescriptors, m_removeEmptyEntitiesDescriptors;
52 set<xmltooling::QName> m_roles;
53 bool m_idp, m_sp, m_authn, m_attr, m_pdp, m_authnq, m_attrq, m_authzq;
56 MetadataFilter* SAML_DLLLOCAL EntityRoleMetadataFilterFactory(const DOMElement* const & e)
58 return new EntityRoleMetadataFilter(e);
64 static const XMLCh RetainedRole[] = UNICODE_LITERAL_12(R,e,t,a,i,n,e,d,R,o,l,e);
65 static const XMLCh removeRolelessEntityDescriptors[] = UNICODE_LITERAL_31(r,e,m,o,v,e,R,o,l,e,l,e,s,s,E,n,t,i,t,y,D,e,s,c,r,i,p,t,o,r,s);
66 static const XMLCh removeEmptyEntitiesDescriptors[] = UNICODE_LITERAL_30(r,e,m,o,v,e,E,m,p,t,y,E,n,t,i,t,i,e,s,D,e,s,c,r,i,p,t,o,r,s);
68 EntityRoleMetadataFilter::EntityRoleMetadataFilter(const DOMElement* e)
69 : m_removeRolelessEntityDescriptors(XMLHelper::getAttrBool(e, true, removeRolelessEntityDescriptors)),
70 m_removeEmptyEntitiesDescriptors(XMLHelper::getAttrBool(e, true, removeEmptyEntitiesDescriptors)),
71 m_idp(false), m_sp(false), m_authn(false), m_attr(false), m_pdp(false), m_authnq(false), m_attrq(false), m_authzq(false)
73 e = XMLHelper::getFirstChildElement(e, RetainedRole);
75 auto_ptr<xmltooling::QName> q(XMLHelper::getNodeValueAsQName(e));
77 if (*q == IDPSSODescriptor::ELEMENT_QNAME)
79 else if (*q == SPSSODescriptor::ELEMENT_QNAME)
81 else if (*q == AuthnAuthorityDescriptor::ELEMENT_QNAME)
83 else if (*q == AttributeAuthorityDescriptor::ELEMENT_QNAME)
85 else if (*q == PDPDescriptor::ELEMENT_QNAME)
87 else if (*q == AuthnQueryDescriptorType::TYPE_QNAME)
89 else if (*q == AttributeQueryDescriptorType::TYPE_QNAME)
91 else if (*q == AuthzDecisionQueryDescriptorType::TYPE_QNAME)
94 m_roles.insert(*q.get());
96 e = XMLHelper::getNextSiblingElement(e, RetainedRole);
100 void EntityRoleMetadataFilter::doFilter(XMLObject& xmlObject) const
107 doFilter(dynamic_cast<EntitiesDescriptor&>(xmlObject));
114 doFilter(dynamic_cast<EntityDescriptor&>(xmlObject));
120 throw MetadataFilterException("EntityRoleWhiteList MetadataFilter was given an improper metadata instance to filter.");
123 void EntityRoleMetadataFilter::doFilter(EntitiesDescriptor& entities) const
125 Category& log=Category::getInstance(SAML_LOGCAT".MetadataFilter.EntityRoleWhiteList");
127 VectorOf(EntityDescriptor) v=entities.getEntityDescriptors();
128 for (VectorOf(EntityDescriptor)::size_type i=0; i<v.size(); ) {
130 if (m_removeRolelessEntityDescriptors) {
131 const EntityDescriptor& e = const_cast<const EntityDescriptor&>(*v[i]);
132 if (e.getIDPSSODescriptors().empty() &&
133 e.getSPSSODescriptors().empty() &&
134 e.getAuthnAuthorityDescriptors().empty() &&
135 e.getAttributeAuthorityDescriptors().empty() &&
136 e.getPDPDescriptors().empty() &&
137 e.getAuthnQueryDescriptorTypes().empty() &&
138 e.getAttributeQueryDescriptorTypes().empty() &&
139 e.getAuthzDecisionQueryDescriptorTypes().empty() &&
140 e.getRoleDescriptors().empty()) {
141 auto_ptr_char temp(e.getEntityID());
142 log.debug("filtering out role-less entity (%s)", temp.get());
143 v.erase(v.begin() + i);
150 VectorOf(EntitiesDescriptor) groups=entities.getEntitiesDescriptors();
151 for (VectorOf(EntitiesDescriptor)::size_type j=0; j<groups.size(); ) {
152 EntitiesDescriptor* group = groups[j];
154 if (m_removeEmptyEntitiesDescriptors && group->getEntitiesDescriptors().empty() && group->getEntityDescriptors().empty()) {
155 auto_ptr_char temp(entities.getName());
156 auto_ptr_char temp2(group->getName());
158 "filtering out empty EntitiesDescriptor (%s) from EntitiesDescriptor (%s)",
159 temp2.get() ? temp2.get() : "unnamed",
160 temp.get() ? temp.get() : "unnamed"
162 groups.erase(groups.begin() + j);
170 void EntityRoleMetadataFilter::doFilter(EntityDescriptor& entity) const
173 entity.getIDPSSODescriptors().clear();
175 entity.getSPSSODescriptors().clear();
177 entity.getAuthnAuthorityDescriptors().clear();
179 entity.getAttributeAuthorityDescriptors().clear();
181 entity.getPDPDescriptors().clear();
183 entity.getAuthnQueryDescriptorTypes().clear();
185 entity.getAttributeQueryDescriptorTypes().clear();
187 entity.getAuthzDecisionQueryDescriptorTypes().clear();
189 VectorOf(RoleDescriptor) v = entity.getRoleDescriptors();
190 for (VectorOf(RoleDescriptor)::size_type i=0; i<v.size(); ) {
191 const xmltooling::QName* type = v[i]->getSchemaType();
192 if (!type || m_roles.find(*type) != m_roles.end())
193 v.erase(v.begin() + i);