Skip to content

Commit

Permalink
Add cmd/authenticator and misc command tweaks
Browse files Browse the repository at this point in the history
Add an authenticator command that will register, and re-authenticate,
a telemetry client system, and will report the current auth token's
issuer and expiration date.

Extended TelemtryClient with methods to get the ExpirationDate() and
Issuer() for the currently active token. Added internal helper methods
to support this.

Quietened some log messages by switching them to debug level and
tweaked some others to be more useful.

Added output messages to cmd/generator describing the steps that it
has performed. Similarly updated cmd/clientds to say that nothing
was found if the client datastore is empty.

Note that this change pulls support for JWT processing into the client
side library.
  • Loading branch information
rtamalin committed Jul 30, 2024
1 parent 7d6373a commit 121d3e8
Show file tree
Hide file tree
Showing 14 changed files with 283 additions and 20 deletions.
15 changes: 15 additions & 0 deletions cmd/authenticator/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module github.com/SUSE/telemetry/cmd/authenticator

go 1.21

replace github.com/SUSE/telemetry => ../../

require github.com/SUSE/telemetry v0.0.0-00010101000000-000000000000

require (
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/mattn/go-sqlite3 v1.14.22 // indirect
github.com/xyproto/randomstring v1.0.5 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
18 changes: 18 additions & 0 deletions cmd/authenticator/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZqKjWU=
github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
124 changes: 124 additions & 0 deletions cmd/authenticator/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package main

import (
"flag"
"fmt"
"log/slog"

"github.com/SUSE/telemetry/pkg/client"
"github.com/SUSE/telemetry/pkg/config"
"github.com/SUSE/telemetry/pkg/logging"
)

// options is a struct of the options
type options struct {
config string
dryrun bool
noregister bool
authenticate bool
debug bool
}

var opts options

func main() {
if err := logging.SetupBasicLogging(opts.debug); err != nil {
panic(err)
}

slog.Debug("Authenticator", slog.Any("options", opts))

cfg, err := config.NewConfig(opts.config)
if err != nil {
slog.Error(
"Failed to load config",
slog.String("config", opts.config),
slog.String("error", err.Error()),
)
panic(err)
}

// setup logging based upon config settings
lm := logging.NewLogManager()
if err := lm.Config(&cfg.Logging); err != nil {
panic(err)
}

// override config log level to debug if option specified
if opts.debug {
lm.SetLevel("DEBUG")
slog.Debug("Debug mode enabled")
}

if err := lm.Setup(); err != nil {
panic(err)
}

tc, err := client.NewTelemetryClient(cfg)
if err != nil {
slog.Error(
"Failed to instantiate TelemetryClient",
slog.String("config", opts.config),
slog.String("error", err.Error()),
)
panic(err)
}

if !opts.noregister {
err = tc.Register()
if err != nil {
slog.Error(
"Failed to register TelemetryClient",
slog.String("error", err.Error()),
)
panic(err)
}
}

if opts.authenticate {
err = tc.Authenticate()
if err != nil {
slog.Error(
"Failed to (re)uthenticate TelemetryClient",
slog.String("error", err.Error()),
)
panic(err)
}
}

issuer, err := tc.AuthIssuer()
if err != nil {
slog.Error(
"AuthIssuer() failed",
slog.String("err", err.Error()),
)
}

expiration, err := tc.AuthExpiration()
if err != nil {
slog.Error(
"AuthExpiration() failed",
slog.String("err", err.Error()),
)
}

fmt.Printf(
"Current Auth Token:\n %-[1]*[2]s %[3]s\n %-[1]*[4]s %[5]s\n %-[1]*[6]s %[7]s\n",
19,
"Issuer:",
issuer,
"Expiration (UTC):",
expiration.UTC().Format("2006-01-02T15:04:05.000000"),
"Expiration (local):",
expiration.Format("2006-01-02T15:04:05.000000Z07:00"),
)
}

func init() {
flag.StringVar(&opts.config, "config", client.CONFIG_PATH, "Path to config file to read")
flag.BoolVar(&opts.debug, "debug", false, "Whether to enable debug level logging.")
flag.BoolVar(&opts.dryrun, "dryrun", false, "Process provided JSON files but do add them to the telemetry staging area.")
flag.BoolVar(&opts.noregister, "noregister", false, "Whether to skip registering the telemetry client if it is needed.")
flag.BoolVar(&opts.authenticate, "authenticate", false, "Whether to (re)authenticate the telemetry client.")
flag.Parse()
}
1 change: 1 addition & 0 deletions cmd/clientds/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ replace github.com/SUSE/telemetry => ../../../telemetry
require github.com/SUSE/telemetry v0.0.0-00010101000000-000000000000

require (
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/mattn/go-sqlite3 v1.14.22 // indirect
github.com/xyproto/randomstring v1.0.5 // indirect
Expand Down
2 changes: 2 additions & 0 deletions cmd/clientds/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
Expand Down
23 changes: 17 additions & 6 deletions cmd/clientds/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ type options struct {
debug bool
}

func (o options) String() string {
return fmt.Sprintf("config=%v, items=%v, bundles=%v, reports=%v, debug=%v", o.config, o.items, o.bundles, o.reports, o.debug)
}

var opts options

func main() {
fmt.Printf("clientds: %s\n", opts)
slog.Debug(
"clientds",
slog.Any("options", opts),
)

if err := logging.SetupBasicLogging(opts.debug); err != nil {
panic(err)
Expand All @@ -41,7 +40,6 @@ func main() {
)
panic(err)
}
fmt.Printf("Config: %+v\n", cfg)

// setup logging based upon config settings
lm := logging.NewLogManager()
Expand Down Expand Up @@ -71,6 +69,9 @@ func main() {

processor := tc.Processor()

// this will be toggled to true if items, bundles or reports were found
foundEntries := false

if opts.items {
itemRows, err := processor.GetItemRows()
if err != nil {
Expand All @@ -87,6 +88,8 @@ func main() {
for i, dataItemRow := range itemRows {
fmt.Printf("Data Item[%d]: %q\n", i, dataItemRow.ItemId)
}

foundEntries = true
}
}

Expand All @@ -106,6 +109,8 @@ func main() {
for i, bundleRow := range bundleRows {
fmt.Printf("Bundle[%d]: %q\n", i, bundleRow.BundleId)
}

foundEntries = true
}
}

Expand All @@ -125,8 +130,14 @@ func main() {
for i, reportRow := range reportRows {
fmt.Printf("Reports[%d]: %q\n", i, reportRow.ReportId)
}

foundEntries = true
}
}

if !foundEntries {
fmt.Println("No items, bundles or reports found in client datastore")
}
}

func init() {
Expand Down
1 change: 1 addition & 0 deletions cmd/generator/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ replace github.com/SUSE/telemetry => ../../
require github.com/SUSE/telemetry v0.0.0-00010101000000-000000000000

require (
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/mattn/go-sqlite3 v1.14.22 // indirect
github.com/xyproto/randomstring v1.0.5 // indirect
Expand Down
2 changes: 2 additions & 0 deletions cmd/generator/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
Expand Down
14 changes: 14 additions & 0 deletions cmd/generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log/slog"
"os"
"path/filepath"

"github.com/SUSE/telemetry/pkg/client"
"github.com/SUSE/telemetry/pkg/config"
Expand Down Expand Up @@ -114,6 +115,13 @@ func main() {
)
panic(err)
}

fmt.Printf(
"Added telemetry data from %q as type %q with tags %s to local datastore\n",
filepath.Base(jsonFile),
opts.telemetry,
opts.tags,
)
}

// create one or more bundles from available data items
Expand All @@ -125,6 +133,8 @@ func main() {
)
panic(err)
}

fmt.Println("Created telemetry bundles from pending telemetry data items")
}

// create one or more reports from available bundles
Expand All @@ -136,6 +146,8 @@ func main() {
)
panic(err)
}

fmt.Println("Created telemetry reports from pending telemetry bundles")
}

// create one or more reports from available bundles and then
Expand All @@ -148,6 +160,8 @@ func main() {
)
panic(err)
}

fmt.Println("Submitted pending telemetry reports")
}
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21
replace github.com/SUSE/telemetry => ../telemetry

require (
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/google/uuid v1.6.0
github.com/mattn/go-sqlite3 v1.14.22
github.com/stretchr/testify v1.9.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
Expand Down
Loading

0 comments on commit 121d3e8

Please sign in to comment.