forked from oscal-compass/compliance-to-policy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add oscal2policy cmd for kyverno to c2pcli
- Loading branch information
Showing
16 changed files
with
730 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
Copyright 2023 IBM Corporation | ||
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 subcommands | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/IBM/compliance-to-policy/cmd/c2pcli/options" | ||
composecmd "github.com/IBM/compliance-to-policy/cmd/compose-kyverno/cmd" | ||
) | ||
|
||
func NewKyvernoSubCommand() *cobra.Command { | ||
opts := options.NewOptions() | ||
|
||
command := &cobra.Command{ | ||
Use: "kyverno", | ||
Short: "C2P CLI Kyverno plugin", | ||
} | ||
|
||
opts.AddFlags(command.Flags()) | ||
|
||
command.AddCommand(composecmd.New()) | ||
|
||
return command | ||
} |
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,83 @@ | ||
/* | ||
Copyright 2023 IBM Corporation | ||
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 cmd | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/IBM/compliance-to-policy/cmd/compose-kyverno/options" | ||
"github.com/IBM/compliance-to-policy/pkg" | ||
"github.com/IBM/compliance-to-policy/pkg/c2pcr" | ||
"github.com/IBM/compliance-to-policy/pkg/kyverno" | ||
typec2pcr "github.com/IBM/compliance-to-policy/pkg/types/c2pcr" | ||
) | ||
|
||
func New() *cobra.Command { | ||
opts := options.NewOptions() | ||
|
||
command := &cobra.Command{ | ||
Use: "compose", | ||
Short: "Compose deliverable Kyverno policies from OSCAL", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if err := opts.Complete(); err != nil { | ||
return err | ||
} | ||
|
||
if err := opts.Validate(); err != nil { | ||
return err | ||
} | ||
return Run(opts) | ||
}, | ||
} | ||
|
||
opts.AddFlags(command.Flags()) | ||
|
||
return command | ||
} | ||
|
||
func Run(options *options.Options) error { | ||
if err := os.MkdirAll(options.OutputDir, os.ModePerm); err != nil { | ||
return err | ||
} | ||
|
||
var c2pcrSpec typec2pcr.Spec | ||
if err := pkg.LoadYamlFileToObject(options.C2PCRPath, &c2pcrSpec); err != nil { | ||
return err | ||
} | ||
|
||
gitUtils := pkg.NewGitUtils(pkg.NewTempDirectory(options.TempDirPath)) | ||
c2pcrParser := c2pcr.NewParser(gitUtils) | ||
c2pcrParsed, err := c2pcrParser.Parse(c2pcrSpec) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
tmpdir := pkg.NewTempDirectory(options.TempDirPath) | ||
composer := kyverno.NewComposer(c2pcrParsed.PolicyResoureDir, tmpdir) | ||
if err := composer.Compose(c2pcrParsed); err != nil { | ||
return err | ||
} | ||
|
||
if options.OutputDir != "" { | ||
if err := composer.CopyAllTo(options.OutputDir); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
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,30 @@ | ||
/* | ||
Copyright 2023 IBM Corporation | ||
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 main | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/IBM/compliance-to-policy/cmd/compose-kyverno/cmd" | ||
) | ||
|
||
func main() { | ||
err := cmd.New().Execute() | ||
if err != nil { | ||
os.Exit(1) | ||
} | ||
} |
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,50 @@ | ||
/* | ||
Copyright 2023 IBM Corporation | ||
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 options | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/spf13/pflag" | ||
) | ||
|
||
type Options struct { | ||
C2PCRPath string | ||
TempDirPath string | ||
OutputDir string | ||
} | ||
|
||
func NewOptions() *Options { | ||
return &Options{} | ||
} | ||
|
||
func (o *Options) AddFlags(fs *pflag.FlagSet) { | ||
fs.StringVar(&o.C2PCRPath, "c2pcr", "", "path to c2p CR") | ||
fs.StringVar(&o.TempDirPath, "temp-dir", "", "path to temp directory") | ||
fs.StringVar(&o.OutputDir, "out", ".", "path to a directory for output manifest files of generated Kyverno Policy manifests") | ||
} | ||
|
||
func (o *Options) Complete() error { | ||
return nil | ||
} | ||
|
||
func (o *Options) Validate() error { | ||
if o.C2PCRPath == "" { | ||
return errors.New("--c2pcr is required") | ||
} | ||
return nil | ||
} |
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 @@ | ||
/* | ||
Copyright 2023 IBM Corporation | ||
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 kyverno | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/IBM/compliance-to-policy/pkg" | ||
typec2pcr "github.com/IBM/compliance-to-policy/pkg/types/c2pcr" | ||
cp "github.com/otiai10/copy" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type Composer struct { | ||
policiesDir string | ||
tempDir pkg.TempDirectory | ||
logger *zap.Logger | ||
} | ||
|
||
func NewComposer(policiesDir string, tempDir pkg.TempDirectory) *Composer { | ||
return &Composer{ | ||
policiesDir: policiesDir, | ||
tempDir: tempDir, | ||
logger: pkg.GetLogger("kyverno/composer"), | ||
} | ||
} | ||
|
||
func (c *Composer) Compose(c2pParsed typec2pcr.C2PCRParsed) error { | ||
for _, componentObject := range c2pParsed.ComponentObjects { | ||
if componentObject.ComponentType == "validation" { | ||
continue | ||
} | ||
for _, ruleObject := range componentObject.RuleObjects { | ||
sourceDir := fmt.Sprintf("%s/%s", c.policiesDir, ruleObject.RuleId) | ||
destDir := fmt.Sprintf("%s/%s", c.tempDir.GetTempDir(), ruleObject.RuleId) | ||
err := cp.Copy(sourceDir, destDir) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (c *Composer) CopyAllTo(destDir string) error { | ||
if _, err := pkg.MakeDir(destDir); err != nil { | ||
return err | ||
} | ||
if err := cp.Copy(c.tempDir.GetTempDir(), destDir); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
Oops, something went wrong.