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

WX-1420 Fix GCP Batch label regex restriction #7355

Merged
merged 7 commits into from
Jan 11, 2024
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 @@ -18,13 +18,15 @@
object GcpLabel {

val MaxLabelLength = 63
val GoogleLabelRegexPattern = "[a-z]([-a-z0-9]*[a-z0-9])?"
val GoogleLabelRegex = GoogleLabelRegexPattern.r
val GoogleLabelKeyRegexPattern = "[a-z]([-a-z_0-9]*[a-z0-9])?" // label key must start with letter
val GoogleLabelValueRegexPattern = "[a-z0-9]([-a-z_0-9]*[a-z0-9])?"
val GoogleLabelKeyRegex = GoogleLabelKeyRegexPattern.r
val GoogleLabelValueRegex = GoogleLabelValueRegexPattern.r

// This function is used to coerce a string into one that meets the requirements for a label submission to Google Pipelines API.
// See 'labels' in https://cloud.google.com/genomics/reference/rpc/google.genomics.v1alpha2#google.genomics.v1alpha2.RunPipelineArgs
// This function is used to coerce a string into one that meets the requirements for a label submission to Google Batch API.
// https://cloud.google.com/compute/docs/labeling-resources#requirements
def safeGoogleName(mainText: String, emptyAllowed: Boolean = false): String =
validateLabelRegex(mainText) match {
validateLabelRegex(mainText, true) match {
case Valid(labelText) => labelText
case invalid @ _ if mainText.equals("") && emptyAllowed => mainText
case invalid @ _ =>
Expand Down Expand Up @@ -57,15 +59,19 @@
}
}

def validateLabelRegex(s: String): ErrorOr[String] =
(GoogleLabelRegex.pattern.matcher(s).matches, s.length <= MaxLabelLength) match {
def validateLabelRegex(s: String, isKey: Boolean): ErrorOr[String] = {
val regexPattern = if (isKey) GoogleLabelKeyRegex.pattern else GoogleLabelValueRegex.pattern
val field = if (isKey) "label key" else "label value"

(regexPattern.matcher(s).matches, s.length <= MaxLabelLength) match {
case (true, true) => s.validNel
case (false, false) =>
s"Invalid label field: `$s` did not match regex '$GoogleLabelRegexPattern' and it is ${s.length} characters. The maximum is $MaxLabelLength.".invalidNel
case (false, _) => s"Invalid label field: `$s` did not match the regex '$GoogleLabelRegexPattern'".invalidNel
s"Invalid $field field: `$s` did not match regex '$regexPattern' and it is ${s.length} characters. The maximum is $MaxLabelLength.".invalidNel

Check warning on line 69 in supportedBackends/google/batch/src/main/scala/cromwell/backend/google/batch/models/GcpLabel.scala

View check run for this annotation

Codecov / codecov/patch

supportedBackends/google/batch/src/main/scala/cromwell/backend/google/batch/models/GcpLabel.scala#L69

Added line #L69 was not covered by tests
case (false, _) => s"Invalid $field field: `$s` did not match the regex '$regexPattern'".invalidNel
case (_, false) =>
s"Invalid label field: `$s` is ${s.length} characters. The maximum is $MaxLabelLength.".invalidNel
s"Invalid $field field: `$s` is ${s.length} characters. The maximum is $MaxLabelLength.".invalidNel

Check warning on line 72 in supportedBackends/google/batch/src/main/scala/cromwell/backend/google/batch/models/GcpLabel.scala

View check run for this annotation

Codecov / codecov/patch

supportedBackends/google/batch/src/main/scala/cromwell/backend/google/batch/models/GcpLabel.scala#L72

Added line #L72 was not covered by tests
}
}

def safeLabels(values: (String, String)*): Seq[GcpLabel] = {
def safeGoogleLabel(kvp: (String, String)): GcpLabel =
Expand All @@ -74,7 +80,7 @@
}

def validateLabel(key: String, value: String): ErrorOr[GcpLabel] =
(validateLabelRegex(key), validateLabelRegex(value)).mapN { (validKey, validValue) =>
(validateLabelRegex(key, true), validateLabelRegex(value, false)).mapN { (validKey, validValue) =>

Check warning on line 83 in supportedBackends/google/batch/src/main/scala/cromwell/backend/google/batch/models/GcpLabel.scala

View check run for this annotation

Codecov / codecov/patch

supportedBackends/google/batch/src/main/scala/cromwell/backend/google/batch/models/GcpLabel.scala#L83

Added line #L83 was not covered by tests
GcpLabel(validKey, validValue)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ class GcpBatchLabelSpec extends AnyFlatSpec with CromwellTimeoutSpec with Matche
"cromwell-root-workflow-id-" -> "cromwell-root-workflow-id---x",
"0-cromwell-root-workflow-id-" -> "x--0-cromwell-root-workflow-id---x",
"Cromwell-root-workflow-id" -> "cromwell-root-workflow-id",
"cromwell_root_workflow_id" -> "cromwell-root-workflow-id",
"too-long-too-long-too-long-too-long-too-long-too-long-too-long-t" -> "too-long-too-long-too-long-too---g-too-long-too-long-too-long-t",
"0-too-long-and-invalid-too-long-and-invalid-too-long-and-invali+" -> "x--0-too-long-and-invalid-too----nvalid-too-long-and-invali---x"
)

googleLabelConversions foreach { case (label: String, conversion: String) =>
it should s"not validate the bad label key '$label'" in {
GcpLabel.validateLabelRegex(label) match {
GcpLabel.validateLabelRegex(label, true) match {
case Invalid(_) => // Good!
case Valid(_) => fail(s"Label validation succeeded but should have failed.")
}
Expand Down
Loading