github.com/containerd/Containerd@v1.4.13/contrib/seccomp/seccomp.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package seccomp
    18  
    19  import (
    20  	"context"
    21  	"encoding/json"
    22  	"fmt"
    23  	"io/ioutil"
    24  
    25  	"github.com/containerd/containerd/containers"
    26  	"github.com/containerd/containerd/oci"
    27  	"github.com/opencontainers/runtime-spec/specs-go"
    28  )
    29  
    30  // WithProfile receives the name of a file stored on disk comprising a json
    31  // formatted seccomp profile, as specified by the opencontainers/runtime-spec.
    32  // The profile is read from the file, unmarshaled, and set to the spec.
    33  func WithProfile(profile string) oci.SpecOpts {
    34  	return func(_ context.Context, _ oci.Client, _ *containers.Container, s *specs.Spec) error {
    35  		s.Linux.Seccomp = &specs.LinuxSeccomp{}
    36  		f, err := ioutil.ReadFile(profile)
    37  		if err != nil {
    38  			return fmt.Errorf("cannot load seccomp profile %q: %v", profile, err)
    39  		}
    40  		if err := json.Unmarshal(f, s.Linux.Seccomp); err != nil {
    41  			return fmt.Errorf("decoding seccomp profile failed %q: %v", profile, err)
    42  		}
    43  		return nil
    44  	}
    45  }
    46  
    47  // WithDefaultProfile sets the default seccomp profile to the spec.
    48  // Note: must follow the setting of process capabilities
    49  func WithDefaultProfile() oci.SpecOpts {
    50  	return func(_ context.Context, _ oci.Client, _ *containers.Container, s *specs.Spec) error {
    51  		s.Linux.Seccomp = DefaultProfile(s)
    52  		return nil
    53  	}
    54  }