-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce the concept of an hybrid ccloud provider, to set acls via a…
…dmin client and translation via api (#492)
- Loading branch information
Showing
4 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
src/main/java/com/purbon/kafka/topology/roles/HybridCCloudAclsProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.purbon.kafka.topology.roles; | ||
|
||
import com.purbon.kafka.topology.AccessControlProvider; | ||
import com.purbon.kafka.topology.Configuration; | ||
import com.purbon.kafka.topology.api.adminclient.TopologyBuilderAdminClient; | ||
import com.purbon.kafka.topology.api.ccloud.CCloudApi; | ||
import com.purbon.kafka.topology.utils.CCloudUtils; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
public class HybridCCloudAclsProvider extends SimpleAclsProvider implements AccessControlProvider { | ||
|
||
private static final Logger LOGGER = LogManager.getLogger(HybridCCloudAclsProvider.class); | ||
|
||
private final CCloudApi cli; | ||
private final String clusterId; | ||
private final Configuration config; | ||
private CCloudUtils cCloudUtils; | ||
|
||
public HybridCCloudAclsProvider( | ||
final TopologyBuilderAdminClient adminClient, final Configuration config) throws IOException { | ||
super(adminClient); | ||
this.cli = new CCloudApi(config.getConfluentCloudClusterUrl(), config); | ||
this.clusterId = config.getConfluentCloudClusterId(); | ||
this.config = config; | ||
this.cCloudUtils = new CCloudUtils(config); | ||
} | ||
|
||
@Override | ||
public void createBindings(Set<TopologyAclBinding> bindings) throws IOException { | ||
var serviceAccountIdByNameMap = cCloudUtils.initializeLookupTable(this.cli); | ||
var mayBeTranslated = bindings | ||
.stream() | ||
.map(binding -> { | ||
try { | ||
return cCloudUtils.translateIfNecessary(binding, serviceAccountIdByNameMap); | ||
} catch (IOException e) { | ||
LOGGER.error(e); | ||
return binding; | ||
} | ||
}).collect(Collectors.toSet()); | ||
super.createBindings(mayBeTranslated); | ||
} | ||
|
||
@Override | ||
public void clearBindings(Set<TopologyAclBinding> bindings) throws IOException { | ||
var serviceAccountIdByNameMap = cCloudUtils.initializeLookupTable(this.cli); | ||
for (TopologyAclBinding binding : bindings) { | ||
adminClient.clearAcls(cCloudUtils.translateIfNecessary(binding, serviceAccountIdByNameMap)); | ||
} | ||
} | ||
|
||
@Override | ||
public Map<String, List<TopologyAclBinding>> listAcls() { | ||
try { | ||
Map<String, List<TopologyAclBinding>> bindings = new HashMap<>(); | ||
for (TopologyAclBinding binding : cli.listAcls(clusterId)) { | ||
String resourceName = binding.getResourceName(); | ||
if (!bindings.containsKey(resourceName)) { | ||
bindings.put(resourceName, new ArrayList<>()); | ||
} | ||
bindings.get(resourceName).add(binding); | ||
} | ||
return bindings; | ||
} catch (IOException e) { | ||
LOGGER.warn(e); | ||
return Collections.emptyMap(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters