Skip to content

Commit

Permalink
refactor!(common): #388 moved WriteOscalModel to the oscal package (c…
Browse files Browse the repository at this point in the history
…omplete-schema)
  • Loading branch information
mike-winberry committed May 12, 2024
1 parent 7e36b90 commit 96c3dfa
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 65 deletions.
3 changes: 1 addition & 2 deletions src/cmd/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

oscalTypes_1_1_2 "github.com/defenseunicorns/go-oscal/src/types/oscal-1-1-2"
"github.com/defenseunicorns/lula/src/pkg/common"
"github.com/defenseunicorns/lula/src/pkg/common/network"
"github.com/defenseunicorns/lula/src/pkg/common/oscal"
"github.com/defenseunicorns/lula/src/pkg/message"
Expand Down Expand Up @@ -106,7 +105,7 @@ var generateComponentCmd = &cobra.Command{
}

// Write the component definition to file
err = common.WriteOscalModel(componentOpts.OutputFile, &model)
err = oscal.WriteOscalModel(componentOpts.OutputFile, &model)
if err != nil {
message.Fatalf(err, "error writing component to file")
}
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ var validateCmd = &cobra.Command{
}

// Write the component definition to file
err = common.WriteOscalModel(opts.OutputFile, &model)
err = oscal.WriteOscalModel(opts.OutputFile, &model)
if err != nil {
message.Fatalf(err, "error writing component to file")
}
Expand Down
59 changes: 0 additions & 59 deletions src/pkg/common/common.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
package common

import (
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/defenseunicorns/go-oscal/src/pkg/utils"
oscalTypes_1_1_2 "github.com/defenseunicorns/go-oscal/src/types/oscal-1-1-2"
"github.com/defenseunicorns/lula/src/pkg/common/oscal"
"github.com/defenseunicorns/lula/src/pkg/domains/api"
kube "github.com/defenseunicorns/lula/src/pkg/domains/kubernetes"
"github.com/defenseunicorns/lula/src/pkg/message"
"github.com/defenseunicorns/lula/src/pkg/providers/kyverno"
"github.com/defenseunicorns/lula/src/pkg/providers/opa"
"github.com/defenseunicorns/lula/src/types"
goversion "github.com/hashicorp/go-version"
"gopkg.in/yaml.v3"
)

const (
Expand Down Expand Up @@ -59,60 +54,6 @@ func ReadFileToBytes(path string) ([]byte, error) {
return data, nil
}

// WriteOscalModel takes a path and writes content to a file while performing checks for existing content
// supports both json and yaml
func WriteOscalModel(filePath string, model *oscalTypes_1_1_2.OscalModels) error {

// if no path or directory add default filename
if filepath.Ext(filePath) == "" {
filePath = filepath.Join(filePath, "oscal.yaml")
}

if err := utils.IsJsonOrYaml(filePath); err != nil {
return err
}

if _, err := os.Stat(filePath); err == nil {
// If the file exists - read the data into the model
existingFileBytes, err := os.ReadFile(filePath)
if err != nil {
return err
}
existingModel, err := oscal.NewOscalModel(existingFileBytes)
if err != nil {
return err
}
// Merge the existing model with the new model
// re-assign to perform common operations below
model, err = oscal.MergeOscalModels(existingModel, model)
if err != nil {
return err
}
}

var b bytes.Buffer

if filepath.Ext(filePath) == ".json" {
jsonEncoder := json.NewEncoder(&b)
jsonEncoder.SetIndent("", " ")
jsonEncoder.Encode(model)
} else {
yamlEncoder := yaml.NewEncoder(&b)
yamlEncoder.SetIndent(2)
yamlEncoder.Encode(model)
}

err := utils.WriteOutput(b.Bytes(), filePath)
if err != nil {
return err
}

message.Infof("OSCAL artifact written to: %s", filePath)

return nil

}

// Returns version validity
func IsVersionValid(versionConstraint string, version string) (bool, error) {
if version == "unset" {
Expand Down
62 changes: 62 additions & 0 deletions src/pkg/common/oscal/complete-schema.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package oscal

import (
"bytes"
"encoding/json"
"os"
"path/filepath"

"github.com/defenseunicorns/go-oscal/src/pkg/utils"
oscalTypes_1_1_2 "github.com/defenseunicorns/go-oscal/src/types/oscal-1-1-2"
"github.com/defenseunicorns/lula/src/pkg/message"
yamlV3 "gopkg.in/yaml.v3"
"sigs.k8s.io/yaml"
)

Expand All @@ -14,6 +22,60 @@ func NewOscalModel(data []byte) (*oscalTypes_1_1_2.OscalModels, error) {
return &oscalModel, nil
}

// WriteOscalModel takes a path and writes content to a file while performing checks for existing content
// supports both json and yaml
func WriteOscalModel(filePath string, model *oscalTypes_1_1_2.OscalModels) error {

// if no path or directory add default filename
if filepath.Ext(filePath) == "" {
filePath = filepath.Join(filePath, "oscal.yaml")
}

if err := utils.IsJsonOrYaml(filePath); err != nil {
return err
}

if _, err := os.Stat(filePath); err == nil {
// If the file exists - read the data into the model
existingFileBytes, err := os.ReadFile(filePath)
if err != nil {
return err
}
existingModel, err := NewOscalModel(existingFileBytes)
if err != nil {
return err
}
// Merge the existing model with the new model
// re-assign to perform common operations below
model, err = MergeOscalModels(existingModel, model)
if err != nil {
return err
}
}

var b bytes.Buffer

if filepath.Ext(filePath) == ".json" {
jsonEncoder := json.NewEncoder(&b)
jsonEncoder.SetIndent("", " ")
jsonEncoder.Encode(model)
} else {
yamlEncoder := yamlV3.NewEncoder(&b)
yamlEncoder.SetIndent(2)
yamlEncoder.Encode(model)
}

err := utils.WriteOutput(b.Bytes(), filePath)
if err != nil {
return err
}

message.Infof("OSCAL artifact written to: %s", filePath)

return nil

}

func MergeOscalModels(existingModel *oscalTypes_1_1_2.OscalModels, newModel *oscalTypes_1_1_2.OscalModels) (*oscalTypes_1_1_2.OscalModels, error) {
var err error
// Now to check each model type - currently only component definition and assessment-results apply
Expand Down
5 changes: 2 additions & 3 deletions src/test/e2e/pod_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/defenseunicorns/go-oscal/src/pkg/validation"
oscalTypes_1_1_2 "github.com/defenseunicorns/go-oscal/src/types/oscal-1-1-2"
"github.com/defenseunicorns/lula/src/cmd/validate"
"github.com/defenseunicorns/lula/src/pkg/common"
"github.com/defenseunicorns/lula/src/pkg/common/oscal"
"github.com/defenseunicorns/lula/src/pkg/message"
"github.com/defenseunicorns/lula/src/test/util"
Expand Down Expand Up @@ -150,7 +149,7 @@ func validatePodLabelPass(ctx context.Context, t *testing.T, config *envconf.Con
}

// Write the component definition to file
err = common.WriteOscalModel("sar-test.yaml", &model)
err = oscal.WriteOscalModel("sar-test.yaml", &model)
if err != nil {
message.Fatalf(err, "error writing component to file")
}
Expand All @@ -167,7 +166,7 @@ func validatePodLabelPass(ctx context.Context, t *testing.T, config *envconf.Con
}

// Write the component definition to file
err = common.WriteOscalModel("sar-test.yaml", &model)
err = oscal.WriteOscalModel("sar-test.yaml", &model)
if err != nil {
message.Fatalf(err, "error writing component to file")
}
Expand Down

0 comments on commit 96c3dfa

Please sign in to comment.