Skip to content

Commit

Permalink
Fix: prevent invalid helm names (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
olevitt authored Sep 7, 2023
1 parent 565a166 commit 57cfa20
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeoutException;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
Expand All @@ -23,6 +24,9 @@ public class HelmInstallService {

private final Logger logger = LoggerFactory.getLogger(HelmInstallService.class);

private final Pattern helmNamePattern =
Pattern.compile("^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$");

public HelmInstallService() {}

public HelmInstaller installChart(
Expand All @@ -36,7 +40,8 @@ public HelmInstaller installChart(
Map<String, String> env,
final boolean skipTlsVerify,
String caFile)
throws InvalidExitValueException, IOException, InterruptedException, TimeoutException {
throws InvalidExitValueException, IOException, InterruptedException, TimeoutException,
IllegalArgumentException {
String command = "helm upgrade --install ";
if (skipTlsVerify) {
command = command.concat("--insecure-skip-tls-verify ");
Expand All @@ -45,7 +50,14 @@ public HelmInstaller installChart(
command.concat(
"--ca-file " + System.getenv("CACERTS_DIR") + "/" + caFile + " ");
}

if (name != null) {
if (!helmNamePattern.matcher(name).matches() || name.length() > 53) {
throw new IllegalArgumentException(
"Invalid release name "
+ name
+ " , must match regex ^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$ and the length must not be longer than 53");
}
command = command.concat(name + " ");
} else {
command = command.concat("--generate-name ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,21 +160,24 @@ public String getInitScript(
mapperHelm.writeValue(values, fusion);
String namespaceId =
kubernetesService.determineNamespaceAndCreateIfNeeded(region, project, user);
HelmInstaller res =
getHelmInstallService()
.installChart(
getHelmConfiguration(region, user),
catalogId + "/" + pkg.getName(),
namespaceId,
requestDTO.getName(),
requestDTO.getPackageVersion(),
requestDTO.isDryRun(),
values,
null,
skipTlsVerify,
caFile);
values.delete();
return List.of(res.getManifest());
try {
HelmInstaller res =
getHelmInstallService()
.installChart(
getHelmConfiguration(region, user),
catalogId + "/" + pkg.getName(),
namespaceId,
requestDTO.getName(),
requestDTO.getPackageVersion(),
requestDTO.isDryRun(),
values,
null,
skipTlsVerify,
caFile);
return List.of(res.getManifest());
} catch (IllegalArgumentException e) {
throw new AccessDeniedException(e.getMessage());
}
}

@Override
Expand Down

0 comments on commit 57cfa20

Please sign in to comment.