Skip to content

Commit

Permalink
Validate that cloud name is valid label value.
Browse files Browse the repository at this point in the history
Currently there is no validation for cloud name,
but its value is being used in label expression while talking
to Hetzner cloud API.

Add validation to config page and enhance documentation,
so it's clear what is expected there.

Fixes: #75

Signed-off-by: Richard Kosegi <richard.kosegi@gmail.com>
  • Loading branch information
rkosegi committed Sep 26, 2023
1 parent be404e0 commit 8acf551
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.SimpleFormatter;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static cloud.dnation.jenkins.plugins.hetzner.HetznerConstants.SHUTDOWN_TIME_BUFFER;

@UtilityClass
public class Helper {
private static final Pattern LABEL_VALUE_RE = Pattern.compile("^(?![0-9]+$)(?!-)[a-zA-Z0-9-_.]{0,63}(?<!-)$");
private static final String SSH_RSA = "ssh-rsa";

/**
Expand Down Expand Up @@ -202,4 +204,11 @@ public static List<HetznerServerAgent> getHetznerAgents() {
.map(HetznerServerAgent.class::cast)
.collect(Collectors.toList());
}

public static boolean isValidLabelValue(String value) {
if (Strings.isNullOrEmpty(value)) {
return false;
}
return LABEL_VALUE_RE.matcher(value).matches();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,15 @@ public FormValidation doVerifyConfiguration(@QueryParameter String credentialsId
}
}

@Restricted(NoExternalUse.class)
@RequirePOST
public FormValidation doCheckCloudName(@QueryParameter String name) {
if (Helper.isValidLabelValue(name)) {

Check warning on line 213 in src/main/java/cloud/dnation/jenkins/plugins/hetzner/HetznerCloud.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 213 is only partially covered, one branch is missing
return FormValidation.ok();
}
return FormValidation.error("Cloud name is not a valid label value: %s", name);

Check warning on line 216 in src/main/java/cloud/dnation/jenkins/plugins/hetzner/HetznerCloud.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 216 is not covered by tests
}

@Restricted(NoExternalUse.class)
@RequirePOST
public ListBoxModel doFillCredentialsIdItems(@AncestorInPath Item owner) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form" xmlns:c="/lib/credentials" xmlns:st="jelly:stapler">
<f:entry title="${%Name}" field="name">
<f:textbox default="hetzner"/>
<f:textbox default="hetzner" checkUrl="'${rootURL}/descriptorByName/cloud.dnation.jenkins.plugins.hetzner.HetznerCloud/checkCloudName?name='+escape(this.value)" />
</f:entry>
<f:entry field="instanceCapStr" title="${%Instance Cap}">
<f:textbox default="10"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@
limitations under the License.
-->
<div>
Provide a name for this Hetzner Cloud
Provide a name for this Hetzner Cloud.
Must be a valid <a href="https://docs.hetzner.cloud/#labels">label value</a>.
<p>
<i>Valid label values must be a string of 63 characters or less and must be empty or begin and end with an alphanumeric character ([a-z0-9A-Z]) with dashes (-), underscores (_), dots (.), and alphanumerics between.</i>
</p>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,13 @@ public void testIsPossiblyLong() {
assertFalse(Helper.isPossiblyLong("0"));
assertFalse(Helper.isPossiblyLong("not-a-number"));
}

@Test
public void testIsValidLabelValue() {
assertFalse(Helper.isValidLabelValue(""));
assertFalse(Helper.isValidLabelValue(null));
assertTrue(Helper.isValidLabelValue("cloud-01"));
assertTrue(Helper.isValidLabelValue("cloud_01"));
assertFalse(Helper.isValidLabelValue("cloud 01"));
}
}

0 comments on commit 8acf551

Please sign in to comment.