SetVariableAssertionBuilder implements PolicyAssertionBuilder { @Override public void buildAssertionElement(Element assertionElement, PolicyBuilderContext policyBuilderContext) throws DocumentParseException { final Document policyDocument = policyBuilderContext.getPolicyDocument(); Element nameElement; try { nameElement = getSingleElement(assertionElement, VARIABLE_TO_SET); } catch (DocumentParseException e) { throw new EntityBuilderException("Could not find VariableToSet element in a SetVariable Assertion."); } String variableName = nameElement.getAttribute(STRING_VALUE); if (variableName.startsWith(PREFIX_ENV)) { assertionElement.insertBefore( createElementWithAttribute(policyDocument, BASE_64_EXPRESSION, ENV_PARAM_NAME, insertPrefixToEnvironmentVariable(variableName, policyBuilderContext.getPolicyName())), assertionElement.getFirstChild() ); } else { try{ PolicyAssertionBuilder.prepareBase64Element(policyDocument, assertionElement, EXPRESSION, BASE_64_EXPRESSION); } catch (EntityBuilderException e){ LOGGER.log(Level.WARNING, "Exception " + e.getMessage()); } } }  @Override void buildAssertionElement(Element assertionElement, PolicyBuilderContext policyBuilderContext); @Override String getAssertionTagName();  }
@Test void testPrepareSetVariableAssertionNoENV() throws DocumentParseException { Element setVariableAssertionElement = createSetVariableAssertion(document, "my-var", "base64Text"); document.appendChild(setVariableAssertionElement); String prefix = "prefix"; setVariableAssertionBuilder.buildAssertionElement(setVariableAssertionElement, policyBuilderContext); Element nameElement = getSingleElement(setVariableAssertionElement, VARIABLE_TO_SET); assertEquals("my-var", nameElement.getAttribute(PolicyEntityBuilder.STRING_VALUE)); Element expressionElement = getSingleElement(setVariableAssertionElement, BASE_64_EXPRESSION); String b64 = expressionElement.getAttribute(PolicyEntityBuilder.STRING_VALUE); assertEquals(Base64.getEncoder().encodeToString("base64Text".getBytes(StandardCharsets.UTF_8)), b64); }
@Test void testPrepareSetVariableAssertionUnescaping() throws DocumentParseException { Element setVariableAssertionElement = createSetVariableAssertion(document, "userSession", "&lt;usersession&gt;\n" + " &lt;user&gt;&lt;![CDATA[${current.username}]]&gt;&lt;/user&gt;\n" + " &lt;role&gt;&lt;![CDATA[${current.user.role}]]&gt;&lt;/role&gt;\n" + " &lt;lookupUser&gt;&lt;![CDATA[${lookupUser}]]&gt;&lt;/lookupUser&gt;\n" + " &lt;synchToken&gt;&lt;![CDATA[${xpathSynchToken.result}]]&gt;&lt;/synchToken&gt;\n" + "&lt;/usersession&gt;"); document.appendChild(setVariableAssertionElement); String prefix = "prefix"; setVariableAssertionBuilder.buildAssertionElement(setVariableAssertionElement, policyBuilderContext); Element nameElement = getSingleElement(setVariableAssertionElement, VARIABLE_TO_SET); assertEquals("userSession", nameElement.getAttribute(PolicyEntityBuilder.STRING_VALUE)); Element expressionElement = getSingleElement(setVariableAssertionElement, BASE_64_EXPRESSION); String expectedB64 = "PHVzZXJzZXNzaW9uPgogPHVzZXI+PCFbQ0RBVEFbJHtjdXJyZW50LnVzZXJuYW1lfV1dPjwvdXNlcj4KIDxyb2xlPjwhW0NEQVRBWyR7Y3VycmVudC51c2VyLnJvbGV9XV0+PC9yb2xlPgogPGxvb2t1cFVzZXI+PCFbQ0RBVEFbJHtsb29rdXBVc2VyfV1dPjwvbG9va3VwVXNlcj4KIDxzeW5jaFRva2VuPjwhW0NEQVRBWyR7eHBhdGhTeW5jaFRva2VuLnJlc3VsdH1dXT48L3N5bmNoVG9rZW4+CjwvdXNlcnNlc3Npb24+"; String b64 = expressionElement.getAttribute(PolicyEntityBuilder.STRING_VALUE); assertEquals(expectedB64, b64); String value = new String(decodeBase64(b64)); assertEquals( "<usersession>\n" + " <user><![CDATA[${current.username}]]></user>\n" + " <role><![CDATA[${current.user.role}]]></role>\n" + " <lookupUser><![CDATA[${lookupUser}]]></lookupUser>\n" + " <synchToken><![CDATA[${xpathSynchToken.result}]]></synchToken>\n" + "</usersession>", value); }
@Test void testPrepareSetVariableAssertionENV() throws DocumentParseException { Element setVariableAssertionElement = createSetVariableAssertion(document, "ENV.my-var", "base64Text"); document.appendChild(setVariableAssertionElement); String prefix = "path.xml"; setVariableAssertionBuilder.buildAssertionElement(setVariableAssertionElement, policyBuilderContext); Element nameElement = getSingleElement(setVariableAssertionElement, VARIABLE_TO_SET); assertEquals("ENV.my-var", nameElement.getAttribute(PolicyEntityBuilder.STRING_VALUE)); Element expressionElement = getSingleElement(setVariableAssertionElement, BASE_64_EXPRESSION); assertEquals(PREFIX_ENV + prefix + ".my-var", expressionElement.getAttribute(SetVariableAssertionBuilder.ENV_PARAM_NAME)); assertFalse(expressionElement.hasAttribute(PolicyEntityBuilder.STRING_VALUE)); }
@Test void testPrepareSetVariableAssertionMissingVariableToSet() { Element setVariableAssertion = document.createElement(SET_VARIABLE); document.appendChild(setVariableAssertion); Element expression = document.createElement(EXPRESSION); expression.appendChild(document.createCDATASection("ashdkjsah")); setVariableAssertion.appendChild(expression); String prefix = "prefix"; assertThrows(EntityBuilderException.class, () -> setVariableAssertionBuilder.buildAssertionElement(setVariableAssertion, policyBuilderContext)); }
@Test void testPrepareSetVariableAssertionMissingBase64Value() throws DocumentParseException { Element setVariableAssertion = document.createElement(SET_VARIABLE); document.appendChild(setVariableAssertion); Element variableToSet = document.createElement(VARIABLE_TO_SET); variableToSet.setAttribute(PolicyEntityBuilder.STRING_VALUE, "my.var"); setVariableAssertion.appendChild(variableToSet); String prefix = "prefix"; setVariableAssertionBuilder.buildAssertionElement(setVariableAssertion, policyBuilderContext); Element nameElement = getSingleElement(setVariableAssertion, VARIABLE_TO_SET); assertEquals("my.var", nameElement.getAttribute(PolicyEntityBuilder.STRING_VALUE)); Element expressionElement = getSingleChildElement(setVariableAssertion, BASE_64_EXPRESSION, true); assertNull(expressionElement); }
@Test void testPrepareSetVariableAssertionNotENVEmptyValue() throws DocumentParseException { Element setVariableAssertionElement = createSetVariableAssertion(document, "my-var", null); document.appendChild(setVariableAssertionElement); String prefix = "prefix"; setVariableAssertionBuilder.buildAssertionElement(setVariableAssertionElement, policyBuilderContext); Element nameElement = getSingleElement(setVariableAssertionElement, VARIABLE_TO_SET); assertEquals("my-var", nameElement.getAttribute(PolicyEntityBuilder.STRING_VALUE)); Element expressionElement = getSingleElement(setVariableAssertionElement, BASE_64_EXPRESSION); String b64 = expressionElement.getAttribute(PolicyEntityBuilder.STRING_VALUE); assertTrue(b64.isEmpty()); }
@Test void testPrepareSetVariableAssertionNotENVTextNode() throws DocumentParseException { Element setVariableAssertion = document.createElement(SET_VARIABLE); document.appendChild(setVariableAssertion); Element expression = document.createElement(EXPRESSION); expression.setTextContent("my \n Text \r\n Content"); setVariableAssertion.appendChild(expression); Element variableToSet = document.createElement(VARIABLE_TO_SET); variableToSet.setAttribute(PolicyEntityBuilder.STRING_VALUE, "my-var"); setVariableAssertion.appendChild(variableToSet); String prefix = "prefix"; setVariableAssertionBuilder.buildAssertionElement(setVariableAssertion, policyBuilderContext); Element nameElement = getSingleElement(setVariableAssertion, VARIABLE_TO_SET); assertEquals("my-var", nameElement.getAttribute(PolicyEntityBuilder.STRING_VALUE)); Element expressionElement = getSingleElement(setVariableAssertion, BASE_64_EXPRESSION); String b64 = expressionElement.getAttribute(PolicyEntityBuilder.STRING_VALUE); assertEquals(Base64.getEncoder().encodeToString("my \n Text \r\n Content".getBytes(StandardCharsets.UTF_8)), b64); }
@Test void testPrepareSetVariableAssertionNotENVElementNode() { Element setVariableAssertion = document.createElement(SET_VARIABLE); document.appendChild(setVariableAssertion); Element expression = document.createElement(EXPRESSION); expression.appendChild(document.createElement("ashdkjsah")); setVariableAssertion.appendChild(expression); assertThrows(EntityBuilderException.class, () -> setVariableAssertionBuilder.buildAssertionElement(setVariableAssertion, policyBuilderContext)); }