-
-
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.
add struct tag for encoding/xml update README update glide.lock
- Loading branch information
Showing
6 changed files
with
105 additions
and
22 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,67 @@ | ||
package report | ||
|
||
import ( | ||
"encoding/xml" | ||
"fmt" | ||
"io/ioutil" | ||
"path/filepath" | ||
"time" | ||
"bytes" | ||
|
||
"github.com/future-architect/vuls/models" | ||
) | ||
|
||
const ( | ||
VulsOpenTag = "<vulsreport>" | ||
VulsCloseTag = "</vulsreport>" | ||
) | ||
|
||
// XMLDirs array of xml files path. | ||
type XMLDirs []string | ||
|
||
func (d XMLDirs) Len() int { | ||
return len(d) | ||
} | ||
func (d XMLDirs) Swap(i, j int) { | ||
d[i], d[j] = d[j], d[i] | ||
} | ||
func (d XMLDirs) Less(i, j int) bool { | ||
return d[j] < d[i] | ||
} | ||
|
||
// 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 | ||
} |