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

Add --experimental_repository_disable_download to allow users disable download for external repos #12940

Closed
wants to merge 3 commits into from
Closed
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 @@ -237,6 +237,8 @@ public void beforeCommand(CommandEnvironment env) {

RepositoryOptions repoOptions = env.getOptions().getOptions(RepositoryOptions.class);
if (repoOptions != null) {
downloadManager.setDisableDownload(repoOptions.disableDownload);

repositoryCache.setHardlink(repoOptions.useHardlinks);
if (repoOptions.experimentalScaleTimeouts > 0.0) {
starlarkRepositoryFunction.setTimeoutScaling(repoOptions.experimentalScaleTimeouts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ public class RepositoryOptions extends OptionsBase {
+ " cache hit, rather than copying. This is intended to save disk space.")
public boolean useHardlinks;

@Option(
name = "experimental_repository_disable_download",
defaultValue = "false",
documentationCategory = OptionDocumentationCategory.BAZEL_CLIENT_OPTIONS,
effectTags = {OptionEffectTag.UNKNOWN},
metadataTags = {OptionMetadataTag.EXPERIMENTAL},
help = "If set, downloading external repositories is not allowed.")
public boolean disableDownload;

@Option(
name = "distdir",
oldName = "experimental_distdir",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class DownloadManager {
private List<Path> distdir = ImmutableList.of();
private UrlRewriter rewriter;
private final Downloader downloader;
private boolean disableDownload = false;

public DownloadManager(RepositoryCache repositoryCache, Downloader downloader) {
this.repositoryCache = repositoryCache;
Expand All @@ -60,6 +61,10 @@ public void setUrlRewriter(UrlRewriter rewriter) {
this.rewriter = rewriter;
}

public void setDisableDownload(boolean disableDownload) {
this.disableDownload = disableDownload;
}

/**
* Downloads file to disk and returns path.
*
Expand Down Expand Up @@ -194,6 +199,10 @@ public Path download(
}
}

if (disableDownload) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I understand correctly that this works because we don't provide any built-in repositories anymore that do downloading and we delegate everything to Starlark code instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only works for repo rule which uses repository_ctx.download.

throw new IOException(String.format("Failed to download repo %s: download is disabled.", repo));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add test cases to verify at least the following cases (starlark_repository_test looks like a logical location, but maybe there is something better):

  1. When the flag is set, downloads don't work
  2. When the flag is set, repositories in distdir are still accessible
  3. Maybe: when the flag is unset, downloads work (although that case is probably covered by existing tests?)

I'd love if this flag to also affected git_repository and the like, but I guess that's not possible unless we put repository download actions into a sandbox which is probably too big a change for now :(

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test cases added.

git_repository uses repository_ctx.execute under the hood so it's impossible for this change to have affect on it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I don't think there is a lot we can do about that :(

}

try {
downloader.download(
urls, authHeaders, checksum, canonicalId, destination, eventHandler, clientEnv, type);
Expand Down
88 changes: 88 additions & 0 deletions src/test/shell/bazel/starlark_repository_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2061,4 +2061,92 @@ EOF
|| fail "Expected success despite needing a file behind basic auth"
}

function test_disable_download_should_prevent_downloading() {
mkdir x
echo 'exports_files(["file.txt"])' > x/BUILD
echo 'Hello World' > x/file.txt
tar cvf x.tar x
sha256=$(sha256sum x.tar | head -c 64)
serve_file x.tar

mkdir main
cd main
cat > WORKSPACE <<EOF
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name="ext",
url = "http://127.0.0.1:$nc_port/x.tar",
sha256="$sha256",
)
EOF
cat > BUILD <<'EOF'
genrule(
name = "it",
srcs = ["@ext//x:file.txt"],
outs = ["it.txt"],
cmd = "cp $< $@",
)
EOF

bazel build --experimental_repository_disable_download //:it > "${TEST_log}" 2>&1 \
&& fail "Expected failure" || :
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does || : do?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the first command failed (exit code is not 0), we need to use : (which is a symbol for true) to set the exit code to 0. Just a trick to ignore the errors. (Copied from other test cases in the same file)

expect_log "Failed to download repo ext: download is disabled"
}

function test_disable_download_should_allow_distdir() {
mkdir x
echo 'exports_files(["file.txt"])' > x/BUILD
echo 'Hello World' > x/file.txt
tar cvf x.tar x
sha256=$(sha256sum x.tar | head -c 64)

mkdir main
cp x.tar main
cd main
cat > WORKSPACE <<EOF
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name="ext",
url = "http://127.0.0.1/x.tar",
sha256="$sha256",
)
EOF
cat > BUILD <<'EOF'
genrule(
name = "it",
srcs = ["@ext//x:file.txt"],
outs = ["it.txt"],
cmd = "cp $< $@",
)
EOF

bazel build --distdir="." --experimental_repository_disable_download //:it || fail "Failed to build"
}

function test_disable_download_should_allow_local_repository() {
mkdir x
echo 'exports_files(["file.txt"])' > x/BUILD
echo 'Hello World' > x/file.txt
touch x/WORKSPACE

mkdir main
cd main
cat > WORKSPACE <<EOF
local_repository(
name="ext",
path="../x",
)
EOF
cat > BUILD <<'EOF'
genrule(
name = "it",
srcs = ["@ext//:file.txt"],
outs = ["it.txt"],
cmd = "cp $< $@",
)
EOF

bazel build --experimental_repository_disable_download //:it || fail "Failed to build"
}

run_suite "local repository tests"