Skip to content

Commit

Permalink
Initial code commit
Browse files Browse the repository at this point in the history
This is extracted from oscalkit project. Similarly as NIST extracted metaschema
out of original OSCAL repo, I am extracting metaschema to a separate repo.

See: https://github.com/usnistgov/metaschema
  • Loading branch information
isimluk committed May 15, 2020
1 parent 44ba118 commit aa80766
Show file tree
Hide file tree
Showing 7 changed files with 913 additions and 0 deletions.
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/gocomply/metaschema

go 1.13

require github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334 h1:VHgatEHNcBFEB7inlalqfNqw65aNkM1lGX2yt3NmbS8=
github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334/go.mod h1:SK73tn/9oHe+/Y0h39VT4UCxmurVJkR5NA7kMEAOgSE=
1 change: 1 addition & 0 deletions metaschema/OSCAL
Submodule OSCAL added at 4fc172
94 changes: 94 additions & 0 deletions metaschema/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// +build ignore

package main

import (
"encoding/xml"
"fmt"
"io"
"log"
"os"
"os/exec"

"github.com/gocomply/metaschema/metaschema"
)

const (
oscalRepo = "https://github.com/usnistgov/OSCAL.git"
metaschemaBaseDir = "OSCAL/src/metaschema/%s"
)

func main() {
rmCmd := exec.Command("rm", "-rf", "OSCAL/")
if err := rmCmd.Run(); err != nil {
log.Fatal(err)
}

cloneCmd := exec.Command("git", "clone", "--depth", "1", oscalRepo)
if err := cloneCmd.Run(); err != nil {
log.Fatal(err)
}

metaschemaPaths := map[string]string{
"validation_root": "oscal_metadata_metaschema.xml",
"nominal_catalog": "oscal_control-common_metaschema.xml",
"catalog": "oscal_catalog_metaschema.xml",
"profile": "oscal_profile_metaschema.xml",
"implementation": "oscal_implementation-common_metaschema.xml",
"ssp": "oscal_ssp_metaschema.xml",
"component": "oscal_component_metaschema.xml",
}

for _, metaschemaPath := range metaschemaPaths {
f, err := os.Open(fmt.Sprintf(metaschemaBaseDir, metaschemaPath))
if err != nil {
log.Fatal(err)
}
defer f.Close()

meta, err := decode(f)
if err != nil {
log.Fatal(err)
}

if err := metaschema.GenerateTypes(meta); err != nil {
log.Fatalf("Error generating go types for metaschema: %s", err)
}
}

rmCmd = exec.Command("rm", "-rf", "OSCAL/")
if err := rmCmd.Run(); err != nil {
log.Fatal(err)
}
}

func decode(r io.Reader) (*metaschema.Metaschema, error) {
var meta metaschema.Metaschema

d := xml.NewDecoder(r)

if err := d.Decode(&meta); err != nil {
return nil, fmt.Errorf("Error decoding metaschema: %s", err)
}

for _, imported := range meta.Import {
if imported.Href == nil {
return nil, fmt.Errorf("import element in %s is missing 'href' attribute", r)
}
imf, err := os.Open(fmt.Sprintf(metaschemaBaseDir, imported.Href.URL.String()))
if err != nil {
return nil, err
}
defer imf.Close()

importedMeta, err := decode(imf)
if err != nil {
return nil, err
}

meta.ImportedMetaschema = append(meta.ImportedMetaschema, *importedMeta)
}
err := meta.LinkDefinitions()

return &meta, err
}
Loading

0 comments on commit aa80766

Please sign in to comment.