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

Issue30 builderclasses #60

Merged
merged 18 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from 14 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
7 changes: 5 additions & 2 deletions src/main/java/io/aiven/klaw/auth/KwAuthenticationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,11 @@ private DirContextOperations searchForUser(DirContext context, String username)
String searchRoot =
this.adRootDn != null ? this.adRootDn : this.searchRootFromPrincipal(bindPrincipal);
String searchFilterUpdated;
if (adFilter != null && !adFilter.equals("")) searchFilterUpdated = adFilter;
else searchFilterUpdated = searchFilter;
if (adFilter != null && !adFilter.equals("")) {
searchFilterUpdated = adFilter;
} else {
searchFilterUpdated = searchFilter;
}

try {
return SpringSecurityLdapTemplate.searchForSingleEntryInternal(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,21 @@ private String validateUrl(String urlFromAddressBar) {
if (urlFromAddressBar != null) {
urlFromAddressBar = urlFromAddressBar.replace(contextPath + "/", "");

if (urlFromAddressBar.contains("login")) urlFromAddressBar = "index";
if (urlFromAddressBar.contains("login")) {
urlFromAddressBar = "index";
}

if (!urlFromAddressBar.contains("loggedin=true")) {
if (urlFromAddressBar.contains("?")) urlFromAddressBar += "&loggedin=true";
else urlFromAddressBar += "?loggedin=true";
if (urlFromAddressBar.contains("?")) {
urlFromAddressBar += "&loggedin=true";
} else {
urlFromAddressBar += "?loggedin=true";
}
}

} else urlFromAddressBar = "";
} else {
urlFromAddressBar = "";
}

return urlFromAddressBar;
}
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/io/aiven/klaw/auth/KwRequestFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ public Authentication attemptAuthentication(
if ("saas".equals(kwInstallationType)) {
String gRecaptchaResponse = request.getParameter("g-recaptcha-response");
boolean captchaResponse = validateCaptchaService.validateCaptcha(gRecaptchaResponse);
if (!captchaResponse) throw new AuthenticationServiceException("Invalid Captcha.");
if (!captchaResponse) {
throw new AuthenticationServiceException("Invalid Captcha.");
}
}

if ("ad".equals(authenticationType)) {
Expand All @@ -68,7 +70,9 @@ public Authentication attemptAuthentication(
// User in KW db
return super.attemptAuthentication(request, response);
}
} else return super.attemptAuthentication(request, response);
} else {
return super.attemptAuthentication(request, response);
}
}

@Override
Expand Down
89 changes: 50 additions & 39 deletions src/main/java/io/aiven/klaw/config/ManageDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,6 @@ public class ManageDatabase implements ApplicationContextAware {
// key tenantId, sub key clusterid Pertenant
private static Map<Integer, Map<Integer, KwClusters>> kwKafkaConnectClustersPertenant;

// private static List<Env> kafkaEnvList;

// private static List<Env> kafkaConnectEnvList;
//
// private static List<Env> schemaEnvList;

private static Map<Integer, List<Env>> kafkaEnvListPerTenant = new HashMap<>();
private static Map<Integer, List<Env>> schemaRegEnvListPerTenant = new HashMap<>();
private static Map<Integer, List<Env>> kafkaConnectEnvListPerTenant = new HashMap<>();
Expand Down Expand Up @@ -136,9 +130,10 @@ public void loadDb() throws Exception {
private void loadStaticDataToDb() {
// add tenant
Optional<KwTenants> kwTenants = handleDbRequests.getMyTenants(KwConstants.DEFAULT_TENANT_ID);
if (kwTenants.isEmpty())
if (kwTenants.isEmpty()) {
handleDbRequests.addNewTenant(
defaultDataService.getDefaultTenant(KwConstants.DEFAULT_TENANT_ID));
}

// add teams
String infraTeam = "INFRATEAM", stagingTeam = "STAGINGTEAM";
Expand Down Expand Up @@ -192,22 +187,16 @@ private void loadStaticDataToDb() {
String productName = "Klaw";
Optional<ProductDetails> productDetails = handleDbRequests.selectProductDetails(productName);
if (productDetails.isPresent()) {
if (!Objects.equals(productDetails.get().getVersion(), kwVersion))
if (!Objects.equals(productDetails.get().getVersion(), kwVersion)) {
handleDbRequests.insertProductDetails(
defaultDataService.getProductDetails(productName, kwVersion));
} else
}
} else {
handleDbRequests.insertProductDetails(
defaultDataService.getProductDetails(productName, kwVersion));
}
}

// @Autowired
// private CreateBulkTests createBulkTests;
// private void runPerfTests() {
// createBulkTests.createTopicRequests();
//// createBulkTests.approveTopicRequests();
//// createBulkTests.getTopics();
// }

private void checkSSOAuthentication() {
if ("db".equals(authenticationType) && "true".equals(ssoEnabled)) {
log.error(
Expand Down Expand Up @@ -252,22 +241,30 @@ public Integer getAllClustersSize() {
}

public List<Env> getKafkaEnvList(int tenantId) {
if (kafkaEnvListPerTenant.get(tenantId).isEmpty()) return new ArrayList<>();
if (kafkaEnvListPerTenant.get(tenantId).isEmpty()) {
return new ArrayList<>();
}
return kafkaEnvListPerTenant.get(tenantId);
}

public List<Env> getSchemaRegEnvList(int tenantId) {
if (schemaRegEnvListPerTenant.get(tenantId).isEmpty()) return new ArrayList<>();
if (schemaRegEnvListPerTenant.get(tenantId).isEmpty()) {
return new ArrayList<>();
}
return schemaRegEnvListPerTenant.get(tenantId);
}

public List<Env> getKafkaConnectEnvList(int tenantId) {
if (kafkaConnectEnvListPerTenant.get(tenantId).isEmpty()) return new ArrayList<>();
if (kafkaConnectEnvListPerTenant.get(tenantId).isEmpty()) {
return new ArrayList<>();
}
return kafkaConnectEnvListPerTenant.get(tenantId);
}

public List<Env> getAllEnvList(int tenantId) {
if (allEnvListPerTenant.get(tenantId).isEmpty()) return new ArrayList<>();
if (allEnvListPerTenant.get(tenantId).isEmpty()) {
return new ArrayList<>();
}
return allEnvListPerTenant.get(tenantId);
}

Expand Down Expand Up @@ -302,12 +299,14 @@ public Set<Integer> getTeamsForTenant(int tenantId) {

public Integer getTeamIdFromTeamName(int tenantId, String teamName) {
Optional<Map.Entry<Integer, String>> optionalTeam;
if (teamName != null)
if (teamName != null) {
optionalTeam =
teamIdAndNamePerTenant.get(tenantId).entrySet().stream()
.filter(a -> Objects.equals(a.getValue(), teamName))
.findFirst();
else return null;
} else {
return null;
}

// unknown team
return optionalTeam.map(Map.Entry::getKey).orElse(null);
Expand Down Expand Up @@ -345,14 +344,15 @@ public Map<String, Map<String, String>> getKwPropertiesMap(int tenantId) {
}

public String getKwPropertyValue(String kwKey, int tenantId) {
if (kwPropertiesMapPerTenant.get(tenantId) != null)
if (kwPropertiesMapPerTenant.get(tenantId) != null) {
return kwPropertiesMapPerTenant.get(tenantId).get(kwKey).get("kwvalue");
else return "";
} else {
return "";
}
}

private void loadEnvsForAllTenants() {
envsOfTenantsMap = new HashMap<>(); // key is tenantid, value is list of envs

for (Integer tenantId : tenantMap.keySet()) {
loadEnvsForOneTenant(tenantId);
}
Expand Down Expand Up @@ -394,7 +394,9 @@ private void loadTenantTeamsForAllTenants() {
}

public void loadTenantTeamsForOneTenant(List<Team> allTeams, Integer tenantId) {
if (allTeams == null) allTeams = handleDbRequests.selectAllTeams(tenantId);
if (allTeams == null) {
allTeams = handleDbRequests.selectAllTeams(tenantId);
}

Map<Integer, List<String>> teamsAndAllowedEnvs = new HashMap<>();
Map<Integer, String> teamsAndNames = new HashMap<>();
Expand All @@ -420,8 +422,9 @@ public Map<Integer, KwTenantConfigModel> getTenantConfig() {

public void setTenantConfig(TenantConfig config) {
KwTenantConfigModel tenantModel = config.getTenantModel();
if (tenantModel != null)
if (tenantModel != null) {
tenantConfig.put(getTenantIdFromName(tenantModel.getTenantName()), tenantModel);
}
}

private void loadKwPropertiesforAllTenants() {
Expand All @@ -439,10 +442,11 @@ private void loadKwPropertiesforAllTenants() {

public void loadKwPropsPerOneTenant(
Map<Integer, Map<String, Map<String, String>>> kwPropertiesMap, Integer tenantId) {
if (kwPropertiesMap == null) kwPropertiesMap = handleDbRequests.selectAllKwProperties();
if (kwPropertiesMap == null) {
kwPropertiesMap = handleDbRequests.selectAllKwProperties();
}

kwPropertiesMapPerTenant.put(tenantId, kwPropertiesMap.get(tenantId));

updateKwTenantConfigPerTenant(tenantId);
}

Expand Down Expand Up @@ -497,14 +501,17 @@ public void loadClustersForOneTenant(
List<KwClusters> schemaRegistryClusters,
List<KwClusters> kafkaConnectClusters,
Integer tenantId) {
if (kafkaClusters == null)
if (kafkaClusters == null) {
kafkaClusters = handleDbRequests.getAllClusters(KafkaClustersType.KAFKA.value, tenantId);
if (schemaRegistryClusters == null)
}
if (schemaRegistryClusters == null) {
schemaRegistryClusters =
handleDbRequests.getAllClusters(KafkaClustersType.SCHEMA_REGISTRY.value, tenantId);
if (kafkaConnectClusters == null)
}
if (kafkaConnectClusters == null) {
kafkaConnectClusters =
handleDbRequests.getAllClusters(KafkaClustersType.KAFKA_CONNECT.value, tenantId);
}

Map<Integer, KwClusters> kwKafkaClusters = new HashMap<>();
Map<Integer, KwClusters> kwSchemaRegClusters = new HashMap<>();
Expand All @@ -531,7 +538,6 @@ public void loadClustersForOneTenant(
kwAllClusters.put(cluster.getClusterId(), cluster);
});
kwKafkaConnectClustersPertenant.put(tenantId, kwKafkaConnectClusters);

kwAllClustersPertenant.put(tenantId, kwAllClusters);
}

Expand Down Expand Up @@ -622,9 +628,11 @@ public void loadEnvMapForOneTenant(Integer tenantId) {
List<String> partitions = new ArrayList<>();

for (int i = 1; i < maxPartitionsInt + 1; i++) {
if (defaultPartitions != null && defaultPartitions.equals(i + ""))
if (defaultPartitions != null && defaultPartitions.equals(i + "")) {
partitions.add(i + " (default)");
else partitions.add(i + "");
} else {
partitions.add(i + "");
}
}
oneEnvParamsMap.put("partitionsList", partitions);
} else if (param.startsWith("default.replication.factor")) {
Expand All @@ -638,8 +646,11 @@ public void loadEnvMapForOneTenant(Integer tenantId) {
List<String> rf = new ArrayList<>();

for (int i = 1; i < maxRfInt + 1; i++) {
if (defaultRf != null && defaultRf.equals(i + "")) rf.add(i + " (default)");
else rf.add(i + "");
if (defaultRf != null && defaultRf.equals(i + "")) {
rf.add(i + " (default)");
} else {
rf.add(i + "");
}
}

oneEnvParamsMap.put("replicationFactorList", rf);
Expand Down Expand Up @@ -748,6 +759,6 @@ public String deleteTenant(int tenantId) {
kwSchemaRegClustersPertenant.remove(tenantId);
kwKafkaConnectClustersPertenant.remove(tenantId);
kwAllClustersPertenant.remove(tenantId);
return "success";
return ApiResultStatus.SUCCESS.value;
}
}
23 changes: 15 additions & 8 deletions src/main/java/io/aiven/klaw/config/SecurityConfigNoSSO.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,11 @@ protected void configure(HttpSecurity http) throws Exception {

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
if (authenticationType != null && authenticationType.equals("db")) dbAuthentication(auth);
else if (authenticationType != null && authenticationType.equals("ldap"))
if (authenticationType != null && authenticationType.equals("db")) {
dbAuthentication(auth);
} else if (authenticationType != null && authenticationType.equals("ldap")) {
ldapAuthentication(auth);
else if (authenticationType != null && authenticationType.equals("ad")) {
} else if (authenticationType != null && authenticationType.equals("ad")) {
auth.authenticationProvider(activeDirectoryLdapAuthenticationProvider())
.userDetailsService(userDetailsService());
} else {
Expand Down Expand Up @@ -177,14 +178,18 @@ public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
provider.setConvertSubErrorCodesToExceptions(true);
provider.setUseAuthenticationRequestCredentials(true);

if (adFilter != null && !adFilter.equals("")) provider.setSearchFilter(adFilter);
if (adFilter != null && !adFilter.equals("")) {
provider.setSearchFilter(adFilter);
}
return provider;
}

private void ldapAuthentication(AuthenticationManagerBuilder auth) throws Exception {
try {
log.info("Ldap authentication configured.");
if (!checkLdapConnectivity()) throw new KlawException("Cannot connect to Ldap !!");
if (!checkLdapConnectivity()) {
throw new KlawException("Cannot connect to Ldap !!");
}

if (encryptionType != null && encryptionType.equals("bcrypt")) {
auth.ldapAuthentication()
Expand Down Expand Up @@ -217,7 +222,6 @@ public InMemoryUserDetailsManager inMemoryUserDetailsManager() throws Exception
final Properties globalUsers = new Properties();
if (authenticationType != null && authenticationType.equals("db")) {
log.info("Loading all users !!");

List<UserInfo> users;

try {
Expand All @@ -240,8 +244,11 @@ public InMemoryUserDetailsManager inMemoryUserDetailsManager() throws Exception
userInfo = iter.next();
try {
String secPwd = userInfo.getPwd();
if (secPwd != null && secPwd.equals("")) secPwd = "gfGF%64GFDd766hfgfHFD$%#453";
else secPwd = decodePwd(secPwd);
if (secPwd != null && secPwd.equals("")) {
secPwd = "gfGF%64GFDd766hfgfHFD$%#453";
} else {
secPwd = decodePwd(secPwd);
}
globalUsers.put(
userInfo.getUsername(),
encoder.encode(secPwd) + "," + userInfo.getRole() + ",enabled");
Expand Down
Loading