Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KNOX-2947 - Deleting provider via hadoop xml resource should check if the provider is used by a descriptor #783

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,7 @@ public interface HadoopXmlResourceMessages {

@Message(level = MessageLevel.INFO, text = "Deleting file {0}")
void deleteFile(String name);

@Message(level = MessageLevel.WARN, text = "Not deleting provider {0} as it is referenced by on ore more descriptors.")
void notDeletingReferenceProvider(String provider);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@
package org.apache.knox.gateway.topology.hadoop.xml;

import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.knox.gateway.i18n.messages.MessagesFactory;
import org.apache.knox.gateway.topology.simple.ProviderConfiguration;
import org.apache.knox.gateway.topology.simple.SimpleDescriptor;

class HadoopXmlResourceParserResult {
private static final HadoopXmlResourceMessages LOG = MessagesFactory.get(HadoopXmlResourceMessages.class);
final Map<String, ProviderConfiguration> providers;
final Set<SimpleDescriptor> descriptors;
private final Set<String> deletedDescriptors;
Expand All @@ -38,7 +42,21 @@ class HadoopXmlResourceParserResult {
this.providers = providers;
this.descriptors = descriptors;
this.deletedDescriptors = deletedDescriptors;
this.deletedProviders = deletedProviders;
this.deletedProviders = nonReferencedProviders(deletedProviders, descriptors);
}

private Set<String> nonReferencedProviders(Set<String> deletedProviders, Set<SimpleDescriptor> descriptors) {
Set<String> referencedProviders = descriptors.stream()
.map(SimpleDescriptor::getProviderConfig).collect(Collectors.toSet());
Set<String> result = new HashSet<>();
for (String provider : deletedProviders) {
if (referencedProviders.contains(provider)) {
LOG.notDeletingReferenceProvider(provider);
} else {
result.add(provider);
}
}
return result;
}

public Map<String, ProviderConfiguration> getProviders() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,13 @@ public void testDelete() throws Exception {
assertEquals(new HashSet<>(Arrays.asList("admin", "knoxsso")), result.getDeletedProviders());
}

@Test
public void testReferencedProviderIsNotDeleted() throws Exception {
String testConfigPath = this.getClass().getClassLoader().getResource("testDelete2.xml").getPath();
HadoopXmlResourceParserResult result = hadoopXmlResourceParser.parse(testConfigPath);
assertEquals(new HashSet<>(Arrays.asList("unused")), result.getDeletedProviders());
}

private void validateTopology1Descriptors(SimpleDescriptor descriptor) {
assertTrue(descriptor.isReadOnly());
assertEquals("topology1", descriptor.getName());
Expand Down
39 changes: 39 additions & 0 deletions gateway-topology-hadoop-xml/src/test/resources/testDelete2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<configuration>
<property>
<name>descriptor_name1</name>
<value>
discoveryType=ClouderaManager#
discoveryAddress=http://host:123#
discoveryUser=user#
discoveryPasswordAlias=alias#
cluster=Cluster 1#
providerConfigRef=used#
app:knoxauth:param1.name=param1.value#
app:admin-ui#
HIVE:url=http://localhost:456#
HIVE:version=1.0#
HIVE:httpclient.connectionTimeout=5m#
HIVE:httpclient.socketTimeout=100m
</value>
</property>
<property>
<name>providerConfigs:used, unused</name>
<value>remove</value>
</property>
</configuration>
Loading