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

Validate that cloud name is valid label value. #76

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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 @@
}
}

@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"));
}
}