Skip to content

Commit 05e758d

Browse files
sgowrojiYannicckolli5“Gowroji
authored
[credentialhelper] Add parser for flag syntax (bazelbuild#15929)
* [credentialhelper] Add parser for flag syntax Progress on bazelbuild#15856 Closes bazelbuild#15906. PiperOrigin-RevId: 462095541 Change-Id: I4d31ad2ae185b63f0945119483ffafd3ba121d3d * Updating implements for Converter.Contextless<UnresolvedScopedCredentialHelper> * changes on implements Converter<UnresolvedScopedCredentialHelper> { Co-authored-by: Yannic Bonenberger <[email protected]> Co-authored-by: Chenchu K <[email protected]> Co-authored-by: “Gowroji <[email protected]>
1 parent 9c2c3de commit 05e758d

File tree

8 files changed

+471
-1
lines changed

8 files changed

+471
-1
lines changed

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

+56
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,22 @@
1414

1515
package com.google.devtools.build.lib.authandtls;
1616

17+
import com.google.auto.value.AutoValue;
18+
import com.google.common.base.Preconditions;
19+
import com.google.common.base.Strings;
20+
import com.google.devtools.common.options.Converter;
1721
import com.google.devtools.common.options.Converters.CommaSeparatedOptionListConverter;
1822
import com.google.devtools.common.options.Converters.DurationConverter;
1923
import com.google.devtools.common.options.Option;
2024
import com.google.devtools.common.options.OptionDocumentationCategory;
2125
import com.google.devtools.common.options.OptionEffectTag;
2226
import com.google.devtools.common.options.OptionMetadataTag;
2327
import com.google.devtools.common.options.OptionsBase;
28+
import com.google.devtools.common.options.OptionsParsingException;
2429
import java.time.Duration;
2530
import java.util.List;
31+
import java.util.Optional;
32+
import javax.annotation.Nullable;
2633

2734
/**
2835
* Common options for authentication and TLS.
@@ -131,4 +138,53 @@ public class AuthAndTLSOptions extends OptionsBase {
131138
+ "granularity; it is an error to set a value less than one second. If keep-alive "
132139
+ "pings are disabled, then this setting is ignored.")
133140
public Duration grpcKeepaliveTimeout;
141+
142+
/** One of the values of the `--credential_helper` flag. */
143+
@AutoValue
144+
public abstract static class UnresolvedScopedCredentialHelper {
145+
/** Returns the scope of the credential helper (if any). */
146+
public abstract Optional<String> getScope();
147+
148+
/** Returns the (unparsed) path of the credential helper. */
149+
public abstract String getPath();
150+
}
151+
152+
/** A {@link Converter} for the `--credential_helper` flag. */
153+
public static final class UnresolvedScopedCredentialHelperConverter
154+
implements Converter<UnresolvedScopedCredentialHelper> {
155+
public static final UnresolvedScopedCredentialHelperConverter INSTANCE =
156+
new UnresolvedScopedCredentialHelperConverter();
157+
158+
@Override
159+
public String getTypeDescription() {
160+
return "An (unresolved) path to a credential helper for a scope.";
161+
}
162+
163+
@Override
164+
public UnresolvedScopedCredentialHelper convert(String input) throws OptionsParsingException {
165+
Preconditions.checkNotNull(input);
166+
167+
int pos = input.indexOf('=');
168+
if (pos >= 0) {
169+
String scope = input.substring(0, pos);
170+
if (Strings.isNullOrEmpty(scope)) {
171+
throw new OptionsParsingException("Scope of credential helper must not be empty");
172+
}
173+
String path = checkPath(input.substring(pos + 1));
174+
return new AutoValue_AuthAndTLSOptions_UnresolvedScopedCredentialHelper(
175+
Optional.of(scope), path);
176+
}
177+
178+
// `input` does not specify a scope.
179+
return new AutoValue_AuthAndTLSOptions_UnresolvedScopedCredentialHelper(
180+
Optional.empty(), checkPath(input));
181+
}
182+
183+
private String checkPath(@Nullable String input) throws OptionsParsingException {
184+
if (Strings.isNullOrEmpty(input)) {
185+
throw new OptionsParsingException("Path to credential helper must not be empty");
186+
}
187+
return input;
188+
}
189+
}
134190
}

src/main/java/com/google/devtools/build/lib/authandtls/credentialhelper/CredentialHelper.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public final class CredentialHelper {
4949
}
5050

5151
@VisibleForTesting
52-
Path getPath() {
52+
public Path getPath() {
5353
return path;
5454
}
5555

src/main/java/com/google/devtools/build/lib/remote/BUILD

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ java_library(
5757
"//src/main/java/com/google/devtools/build/lib/analysis:top_level_artifact_context",
5858
"//src/main/java/com/google/devtools/build/lib/analysis/platform:platform_utils",
5959
"//src/main/java/com/google/devtools/build/lib/authandtls",
60+
"//src/main/java/com/google/devtools/build/lib/authandtls/credentialhelper",
6061
"//src/main/java/com/google/devtools/build/lib/bazel/repository/downloader",
6162
"//src/main/java/com/google/devtools/build/lib/buildeventstream",
6263
"//src/main/java/com/google/devtools/build/lib/clock",
@@ -99,6 +100,7 @@ java_library(
99100
"//src/main/java/com/google/devtools/common/options",
100101
"//src/main/protobuf:failure_details_java_proto",
101102
"//third_party:auth",
103+
"//third_party:auto_value",
102104
"//third_party:caffeine",
103105
"//third_party:flogger",
104106
"//third_party:guava",

src/main/java/com/google/devtools/build/lib/remote/RemoteModule.java

+38
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import build.bazel.remote.execution.v2.DigestFunction;
2020
import build.bazel.remote.execution.v2.ServerCapabilities;
2121
import com.google.auth.Credentials;
22+
import com.google.auto.value.AutoValue;
2223
import com.google.common.annotations.VisibleForTesting;
2324
import com.google.common.base.Ascii;
2425
import com.google.common.base.Preconditions;
@@ -45,11 +46,14 @@
4546
import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget;
4647
import com.google.devtools.build.lib.analysis.test.TestProvider;
4748
import com.google.devtools.build.lib.authandtls.AuthAndTLSOptions;
49+
import com.google.devtools.build.lib.authandtls.AuthAndTLSOptions.UnresolvedScopedCredentialHelper;
4850
import com.google.devtools.build.lib.authandtls.CallCredentialsProvider;
4951
import com.google.devtools.build.lib.authandtls.GoogleAuthUtils;
5052
import com.google.devtools.build.lib.authandtls.Netrc;
5153
import com.google.devtools.build.lib.authandtls.NetrcCredentials;
5254
import com.google.devtools.build.lib.authandtls.NetrcParser;
55+
import com.google.devtools.build.lib.authandtls.credentialhelper.CredentialHelperEnvironment;
56+
import com.google.devtools.build.lib.authandtls.credentialhelper.CredentialHelperProvider;
5357
import com.google.devtools.build.lib.bazel.repository.downloader.Downloader;
5458
import com.google.devtools.build.lib.buildeventstream.BuildEventArtifactUploader;
5559
import com.google.devtools.build.lib.buildeventstream.LocalFilesArtifactUploader;
@@ -77,6 +81,7 @@
7781
import com.google.devtools.build.lib.runtime.BuildEventArtifactUploaderFactory;
7882
import com.google.devtools.build.lib.runtime.Command;
7983
import com.google.devtools.build.lib.runtime.CommandEnvironment;
84+
import com.google.devtools.build.lib.runtime.CommandLinePathFactory;
8085
import com.google.devtools.build.lib.runtime.RepositoryRemoteExecutor;
8186
import com.google.devtools.build.lib.runtime.RepositoryRemoteExecutorFactory;
8287
import com.google.devtools.build.lib.runtime.ServerBuilder;
@@ -1134,4 +1139,37 @@ static Credentials newCredentials(
11341139

11351140
return creds;
11361141
}
1142+
1143+
@VisibleForTesting
1144+
static CredentialHelperProvider newCredentialHelperProvider(
1145+
CredentialHelperEnvironment environment,
1146+
CommandLinePathFactory pathFactory,
1147+
List<UnresolvedScopedCredentialHelper> helpers)
1148+
throws IOException {
1149+
Preconditions.checkNotNull(environment);
1150+
Preconditions.checkNotNull(pathFactory);
1151+
Preconditions.checkNotNull(helpers);
1152+
1153+
CredentialHelperProvider.Builder builder = CredentialHelperProvider.builder();
1154+
for (UnresolvedScopedCredentialHelper helper : helpers) {
1155+
Optional<String> scope = helper.getScope();
1156+
Path path = pathFactory.create(environment.getClientEnvironment(), helper.getPath());
1157+
if (scope.isPresent()) {
1158+
builder.add(scope.get(), path);
1159+
} else {
1160+
builder.add(path);
1161+
}
1162+
}
1163+
return builder.build();
1164+
}
1165+
1166+
@VisibleForTesting
1167+
@AutoValue
1168+
abstract static class ScopedCredentialHelper {
1169+
/** Returns the scope of the credential helper (if any). */
1170+
public abstract Optional<String> getScope();
1171+
1172+
/** Returns the path of the credential helper. */
1173+
public abstract Path getPath();
1174+
}
11371175
}

src/test/java/com/google/devtools/build/lib/authandtls/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ java_library(
2525
),
2626
deps = [
2727
"//src/main/java/com/google/devtools/build/lib/authandtls",
28+
"//src/main/java/com/google/devtools/common/options",
2829
"//third_party:guava",
2930
"//third_party:junit4",
3031
"//third_party:mockito",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright 2022 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+
15+
package com.google.devtools.build.lib.authandtls;
16+
17+
import static com.google.common.truth.Truth.assertThat;
18+
import static com.google.common.truth.Truth8.assertThat;
19+
import static org.junit.Assert.assertThrows;
20+
21+
import com.google.devtools.build.lib.authandtls.AuthAndTLSOptions.UnresolvedScopedCredentialHelper;
22+
import com.google.devtools.build.lib.authandtls.AuthAndTLSOptions.UnresolvedScopedCredentialHelperConverter;
23+
import com.google.devtools.common.options.OptionsParsingException;
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
import org.junit.runners.JUnit4;
27+
28+
/** Test for {@link UnresolvedScopedCredentialHelperConverter}. */
29+
@RunWith(JUnit4.class)
30+
public class UnresolvedScopedCredentialHelperConverterTest {
31+
@Test
32+
public void convertAbsolutePath() throws Exception {
33+
UnresolvedScopedCredentialHelper helper1 =
34+
UnresolvedScopedCredentialHelperConverter.INSTANCE.convert("/absolute/path");
35+
assertThat(helper1.getScope()).isEmpty();
36+
assertThat(helper1.getPath()).isEqualTo("/absolute/path");
37+
38+
UnresolvedScopedCredentialHelper helper2 =
39+
UnresolvedScopedCredentialHelperConverter.INSTANCE.convert("example.com=/absolute/path");
40+
assertThat(helper2.getScope()).hasValue("example.com");
41+
assertThat(helper2.getPath()).isEqualTo("/absolute/path");
42+
43+
UnresolvedScopedCredentialHelper helper3 =
44+
UnresolvedScopedCredentialHelperConverter.INSTANCE.convert("*.example.com=/absolute/path");
45+
assertThat(helper3.getScope()).hasValue("*.example.com");
46+
assertThat(helper3.getPath()).isEqualTo("/absolute/path");
47+
}
48+
49+
@Test
50+
public void convertRootRelativePath() throws Exception {
51+
UnresolvedScopedCredentialHelper helper1 =
52+
UnresolvedScopedCredentialHelperConverter.INSTANCE.convert("%workspace%/path");
53+
assertThat(helper1.getScope()).isEmpty();
54+
assertThat(helper1.getPath()).isEqualTo("%workspace%/path");
55+
56+
UnresolvedScopedCredentialHelper helper2 =
57+
UnresolvedScopedCredentialHelperConverter.INSTANCE.convert("example.com=%workspace%/path");
58+
assertThat(helper2.getScope()).hasValue("example.com");
59+
assertThat(helper2.getPath()).isEqualTo("%workspace%/path");
60+
61+
UnresolvedScopedCredentialHelper helper3 =
62+
UnresolvedScopedCredentialHelperConverter.INSTANCE.convert(
63+
"*.example.com=%workspace%/path");
64+
assertThat(helper3.getScope()).hasValue("*.example.com");
65+
assertThat(helper3.getPath()).isEqualTo("%workspace%/path");
66+
}
67+
68+
@Test
69+
public void convertPathLookup() throws Exception {
70+
UnresolvedScopedCredentialHelper helper1 =
71+
UnresolvedScopedCredentialHelperConverter.INSTANCE.convert("foo");
72+
assertThat(helper1.getScope()).isEmpty();
73+
assertThat(helper1.getPath()).isEqualTo("foo");
74+
75+
UnresolvedScopedCredentialHelper helper2 =
76+
UnresolvedScopedCredentialHelperConverter.INSTANCE.convert("example.com=foo");
77+
assertThat(helper2.getScope()).hasValue("example.com");
78+
assertThat(helper2.getPath()).isEqualTo("foo");
79+
80+
UnresolvedScopedCredentialHelper helper3 =
81+
UnresolvedScopedCredentialHelperConverter.INSTANCE.convert("*.example.com=foo");
82+
assertThat(helper3.getScope()).hasValue("*.example.com");
83+
assertThat(helper3.getPath()).isEqualTo("foo");
84+
}
85+
86+
@Test
87+
public void emptyPath() {
88+
assertThrows(
89+
OptionsParsingException.class,
90+
() -> UnresolvedScopedCredentialHelperConverter.INSTANCE.convert(""));
91+
assertThrows(
92+
OptionsParsingException.class,
93+
() -> UnresolvedScopedCredentialHelperConverter.INSTANCE.convert("foo="));
94+
assertThrows(
95+
OptionsParsingException.class,
96+
() -> UnresolvedScopedCredentialHelperConverter.INSTANCE.convert("="));
97+
}
98+
99+
@Test
100+
public void emptyScope() {
101+
assertThrows(
102+
OptionsParsingException.class,
103+
() -> UnresolvedScopedCredentialHelperConverter.INSTANCE.convert("=/foo"));
104+
assertThrows(
105+
OptionsParsingException.class,
106+
() -> UnresolvedScopedCredentialHelperConverter.INSTANCE.convert("="));
107+
}
108+
}

src/test/java/com/google/devtools/build/lib/remote/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ java_test(
5656
"//src/main/java/com/google/devtools/build/lib/analysis:server_directories",
5757
"//src/main/java/com/google/devtools/build/lib/analysis/platform:platform_utils",
5858
"//src/main/java/com/google/devtools/build/lib/authandtls",
59+
"//src/main/java/com/google/devtools/build/lib/authandtls/credentialhelper",
5960
"//src/main/java/com/google/devtools/build/lib/buildeventstream",
6061
"//src/main/java/com/google/devtools/build/lib/clock",
6162
"//src/main/java/com/google/devtools/build/lib/collect/nestedset",

0 commit comments

Comments
 (0)