Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions cmd/nerdctl/apparmor_inspect_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ package main
import (
"fmt"

"github.com/containerd/containerd/contrib/apparmor"
"github.com/containerd/nerdctl/pkg/api/types"
"github.com/containerd/nerdctl/pkg/cmd/apparmor"
"github.com/containerd/nerdctl/pkg/defaults"
"github.com/spf13/cobra"
)
Expand All @@ -37,10 +38,7 @@ func newApparmorInspectCommand() *cobra.Command {
}

func apparmorInspectAction(cmd *cobra.Command, args []string) error {
b, err := apparmor.DumpDefaultProfile(defaults.AppArmorProfileName)
if err != nil {
return err
}
_, err = fmt.Fprint(cmd.OutOrStdout(), b)
return err
options := &types.InspectCommandOptions{}
options.Writer = cmd.OutOrStdout()
return apparmor.Inspect(options)
}
6 changes: 2 additions & 4 deletions cmd/nerdctl/apparmor_load_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ package main
import (
"fmt"

"github.com/containerd/containerd/contrib/apparmor"
"github.com/containerd/nerdctl/pkg/cmd/apparmor"
"github.com/containerd/nerdctl/pkg/defaults"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -38,6 +37,5 @@ func newApparmorLoadCommand() *cobra.Command {
}

func apparmorLoadAction(cmd *cobra.Command, args []string) error {
logrus.Infof("Loading profile %q", defaults.AppArmorProfileName)
return apparmor.LoadDefaultProfile(defaults.AppArmorProfileName)
return apparmor.Load()
}
60 changes: 7 additions & 53 deletions cmd/nerdctl/apparmor_ls_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,8 @@
package main

import (
"bytes"
"errors"
"fmt"
"text/tabwriter"
"text/template"

"github.com/containerd/nerdctl/pkg/apparmorutil"
"github.com/containerd/nerdctl/pkg/formatter"
"github.com/containerd/nerdctl/pkg/api/types"
"github.com/containerd/nerdctl/pkg/cmd/apparmor"
"github.com/spf13/cobra"
)

Expand All @@ -48,57 +42,17 @@ func newApparmorLsCommand() *cobra.Command {
}

func apparmorLsAction(cmd *cobra.Command, args []string) error {
options := &types.LsCommandOptions{}
quiet, err := cmd.Flags().GetBool("quiet")
if err != nil {
return err
}
w := cmd.OutOrStdout()
var tmpl *template.Template
options.Quiet = quiet
options.Writer = cmd.OutOrStdout()
format, err := cmd.Flags().GetString("format")
if err != nil {
return err
}
switch format {
case "", "table", "wide":
w = tabwriter.NewWriter(cmd.OutOrStdout(), 4, 8, 4, ' ', 0)
if !quiet {
fmt.Fprintln(w, "NAME\tMODE")
}
case "raw":
return errors.New("unsupported format: \"raw\"")
default:
if quiet {
return errors.New("format and quiet must not be specified together")
}
var err error
tmpl, err = formatter.ParseTemplate(format)
if err != nil {
return err
}
}

profiles, err := apparmorutil.Profiles()
if err != nil {
return err
}

for _, f := range profiles {
if tmpl != nil {
var b bytes.Buffer
if err := tmpl.Execute(&b, f); err != nil {
return err
}
if _, err = fmt.Fprintf(w, b.String()+"\n"); err != nil {
return err
}
} else if quiet {
fmt.Fprintln(w, f.Name)
} else {
fmt.Fprintf(w, "%s\t%s\n", f.Name, f.Mode)
}
}
if f, ok := w.(formatter.Flusher); ok {
return f.Flush()
}
return nil
options.Format = format
return apparmor.Ls(options)
}
9 changes: 5 additions & 4 deletions cmd/nerdctl/apparmor_unload_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ package main
import (
"fmt"

"github.com/containerd/nerdctl/pkg/apparmorutil"
"github.com/containerd/nerdctl/pkg/api/types"
"github.com/containerd/nerdctl/pkg/cmd/apparmor"
"github.com/containerd/nerdctl/pkg/defaults"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -43,8 +43,9 @@ func apparmorUnloadAction(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
target = args[0]
}
logrus.Infof("Unloading profile %q", target)
return apparmorutil.Unload(target)
options := &types.UnloadCommandOptions{}
options.Target = target
return apparmor.Unload(options)
}

func apparmorUnloadShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/nerdctl/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import (
"strings"
"text/template"

"github.com/containerd/nerdctl/pkg/formatter"
"golang.org/x/text/cases"
"golang.org/x/text/language"

"github.com/containerd/containerd/api/services/introspection/v1"
"github.com/containerd/nerdctl/pkg/formatter"
"github.com/containerd/nerdctl/pkg/infoutil"
"github.com/containerd/nerdctl/pkg/inspecttypes/dockercompat"
"github.com/containerd/nerdctl/pkg/inspecttypes/native"
Expand Down
38 changes: 38 additions & 0 deletions pkg/api/types/apparmor_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright The containerd Authors.

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.
*/

package types

import "io"

type InspectCommandOptions struct {
// Writer is the output writer
Writer io.Writer
}

type UnloadCommandOptions struct {
// Target is the profile name
Target string
}

type LsCommandOptions struct {
// Only display profile names
Quiet bool
// Format the output using the given go template
Format string
// Writer is the output writer
Writer io.Writer
}
34 changes: 34 additions & 0 deletions pkg/cmd/apparmor/inspect_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Copyright The containerd Authors.

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.
*/

package apparmor

import (
"fmt"

"github.com/containerd/containerd/contrib/apparmor"
"github.com/containerd/nerdctl/pkg/api/types"
"github.com/containerd/nerdctl/pkg/defaults"
)

func Inspect(options *types.InspectCommandOptions) error {
b, err := apparmor.DumpDefaultProfile(defaults.AppArmorProfileName)
if err != nil {
return err
}
_, err = fmt.Fprint(options.Writer, b)
return err
}
28 changes: 28 additions & 0 deletions pkg/cmd/apparmor/load_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright The containerd Authors.

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.
*/

package apparmor

import (
"github.com/containerd/containerd/contrib/apparmor"
"github.com/containerd/nerdctl/pkg/defaults"
"github.com/sirupsen/logrus"
)

func Load() error {
logrus.Infof("Loading profile %q", defaults.AppArmorProfileName)
return apparmor.LoadDefaultProfile(defaults.AppArmorProfileName)
}
79 changes: 79 additions & 0 deletions pkg/cmd/apparmor/ls_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Copyright The containerd Authors.

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.
*/

package apparmor

import (
"bytes"
"errors"
"fmt"
"text/tabwriter"
"text/template"

"github.com/containerd/nerdctl/pkg/api/types"
"github.com/containerd/nerdctl/pkg/apparmorutil"
"github.com/containerd/nerdctl/pkg/formatter"
)

func Ls(options *types.LsCommandOptions) error {
quiet := options.Quiet
w := options.Writer
var tmpl *template.Template
format := options.Format
switch format {
case "", "table", "wide":
w = tabwriter.NewWriter(options.Writer, 4, 8, 4, ' ', 0)
if !quiet {
fmt.Fprintln(w, "NAME\tMODE")
}
case "raw":
return errors.New("unsupported format: \"raw\"")
default:
if quiet {
return errors.New("format and quiet must not be specified together")
}
var err error
tmpl, err = formatter.ParseTemplate(format)
if err != nil {
return err
}
}

profiles, err := apparmorutil.Profiles()
if err != nil {
return err
}

for _, f := range profiles {
if tmpl != nil {
var b bytes.Buffer
if err := tmpl.Execute(&b, f); err != nil {
return err
}
if _, err = fmt.Fprintf(w, b.String()+"\n"); err != nil {
return err
}
} else if quiet {
fmt.Fprintln(w, f.Name)
} else {
fmt.Fprintf(w, "%s\t%s\n", f.Name, f.Mode)
}
}
if f, ok := w.(formatter.Flusher); ok {
return f.Flush()
}
return nil
}
28 changes: 28 additions & 0 deletions pkg/cmd/apparmor/unload_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Copyright The containerd Authors.

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.
*/

package apparmor

import (
"github.com/containerd/nerdctl/pkg/api/types"
"github.com/containerd/nerdctl/pkg/apparmorutil"
"github.com/sirupsen/logrus"
)

func Unload(options *types.UnloadCommandOptions) error {
logrus.Infof("Unloading profile %q", options.Target)
return apparmorutil.Unload(options.Target)
}