Skip to content

Commit 99b9051

Browse files
committed
Add truststore attrs to maven.install
1 parent 50b2011 commit 99b9051

3 files changed

Lines changed: 80 additions & 1 deletion

File tree

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,19 @@ build --repo_env=JDK_JAVA_OPTIONS=-Djavax.net.ssl.trustStore=<path-to-cacerts>
10581058
```
10591059
can be added to your .bazelrc file if you need to specify custom cacerts for artifact resolution.
10601060

1061+
If you are using Bzlmod, you can also set truststore-related JVM options directly on `maven.install`:
1062+
1063+
```python
1064+
maven.install(
1065+
# ...
1066+
truststore = "//path/to:cacerts",
1067+
truststore_password = "//path/to:truststore_password", # file contains the password
1068+
truststore_type = "PKCS12",
1069+
)
1070+
```
1071+
1072+
If `JDK_JAVA_OPTIONS` is set, it takes precedence over the `truststore*` settings.
1073+
10611074
### Provide JVM options for Coursier with `COURSIER_OPTS`
10621075

10631076
You can set up `COURSIER_OPTS` environment variable to provide some additional JVM options for Coursier.

private/extensions/maven.bzl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ install = tag_class(
7575

7676
# Configuration "stuff"
7777
"additional_netrc_lines": attr.string_list(doc = "Additional lines prepended to the netrc file used by `http_file` (with `maven_install_json` only).", default = []),
78+
"truststore": attr.label(allow_single_file = True, doc = "Optional Java truststore file to use for Maven resolution."),
79+
"truststore_password": attr.label(allow_single_file = True, doc = "Optional Java truststore password file for Maven resolution."),
80+
"truststore_type": attr.string(doc = "Optional Java truststore type (for example, JKS or PKCS12) for Maven resolution."),
7881
"use_credentials_from_home_netrc_file": attr.bool(doc = "Whether to pass machine login credentials from the ~/.netrc file to coursier.", default = False),
7982
"duplicate_version_warning": attr.string(
8083
doc = """What to do if there are duplicate artifacts
@@ -471,6 +474,25 @@ def _process_module_tags(mctx):
471474
[None, "warn"],
472475
)
473476

477+
repo["truststore"] = _fail_if_different(
478+
"truststore",
479+
repo.get("truststore"),
480+
install.truststore,
481+
[None],
482+
)
483+
repo["truststore_password"] = _fail_if_different(
484+
"truststore_password",
485+
repo.get("truststore_password"),
486+
install.truststore_password,
487+
[None],
488+
)
489+
repo["truststore_type"] = _fail_if_different(
490+
"truststore_type",
491+
repo.get("truststore_type"),
492+
install.truststore_type,
493+
[None, ""],
494+
)
495+
474496
# Get the longest timeout
475497
timeout = repo.get("resolve_timeout", install.resolve_timeout)
476498
if install.resolve_timeout > timeout:
@@ -666,6 +688,9 @@ def maven_impl(mctx):
666688
repo["fetch_javadoc"] = install.fetch_javadoc
667689
repo["fetch_sources"] = install.fetch_sources
668690
repo["resolver"] = install.resolver
691+
repo["truststore"] = install.truststore
692+
repo["truststore_password"] = install.truststore_password
693+
repo["truststore_type"] = install.truststore_type
669694
repo["strict_visibility"] = install.strict_visibility
670695
if len(install.repositories):
671696
mapped_repos = []
@@ -727,6 +752,9 @@ def maven_impl(mctx):
727752
use_credentials_from_home_netrc_file = repo.get("use_credentials_from_home_netrc_file"),
728753
maven_install_json = repo.get("lock_file"),
729754
dependency_index = repo.get("dependency_index"),
755+
truststore = repo.get("truststore"),
756+
truststore_password = repo.get("truststore_password"),
757+
truststore_type = repo.get("truststore_type"),
730758
resolve_timeout = repo.get("resolve_timeout"),
731759
use_starlark_android_rules = repo.get("use_starlark_android_rules"),
732760
aar_import_bzl_label = repo.get("aar_import_bzl_label"),
@@ -787,6 +815,9 @@ def maven_impl(mctx):
787815
generate_compat_repositories = False,
788816
maven_install_json = repo.get("lock_file"),
789817
dependency_index = repo.get("dependency_index"),
818+
truststore = repo.get("truststore"),
819+
truststore_password = repo.get("truststore_password"),
820+
truststore_type = repo.get("truststore_type"),
790821
override_targets = overrides.get(name),
791822
override_target_visibilities = override_visibilities.get(name, {}),
792823
strict_visibility = repo.get("strict_visibility"),

private/rules/coursier.bzl

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,31 @@ def _is_file(repository_ctx, path):
158158
def _is_directory(repository_ctx, path):
159159
return repository_ctx.which("test") and repository_ctx.execute(["test", "-d", path]).return_code == 0
160160

161+
def _jdk_java_options(repository_ctx):
162+
jdk_java_options = repository_ctx.os.environ.get("JDK_JAVA_OPTIONS")
163+
if jdk_java_options:
164+
return jdk_java_options
165+
truststore = repository_ctx.attr.truststore
166+
if not truststore:
167+
return None
168+
truststore_path = repository_ctx.path(truststore)
169+
if not _is_file(repository_ctx, str(truststore_path)):
170+
return None
171+
jvm_flags = [
172+
"-Djavax.net.ssl.trustStore=" + str(truststore_path),
173+
]
174+
truststore_password = repository_ctx.attr.truststore_password
175+
if truststore_password:
176+
truststore_password_path = repository_ctx.path(truststore_password)
177+
if _is_file(repository_ctx, str(truststore_password_path)):
178+
truststore_password_value = repository_ctx.read(str(truststore_password_path)).strip()
179+
if truststore_password_value:
180+
jvm_flags.append("-Djavax.net.ssl.trustStorePassword=" + truststore_password_value)
181+
truststore_type = repository_ctx.attr.truststore_type
182+
if truststore_type:
183+
jvm_flags.append("-Djavax.net.ssl.trustStoreType=" + truststore_type)
184+
return " ".join(jvm_flags)
185+
161186
def _is_unpinned(repository_ctx):
162187
return repository_ctx.attr.pinned_repo_name != ""
163188

@@ -852,7 +877,7 @@ def generate_pin_target(repository_ctx, unpinned_pin_target):
852877
boms = repr(repository_ctx.attr.boms),
853878
artifacts = repr(repository_ctx.attr.artifacts),
854879
excluded_artifacts = repr(repository_ctx.attr.excluded_artifacts),
855-
jvm_flags = repr(repository_ctx.os.environ.get("JDK_JAVA_OPTIONS")),
880+
jvm_flags = repr(_jdk_java_options(repository_ctx)),
856881
repos = repr(repository_ctx.attr.repositories),
857882
fetch_sources = repr(repository_ctx.attr.fetch_sources),
858883
fetch_javadocs = repr(repository_ctx.attr.fetch_javadoc),
@@ -1065,6 +1090,10 @@ def make_coursier_dep_tree(
10651090
# https://github.com/coursier/coursier/blob/1cbbf39b88ee88944a8d892789680cdb15be4714/modules/paths/src/main/java/coursier/paths/CoursierPaths.java#L29-L56
10661091
environment = {"COURSIER_CACHE": str(repository_ctx.path(coursier_cache_location))}
10671092

1093+
jdk_java_options = _jdk_java_options(repository_ctx)
1094+
if jdk_java_options:
1095+
environment["JDK_JAVA_OPTIONS"] = jdk_java_options
1096+
10681097
cmd.extend(additional_coursier_options)
10691098

10701099
# Use an argsfile to avoid command line length limits, requires Java version > 8
@@ -1591,6 +1620,9 @@ pinned_coursier_fetch = repository_rule(
15911620
"generate_compat_repositories": attr.bool(default = False), # generate a compatible layer with repositories for each artifact
15921621
"maven_install_json": attr.label(allow_single_file = True),
15931622
"dependency_index": attr.label(allow_single_file = True),
1623+
"truststore": attr.label(allow_single_file = True),
1624+
"truststore_password": attr.label(allow_single_file = True),
1625+
"truststore_type": attr.string(),
15941626
"override_targets": attr.string_dict(default = {}),
15951627
"override_target_visibilities": attr.string_list_dict(default = {}),
15961628
"strict_visibility": attr.bool(
@@ -1663,6 +1695,9 @@ coursier_fetch = repository_rule(
16631695
),
16641696
"maven_install_json": attr.label(allow_single_file = True),
16651697
"dependency_index": attr.label(allow_single_file = True),
1698+
"truststore": attr.label(allow_single_file = True),
1699+
"truststore_password": attr.label(allow_single_file = True),
1700+
"truststore_type": attr.string(),
16661701
"override_targets": attr.string_dict(default = {}),
16671702
"override_target_visibilities": attr.string_list_dict(default = {}),
16681703
"strict_visibility": attr.bool(

0 commit comments

Comments
 (0)