Skip to content

Commit

Permalink
Merge pull request #339 from bmc-toolbox/smc-support-olderfw
Browse files Browse the repository at this point in the history
supermicro/firmware: support firmware installs on BMCs running older firmware
  • Loading branch information
joelrebel authored Jul 13, 2023
2 parents 37fb7ea + a17da39 commit 13adab0
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
7 changes: 6 additions & 1 deletion providers/supermicro/firmware_bios.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,12 @@ func (c *Client) uploadBIOSFirmware(ctx context.Context, fwReader io.Reader) err
case "csrf-token":
// Add csrf token field
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition", `form-data; name="CSRF-TOKEN"`)
// BMCs with newer firmware (>=1.74.09) accept the form with this name value
// h.Set("Content-Disposition", `form-data; name="CSRF-TOKEN"`)
//
// the BMCs running older firmware (<=1.23.06) versions expects the name value in this format
// and the newer firmware (>=1.74.09) seem to be backwards compatible with this name value format.
h.Set("Content-Disposition", `form-data; name="CSRF_TOKEN"`)

if partWriter, err = payloadWriter.CreatePart(h); err != nil {
return errors.Wrap(ErrMultipartForm, err.Error())
Expand Down
7 changes: 6 additions & 1 deletion providers/supermicro/firmware_bmc.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,12 @@ func (c *Client) uploadBMCFirmware(ctx context.Context, fwReader io.Reader) erro
case "csrf-token":
// Add csrf token field
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition", `form-data; name="CSRF-TOKEN"`)
// BMCs with newer firmware (>=1.74.09) accept the form with this name value
// h.Set("Content-Disposition", `form-data; name="CSRF-TOKEN"`)
//
// the BMCs running older firmware (<=1.23.06) versions expects the name value in this format
// this seems to be backwards compatible with the newer firmware.
h.Set("Content-Disposition", `form-data; name="CSRF_TOKEN"`)

if partWriter, err = payloadWriter.CreatePart(h); err != nil {
return errors.Wrap(ErrMultipartForm, err.Error())
Expand Down
2 changes: 1 addition & 1 deletion providers/supermicro/firmware_bmc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func Test_uploadBMCFirmware(t *testing.T) {
part, err = reader.NextPart()
assert.Nil(t, err)

assert.Equal(t, `form-data; name="CSRF-TOKEN"`, part.Header.Get("Content-Disposition"))
assert.Equal(t, `form-data; name="CSRF_TOKEN"`, part.Header.Get("Content-Disposition"))
},
},
}
Expand Down
26 changes: 26 additions & 0 deletions providers/supermicro/supermicro.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,20 @@ func (c *Client) query(ctx context.Context, endpoint, method string, payload io.

if c.csrfToken != "" {
req.Header.Add("Csrf-Token", c.csrfToken)
// because old firmware
req.Header.Add("CSRF_TOKEN", c.csrfToken)
}

// required on X11SCM-F with 1.23.06 and older BMC firmware
// https://go.googlesource.com/go/+/go1.20/src/net/http/request.go#124
req.Host, err = hostIP(c.host)
if err != nil {
return nil, 0, err
}

// required on X11SCM-F with 1.23.06 and older BMC firmware
req.Header.Add("Referer", c.host)

for k, v := range headers {
req.Header.Add(k, v)
}
Expand Down Expand Up @@ -411,3 +423,17 @@ func (c *Client) query(ctx context.Context, endpoint, method string, payload io.

return body, resp.StatusCode, nil
}

func hostIP(hostURL string) (string, error) {
hostURLParsed, err := url.Parse(hostURL)
if err != nil {
return "", err
}

if strings.Contains(hostURLParsed.Host, ":") {
return strings.Split(hostURLParsed.Host, ":")[0], nil

}

return hostURLParsed.Host, nil
}
5 changes: 5 additions & 0 deletions providers/supermicro/supermicro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ func Test_parseToken(t *testing.T) {
</html>`),
"fYQ/Xhd1AvA+kP/bM/tO5mhOzv4eM5evCOH/YSuBN70",
},
{
"token with key type 4 found",
[]byte(`<script>SmcCsrfInsert ("CSRF_TOKEN", "RYjdEjWIhU+PCRFMBP2ZRPPePcQ4n3dM3s+rCgTnBBU");</script></body>`),
"RYjdEjWIhU+PCRFMBP2ZRPPePcQ4n3dM3s+rCgTnBBU",
},
}

for _, tc := range testcases {
Expand Down

0 comments on commit 13adab0

Please sign in to comment.