Skip to content

Commit 589d95b

Browse files
committed
[credentialhelper] Add parser for flag syntax
Progress on bazelbuild#15856
1 parent 33516e2 commit 589d95b

File tree

6 files changed

+303
-2
lines changed

6 files changed

+303
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
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/authandtls/credentialhelper/CredentialHelperProvider.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,23 @@ private void checkHelper(Path path) throws IOException {
129129
path.isExecutable(), "Credential helper %s is not executable", path);
130130
}
131131

132+
/**
133+
* Adds a credential helper to use for all {@link URI}s matching the provided pattern, or as
134+
* default credential helper if {@code pattern} is empty.
135+
*
136+
* <p>See {@link #add(String, Path)} for the syntax of {@code pattern}.
137+
*/
138+
public Builder add(Optional<String> pattern, Path helper) throws IOException {
139+
Preconditions.checkNotNull(pattern);
140+
Preconditions.checkNotNull(helper);
141+
142+
if (pattern.isPresent()) {
143+
return add(pattern.get(), helper);
144+
} else {
145+
return add(helper);
146+
}
147+
}
148+
132149
/**
133150
* Adds a default credential helper to use for all {@link URI}s that don't specify a more
134151
* specific credential helper.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ java_library(
5959
"//src/main/java/com/google/devtools/build/lib/analysis:top_level_artifact_context",
6060
"//src/main/java/com/google/devtools/build/lib/analysis/platform:platform_utils",
6161
"//src/main/java/com/google/devtools/build/lib/authandtls",
62+
"//src/main/java/com/google/devtools/build/lib/authandtls/credentialhelper",
6263
"//src/main/java/com/google/devtools/build/lib/bazel/repository/downloader",
6364
"//src/main/java/com/google/devtools/build/lib/buildeventstream",
6465
"//src/main/java/com/google/devtools/build/lib/clock",
@@ -102,6 +103,7 @@ java_library(
102103
"//src/main/java/com/google/devtools/common/options",
103104
"//src/main/protobuf:failure_details_java_proto",
104105
"//third_party:auth",
106+
"//third_party:auto_value",
105107
"//third_party:caffeine",
106108
"//third_party:flogger",
107109
"//third_party:guava",

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

Lines changed: 53 additions & 0 deletions
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;
@@ -50,6 +51,8 @@
5051
import com.google.devtools.build.lib.authandtls.Netrc;
5152
import com.google.devtools.build.lib.authandtls.NetrcCredentials;
5253
import com.google.devtools.build.lib.authandtls.NetrcParser;
54+
import com.google.devtools.build.lib.authandtls.credentialhelper.CredentialHelperEnvironment;
55+
import com.google.devtools.build.lib.authandtls.credentialhelper.CredentialHelperProvider;
5356
import com.google.devtools.build.lib.bazel.repository.downloader.Downloader;
5457
import com.google.devtools.build.lib.buildeventstream.BuildEventArtifactUploader;
5558
import com.google.devtools.build.lib.buildeventstream.LocalFilesArtifactUploader;
@@ -78,6 +81,7 @@
7881
import com.google.devtools.build.lib.runtime.BuildEventArtifactUploaderFactory;
7982
import com.google.devtools.build.lib.runtime.Command;
8083
import com.google.devtools.build.lib.runtime.CommandEnvironment;
84+
import com.google.devtools.build.lib.runtime.CommandLinePathFactory;
8185
import com.google.devtools.build.lib.runtime.RepositoryRemoteExecutor;
8286
import com.google.devtools.build.lib.runtime.RepositoryRemoteExecutorFactory;
8387
import com.google.devtools.build.lib.runtime.ServerBuilder;
@@ -1130,4 +1134,53 @@ static Credentials newCredentials(
11301134

11311135
return creds;
11321136
}
1137+
1138+
@VisibleForTesting
1139+
static CredentialHelperProvider newCredentialHelperProvider(
1140+
CredentialHelperEnvironment environment,
1141+
CommandLinePathFactory pathFactory,
1142+
List<String> inputs)
1143+
throws IOException {
1144+
Preconditions.checkNotNull(environment);
1145+
Preconditions.checkNotNull(pathFactory);
1146+
Preconditions.checkNotNull(inputs);
1147+
1148+
CredentialHelperProvider.Builder builder = CredentialHelperProvider.builder();
1149+
for (String input : inputs) {
1150+
ScopedCredentialHelper helper = parseCredentialHelperFlag(environment, pathFactory, input);
1151+
builder.add(helper.getScope(), helper.getPath());
1152+
}
1153+
return builder.build();
1154+
}
1155+
1156+
@VisibleForTesting
1157+
static ScopedCredentialHelper parseCredentialHelperFlag(
1158+
CredentialHelperEnvironment environment, CommandLinePathFactory pathFactory, String input)
1159+
throws IOException {
1160+
Preconditions.checkNotNull(environment);
1161+
Preconditions.checkNotNull(pathFactory);
1162+
Preconditions.checkNotNull(input);
1163+
1164+
int pos = input.indexOf('=');
1165+
if (pos > 0) {
1166+
String scope = input.substring(0, pos);
1167+
String path = input.substring(pos + 1);
1168+
return new AutoValue_RemoteModule_ScopedCredentialHelper(
1169+
Optional.of(scope), pathFactory.create(environment.getClientEnvironment(), path));
1170+
}
1171+
1172+
// `input` does not specify a scope.
1173+
return new AutoValue_RemoteModule_ScopedCredentialHelper(
1174+
Optional.empty(), pathFactory.create(environment.getClientEnvironment(), input));
1175+
}
1176+
1177+
@VisibleForTesting
1178+
@AutoValue
1179+
static abstract class ScopedCredentialHelper {
1180+
/** Returns the scope of the credential helper (if any). */
1181+
public abstract Optional<String> getScope();
1182+
1183+
/** Returns the path of the credential helper. */
1184+
public abstract Path getPath();
1185+
}
11331186
}

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

Lines changed: 1 addition & 0 deletions
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)