generate

package
v0.3.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2020 License: CC0-1.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Catalog = cli.Command{
	Name:  "catalogs",
	Usage: "generates json/xml catalogs provided profile",
	Flags: []cli.Flag{
		cli.StringFlag{
			Name:        "profile, p",
			Usage:       "profile to intersect against",
			Destination: &profilePath,
		},
		cli.StringFlag{
			Name:        "output, o",
			Usage:       "output filename",
			Destination: &outputFileName,
			Value:       "output",
		},
		cli.BoolFlag{

			Name:        "json, j",
			Usage:       "flag for generating catalogs in json",
			Destination: &isJSON,
		},
	},
	Before: func(c *cli.Context) error {
		if profilePath == "" {
			return cli.NewExitError("gocomply_oscalkit generate is missing the --profile flag", 1)
		}
		return nil
	},
	Action: func(c *cli.Context) error {
		os, err := oscal_source.Open(profilePath)
		if err != nil {
			return cli.NewExitError(err, 1)
		}
		defer os.Close()

		profile, err := generator.ReadProfile(os.OSCAL())
		if err != nil {
			return cli.NewExitError(err, 1)
		}

		profile, err = generator.SetBasePath(profile, profilePath)
		if err != nil {
			return cli.NewExitError(fmt.Errorf("failed to setup href path for profiles: %v", err), 1)
		}

		catalogs, err := generator.CreateCatalogsFromProfile(profile)
		if err != nil {
			return cli.NewExitError(fmt.Sprintf("cannot create catalogs from profile, err: %v", err), 1)
		}

		var bytes []byte
		if !isJSON {
			bytes, err = xml.MarshalIndent(catalogs, "", "  ")
			if err != nil {
				return err
			}
			return ioutil.WriteFile(outputFileName+".xml", bytes, 0644)
		}
		bytes, err = json.MarshalIndent(catalogs, "", "  ")
		if err != nil {
			return err
		}
		return ioutil.WriteFile(outputFileName+".json", bytes, 0644)

	},
	After: func(c *cli.Context) error {
		logrus.Info("catalog file generated")
		return nil
	},
}

Catalog generates json/xml catalogs

View Source
var Code = cli.Command{
	Name:  "code",
	Usage: "generates go code against provided profile",
	Flags: []cli.Flag{
		cli.StringFlag{
			Name:        "profile, p",
			Usage:       "profile to intersect against",
			Destination: &profilePath,
		},
		cli.StringFlag{
			Name:        "output, o",
			Usage:       "output filename",
			Destination: &outputFileName,
			Value:       "output.go",
		},
		cli.StringFlag{
			Name:        "package, pkg",
			Usage:       "package name for generated go file (default is oscalkit)",
			Destination: &packageName,
			Value:       "oscalkit",
		},
	},
	Before: func(c *cli.Context) error {
		if profilePath == "" {
			return cli.NewExitError("gocomply_oscalkit generate is missing the --profile flag", 1)
		}
		return nil
	},
	Action: func(c *cli.Context) error {
		err := validatePackageName(packageName)
		if err != nil {
			return cli.NewExitError(err, 1)
		}

		osource, err := oscal_source.Open(profilePath)
		if err != nil {
			return cli.NewExitError(err, 1)
		}
		defer osource.Close()

		profile, err := generator.ReadProfile(osource.OSCAL())
		if err != nil {
			return cli.NewExitError(err, 1)
		}
		newFile, err := os.Create(outputFileName)
		if err != nil {
			return cli.NewExitError("cannot create file for catalogs", 1)
		}
		defer newFile.Close()
		profile, err = generator.SetBasePath(profile, profilePath)
		if err != nil {
			return cli.NewExitError(fmt.Errorf("failed to setup href path for profiles: %v", err), 1)
		}
		catalogs, err := generator.CreateCatalogsFromProfile(profile)
		if err != nil {
			return cli.NewExitError(fmt.Sprintf("cannot create catalogs from profile, err: %v", err), 1)
		}
		t, err := templates.GetCatalogTemplate()
		if err != nil {
			return cli.NewExitError("cannot fetch template", 1)
		}
		err = t.Execute(newFile, struct {
			Catalogs    []*catalog.Catalog
			PackageName string
		}{catalogs, packageName})

		if err != nil {
			return cli.NewExitError(fmt.Sprintf("cannot write file for catalogs, err: %v", err), 1)
		}

		b, err := ioutil.ReadFile(outputFileName)
		if err != nil {
			return cli.NewExitError(fmt.Sprintf("cannot open %s file", outputFileName), 1)
		}
		b, err = format.Source(b)
		if err != nil {
			return cli.NewExitError(fmt.Sprintf("cannot format %s file", outputFileName), 1)
		}
		err = ioutil.WriteFile(outputFileName, b, 0)
		if err != nil {
			return cli.NewExitError(fmt.Sprintf("cannot write formmated "), 1)
		}
		logrus.Info(fmt.Sprintf("%s file created.", outputFileName))
		return nil

	},
}

Code Cli command to generate go code for controls in catalog

View Source
var Generate = cli.Command{
	Name:  "generate",
	Usage: "generates catalogs code/xml/json against provided profile",
	Subcommands: []cli.Command{
		Catalog,
		Code,
		Implementation,
	},
}

Generate Cli command to generate go code for controls

View Source
var Implementation = cli.Command{
	Name:  "implementation",
	Usage: "generates go code for implementation against provided profile and excel sheet",
	Flags: []cli.Flag{
		cli.StringFlag{
			Name:        "excel, e",
			Usage:       "excel sheet to get component configs",
			Destination: &excelSheet,
		},
		cli.StringFlag{
			Name:        "output, o",
			Usage:       "output filename",
			Destination: &outputFileName,
			Value:       "implementation.go",
		},
		cli.StringFlag{
			Name:        "package, pkg",
			Usage:       "package name for generated go file (default is oscalkit)",
			Destination: &packageName,
			Value:       "oscalkit",
		},
	},
	Before: func(c *cli.Context) error {
		if excelSheet == "" {
			return cli.NewExitError("gocomply_oscalkit implementation is missing --excel flag", 1)
		}
		return nil
	},
	Action: func(c *cli.Context) error {
		err := validatePackageName(packageName)
		if err != nil {
			return cli.NewExitError(err, 1)
		}

		excelF, err := generator.GetFilePath(excelSheet)
		if err != nil {
			return err
		}
		b, err := ioutil.ReadFile(excelF)
		if err != nil {
			return err
		}

		outputFile, err := os.Create(outputFileName)
		if err != nil {
			return fmt.Errorf("cannot create file for implementation: err: %v", err)
		}
		defer outputFile.Close()

		var records [][]string
		reader := bytes.NewReader(b)
		r := csv.NewReader(reader)
		for {
			record, err := r.Read()
			if err == io.EOF {
				break
			}
			if err != nil {
				log.Fatal(err)
			}
			records = append(records, record)
		}

		catalog := impl.NISTCatalog{ID: "NIST_SP-800-53"}
		implementationData := impl.GenerateImplementation(records, &catalog)
		t, err := templates.GetImplementationTemplate()
		if err != nil {
			return fmt.Errorf("cannot get implementation template err %v", err)
		}
		err = t.Execute(outputFile, struct {
			Implementation implementation.Implementation
			PackageName    string
		}{implementationData, packageName})
		if err != nil {
			return err
		}
		b, err = ioutil.ReadFile(outputFileName)
		if err != nil {
			return cli.NewExitError(fmt.Sprintf("cannot open %s file", outputFileName), 1)
		}
		b, err = format.Source(b)
		if err != nil {
			logrus.Warn(fmt.Sprintf("cannot format %s file", outputFileName))
			return cli.NewExitError(err, 1)
		}
		err = ioutil.WriteFile(outputFileName, b, 0)
		if err != nil {
			return cli.NewExitError(fmt.Sprintf("cannot write formmated "), 1)
		}
		return nil
	},
}

Implementation generates implemntation

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL