Allow spec file permisions to be specified

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar 2023-05-02 16:27:50 +02:00
parent fe8ef9e0bd
commit 367a30827f
2 changed files with 23 additions and 3 deletions

View File

@ -18,6 +18,7 @@ package spec
import ( import (
"fmt" "fmt"
"os"
"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/transform" "github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/transform"
"github.com/container-orchestrated-devices/container-device-interface/pkg/cdi" "github.com/container-orchestrated-devices/container-device-interface/pkg/cdi"
@ -33,6 +34,7 @@ type builder struct {
edits specs.ContainerEdits edits specs.ContainerEdits
format string format string
noSimplify bool noSimplify bool
permissions os.FileMode
} }
// newBuilder creates a new spec builder with the supplied options // newBuilder creates a new spec builder with the supplied options
@ -60,7 +62,9 @@ func newBuilder(opts ...Option) *builder {
if s.format == "" { if s.format == "" {
s.format = FormatYAML s.format = FormatYAML
} }
if s.permissions == 0 {
s.permissions = 0600
}
return s return s
} }
@ -157,3 +161,10 @@ func WithRawSpec(raw *specs.Spec) Option {
o.raw = raw o.raw = raw
} }
} }
// WithPermissions sets the permissions for the generated spec file
func WithPermissions(permissions os.FileMode) Option {
return func(o *builder) {
o.permissions = permissions
}
}

View File

@ -28,7 +28,8 @@ import (
type spec struct { type spec struct {
*specs.Spec *specs.Spec
format string format string
permissions os.FileMode
} }
var _ Interface = (*spec)(nil) var _ Interface = (*spec)(nil)
@ -51,7 +52,15 @@ func (s *spec) Save(path string) error {
cdi.WithSpecDirs(specDir), cdi.WithSpecDirs(specDir),
) )
return registry.SpecDB().WriteSpec(s.Raw(), filepath.Base(path)) if err := registry.SpecDB().WriteSpec(s.Raw(), filepath.Base(path)); err != nil {
return fmt.Errorf("failed to write spec: %w", err)
}
if err := os.Chmod(path, s.permissions); err != nil {
return fmt.Errorf("failed to set permissions on spec file: %w", err)
}
return nil
} }
// WriteTo writes the spec to the specified writer. // WriteTo writes the spec to the specified writer.