|
| 1 | +# Copyright 2017 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 | +"""Rules for creating password files and entries.""" |
| 15 | + |
| 16 | +load("@bazel_skylib//lib:paths.bzl", "paths") |
| 17 | + |
| 18 | +_join_path = paths.join |
| 19 | + |
| 20 | +PasswdFileContentProviderInfo = provider( |
| 21 | + fields = [ |
| 22 | + "username", |
| 23 | + "uid", |
| 24 | + "gid", |
| 25 | + "info", |
| 26 | + "home", |
| 27 | + "create_home", |
| 28 | + "shell", |
| 29 | + "name", |
| 30 | + ], |
| 31 | +) |
| 32 | + |
| 33 | +def _passwd_entry_impl(ctx): |
| 34 | + """Creates a passwd_file_content_provider containing a single entry.""" |
| 35 | + return [PasswdFileContentProviderInfo( |
| 36 | + username = ctx.attr.username, |
| 37 | + uid = ctx.attr.uid, |
| 38 | + gid = ctx.attr.gid, |
| 39 | + info = ctx.attr.info, |
| 40 | + home = ctx.attr.home, |
| 41 | + create_home = ctx.attr.create_home, |
| 42 | + shell = ctx.attr.shell, |
| 43 | + name = ctx.attr.name, |
| 44 | + )] |
| 45 | + |
| 46 | +def _passwd_file_impl(ctx): |
| 47 | + """Core implementation of passwd_file.""" |
| 48 | + f = "".join(["%s:x:%s:%s:%s:%s:%s\n" % ( |
| 49 | + entry[PasswdFileContentProviderInfo].username, |
| 50 | + entry[PasswdFileContentProviderInfo].uid, |
| 51 | + entry[PasswdFileContentProviderInfo].gid, |
| 52 | + entry[PasswdFileContentProviderInfo].info, |
| 53 | + entry[PasswdFileContentProviderInfo].home, |
| 54 | + entry[PasswdFileContentProviderInfo].shell, |
| 55 | + ) for entry in ctx.attr.entries]) |
| 56 | + passwd_file = ctx.actions.declare_file(ctx.label.name) |
| 57 | + ctx.actions.write(output = passwd_file, content = f) |
| 58 | + return DefaultInfo(files = depset([passwd_file])) |
| 59 | + |
| 60 | +def _format_onwer(t): |
| 61 | + return ("--owners=%s=%s" % (t[0], t[1])) |
| 62 | + |
| 63 | +def _build_homedirs_tar(ctx, passwd_file): |
| 64 | + homedirs = [] |
| 65 | + owners_map = {} |
| 66 | + for entry in ctx.attr.entries: |
| 67 | + if entry[PasswdFileContentProviderInfo].create_home: |
| 68 | + homedir = entry[PasswdFileContentProviderInfo].home |
| 69 | + owners_map[homedir] = "{uid}.{gid}".format( |
| 70 | + uid = entry[PasswdFileContentProviderInfo].uid, |
| 71 | + gid = entry[PasswdFileContentProviderInfo].gid, |
| 72 | + ) |
| 73 | + homedirs.append(homedir) |
| 74 | + dest_file = _join_path( |
| 75 | + ctx.attr.passwd_file_pkg_dir, |
| 76 | + ctx.label.name, |
| 77 | + ) |
| 78 | + args = ctx.actions.args() |
| 79 | + args.add(ctx.outputs.passwd_tar, format = "--output=%s") |
| 80 | + args.add("--mode=0o700") |
| 81 | + args.add(passwd_file, format = "--file=%s=" + dest_file) |
| 82 | + args.add(dest_file, format = "--modes=%s=" + ctx.attr.passwd_file_mode) |
| 83 | + |
| 84 | + args.add_all(homedirs, format_each = "--empty_dir=%s") |
| 85 | + args.add_all(owners_map.items(), map_each = _format_onwer) |
| 86 | + ctx.actions.run( |
| 87 | + executable = ctx.executable.build_tar, |
| 88 | + inputs = [passwd_file], |
| 89 | + outputs = [ctx.outputs.passwd_tar], |
| 90 | + mnemonic = "PasswdTar", |
| 91 | + arguments = [args], |
| 92 | + ) |
| 93 | + |
| 94 | +def _passwd_tar_impl(ctx): |
| 95 | + """Core implementation of passwd_tar.""" |
| 96 | + f = "".join(["%s:x:%s:%s:%s:%s:%s\n" % ( |
| 97 | + entry[PasswdFileContentProviderInfo].username, |
| 98 | + entry[PasswdFileContentProviderInfo].uid, |
| 99 | + entry[PasswdFileContentProviderInfo].gid, |
| 100 | + entry[PasswdFileContentProviderInfo].info, |
| 101 | + entry[PasswdFileContentProviderInfo].home, |
| 102 | + entry[PasswdFileContentProviderInfo].shell, |
| 103 | + ) for entry in ctx.attr.entries]) |
| 104 | + |
| 105 | + passwd_file = ctx.actions.declare_file(ctx.label.name) |
| 106 | + ctx.actions.write(output = passwd_file, content = f) |
| 107 | + |
| 108 | + _build_homedirs_tar(ctx, passwd_file) |
| 109 | + |
| 110 | + return DefaultInfo(files = depset([ctx.outputs.passwd_tar])) |
| 111 | + |
| 112 | +passwd_entry = rule( |
| 113 | + attrs = { |
| 114 | + "create_home": attr.bool(default = True), |
| 115 | + "gid": attr.int(default = 1000), |
| 116 | + "home": attr.string(default = "/home"), |
| 117 | + "info": attr.string(default = "user"), |
| 118 | + "shell": attr.string(default = "/bin/bash"), |
| 119 | + "uid": attr.int(default = 1000), |
| 120 | + "username": attr.string(mandatory = True), |
| 121 | + }, |
| 122 | + implementation = _passwd_entry_impl, |
| 123 | +) |
| 124 | + |
| 125 | +passwd_file = rule( |
| 126 | + attrs = { |
| 127 | + "entries": attr.label_list( |
| 128 | + allow_empty = False, |
| 129 | + providers = [PasswdFileContentProviderInfo], |
| 130 | + ), |
| 131 | + }, |
| 132 | + executable = False, |
| 133 | + implementation = _passwd_file_impl, |
| 134 | +) |
| 135 | + |
| 136 | +passwd_tar = rule( |
| 137 | + attrs = { |
| 138 | + "build_tar": attr.label( |
| 139 | + default = Label("//container:build_tar"), |
| 140 | + cfg = "exec", |
| 141 | + executable = True, |
| 142 | + allow_files = True, |
| 143 | + ), |
| 144 | + "entries": attr.label_list( |
| 145 | + allow_empty = False, |
| 146 | + providers = [PasswdFileContentProviderInfo], |
| 147 | + ), |
| 148 | + "passwd_file_mode": attr.string(default = "0o644"), |
| 149 | + "passwd_file_pkg_dir": attr.string(mandatory = True), |
| 150 | + }, |
| 151 | + executable = False, |
| 152 | + outputs = { |
| 153 | + "passwd_tar": "%{name}.tar", |
| 154 | + }, |
| 155 | + implementation = _passwd_tar_impl, |
| 156 | +) |
0 commit comments