Skip to content

Commit 7b31180

Browse files
committed
tls: Allow TLS flags to be reset to default values
1 parent 1a938e2 commit 7b31180

File tree

3 files changed

+81
-3
lines changed

3 files changed

+81
-3
lines changed

src/main/java/com/google/devtools/build/lib/authandtls/AuthAndTLSOptions.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.devtools.common.options.Converter;
2121
import com.google.devtools.common.options.Converters.CommaSeparatedOptionListConverter;
2222
import com.google.devtools.common.options.Converters.DurationConverter;
23+
import com.google.devtools.common.options.Converters.NullableStringConverter;
2324
import com.google.devtools.common.options.Option;
2425
import com.google.devtools.common.options.OptionDocumentationCategory;
2526
import com.google.devtools.common.options.OptionEffectTag;
@@ -81,29 +82,34 @@ public abstract class AuthAndTLSOptions extends OptionsBase {
8182
@Option(
8283
name = "tls_certificate",
8384
defaultValue = "null",
85+
converter = NullableStringConverter.class,
8486
documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
8587
effectTags = {OptionEffectTag.UNKNOWN},
86-
help = "Specify a path to a TLS certificate that is trusted to sign server certificates.")
88+
help =
89+
"Specify a path to a TLS certificate that is trusted to sign server certificates."
90+
+ " An empty value resets the flag to its default.")
8791
public abstract String getTlsCertificate();
8892

8993
@Option(
9094
name = "tls_client_certificate",
9195
defaultValue = "null",
96+
converter = NullableStringConverter.class,
9297
documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
9398
effectTags = {OptionEffectTag.UNKNOWN},
9499
help =
95100
"Specify the TLS client certificate to use; you also need to provide a client key to "
96-
+ "enable client authentication.")
101+
+ "enable client authentication. An empty value resets the flag to its default.")
97102
public abstract String getTlsClientCertificate();
98103

99104
@Option(
100105
name = "tls_client_key",
101106
defaultValue = "null",
107+
converter = NullableStringConverter.class,
102108
documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
103109
effectTags = {OptionEffectTag.UNKNOWN},
104110
help =
105111
"Specify the TLS client key to use; you also need to provide a client certificate to "
106-
+ "enable client authentication.")
112+
+ "enable client authentication. An empty value resets the flag to its default.")
107113
public abstract String getTlsClientKey();
108114

109115
@Option(

src/main/java/com/google/devtools/common/options/Converters.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,28 @@ public String getTypeDescription() {
7878
}
7979
}
8080

81+
/**
82+
* Converter for nullable strings: treats an empty string as {@code null}, and passes any other
83+
* value through unchanged. Use this for optional file-path flags that have {@code defaultValue =
84+
* "null"} so that {@code --flag=} on the command line resets the flag to its unset state instead
85+
* of being interpreted as a filename.
86+
*/
87+
public static class NullableStringConverter extends Converter.Contextless<String> {
88+
@Override
89+
@Nullable
90+
public String convert(String input) {
91+
if (input.isEmpty()) {
92+
return null;
93+
}
94+
return input;
95+
}
96+
97+
@Override
98+
public String getTypeDescription() {
99+
return "a nullable string";
100+
}
101+
}
102+
81103
/** Standard converter for integers. */
82104
public static class IntegerConverter extends Converter.Contextless<Integer> {
83105
@Override
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2026 The Bazel Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
package com.google.devtools.common.options;
15+
16+
import static com.google.common.truth.Truth.assertThat;
17+
18+
import com.google.devtools.common.options.Converters.NullableStringConverter;
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
import org.junit.runners.JUnit4;
22+
23+
/** Tests for {@link NullableStringConverter}. */
24+
@RunWith(JUnit4.class)
25+
public class NullableStringConverterTest {
26+
27+
private final NullableStringConverter converter = new NullableStringConverter();
28+
29+
@Test
30+
public void emptyStringReturnsNull() throws OptionsParsingException {
31+
assertThat(converter.convert("")).isNull();
32+
}
33+
34+
@Test
35+
public void literalNullStringPassesThrough() throws OptionsParsingException {
36+
// The framework handles defaultValue = "null" specially without invoking the converter.
37+
// This test checks that if "null" makes it to the converter, then it is treated literally.
38+
assertThat(converter.convert("null")).isEqualTo("null");
39+
}
40+
41+
@Test
42+
public void regularPathPassesThrough() throws OptionsParsingException {
43+
assertThat(converter.convert("/path/to/cert.pem")).isEqualTo("/path/to/cert.pem");
44+
}
45+
46+
@Test
47+
public void arbitraryStringPassesThrough() throws OptionsParsingException {
48+
assertThat(converter.convert("some-value")).isEqualTo("some-value");
49+
}
50+
}

0 commit comments

Comments
 (0)