From a1d2a445b7167505eb1d227745b8decc4739291f Mon Sep 17 00:00:00 2001 From: Fadi Abbud Date: Wed, 5 Jan 2022 12:29:35 +0100 Subject: [PATCH] Add some code documentation (csaf_uploader) --- cmd/csaf_uploader/main.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/cmd/csaf_uploader/main.go b/cmd/csaf_uploader/main.go index 0a4b42a..1e47fc8 100644 --- a/cmd/csaf_uploader/main.go +++ b/cmd/csaf_uploader/main.go @@ -6,6 +6,7 @@ // SPDX-FileCopyrightText: 2021 German Federal Office for Information Security (BSI) // Software-Engineering: 2021 Intevation GmbH +// Implemnts a command line tool that loads csaf documents to a trusted provider package main import ( @@ -28,6 +29,7 @@ import ( "golang.org/x/crypto/ssh/terminal" ) +// The supported flag options of the uploader command line type options struct { 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"` @@ -53,12 +55,14 @@ type processor struct { keyRing *crypto.KeyRing } +// Config-files pathes var iniPaths = []string{ "~/.config/csaf/uploader.ini", "~/.csaf_uploader.ini", "csaf_uploader.ini", } +// Load the OpenPGP key func loadKey(filename string) (*crypto.Key, error) { f, err := os.Open(filename) if err != nil { @@ -107,6 +111,8 @@ func newProcessor(opts *options) (*processor, error) { return &p, nil } +// httpClient Initializes the http client according +// to the "Insecure" flag option and return it func (p *processor) httpClient() *http.Client { var client http.Client if p.opts.Insecure { @@ -119,6 +125,7 @@ func (p *processor) httpClient() *http.Client { return &client } +// writeStrings prints the passed messages under the specific passed header func writeStrings(header string, messages []string) { if len(messages) > 0 { 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 { req, err := http.NewRequest(http.MethodGet, p.opts.URL+"/api/create", nil) if err != nil { @@ -163,6 +172,9 @@ func (p *processor) create() error { 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) { data, err := os.ReadFile(filename) if err != nil { @@ -245,6 +257,8 @@ func (p *processor) uploadRequest(filename string) (*http.Request, error) { 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 { req, err := p.uploadRequest(filename) @@ -286,6 +300,8 @@ func (p *processor) process(filename string) error { 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 { for _, f := range iniPaths { name, err := homedir.Expand(f) @@ -300,6 +316,7 @@ func findIniFile() string { return "" } +// readInteractive prints a message to command line and retrives the password from it. func readInteractive(prompt string, pw **string) error { fmt.Print(prompt) p, err := terminal.ReadPassword(int(os.Stdin.Fd()))