Skip to content

Commit

Permalink
setup selinux labels for build containers
Browse files Browse the repository at this point in the history
  • Loading branch information
bparees committed Oct 30, 2017
1 parent 76b71b2 commit bd52969
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/build/builder/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,11 @@ func (d *DockerBuilder) dockerBuild(dir string, tag string, secrets []buildapi.S
}
opts.NetworkMode = network
if len(resolvConfHostPath) != 0 {
label, err := getSELinuxLabel()
if err != nil {
return fmt.Errorf("could not retrieve selinux label due to error: %v", err)
}
opts.SecurityOpt = []string{"label=" + label}
opts.BuildBinds = fmt.Sprintf("[\"%s:/etc/resolv.conf\"]", resolvConfHostPath)
}
// Though we are capped on memory and cpu at the cgroup parent level,
Expand Down
5 changes: 5 additions & 0 deletions pkg/build/builder/sti.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ func (s *S2IBuilder) Build() error {
}

if len(resolvConfHostPath) != 0 {
label, err := getSELinuxLabel()
if err != nil {
return fmt.Errorf("could not retrieve selinux label due to error: %v", err)
}
config.SecurityOpt = []string{"label=" + label}
config.BuildVolumes = []string{fmt.Sprintf("%s:/etc/resolv.conf", resolvConfHostPath)}
}

Expand Down
27 changes: 27 additions & 0 deletions pkg/build/builder/util_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
package builder

import (
"bufio"
"errors"
"fmt"
"os"
"regexp"
"strconv"

"github.com/google/cadvisor/container/crio"
Expand All @@ -15,6 +17,10 @@ import (
s2iapi "github.com/openshift/source-to-image/pkg/api"
)

var (
selinuxLabelPattern = regexp.MustCompile(`"system_u:object_r:container_file_t:(.*?)"`)
)

// getContainerNetworkConfig determines whether the builder is running as a container
// by examining /proc/self/cgroup. This context is then passed to source-to-image.
// It returns a suitable argument for NetworkMode. If the container platform is
Expand Down Expand Up @@ -98,3 +104,24 @@ func getCgroupParent() (string, error) {
glog.V(6).Infof("found cgroup values map: %v", cgMap)
return extractParentFromCgroupMap(cgMap)
}

// getSELinuxLabel retrieves the selinux label associated w/ this container.
func getSELinuxLabel() (string, error) {
f, err := os.Open("/proc/mounts")
if err != nil {
return "", err
}
defer f.Close()
s := bufio.NewScanner(f)

for s.Scan() {
if err := s.Err(); err != nil {
return "", err
}
if match := selinuxLabelPattern.FindStringSubmatch(s.Text()); match != nil {
glog.V(6).Infof("found selinux labels: %v", match[1])
return match[1], nil
}
}
return "", nil
}
5 changes: 5 additions & 0 deletions pkg/build/builder/util_unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@ func GetCGroupLimits() (*s2iapi.CGroupLimits, error) {
func getCgroupParent() (string, error) {
return "", errors.New("getCgroupParent is unsupported on this platform")
}

// getSELinuxLabel retrieves the selinux label associated w/ this container.
func getSELinuxLabel() (string, error) {
return "", errors.New("getSELinuxLabel is unsupported on this platform")
}

0 comments on commit bd52969

Please sign in to comment.