Skip to content
This repository has been archived by the owner on Aug 20, 2021. It is now read-only.

Commit

Permalink
Merge pull request #88 from isimluk/fix-metachema-generation
Browse files Browse the repository at this point in the history
Revamp project to work with latest OSCAL
  • Loading branch information
anweiss authored Feb 5, 2020
2 parents 2d10adb + 97c7ad1 commit c6af1c9
Show file tree
Hide file tree
Showing 84 changed files with 3,169 additions and 69,604 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.11-alpine AS builder
FROM golang:1.13-alpine AS builder
WORKDIR /go/src/github.com/docker/oscalkit
ARG VERSION
ARG BUILD
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile.build
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
FROM golang:1.11 AS race-detector
FROM golang:1.13 AS race-detector
WORKDIR /go/src/github.com/docker/oscalkit
COPY . .
WORKDIR /go/src/github.com/docker/oscalkit/cli
RUN go build -race

FROM golang:1.11
FROM golang:1.13
ARG GOOS
ARG GOARCH
ARG VERSION
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.generate
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
FROM golang:1.11-alpine
FROM golang:1.13-alpine
RUN apk add --no-cache git
WORKDIR /go/src/github.com/docker/oscalkit/metaschema
6 changes: 0 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ generate:

test:
@echo "Running Oscalkit test Utility"
@sh test_util/RunTest.sh -p test_util/artifacts/NIST_SP-800-53_rev4_HIGH-baseline_profile.xml
@sh test_util/RunTest.sh -p test_util/artifacts/NIST_SP-800-53_rev4_MODERATE-baseline_profile.xml
@sh test_util/RunTest.sh -p test_util/artifacts/NIST_SP-800-53_rev4_LOW-baseline_profile.xml
@sh test_util/RunTest.sh -p test_util/artifacts/FedRAMP_HIGH-baseline_profile.xml
@sh test_util/RunTest.sh -p test_util/artifacts/FedRAMP_MODERATE-baseline_profile.xml
@sh test_util/RunTest.sh -p test_util/artifacts/FedRAMP_LOW-baseline_profile.xml
@echo "Running remaining tests"
@go test -race -coverprofile=coverage.txt -covermode=atomic -v $(shell go list ./... | grep -v "/vendor/\|/test_util/src")

Expand Down
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ Convert OpenControl project to OSCAL-formatted JSON:

### Validate against XML and JSON schemas

The tool supports validation of OSCAL-formatted XML and JSON files against the corresponding OSCAL XML schemas (.xsd) and JSON schemas. XML schema validation requires the `xmllint` tool on the local machine (included with macOS and Linux. Windows installation instructions [here](https://stackoverflow.com/a/21227833))
The tool supports validation of OSCAL-formatted XML and JSON files against the corresponding OSCAL XML schemas (.xsd) and JSON schemas. Schemas are packaged with the tool and found automatically based on the type of OSCAL file. XML schema validation requires the `xmllint` tool on the local machine (included with macOS and Linux. Windows installation instructions [here](https://stackoverflow.com/a/21227833))

```
NAME:
Expand All @@ -171,9 +171,6 @@ USAGE:
DESCRIPTION:
Validate OSCAL-formatted XML files against a specific XML schema (.xsd)
or OSCAL-formatted JSON files against a specific JSON schema
OPTIONS:
--schema value, -s value schema file to validate against
```

#### Examples
Expand Down
1 change: 1 addition & 0 deletions cli/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func Execute() error {
return nil
}
app.Commands = []cli.Command{
Info,
convert.Convert,
Validate,
Sign,
Expand Down
3 changes: 2 additions & 1 deletion cli/cmd/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var Convert = cli.Command{
Usage: "convert between one or more OSCAL file formats and from OpenControl format",
Subcommands: []cli.Command{
ConvertOSCAL,
// ConvertOpenControl,
ConvertHTML,
ConvertOpenControl,
},
}
68 changes: 68 additions & 0 deletions cli/cmd/convert/html.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package convert

import (
"fmt"
"github.com/docker/oscalkit/pkg/oscal_source"
"github.com/urfave/cli"
"os"
)

// ConvertHTML ...
var ConvertHTML = cli.Command{
Name: "html",
Usage: "convert OSCAL file to human readable HTML",
Description: `The command accepts source file and generates HTML representation of given file`,
ArgsUsage: "[source-file]",
Flags: []cli.Flag{
cli.StringFlag{
Name: "output-path, o",
Usage: "Output path for converted file(s). Defaults to current working directory",
Destination: &outputPath,
},
},
Before: func(c *cli.Context) error {
if c.NArg() != 1 {
// Check for stdin
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
return nil
}

return cli.NewExitError("oscalkit convert html requires at one argument", 1)
}

return nil
},
Action: func(c *cli.Context) error {
for _, sourcePath := range c.Args() {
source, err := oscal_source.Open(sourcePath)
if err != nil {
return cli.NewExitError(fmt.Sprintf("could not load input file: %s", err), 1)
}
defer source.Close()

buffer, err := source.HTML()
if err != nil {
return cli.NewExitError(fmt.Sprintf("could convert to HTML: %s", err), 1)
}
if outputPath == "" {
fmt.Println(buffer.String())
return nil
}

f, err := os.Create(outputPath)
if err != nil {
return cli.NewExitError(fmt.Sprintf("could write to file: %s", err), 1)
}
_, err = f.WriteString(buffer.String())
if err != nil {
return cli.NewExitError(fmt.Sprintf("could write to file: %s", err), 1)
}
err = f.Close()
if err != nil {
return cli.NewExitError(fmt.Sprintf("failed to close file: %s", err), 1)
}
}
return nil
},
}
91 changes: 24 additions & 67 deletions cli/cmd/convert/opencontrol.go
Original file line number Diff line number Diff line change
@@ -1,71 +1,28 @@
package convert

var includeXML bool
import (
"github.com/docker/oscalkit/pkg/oc2oscal"
"github.com/urfave/cli"
)

// ConvertOpenControl ...
// var ConvertOpenControl = cli.Command{
// Name: "opencontrol",
// Usage: `convert from OpenControl format to OSCAL "implementation" format`,
// Description: `Convert OpenControl-formatted "component" and "opencontrol" YAML into
// OSCAL-formatted "implementation" layer JSON`,
// ArgsUsage: "[opencontrol.yaml-filepath] [opencontrols-dir-path]",
// Flags: []cli.Flag{
// cli.BoolFlag{
// Name: "yaml, y",
// Usage: "Generate YAML in addition to JSON",
// Destination: &yaml,
// },
// cli.BoolFlag{
// Name: "xml, x",
// Usage: "Generate XML in addition to JSON",
// Destination: &includeXML,
// },
// },
// Before: func(c *cli.Context) error {
// if c.NArg() != 2 {
// return cli.NewExitError("Missing opencontrol.yaml file and path to opencontrols/ directory", 1)
// }

// return nil
// },
// Action: func(c *cli.Context) error {
// ocOSCAL, err := oscal.NewFromOC(oscal.OpenControlOptions{
// OpenControlYAMLFilepath: c.Args().First(),
// OpenControlsDir: c.Args()[1],
// })
// if err != nil {
// return cli.NewExitError(err, 1)
// }

// if includeXML {
// rawXMLOCOSCAL, err := ocOSCAL.XML(true)
// if err != nil {
// return cli.NewExitError(fmt.Sprintf("Error producing raw XML: %s", err), 1)
// }
// if err := ioutil.WriteFile("opencontrol-oscal.xml", rawXMLOCOSCAL, 0644); err != nil {
// return cli.NewExitError(err, 1)
// }
// }

// if yaml {
// rawYAMLOCOSCAL, err := ocOSCAL.YAML()
// if err != nil {
// return cli.NewExitError(err, 1)
// }
// if err := ioutil.WriteFile("opencontrol-oscal.yaml", rawYAMLOCOSCAL, 0644); err != nil {
// return cli.NewExitError(err, 1)
// }
// }

// rawOCOSCAL, err := ocOSCAL.JSON(true)
// if err != nil {
// return cli.NewExitError(err, 1)
// }

// if err := ioutil.WriteFile("opencontrol-oscal.json", rawOCOSCAL, 0644); err != nil {
// return cli.NewExitError(err, 1)
// }

// return nil
// },
// }
var ConvertOpenControl = cli.Command{
Name: "opencontrol",
Usage: `convert from OpenControl format to OSCAL "implementation" format`,
Description: `Convert OpenControl masonry repository into OSCAL directory`,
ArgsUsage: "[masonry-repository] [output-directory]",
Before: func(c *cli.Context) error {
if c.NArg() != 2 {
return cli.NewExitError("Missing masonry repository or output directory", 1)
}
return nil
},
Action: func(c *cli.Context) error {
err := oc2oscal.Convert(c.Args()[0], c.Args()[1])
if err != nil {
return cli.NewExitError(err, 1)
}

return nil
},
}
18 changes: 4 additions & 14 deletions cli/cmd/generate/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"encoding/xml"
"fmt"
"io/ioutil"
"os"

"github.com/sirupsen/logrus"

"github.com/docker/oscalkit/generator"
"github.com/docker/oscalkit/pkg/oscal_source"
"github.com/urfave/cli"
)

Expand Down Expand Up @@ -45,23 +45,13 @@ var Catalog = cli.Command{
return nil
},
Action: func(c *cli.Context) error {

profilePath, err := generator.GetAbsolutePath(profilePath)
if err != nil {
return cli.NewExitError(fmt.Sprintf("cannot get absolute path, err: %v", err), 1)
}

_, err = os.Stat(profilePath)
if err != nil {
return cli.NewExitError(fmt.Sprintf("cannot fetch file, err %v", err), 1)
}
f, err := os.Open(profilePath)
os, err := oscal_source.Open(profilePath)
if err != nil {
return cli.NewExitError(err, 1)
}
defer f.Close()
defer os.Close()

profile, err := generator.ReadProfile(f)
profile, err := generator.ReadProfile(os.OSCAL())
if err != nil {
return cli.NewExitError(err, 1)
}
Expand Down
16 changes: 4 additions & 12 deletions cli/cmd/generate/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"regexp"

"github.com/docker/oscalkit/generator"
"github.com/docker/oscalkit/pkg/oscal_source"
"github.com/docker/oscalkit/templates"
"github.com/docker/oscalkit/types/oscal/catalog"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -53,22 +54,13 @@ var Code = cli.Command{
return cli.NewExitError(err, 1)
}

profilePath, err := generator.GetAbsolutePath(profilePath)
if err != nil {
return cli.NewExitError(fmt.Sprintf("cannot get absolute path, err: %v", err), 1)
}

_, err = os.Stat(profilePath)
if err != nil {
return cli.NewExitError(fmt.Sprintf("cannot fetch file, err %v", err), 1)
}
f, err := os.Open(profilePath)
osource, err := oscal_source.Open(profilePath)
if err != nil {
return cli.NewExitError(err, 1)
}
defer f.Close()
defer osource.Close()

profile, err := generator.ReadProfile(f)
profile, err := generator.ReadProfile(osource.OSCAL())
if err != nil {
return cli.NewExitError(err, 1)
}
Expand Down
71 changes: 71 additions & 0 deletions cli/cmd/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package cmd

import (
"fmt"

"github.com/docker/oscalkit/pkg/oscal/constants"
"github.com/docker/oscalkit/pkg/oscal_source"
"github.com/docker/oscalkit/types/oscal/catalog"
"github.com/urfave/cli"
)

// Catalog generates json/xml catalogs
var Info = cli.Command{
Name: "info",
Usage: "Provides information about particular OSCAL resource",
ArgsUsage: "[file]",
Action: func(c *cli.Context) error {
for _, filePath := range c.Args() {
os, err := oscal_source.Open(filePath)
if err != nil {
return cli.NewExitError(fmt.Sprintf("Could not open oscal file: %v", err), 1)
}
defer os.Close()

o := os.OSCAL()
switch o.DocumentType() {
case constants.SSPDocument:
fmt.Println("OSCAL System Security Plan")
fmt.Println("ID:\t", o.SystemSecurityPlan.Id)
printMetadata(o.SystemSecurityPlan.Metadata)
return nil
case constants.ComponentDocument:
fmt.Println("OSCAL Component (represents information about particular software asset/component)")
printMetadata(o.Component.Metadata)
return nil
case constants.ProfileDocument:
fmt.Println("OSCAL Profile (represents subset of controls from OSCAL catalog(s))")
fmt.Println("ID:\t", o.Profile.Id)
printMetadata(o.Profile.Metadata)
return nil
case constants.CatalogDocument:
fmt.Println("OSCAL Catalog (represents library of control assessment objectives and activities)")
fmt.Println("ID:\t", o.Catalog.Id)
printMetadata(o.Catalog.Metadata)
return nil
}
return cli.NewExitError("Unrecognized OSCAL resource", 1)
}
return cli.NewExitError("No file provided", 1)
},
}

func printMetadata(m *catalog.Metadata) {
if m == nil {
return
}
fmt.Println("Metadata:")
fmt.Println("\tTitle:\t\t\t", m.Title)
if m.Published != "" {
fmt.Println("\tPublished:\t\t", m.Published)
}
if m.LastModified != "" {
fmt.Println("\tLast Modified:\t\t", m.LastModified)
}
if m.Version != "" {
fmt.Println("\tDocument Version:\t", m.Version)
}
if m.OscalVersion != "" {
fmt.Println("\tOSCAL Version:\t\t", m.OscalVersion)
}
}
Loading

0 comments on commit c6af1c9

Please sign in to comment.