-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #240 from gleentea/feature/report-xml
Add the XML output
- Loading branch information
Showing
6 changed files
with
92 additions
and
23 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,54 @@ | ||
package report | ||
|
||
import ( | ||
"bytes" | ||
"encoding/xml" | ||
"fmt" | ||
"io/ioutil" | ||
"path/filepath" | ||
"time" | ||
|
||
"github.com/future-architect/vuls/models" | ||
) | ||
|
||
const ( | ||
vulsOpenTag = "<vulsreport>" | ||
vulsCloseTag = "</vulsreport>" | ||
) | ||
|
||
// XMLWriter writes results to file. | ||
type XMLWriter struct { | ||
ScannedAt time.Time | ||
} | ||
|
||
func (w XMLWriter) Write(scanResults []models.ScanResult) (err error) { | ||
var path string | ||
if path, err = ensureResultDir(w.ScannedAt); err != nil { | ||
return fmt.Errorf("Failed to make direcotory/symlink : %s", err) | ||
} | ||
|
||
for _, scanResult := range scanResults { | ||
scanResult.ScannedAt = w.ScannedAt | ||
} | ||
|
||
var xmlBytes []byte | ||
for _, r := range scanResults { | ||
xmlPath := "" | ||
if len(r.Container.ContainerID) == 0 { | ||
xmlPath = filepath.Join(path, fmt.Sprintf("%s.xml", r.ServerName)) | ||
} else { | ||
xmlPath = filepath.Join(path, | ||
fmt.Sprintf("%s_%s.xml", r.ServerName, r.Container.Name)) | ||
} | ||
|
||
if xmlBytes, err = xml.Marshal(r); err != nil { | ||
return fmt.Errorf("Failed to Marshal to XML: %s", err) | ||
} | ||
|
||
allBytes := bytes.Join([][]byte{[]byte(xml.Header + vulsOpenTag), xmlBytes, []byte(vulsCloseTag)}, []byte{}) | ||
if err := ioutil.WriteFile(xmlPath, allBytes, 0600); err != nil { | ||
return fmt.Errorf("Failed to write XML. path: %s, err: %s", xmlPath, err) | ||
} | ||
} | ||
return nil | ||
} |