Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redfish useraccounts #242

Merged
merged 13 commits into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ With approval from [Booking.com](http://www.booking.com), the code and
specification were generalized and published as Open Source on github, for
which the authors would like to express their gratitude.

bmclib interfaces with Redfish with https://github.com/stmcginnis/gofish

#### Authors
- Juliano Martinez
- Joel Rebello
- Joel Rebello
- Guilherme M. Schroeder
- Mariano Guezuraga
48 changes: 48 additions & 0 deletions examples/v1/users/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"context"
"log"
"time"

"github.com/bmc-toolbox/bmclib"
"github.com/bombsimon/logrusr"
"github.com/sirupsen/logrus"
)

func main() {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()

// set BMC parameters here
host := ""
port := ""
user := ""
pass := ""
sfunkhouser marked this conversation as resolved.
Show resolved Hide resolved

l := logrus.New()
l.Level = logrus.DebugLevel
logger := logrusr.NewLogger(l)

if host == "" || user == "" || pass == "" {
log.Fatal("required host/user/pass parameters not defined")
}

cl := bmclib.NewClient(host, port, user, pass, bmclib.WithLogger(logger))

// we may want to specify multiple protocols here
cl.Registry.Drivers = cl.Registry.Using("redfish")

err := cl.Open(ctx)
if err != nil {
log.Fatal(err, "bmc login failed")
}

defer cl.Close(ctx)

_, err = cl.CreateUser(ctx, "foobar", "sekurity101", "Administrator")
if err != nil {
l.Error(err)
}

}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.7.1
github.com/stmcginnis/gofish v0.8.0
github.com/stmcginnis/gofish v0.12.0
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/crypto v0.0.0-20210317152858-513c2a44f670
golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.7.1 h1:pM5oEahlgWv/WnHXpgbKz7iLIxRf65tye2Ci+XFK5sk=
github.com/spf13/viper v1.7.1/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg=
github.com/stmcginnis/gofish v0.8.0 h1:lGMKmyHQr8rG6SHKL9SU8BuA4bV/r3Pj/8J+DkuT0gY=
github.com/stmcginnis/gofish v0.8.0/go.mod h1:BGtQsY16q48M2K6KDAs38QXtNoHrkXaY/WZ/mmyMgNc=
github.com/stmcginnis/gofish v0.12.0 h1:6UbNePjA++XkHtCKKLr7envKENxljJ1YyD8f4vS3Zeo=
github.com/stmcginnis/gofish v0.12.0/go.mod h1:BGtQsY16q48M2K6KDAs38QXtNoHrkXaY/WZ/mmyMgNc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
Expand Down
7 changes: 7 additions & 0 deletions providers/redfish/redfish.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package redfish
import (
"context"
"fmt"
"os"
"strings"
"time"

Expand Down Expand Up @@ -41,13 +42,19 @@ type Conn struct {

// Open a connection to a BMC via redfish
func (c *Conn) Open(ctx context.Context) (err error) {

config := gofish.ClientConfig{
Endpoint: "https://" + c.Host,
Username: c.User,
Password: c.Pass,
Insecure: true,
}

debug := os.Getenv("DEBUG_BMCLIB")
sfunkhouser marked this conversation as resolved.
Show resolved Hide resolved
if debug == "true" {
config.DumpWriter = os.Stdout
}

c.conn, err = gofish.ConnectContext(ctx, config)
if err != nil {
return err
Expand Down
143 changes: 143 additions & 0 deletions providers/redfish/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package redfish

import (
"context"

"github.com/pkg/errors"
"github.com/stmcginnis/gofish/redfish"
)

var (
ErrNoUserSlotsAvailable = errors.New("no user account slots available")
ErrUserNotPresent = errors.New("given user not present")
ErrUserPassParams = errors.New("user and pass parameters required")
ErrUserExists = errors.New("user exists")
ErrInvalidUserRole = errors.New("invalid user role")
ValidRoles = []string{"Administrator", "Operator", "ReadOnly", "None"}
)

// UserRead returns a list of enabled user accounts
func (c *Conn) UserRead(ctx context.Context) (users []map[string]string, err error) {
sfunkhouser marked this conversation as resolved.
Show resolved Hide resolved
service, err := c.conn.Service.AccountService()
if err != nil {
return nil, err
}

accounts, err := service.Accounts()
if err != nil {
return nil, err
}

users = make([]map[string]string, 0)

for _, account := range accounts {
if account.Enabled {
user := map[string]string{
"ID": account.ID,
"Name": account.Name,
"Username": account.UserName,
"RoleID": account.RoleID,
}
users = append(users, user)
}
}

return users, nil
}

// UserUpdate updates a user password and role
func (c *Conn) UserUpdate(ctx context.Context, user, pass, role string) (ok bool, err error) {
service, err := c.conn.Service.AccountService()
if err != nil {
return false, err
}

accounts, err := service.Accounts()
if err != nil {
return false, err
}

for _, account := range accounts {
if account.UserName == user {
var change bool
if pass != "" {
account.Password = pass
change = true
}
if role != "" {
account.RoleID = role
change = true
}

if change {
err := account.Update()
if err != nil {
return false, err
}
return true, nil
}
}
}

return ok, ErrUserNotPresent
}

// UserCreate adds a new user account
func (c *Conn) UserCreate(ctx context.Context, user, pass, role string) (ok bool, err error) {
if !StringInSlice(role, ValidRoles) {
return false, ErrInvalidUserRole
}

if user == "" || pass == "" {
return false, ErrUserPassParams
}

service, err := c.conn.Service.AccountService()
if err != nil {
return false, err
}

// fetch current list of accounts
accounts, err := service.Accounts()
if err != nil {
return false, err
}

// identify account slot not in use
for _, account := range accounts {
// Dell iDracs don't want us to create accounts in these slots
if StringInSlice(account.ID, []string{"1"}) {
continue
}

account := account
sfunkhouser marked this conversation as resolved.
Show resolved Hide resolved
if account.UserName == user {
return false, errors.Wrap(ErrUserExists, user)
}

if !account.Enabled && account.UserName == "" {
account.Enabled = true
account.UserName = user
account.Password = pass
account.RoleID = role
account.AccountTypes = []redfish.AccountTypes{"Redfish", "OEM"}

err := account.Update()
if err != nil {
return false, err
}
return true, nil
}
}

return false, ErrNoUserSlotsAvailable
}

func StringInSlice(str string, sl []string) bool {
sfunkhouser marked this conversation as resolved.
Show resolved Hide resolved
for _, s := range sl {
if str == s {
return true
}
}
return false
}