UpgradeCatalog220 extends AbstractUpgradeCatalog { protected void updateKnoxTopology() throws AmbariException { AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class); for (final Cluster cluster : getCheckedClusterMap(ambariManagementController.getClusters()).values()) { Config topology = cluster.getDesiredConfigByType(TOPOLOGY_CONFIG); if (topology != null) { String content = topology.getProperties().get(CONTENT_PROPERTY); if (content != null) { Document topologyXml = convertStringToDocument(content); if (topologyXml != null) { Element root = topologyXml.getDocumentElement(); if (root != null) { NodeList providerNodes = root.getElementsByTagName("provider"); boolean authorizationProviderExists = false; try { for (int i = 0; i < providerNodes.getLength(); i++) { Node providerNode = providerNodes.item(i); NodeList childNodes = providerNode.getChildNodes(); for (int k = 0; k < childNodes.getLength(); k++) { Node child = childNodes.item(k); child.normalize(); String childTextContent = child.getTextContent(); if (childTextContent != null && childTextContent.toLowerCase().equals("authorization")) { authorizationProviderExists = true; break; } } if (authorizationProviderExists) { break; } } } catch(Exception e) { e.printStackTrace(); LOG.error("Error occurred during check 'authorization' provider already exists in topology." + e); return; } if (!authorizationProviderExists) { NodeList nodeList = root.getElementsByTagName("gateway"); if (nodeList != null && nodeList.getLength() > 0) { boolean rangerPluginEnabled = isConfigEnabled(cluster, AbstractUpgradeCatalog.CONFIGURATION_TYPE_RANGER_KNOX_PLUGIN_PROPERTIES, AbstractUpgradeCatalog.PROPERTY_RANGER_KNOX_PLUGIN_ENABLED); Node gatewayNode = nodeList.item(0); Element newProvider = topologyXml.createElement("provider"); Element role = topologyXml.createElement("role"); role.appendChild(topologyXml.createTextNode("authorization")); newProvider.appendChild(role); Element name = topologyXml.createElement("name"); if (rangerPluginEnabled) { name.appendChild(topologyXml.createTextNode("XASecurePDPKnox")); } else { name.appendChild(topologyXml.createTextNode("AclsAuthz")); } newProvider.appendChild(name); Element enabled = topologyXml.createElement("enabled"); enabled.appendChild(topologyXml.createTextNode("true")); newProvider.appendChild(enabled); gatewayNode.appendChild(newProvider); DOMSource topologyDomSource = new DOMSource(root); StringWriter writer = new StringWriter(); try { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http: transformer.transform(topologyDomSource, new StreamResult(writer)); } catch (TransformerConfigurationException e) { e.printStackTrace(); LOG.error("Unable to create transformer instance, to convert Document(XML) to String. " + e); return; } catch (TransformerException e) { e.printStackTrace(); LOG.error("Unable to transform Document(XML) to StringWriter. " + e); return; } content = writer.toString(); Map<String, String> updates = Collections.singletonMap(CONTENT_PROPERTY, content); updateConfigurationPropertiesForCluster(cluster, TOPOLOGY_CONFIG, updates, true, false); } } } } } } } } @Inject  UpgradeCatalog220(Injector injector); @Override String getTargetVersion(); @Override String getSourceVersion(); @Transactional void bootstrapRepoVersionForHDP21(); static final String UPGRADE_PACKAGE_COL; static final String UPGRADE_TYPE_COL; static final String REPO_VERSION_TABLE; }
@Test public void testupdateKnoxTopology_NoRangerPlugin() throws Exception { EasyMockSupport easyMockSupport = new EasyMockSupport(); final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class); final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class); final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class); final Map<String, String> propertiesTopologyWithoutAuthorizationProvider = new HashMap<String, String>() {{ put("content", "<topology> <gateway> </gateway> </topology>"); }}; final Map<String, String> propertiesTopologyExpected = new HashMap<String, String>() {{ put("content", "<topology> <gateway> <provider>\n" + " <role>authorization</role>\n" + " <name>AclsAuthz</name>\n" + " <enabled>true</enabled>\n" + " </provider>\n" + " </gateway> </topology>\n"); }}; final Config mockTopologyConf = easyMockSupport.createNiceMock(Config.class); final Injector mockInjector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { bind(AmbariManagementController.class).toInstance(mockAmbariManagementController); bind(Clusters.class).toInstance(mockClusters); bind(EntityManager.class).toInstance(entityManager); bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class)); bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class)); } }); expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once(); expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{ put("cl1", mockClusterExpected); }}).atLeastOnce(); expect(mockClusterExpected.getDesiredConfigByType("topology")).andReturn(mockTopologyConf).atLeastOnce(); expect(mockTopologyConf.getProperties()).andReturn(propertiesTopologyWithoutAuthorizationProvider).once(); UpgradeCatalog220 upgradeCatalog220 = createMockBuilder(UpgradeCatalog220.class) .withConstructor(Injector.class) .withArgs(mockInjector) .addMockedMethod("updateConfigurationPropertiesForCluster", Cluster.class, String.class, Map.class, boolean.class, boolean.class) .createMock(); upgradeCatalog220.updateConfigurationPropertiesForCluster(mockClusterExpected, "topology", propertiesTopologyExpected, true, false); expectLastCall().once(); easyMockSupport.replayAll(); replay(upgradeCatalog220); upgradeCatalog220.updateKnoxTopology(); easyMockSupport.verifyAll(); }
@Test public void testupdateKnoxTopology_ProviderAlreadyExists() throws Exception { EasyMockSupport easyMockSupport = new EasyMockSupport(); final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class); final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class); final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class); final Map<String, String> propertiesTopologyWitAuthorizationProvider = new HashMap<String, String>() {{ put("content", "<topology> <gateway> <provider>" + "<role>authorization</role>" + "<name>AclsAuthz</name>" + "<enabled>true</enabled>" + "</provider>" + "</gateway> </topology>\n"); }}; final Config mockTopologyConf = easyMockSupport.createNiceMock(Config.class); final Injector mockInjector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { bind(AmbariManagementController.class).toInstance(mockAmbariManagementController); bind(Clusters.class).toInstance(mockClusters); bind(EntityManager.class).toInstance(entityManager); bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class)); bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class)); } }); expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once(); expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{ put("cl1", mockClusterExpected); }}).atLeastOnce(); expect(mockClusterExpected.getDesiredConfigByType("topology")).andReturn(mockTopologyConf).atLeastOnce(); expect(mockTopologyConf.getProperties()).andReturn(propertiesTopologyWitAuthorizationProvider).once(); UpgradeCatalog220 upgradeCatalog220 = createMockBuilder(UpgradeCatalog220.class) .withConstructor(Injector.class) .withArgs(mockInjector) .addMockedMethod("updateConfigurationPropertiesForCluster", Cluster.class, String.class, Map.class, boolean.class, boolean.class) .createMock(); easyMockSupport.replayAll(); replay(upgradeCatalog220); upgradeCatalog220.updateKnoxTopology(); easyMockSupport.verifyAll(); }
@Test public void testupdateKnoxTopology_RangerPluginAvailable() throws Exception { EasyMockSupport easyMockSupport = new EasyMockSupport(); final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class); final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class); final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class); final Map<String, String> propertiesTopologyWithoutAuthorizationProvider = new HashMap<String, String>() {{ put("content", "<topology> <gateway> </gateway> </topology>"); }}; final Map<String, String> propertiesRangerKnoxPluginProperties = new HashMap<String, String>() {{ put("ranger-knox-plugin-enabled", "Yes"); }}; final Map<String, String> propertiesTopologyExpected = new HashMap<String, String>() {{ put("content", "<topology> <gateway> <provider>\n" + " <role>authorization</role>\n" + " <name>XASecurePDPKnox</name>\n" + " <enabled>true</enabled>\n" + " </provider>\n" + " </gateway> </topology>\n"); }}; final Config mockTopologyConf = easyMockSupport.createNiceMock(Config.class); final Config mockRangerKnoxPluginConf = easyMockSupport.createNiceMock(Config.class); final Injector mockInjector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { bind(AmbariManagementController.class).toInstance(mockAmbariManagementController); bind(Clusters.class).toInstance(mockClusters); bind(EntityManager.class).toInstance(entityManager); bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class)); bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class)); } }); expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once(); expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{ put("cl1", mockClusterExpected); }}).atLeastOnce(); expect(mockClusterExpected.getDesiredConfigByType("topology")).andReturn(mockTopologyConf).atLeastOnce(); expect(mockClusterExpected.getDesiredConfigByType("ranger-knox-plugin-properties")).andReturn(mockRangerKnoxPluginConf).atLeastOnce(); expect(mockTopologyConf.getProperties()).andReturn(propertiesTopologyWithoutAuthorizationProvider).once(); expect(mockRangerKnoxPluginConf.getProperties()).andReturn(propertiesRangerKnoxPluginProperties).once(); UpgradeCatalog220 upgradeCatalog220 = createMockBuilder(UpgradeCatalog220.class) .withConstructor(Injector.class) .withArgs(mockInjector) .addMockedMethod("updateConfigurationPropertiesForCluster", Cluster.class, String.class, Map.class, boolean.class, boolean.class) .createMock(); upgradeCatalog220.updateConfigurationPropertiesForCluster(mockClusterExpected, "topology", propertiesTopologyExpected, true, false); expectLastCall().once(); easyMockSupport.replayAll(); replay(upgradeCatalog220); upgradeCatalog220.updateKnoxTopology(); easyMockSupport.verifyAll(); }