1
0
Fork 0
mirror of https://github.com/gocsaf/csaf.git synced 2025-12-22 18:15:42 +01:00

Add some code documentation (csaf_uploader)

This commit is contained in:
Fadi Abbud 2022-01-05 12:29:35 +01:00
parent 934d1759e9
commit a1d2a445b7

View file

@ -6,6 +6,7 @@
// SPDX-FileCopyrightText: 2021 German Federal Office for Information Security (BSI) <https://www.bsi.bund.de> // SPDX-FileCopyrightText: 2021 German Federal Office for Information Security (BSI) <https://www.bsi.bund.de>
// Software-Engineering: 2021 Intevation GmbH <https://intevation.de> // Software-Engineering: 2021 Intevation GmbH <https://intevation.de>
// Implemnts a command line tool that loads csaf documents to a trusted provider
package main package main
import ( import (
@ -28,6 +29,7 @@ import (
"golang.org/x/crypto/ssh/terminal" "golang.org/x/crypto/ssh/terminal"
) )
// The supported flag options of the uploader command line
type options struct { type options struct {
Action string `short:"a" long:"action" choice:"upload" choice:"create" default:"upload" description:"Action to perform"` Action string `short:"a" long:"action" choice:"upload" choice:"create" default:"upload" description:"Action to perform"`
URL string `short:"u" long:"url" description:"URL of the CSAF provider" default:"https://localhost/cgi-bin/csaf_provider.go" value-name:"URL"` URL string `short:"u" long:"url" description:"URL of the CSAF provider" default:"https://localhost/cgi-bin/csaf_provider.go" value-name:"URL"`
@ -53,12 +55,14 @@ type processor struct {
keyRing *crypto.KeyRing keyRing *crypto.KeyRing
} }
// Config-files pathes
var iniPaths = []string{ var iniPaths = []string{
"~/.config/csaf/uploader.ini", "~/.config/csaf/uploader.ini",
"~/.csaf_uploader.ini", "~/.csaf_uploader.ini",
"csaf_uploader.ini", "csaf_uploader.ini",
} }
// Load the OpenPGP key
func loadKey(filename string) (*crypto.Key, error) { func loadKey(filename string) (*crypto.Key, error) {
f, err := os.Open(filename) f, err := os.Open(filename)
if err != nil { if err != nil {
@ -107,6 +111,8 @@ func newProcessor(opts *options) (*processor, error) {
return &p, nil return &p, nil
} }
// httpClient Initializes the http client according
// to the "Insecure" flag option and return it
func (p *processor) httpClient() *http.Client { func (p *processor) httpClient() *http.Client {
var client http.Client var client http.Client
if p.opts.Insecure { if p.opts.Insecure {
@ -119,6 +125,7 @@ func (p *processor) httpClient() *http.Client {
return &client return &client
} }
// writeStrings prints the passed messages under the specific passed header
func writeStrings(header string, messages []string) { func writeStrings(header string, messages []string) {
if len(messages) > 0 { if len(messages) > 0 {
fmt.Println(header) fmt.Println(header)
@ -128,6 +135,8 @@ func writeStrings(header string, messages []string) {
} }
} }
// create makes and sends the request for creating the initial files and directories
// It prints the response messages
func (p *processor) create() error { func (p *processor) create() error {
req, err := http.NewRequest(http.MethodGet, p.opts.URL+"/api/create", nil) req, err := http.NewRequest(http.MethodGet, p.opts.URL+"/api/create", nil)
if err != nil { if err != nil {
@ -163,6 +172,9 @@ func (p *processor) create() error {
return nil return nil
} }
// UpoladRequest creates the request for uploading a csaf document by passing the filename.
// According to the flags values the multipart sections of the request are established.
// It returns the created http request
func (p *processor) uploadRequest(filename string) (*http.Request, error) { func (p *processor) uploadRequest(filename string) (*http.Request, error) {
data, err := os.ReadFile(filename) data, err := os.ReadFile(filename)
if err != nil { if err != nil {
@ -245,6 +257,8 @@ func (p *processor) uploadRequest(filename string) (*http.Request, error) {
return req, nil return req, nil
} }
// process calls the uploadRequest to establish the request and sends it.
// It prints the response messages
func (p *processor) process(filename string) error { func (p *processor) process(filename string) error {
req, err := p.uploadRequest(filename) req, err := p.uploadRequest(filename)
@ -286,6 +300,8 @@ func (p *processor) process(filename string) error {
return nil return nil
} }
// findIniFile looks for a file in the pre-defined pathes in "iniPaths".
// The returned value will be the name of file if found, otherwise an empty string.
func findIniFile() string { func findIniFile() string {
for _, f := range iniPaths { for _, f := range iniPaths {
name, err := homedir.Expand(f) name, err := homedir.Expand(f)
@ -300,6 +316,7 @@ func findIniFile() string {
return "" return ""
} }
// readInteractive prints a message to command line and retrives the password from it.
func readInteractive(prompt string, pw **string) error { func readInteractive(prompt string, pw **string) error {
fmt.Print(prompt) fmt.Print(prompt)
p, err := terminal.ReadPassword(int(os.Stdin.Fd())) p, err := terminal.ReadPassword(int(os.Stdin.Fd()))