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

oob/bmc: redfish user account management #120

Merged
merged 3 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/tinkerbell/pbnj
go 1.16

require (
github.com/bmc-toolbox/bmclib v0.4.16-0.20220202200536-079d247f718c
github.com/bmc-toolbox/bmclib v0.4.16-0.20220224155224-80ddde802be7
github.com/cristalhq/jwt/v3 v3.1.0
github.com/equinix-labs/otel-init-go v0.0.5
github.com/fatih/color v1.13.0
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84=
github.com/bmc-toolbox/bmclib v0.4.16-0.20220202200536-079d247f718c h1:ZmX1a+jRaOch/drczXW76JxH1e7QNv5sLVVLfcamsWM=
github.com/bmc-toolbox/bmclib v0.4.16-0.20220202200536-079d247f718c/go.mod h1:eYUZRvpVK2o9CtZ6ER608BRCgf/w/wt6rvjqBvNSeOM=
github.com/bmc-toolbox/bmclib v0.4.16-0.20220222141527-4a6f31e75616 h1:3awZHamUUM61M+KCTsyAoWKUZZ3ao7G7OfnZ5FYwbj0=
github.com/bmc-toolbox/bmclib v0.4.16-0.20220222141527-4a6f31e75616/go.mod h1:eYUZRvpVK2o9CtZ6ER608BRCgf/w/wt6rvjqBvNSeOM=
github.com/bmc-toolbox/bmclib v0.4.16-0.20220224155224-80ddde802be7 h1:Ns/rK1cb6WN7GcnEo0rB9fcpMbjxeLWiYJmUxwdFaoQ=
github.com/bmc-toolbox/bmclib v0.4.16-0.20220224155224-80ddde802be7/go.mod h1:eYUZRvpVK2o9CtZ6ER608BRCgf/w/wt6rvjqBvNSeOM=
github.com/bombsimon/logrusr/v2 v2.0.1 h1:1VgxVNQMCvjirZIYaT9JYn6sAVGVEcNtRE0y4mvaOAM=
github.com/bombsimon/logrusr/v2 v2.0.1/go.mod h1:ByVAX+vHdLGAfdroiMg6q0zgq2FODY2lc5YJvzmOJio=
github.com/cenkalti/backoff/v4 v4.1.2 h1:6Yo7N8UP2K6LWZnW94DLVSSrbobcWdVzAYOisuDPIFo=
Expand Down
30 changes: 22 additions & 8 deletions grpc/oob/bmc/bmc.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,23 @@ func NewBMCResetter(opts ...Option) (*Action, error) {
}

// setupConnection connects to the BMC, returning BMC management methods.
func (m Action) setupConnection(ctx context.Context, u *bmclibUserManagement) ([]oob.BMC, error) {
connections := map[string]interface{}{"bmclib": u}
func (m Action) setupConnection(ctx context.Context, user, password, host string, creds *v1.UserCreds) ([]oob.BMC, error) {
connections := map[string]interface{}{
"bmclib": &bmclibUserManagement{
user: user,
password: password,
host: host,
log: m.Log,
creds: creds,
},
"bmclibNext": &bmclibNextUserManagement{
user: user,
password: password,
host: host,
log: m.Log,
creds: creds,
},
}

m.SendStatusMessage("connecting to BMC")
successfulConnections, err := common.EstablishConnections(ctx, connections)
Expand All @@ -117,8 +132,7 @@ func (m Action) setupConnection(ctx context.Context, u *bmclibUserManagement) ([
var actions []oob.BMC
for _, elem := range successfulConnections {
conn := connections[elem]

if r, ok := conn.(common.Connection); ok {
if r, ok := conn.(common.Connection); ok && elem == "bmclib" {
defer r.Close(ctx) // nolint:revive // defer in a loop is OK here, as loop length is limited
}

Expand All @@ -141,10 +155,10 @@ func (m Action) CreateUser(ctx context.Context) error {
}

creds := m.CreateUserRequest.GetUserCreds()
status := fmt.Sprintf("updating user %q", creds.GetUsername())
status := fmt.Sprintf("creating user %q", creds.GetUsername())
m.SendStatusMessage(status)

actions, err := m.setupConnection(ctx, &bmclibUserManagement{user: user, password: password, host: host, log: m.Log, creds: creds})
actions, err := m.setupConnection(ctx, user, password, host, creds)
if err != nil {
m.SendStatusMessage("connection setup failed")
return err
Expand Down Expand Up @@ -175,7 +189,7 @@ func (m Action) UpdateUser(ctx context.Context) error {
status := fmt.Sprintf("updating user %q", creds.GetUsername())
m.SendStatusMessage(status)

actions, err := m.setupConnection(ctx, &bmclibUserManagement{user: user, password: password, host: host, log: m.Log, creds: creds})
actions, err := m.setupConnection(ctx, user, password, host, creds)
if err != nil {
m.SendStatusMessage("connection setup failed")
return err
Expand Down Expand Up @@ -205,7 +219,7 @@ func (m Action) DeleteUser(ctx context.Context) error {
status := fmt.Sprintf("deleting user %q", creds.GetUsername())
m.SendStatusMessage(status)

actions, err := m.setupConnection(ctx, &bmclibUserManagement{user: user, password: password, host: host, log: m.Log, creds: creds})
actions, err := m.setupConnection(ctx, user, password, host, creds)
if err != nil {
m.SendStatusMessage("connectiion setup failed")
return err
Expand Down
106 changes: 106 additions & 0 deletions grpc/oob/bmc/users_bmclibnext.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package bmc

import (
"context"
"fmt"

"github.com/bmc-toolbox/bmclib"
"github.com/go-logr/logr"
v1 "github.com/tinkerbell/pbnj/api/v1"
"github.com/tinkerbell/pbnj/pkg/repository"
)

// bmclibNextUserManagement wraps attributes to manage user accounts.
type bmclibNextUserManagement struct {
conn *bmclib.Client
host string
log logr.Logger
password string
user string
creds *v1.UserCreds
}

// Connect sets up the BMC client connection.
func (b *bmclibNextUserManagement) Connect(ctx context.Context) error {
var errMsg repository.Error

client := bmclib.NewClient(b.host, "", b.user, b.password, bmclib.WithLogger(b.log))
client.Registry.Drivers = client.Registry.Using("redfish")

if err := client.Open(ctx); err != nil {
errMsg.Code = v1.Code_value["UNKNOWN"]
errMsg.Message = err.Error()
return &errMsg
}

b.conn = client

return nil
}

// Close closes the BMC client connection.
func (b *bmclibNextUserManagement) Close(ctx context.Context) {
b.conn.Close(ctx)
}

// CreateUser creates a user account.
func (b *bmclibNextUserManagement) CreateUser(ctx context.Context) error {
ok, err := b.conn.CreateUser(ctx, b.creds.Username, b.creds.Password, userRole(b.creds.UserRole))
if !ok && err == nil {
err = fmt.Errorf("error creating user")
}

if err != nil {
return &repository.Error{
Code: v1.Code_value["UNKNOWN"],
Message: err.Error(),
}
}

return nil
}

// UpdateUser updates a user account.
func (b *bmclibNextUserManagement) UpdateUser(ctx context.Context) error {
ok, err := b.conn.UpdateUser(ctx, b.creds.Username, b.creds.Password, userRole(b.creds.UserRole))
if !ok && err == nil {
err = fmt.Errorf("error updating user")
}

if err != nil {
return &repository.Error{
Code: v1.Code_value["UNKNOWN"],
Message: err.Error(),
}
}
return nil
}

// DeleteUser deletes a user account.
func (b *bmclibNextUserManagement) DeleteUser(ctx context.Context) error {
ok, err := b.conn.DeleteUser(ctx, b.creds.Username)
if !ok && err == nil {
err = fmt.Errorf("error deleting user")
}

if err != nil {
return &repository.Error{
Code: v1.Code_value["UNKNOWN"],
Message: err.Error(),
}
}

return nil
}

// userRole returns the Redfish equivalent role for a v1.UserRole string.
func userRole(role v1.UserRole) string {
var r string
switch role.String() {
case "USER_ROLE_USER":
r = "Operator"
default:
r = "Administrator"
}
return r
}