Skip to content

Commit 9871ac4

Browse files
committed
[BUG] 🐛 Fix bug when user have no groups
1 parent 0766019 commit 9871ac4

File tree

1 file changed

+56
-59
lines changed

1 file changed

+56
-59
lines changed

src/main/java/io/insee/dev/k8sonboarding/service/OnboardingService.java

Lines changed: 56 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.insee.dev.k8sonboarding.service;
22

3+
import java.util.ArrayList;
34
import java.util.List;
45
import java.util.Map;
56
import java.util.regex.Matcher;
@@ -34,7 +35,7 @@
3435
@Service
3536
public class OnboardingService {
3637

37-
private static final Logger logger = LoggerFactory.getLogger(OnboardingService.class);
38+
private static final Logger logger = LoggerFactory.getLogger(OnboardingService.class);
3839

3940
public static final String ADMIN = "admin";
4041
public static final String API_GROUP = "rbac.authorization.k8s.io";
@@ -43,14 +44,14 @@ public class OnboardingService {
4344
public static final String LABEL_CREATED_BY = "created_by";
4445
public static final String CLUSTER_ROLE = "ClusterRole";
4546

46-
public static final String NO_QUOTA_VALUE="0";
47+
public static final String NO_QUOTA_VALUE = "0";
4748
public static final String RESOURCE_QUOTA_REQUESTS_STORAGE = "requests.storage";
4849

4950
@Value("${spring.application.name:k8s-onboarding}")
5051
private String appName;
5152

5253
@Autowired
53-
QuotaProperties quotaProperties;
54+
QuotaProperties quotaProperties;
5455

5556
@Autowired
5657
ClusterProperties clusterProperty;
@@ -92,52 +93,50 @@ public void createNamespace(User user, String groupId) {
9293
.addToLabels(LABEL_CREATED_BY, appName).endMetadata().build();
9394
kubernetesClient.namespaces().resource(ns).create();
9495

95-
applyQuotas(namespaceId, quotaProperties, true);
96+
applyQuotas(namespaceId, quotaProperties, true);
9697
}
9798
}
9899

99-
/**
100-
*
101-
* @param namespaceId
102-
* @param inputQuota
103-
* @param overrideExisting
104-
*/
105-
private void applyQuotas(String namespaceId, QuotaProperties inputQuota, boolean overrideExisting) {
106-
ResourceQuotaBuilder resourceQuotaBuilder = new ResourceQuotaBuilder();
107-
resourceQuotaBuilder.withNewMetadata()
108-
.withLabels(Map.of(LABEL_CREATED_BY, appName))
109-
.withName(namespaceId)
110-
.withNamespace(namespaceId)
111-
.endMetadata();
112-
113-
Map<String, String> quotasToApply = inputQuota.asMap();
114-
115-
if (quotasToApply.entrySet().stream().filter(e -> e.getValue() != null).count() == 0) {
116-
return;
117-
}
118-
119-
ResourceQuotaFluent.SpecNested<ResourceQuotaBuilder> resourceQuotaBuilderSpecNested = resourceQuotaBuilder
120-
.withNewSpec();
121-
quotasToApply.entrySet().stream().filter(e -> e.getValue() != null).forEach(e -> resourceQuotaBuilderSpecNested.addToHard(e.getKey(),Quantity.parse(e.getValue())));
122-
resourceQuotaBuilderSpecNested.endSpec();
123-
124-
ResourceQuota quota = resourceQuotaBuilder.build();
125-
if (overrideExisting) {
126-
kubernetesClient.resource(quota).inNamespace(namespaceId).createOrReplace();
127-
}
128-
else {
129-
try {
130-
kubernetesClient.resource(quota).inNamespace(namespaceId).create();
131-
}
132-
catch (KubernetesClientException e) {
133-
if (e.getCode() != 409) {
134-
// This is not a "quota already in place" error
135-
throw e;
136-
}
137-
}
138-
}
139-
}
100+
/**
101+
*
102+
* @param namespaceId
103+
* @param inputQuota
104+
* @param overrideExisting
105+
*/
106+
private void applyQuotas(String namespaceId, QuotaProperties inputQuota, boolean overrideExisting) {
107+
ResourceQuotaBuilder resourceQuotaBuilder = new ResourceQuotaBuilder();
108+
resourceQuotaBuilder.withNewMetadata()
109+
.withLabels(Map.of(LABEL_CREATED_BY, appName))
110+
.withName(namespaceId)
111+
.withNamespace(namespaceId)
112+
.endMetadata();
113+
114+
Map<String, String> quotasToApply = inputQuota.asMap();
115+
116+
if (quotasToApply.entrySet().stream().filter(e -> e.getValue() != null).count() == 0) {
117+
return;
118+
}
140119

120+
ResourceQuotaFluent.SpecNested<ResourceQuotaBuilder> resourceQuotaBuilderSpecNested = resourceQuotaBuilder
121+
.withNewSpec();
122+
quotasToApply.entrySet().stream().filter(e -> e.getValue() != null)
123+
.forEach(e -> resourceQuotaBuilderSpecNested.addToHard(e.getKey(), Quantity.parse(e.getValue())));
124+
resourceQuotaBuilderSpecNested.endSpec();
125+
126+
ResourceQuota quota = resourceQuotaBuilder.build();
127+
if (overrideExisting) {
128+
kubernetesClient.resource(quota).inNamespace(namespaceId).createOrReplace();
129+
} else {
130+
try {
131+
kubernetesClient.resource(quota).inNamespace(namespaceId).create();
132+
} catch (KubernetesClientException e) {
133+
if (e.getCode() != 409) {
134+
// This is not a "quota already in place" error
135+
throw e;
136+
}
137+
}
138+
}
139+
}
141140

142141
/**
143142
* Currently, namespaceid is ignored
@@ -170,15 +169,13 @@ public RoleBinding addPermissionsToNamespace(User user, String group) {
170169
return null;
171170
}
172171

173-
174-
175-
public boolean checkNamespaceExists(String namespaceId) {
172+
public boolean checkNamespaceExists(String namespaceId) {
176173
final Namespace namespace = kubernetesClient.namespaces().withName(namespaceId).get();
177174
return namespace != null;
178175
}
179176

180177
public boolean checkPermissionsExists(String namespaceId) {
181-
final RoleBinding roleBinding = kubernetesClient.rbac().roleBindings().inNamespace(namespaceId)
178+
final RoleBinding roleBinding = kubernetesClient.rbac().roleBindings().inNamespace(namespaceId)
182179
.withName(clusterProperty.getNameNamespaceAdmin()).get();
183180
return (roleBinding != null && !roleBinding.getSubjects().isEmpty());
184181
}
@@ -206,8 +203,8 @@ public String getNamespaceId(User user, String group) {
206203
}
207204

208205
private String optionallyRemoveSuffix(String rawGroup) {
209-
if (doesRemoveSuffix){
210-
rawGroup=StringUtils.substringBefore(rawGroup, '_');
206+
if (doesRemoveSuffix) {
207+
rawGroup = StringUtils.substringBefore(rawGroup, '_');
211208
return rawGroup;
212209
}
213210
return rawGroup;
@@ -240,23 +237,23 @@ public void setKubernetesClient(KubernetesClient kubernetesClient) {
240237
}
241238

242239
public List<AllowedGroup> getAllowedAndFilteredGroupsForUser(User user) {
243-
List<String> allGroups = user.getGroups();
240+
List<String> allGroups = user.getGroups() == null ? new ArrayList<>() : user.getGroups();
244241
return allGroups
245-
.stream()
246-
.filter(
247-
this::checkGroupMatchesFilter
248-
).map(
249-
group -> new AllowedGroup(optionallyRemoveSuffix(group),group)
250-
).collect(Collectors.toList());
242+
.stream()
243+
.filter(
244+
this::checkGroupMatchesFilter)
245+
.map(
246+
group -> new AllowedGroup(optionallyRemoveSuffix(group), group))
247+
.collect(Collectors.toList());
251248
}
252249

253250
@Value("${io.insee.dev.k8sonboarding.ui.groupFilter:.*}")
254251
private String groupFilter;
252+
255253
private boolean checkGroupMatchesFilter(String group) {
256254
var groupFilterPattern = Pattern.compile(groupFilter);
257255
Matcher m = groupFilterPattern.matcher(group);
258256
return m.matches();
259257
}
260258

261-
262259
}

0 commit comments

Comments
 (0)