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 * EntityRoleMetadataFilter.cpp
24 * Removes non-whitelisted roles from a metadata instance.
28 #include "saml2/metadata/Metadata.h"
29 #include "saml2/metadata/MetadataFilter.h"
31 #include <xmltooling/logging.h>
32 #include <xmltooling/util/NDC.h>
34 using namespace opensaml::saml2md;
35 using namespace xmltooling::logging;
36 using namespace xmltooling;
37 using namespace boost;
43 class SAML_DLLLOCAL EntityRoleMetadataFilter : public MetadataFilter
46 EntityRoleMetadataFilter(const DOMElement* e);
47 ~EntityRoleMetadataFilter() {}
49 const char* getId() const { return ENTITYROLE_METADATA_FILTER; }
50 void doFilter(XMLObject& xmlObject) const;
53 void doFilter(EntityDescriptor& entity) const;
54 void doFilter(EntitiesDescriptor& entities) const;
56 bool m_removeRolelessEntityDescriptors, m_removeEmptyEntitiesDescriptors;
57 set<xmltooling::QName> m_roles;
58 bool m_idp, m_sp, m_authn, m_attr, m_pdp, m_authnq, m_attrq, m_authzq;
61 MetadataFilter* SAML_DLLLOCAL EntityRoleMetadataFilterFactory(const DOMElement* const & e)
63 return new EntityRoleMetadataFilter(e);
69 static const XMLCh RetainedRole[] = UNICODE_LITERAL_12(R,e,t,a,i,n,e,d,R,o,l,e);
70 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);
71 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);
73 EntityRoleMetadataFilter::EntityRoleMetadataFilter(const DOMElement* e)
74 : m_removeRolelessEntityDescriptors(XMLHelper::getAttrBool(e, true, removeRolelessEntityDescriptors)),
75 m_removeEmptyEntitiesDescriptors(XMLHelper::getAttrBool(e, true, removeEmptyEntitiesDescriptors)),
76 m_idp(false), m_sp(false), m_authn(false), m_attr(false), m_pdp(false), m_authnq(false), m_attrq(false), m_authzq(false)
78 e = XMLHelper::getFirstChildElement(e, RetainedRole);
80 auto_ptr<xmltooling::QName> q(XMLHelper::getNodeValueAsQName(e));
82 if (*q == IDPSSODescriptor::ELEMENT_QNAME)
84 else if (*q == SPSSODescriptor::ELEMENT_QNAME)
86 else if (*q == AuthnAuthorityDescriptor::ELEMENT_QNAME)
88 else if (*q == AttributeAuthorityDescriptor::ELEMENT_QNAME)
90 else if (*q == PDPDescriptor::ELEMENT_QNAME)
92 else if (*q == AuthnQueryDescriptorType::TYPE_QNAME)
94 else if (*q == AttributeQueryDescriptorType::TYPE_QNAME)
96 else if (*q == AuthzDecisionQueryDescriptorType::TYPE_QNAME)
101 e = XMLHelper::getNextSiblingElement(e, RetainedRole);
105 void EntityRoleMetadataFilter::doFilter(XMLObject& xmlObject) const
112 doFilter(dynamic_cast<EntitiesDescriptor&>(xmlObject));
119 doFilter(dynamic_cast<EntityDescriptor&>(xmlObject));
125 throw MetadataFilterException("EntityRoleWhiteList MetadataFilter was given an improper metadata instance to filter.");
128 void EntityRoleMetadataFilter::doFilter(EntitiesDescriptor& entities) const
130 Category& log=Category::getInstance(SAML_LOGCAT".MetadataFilter.EntityRoleWhiteList");
132 VectorOf(EntityDescriptor) v = entities.getEntityDescriptors();
133 for (VectorOf(EntityDescriptor)::size_type i = 0; i < v.size(); ) {
135 if (m_removeRolelessEntityDescriptors) {
136 const EntityDescriptor& e = const_cast<const EntityDescriptor&>(*v[i]);
137 if (e.getIDPSSODescriptors().empty() &&
138 e.getSPSSODescriptors().empty() &&
139 e.getAuthnAuthorityDescriptors().empty() &&
140 e.getAttributeAuthorityDescriptors().empty() &&
141 e.getPDPDescriptors().empty() &&
142 e.getAuthnQueryDescriptorTypes().empty() &&
143 e.getAttributeQueryDescriptorTypes().empty() &&
144 e.getAuthzDecisionQueryDescriptorTypes().empty() &&
145 e.getRoleDescriptors().empty()) {
146 auto_ptr_char temp(e.getEntityID());
147 log.debug("filtering out role-less entity (%s)", temp.get());
148 v.erase(v.begin() + i);
155 VectorOf(EntitiesDescriptor) groups = entities.getEntitiesDescriptors();
156 for (VectorOf(EntitiesDescriptor)::size_type j = 0; j < groups.size(); ) {
157 EntitiesDescriptor* group = groups[j];
159 if (m_removeEmptyEntitiesDescriptors && group->getEntitiesDescriptors().empty() && group->getEntityDescriptors().empty()) {
160 auto_ptr_char temp(entities.getName());
161 auto_ptr_char temp2(group->getName());
163 "filtering out empty EntitiesDescriptor (%s) from EntitiesDescriptor (%s)",
164 temp2.get() ? temp2.get() : "unnamed",
165 temp.get() ? temp.get() : "unnamed"
167 groups.erase(groups.begin() + j);
175 void EntityRoleMetadataFilter::doFilter(EntityDescriptor& entity) const
178 entity.getIDPSSODescriptors().clear();
180 entity.getSPSSODescriptors().clear();
182 entity.getAuthnAuthorityDescriptors().clear();
184 entity.getAttributeAuthorityDescriptors().clear();
186 entity.getPDPDescriptors().clear();
188 entity.getAuthnQueryDescriptorTypes().clear();
190 entity.getAttributeQueryDescriptorTypes().clear();
192 entity.getAuthzDecisionQueryDescriptorTypes().clear();
194 VectorOf(RoleDescriptor) v = entity.getRoleDescriptors();
195 for (VectorOf(RoleDescriptor)::size_type i = 0; i < v.size(); ) {
196 const xmltooling::QName* type = v[i]->getSchemaType();
197 if (!type || m_roles.find(*type) != m_roles.end())
198 v.erase(v.begin() + i);