diff --git a/examples/sel/main.go b/examples/sel/main.go new file mode 100644 index 00000000..803d38ff --- /dev/null +++ b/examples/sel/main.go @@ -0,0 +1,68 @@ +package main + +import ( + "context" + "crypto/x509" + "flag" + "io/ioutil" + "time" + + "github.com/bmc-toolbox/bmclib/v2" + "github.com/bmc-toolbox/bmclib/v2/providers" + "github.com/bombsimon/logrusr/v2" + "github.com/sirupsen/logrus" +) + +func main() { + user := flag.String("user", "", "Username to login with") + pass := flag.String("password", "", "Username to login with") + host := flag.String("host", "", "BMC hostname to connect to") + withSecureTLS := flag.Bool("secure-tls", false, "Enable secure TLS") + certPoolFile := flag.String("cert-pool", "", "Path to an file containing x509 CAs. An empty string uses the system CAs. Only takes effect when --secure-tls=true") + flag.Parse() + + l := logrus.New() + l.Level = logrus.DebugLevel + logger := logrusr.New(l) + + if *host == "" || *user == "" || *pass == "" { + l.Fatal("required host/user/pass parameters not defined") + } + + clientOpts := []bmclib.Option{ + bmclib.WithLogger(logger), + bmclib.WithRedfishUseBasicAuth(true), + } + + if *withSecureTLS { + var pool *x509.CertPool + if *certPoolFile != "" { + pool = x509.NewCertPool() + data, err := ioutil.ReadFile(*certPoolFile) + if err != nil { + l.Fatal(err) + } + pool.AppendCertsFromPEM(data) + } + // a nil pool uses the system certs + clientOpts = append(clientOpts, bmclib.WithSecureTLS(pool)) + } + + cl := bmclib.NewClient(*host, *user, *pass, clientOpts...) + cl.Registry.Drivers = cl.Registry.Supports(providers.FeatureSELClear) + + ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) + defer cancel() + + err := cl.Open(ctx) + if err != nil { + l.WithError(err).Fatal(err, "BMC login failed") + } + defer cl.Close(ctx) + + err = cl.ClearSEL(ctx) + if err != nil { + l.WithError(err).Error() + } + l.Info("SEL cleared") +}