Skip to content

Commit

Permalink
[TECH] sync with upstream (Cloud-Foundations#17)
Browse files Browse the repository at this point in the history
* add flavour, version command, fix version source (Cloud-Foundations#229)

- make makefile single source of truth for version
- trigger the flow in the tests

* minor tests enhancements (Cloud-Foundations#232)

---------

Co-authored-by: Dušan Klinec <[email protected]>
Co-authored-by: cviecco <[email protected]>
  • Loading branch information
3 people authored May 28, 2024
1 parent eee1a3e commit d644497
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ endif
BINARY=keymaster

# These are the values we want to pass for Version and BuildTime
VERSION?=1.15.12
VERSION?=1.15.13
DEFAULT_HOST?=
VERSION_FLAVOUR?=
EXTRA_LDFLAGS?=
Expand Down
38 changes: 25 additions & 13 deletions cmd/keymaster/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
Expand Down Expand Up @@ -475,7 +476,7 @@ func getHttpClient(rootCAs *x509.CertPool, logger log.DebugLogger) (*http.Client
}
if *roundRobinDialer {
if rrDialer, err := rrdialer.New(rawDialer, "", logger); err != nil {
logger.Fatalln(err)
return nil, err
} else {
defer rrDialer.WaitForBackgroundResults(time.Second)
dialer = rrDialer
Expand All @@ -488,38 +489,37 @@ func getHttpClient(rootCAs *x509.CertPool, logger log.DebugLogger) (*http.Client
}

func Usage() {
computeUserAgent()
fmt.Fprintf(os.Stderr, "Usage: %s [flags...] [aws-role-cert]\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Version: %s\n", userAgentString)
flag.PrintDefaults()
}

func main() {
// We assume here flags are parsed
func mainWithError(stdout io.Writer, logger log.DebugLogger) error {
computeUserAgent()
flag.Usage = Usage
flag.Parse()
logger := cmdlogger.New()
if *printVersion {
fmt.Println(Version)
return
fmt.Fprintln(stdout, Version)
return nil
}
rootCAs, err := maybeGetRootCas(*rootCAFilename, logger)
if err != nil {
logger.Fatal(err)
return err
}
client, err := getHttpClient(rootCAs, logger)
if err != nil {
logger.Fatal(err)
return err
}
if *checkDevices {
err = u2f.CheckU2FDevices(logger)
if err != nil {
logger.Fatal(err)
return err
}
return
return nil
}
userName, homeDir, err := util.GetUserNameAndHomeDir()
if err != nil {
logger.Fatal(err)
return err
}
config := loadConfigFile(client, logger)
logger.Debugf(3, "loaded Config=%+v", config)
Expand All @@ -543,7 +543,19 @@ func main() {
err = setupCerts(userName, homeDir, config, client, logger)
}
if err != nil {
logger.Fatal(err)
return err
}
logger.Printf("Success")
return nil
}

func main() {
flag.Usage = Usage
flag.Parse()
logger := cmdlogger.New()
err := mainWithError(os.Stdout, logger)
if err != nil {
logger.Fatal(err)
}

}
31 changes: 31 additions & 0 deletions cmd/keymaster/main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"crypto/ed25519"
"crypto/rand"
"crypto/tls"
Expand All @@ -20,6 +21,7 @@ import (

"github.com/Cloud-Foundations/golib/pkg/log/testlogger"
"github.com/Cloud-Foundations/keymaster/lib/client/config"
"github.com/Cloud-Foundations/keymaster/lib/client/twofa/u2f"
"github.com/Cloud-Foundations/keymaster/lib/client/util"
"github.com/Cloud-Foundations/keymaster/lib/webapi/v0/proto"
)
Expand Down Expand Up @@ -285,3 +287,32 @@ func TestMainPrintVersion(t *testing.T) {
}()
<-done
}

func TestMainSimple(t *testing.T) {
logger := testlogger.New(t)
var b bytes.Buffer

// version
*printVersion = true
err := mainWithError(&b, logger)
if err != nil {
t.Fatal(err)
}
t.Logf("versionout='%s'", b.String())
// TODO: compara out to version string
*printVersion = false
b.Reset()

// checkDevices
*checkDevices = true
// As of May 2024, no devices returns an error on checkForDevices
// Because this will run inside or outside testing infra, we can
// only check if the error is consistent if any
checkDevRvalue := u2f.CheckU2FDevices(logger)
err = mainWithError(&b, logger)
if err != nil && (err.Error() != checkDevRvalue.Error()) {
t.Fatalf("manual an executed error mismatch mainerr=%s; chdevDerr=%s", err, checkDevRvalue)
}
*checkDevices = false
b.Reset()
}

0 comments on commit d644497

Please sign in to comment.