11"Implementation details for attest rule"
22
3+ load ("@aspect_bazel_lib//lib:paths.bzl" , "BASH_RLOCATION_FUNCTION" , "to_rlocation_path" )
4+ load ("@aspect_bazel_lib//lib:windows_utils.bzl" , "create_windows_native_launcher_script" )
5+
36_DOC = """Attest an oci_image using cosign binary at a remote registry.
47
58```starlark
@@ -52,8 +55,17 @@ _attrs = {
5255 Digests and tags are not allowed. If this attribute is not set, the repository must be passed at runtime via the `--repository` flag.
5356 """ ),
5457 "_attest_sh_tpl" : attr .label (default = "attest.sh.tpl" , allow_single_file = True ),
58+ "_runfiles" : attr .label (default = "@bazel_tools//tools/bash/runfiles" ),
5559}
5660
61+ def _windows_host (ctx ):
62+ """Returns true if the host platform is windows.
63+
64+ The typical approach using ctx.target_platform_has_constraint does not work for transitioned
65+ build targets. We need to know the host platform, not the target platform.
66+ """
67+ return ctx .configuration .host_path_separator == ";"
68+
5769def _cosign_attest_impl (ctx ):
5870 cosign = ctx .toolchains ["@rules_oci//cosign:toolchain_type" ]
5971 jq = ctx .toolchains ["@aspect_bazel_lib//lib:jq_toolchain_type" ]
@@ -63,31 +75,34 @@ def _cosign_attest_impl(ctx):
6375
6476 fixed_args = [
6577 "--predicate" ,
66- ctx .file .predicate . short_path ,
78+ to_rlocation_path ( ctx , ctx .file .predicate ) ,
6779 "--type" ,
6880 ctx .attr .type ,
6981 ]
7082 if ctx .attr .repository :
7183 fixed_args .extend (["--repository" , ctx .attr .repository ])
7284
73- executable = ctx .actions .declare_file ("cosign_attest_{}.sh" .format (ctx .label .name ))
85+ bash_launcher = ctx .actions .declare_file ("cosign_attest_{}.sh" .format (ctx .label .name ))
7486 ctx .actions .expand_template (
7587 template = ctx .file ._attest_sh_tpl ,
76- output = executable ,
88+ output = bash_launcher ,
7789 is_executable = True ,
7890 substitutions = {
79- "{{cosign_path}}" : cosign .cosign_info .binary .short_path ,
80- "{{jq_path}}" : jq .jqinfo .bin .short_path ,
81- "{{image_dir}}" : ctx .file .image .short_path ,
91+ "{{BASH_RLOCATION_FUNCTION}}" : BASH_RLOCATION_FUNCTION ,
92+ "{{cosign_path}}" : to_rlocation_path (ctx , cosign .cosign_info .binary ),
93+ "{{jq_path}}" : to_rlocation_path (ctx , jq .jqinfo .bin ),
94+ "{{image_dir}}" : to_rlocation_path (ctx , ctx .file .image ),
8295 "{{fixed_args}}" : " " .join (fixed_args ),
8396 "{{type}}" : ctx .attr .type ,
8497 },
8598 )
8699
87- runfiles = ctx .runfiles (files = [ctx .file .image , ctx .file .predicate ])
100+ executable = create_windows_native_launcher_script (ctx , bash_launcher ) if _windows_host (ctx ) else bash_launcher
101+ runfiles = ctx .runfiles (files = [ctx .file .image , ctx .file .predicate , bash_launcher ])
88102 runfiles = runfiles .merge (ctx .attr .image [DefaultInfo ].default_runfiles )
89103 runfiles = runfiles .merge (jq .default .default_runfiles )
90104 runfiles = runfiles .merge (cosign .default .default_runfiles )
105+ runfiles = runfiles .merge (ctx .attr ._runfiles .default_runfiles )
91106
92107 return DefaultInfo (executable = executable , runfiles = runfiles )
93108
@@ -97,6 +112,7 @@ cosign_attest = rule(
97112 doc = _DOC ,
98113 executable = True ,
99114 toolchains = [
115+ "@bazel_tools//tools/sh:toolchain_type" ,
100116 "@rules_oci//cosign:toolchain_type" ,
101117 "@aspect_bazel_lib//lib:jq_toolchain_type" ,
102118 ],
0 commit comments