-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from rmohr/xattrs
Add a command to bazeldnf which allow setting capabilities and selinux labels on existing tar streams
- Loading branch information
Showing
15 changed files
with
315 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package main | ||
|
||
import ( | ||
"archive/tar" | ||
"os" | ||
"strings" | ||
|
||
"github.com/rmohr/bazeldnf/pkg/xattr" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type xattrOpts struct { | ||
filePrefix string | ||
tarFileInput string | ||
tarFileOutput string | ||
capabilities map[string]string | ||
selinuxLabels map[string]string | ||
} | ||
|
||
var xattropts = xattrOpts{} | ||
|
||
func NewXATTRCmd() *cobra.Command { | ||
xattrCmd := &cobra.Command{ | ||
Use: "xattr", | ||
Short: "Modify xattrs on tar file members", | ||
Args: cobra.ExactArgs(0), | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
capabilityMap := map[string][]string{} | ||
for file, caps := range xattropts.capabilities { | ||
split := strings.Split(caps, ":") | ||
if len(split) > 0 { | ||
capabilityMap["./"+strings.TrimPrefix(file, "/")] = split | ||
} | ||
} | ||
labelMap := map[string]string{} | ||
for file, label := range xattropts.selinuxLabels { | ||
labelMap["./"+strings.TrimPrefix(file, "/")] = label | ||
} | ||
|
||
streamInput := os.Stdin | ||
if xattropts.tarFileInput != "" { | ||
streamInput, err = os.Open(xattropts.tarFileInput) | ||
if err != nil { | ||
return err | ||
} | ||
defer streamInput.Close() | ||
} | ||
|
||
streamOutput := os.Stdout | ||
if xattropts.tarFileOutput != "" { | ||
streamOutput, err = os.OpenFile(xattropts.tarFileOutput, os.O_WRONLY|os.O_CREATE, os.ModePerm) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
tarWriter := tar.NewWriter(streamOutput) | ||
defer tarWriter.Close() | ||
return xattr.Apply(tar.NewReader(streamInput), tarWriter , capabilityMap, labelMap) | ||
}, | ||
} | ||
|
||
xattrCmd.Flags().StringVarP(&xattropts.tarFileInput, "input", "i", "", "location from where to read the tar file (defaults to stdin)") | ||
xattrCmd.Flags().StringVarP(&xattropts.tarFileOutput, "output", "o", "", "where to write the file to (defaults to stdout)") | ||
xattrCmd.Flags().StringToStringVarP(&xattropts.capabilities, "capabilities", "c", map[string]string{}, "capabilities of files (--capabilities=/bin/ls=cap_net_bind_service)") | ||
xattrCmd.Flags().StringToStringVar(&xattropts.selinuxLabels, "selinux-labels", map[string]string{}, "selinux labels of files (--selinux-labels=/bin/ls=unconfined_u:object_r:default_t:s0)") | ||
return xattrCmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# Copyright 2014 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
def _xattrs_impl(ctx): | ||
out = ctx.outputs.out | ||
args = ["xattr", "-i", ctx.files.tar[0].path, "-o", out.path] | ||
|
||
if ctx.attr.capabilities: | ||
capabilities = [] | ||
for k, v in ctx.attr.capabilities.items(): | ||
capabilities += [k + "=" + ":".join(v)] | ||
args += ["--capabilities", ",".join(capabilities)] | ||
|
||
if ctx.attr.selinux_labels: | ||
selinux_labels = [] | ||
for k, v in ctx.attr.selinux_labels.items(): | ||
selinux_labels += [k + "=" + v] | ||
args += ["--selinux-labels", ",".join(selinux_labels)] | ||
|
||
ctx.actions.run( | ||
inputs = ctx.files.tar, | ||
outputs = [out], | ||
arguments = args, | ||
progress_message = "Enriching %s with xattrs" % ctx.label.name, | ||
executable = ctx.executable._bazeldnf, | ||
) | ||
|
||
return [DefaultInfo(files = depset([ctx.outputs.out]))] | ||
|
||
_xattrs_attrs = { | ||
"tar": attr.label(allow_single_file = True), | ||
"_bazeldnf": attr.label( | ||
executable = True, | ||
cfg = "exec", | ||
allow_files = True, | ||
default = Label("//cmd:cmd"), | ||
), | ||
"capabilities": attr.string_list_dict(), | ||
"selinux_labels": attr.string_dict(), | ||
"out": attr.output(mandatory = True), | ||
} | ||
|
||
_xattrs = rule( | ||
implementation = _xattrs_impl, | ||
attrs = _xattrs_attrs, | ||
) | ||
|
||
def xattrs(**kwargs): | ||
basename = kwargs["name"] | ||
tarname = basename + ".tar" | ||
_xattrs( | ||
out = tarname, | ||
**kwargs | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
||
go_library( | ||
name = "xattr", | ||
srcs = ["xattr.go"], | ||
importpath = "github.com/rmohr/bazeldnf/pkg/xattr", | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
go_test( | ||
name = "xattr_test", | ||
srcs = ["xattr_test.go"], | ||
data = glob(["testdata/**"]), | ||
embed = [":xattr"], | ||
deps = ["@com_github_onsi_gomega//:go_default_library"], | ||
) |
Binary file not shown.
Oops, something went wrong.