Skip to content

Commit

Permalink
pr: add password rotation method of session logon API
Browse files Browse the repository at this point in the history
Signed-off-by: Yanlin Ren <[email protected]>
  • Loading branch information
Ren YanLin authored and Ren YanLin committed Oct 17, 2023
1 parent 7f3407a commit d05b12f
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 7 deletions.
68 changes: 61 additions & 7 deletions pkg/zhmcclient/client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

// Copyright 2021-2023 IBM Corp. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -84,6 +83,12 @@ type LogonData struct {
Password string `json:"password"`
}

type ChangePasswordData struct {
Userid string `json:"userid"`
Password string `json:"password"`
NewPassword string `json:"new-password"`
}

// TODO, Use cache and use JobTopic, ObjectTopic to update cache
type Session struct {
MajorVersion int `json:"api-major-version,omitempty"`
Expand All @@ -105,12 +110,7 @@ type Client struct {
traceOutput io.Writer
}

func NewClient(endpoint string, opts *Options, l Logger) (ClientAPI, *HmcError) {

if l != nil {
logger = l
}

func newClientStruct(endpoint string, opts *Options) (*Client, *HmcError) {
tslConfig, err := SetCertificate(opts, &tls.Config{})
if err != nil {
return nil, err
Expand Down Expand Up @@ -142,6 +142,19 @@ func NewClient(endpoint string, opts *Options, l Logger) (ClientAPI, *HmcError)
Password: opts.Password,
},
}
return client, nil
}

func NewClient(endpoint string, opts *Options, l Logger) (ClientAPI, *HmcError) {

if l != nil {
logger = l
}

client, err := newClientStruct(endpoint, opts)
if err != nil {
return nil, err
}

err = client.Logon()
if err != nil {
Expand Down Expand Up @@ -256,6 +269,47 @@ func (c *Client) Logon() *HmcError {
return GenerateErrorFromResponse(responseBody)
}

// login and change password, then end session
func ChangePassword(endpoint string, opts *Options, newPassword string) *HmcError {
c, err := newClientStruct(endpoint, opts)
if err != nil {
return err
}

c.clearSession()
url := c.CloneEndpointURL()
if url == nil {
return &HmcError{Reason: int(ERR_CODE_HMC_INVALID_URL), Message: ERR_MSG_EMPTY_JOB_URI}
}
url.Path = path.Join(url.Path, "/api/sessions")

changePasswordData := ChangePasswordData{
Userid: c.logondata.Userid,
Password: c.logondata.Password,
NewPassword: newPassword,
}

status, responseBody, hmcErr := c.executeMethod(http.MethodPost, url.String(), changePasswordData, "")

defer c.Logoff()

if hmcErr != nil {
return hmcErr
}

if status == http.StatusOK || status == http.StatusCreated {
session := &Session{}
err := json.Unmarshal(responseBody, session)
if err != nil {
return getHmcErrorFromErr(ERR_CODE_HMC_UNMARSHAL_FAIL, err)
}
c.session = session
return nil
}

return GenerateErrorFromResponse(responseBody)
}

func (c *Client) LogonConsole() (sessionID string, status int, err *HmcError) {
url := c.CloneEndpointURL()
url.Path = path.Join(url.Path, "/api/sessions")
Expand Down
21 changes: 21 additions & 0 deletions pkg/zhmcclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,27 @@ var _ = Describe("client", func() {
})
})

Describe("ChangePassword", func() {
BeforeEach(func() {
hmcErr = &HmcError{
Reason: int(ERR_CODE_HMC_BAD_REQUEST),
Message: "error message",
}
})
Context("When ChangePassword is Executed", func() {
It("Check the result of ChangePassword", func() {
var endpoint string
opts := &Options{
SkipCert: false,
Username: "",
Password: "",
}
err := ChangePassword(endpoint, opts, "")
Expect(err.Error()).ToNot(BeNil())
})
})
})

Describe("SetCertificate", func() {
Context("When skipcert is false", func() {
It("returns tls config without CaCert", func() {
Expand Down

0 comments on commit d05b12f

Please sign in to comment.