Skip to content

Commit ff74a8c

Browse files
committed
change authentication x-auth-token
1 parent 45e6149 commit ff74a8c

File tree

6 files changed

+113
-1
lines changed

6 files changed

+113
-1
lines changed

pkg/monitor-hw/cmd/prom.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func startExporter(ruleGetter redfish.RuleGetter, client redfish.Client) error {
2727

2828
well.Go(func(ctx context.Context) error {
2929
for {
30+
client.Login(ctx)
3031
collector.Update(ctx)
3132
select {
3233
case <-time.After(time.Duration(opts.interval) * time.Second):

pkg/monitor-hw/cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ var rootCmd = &cobra.Command{
7878
if err != nil {
7979
return err
8080
}
81+
8182
client = cl
8283
ruleGetter = func(ctx context.Context) (*redfish.CollectRule, error) {
8384
version, err := cl.GetVersion(ctx)

redfish/collector.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ const namespace = "hw"
1616
type Client interface {
1717
Traverse(ctx context.Context, rule *CollectRule) Collected
1818
GetVersion(ctx context.Context) (string, error)
19+
Login(ctx context.Context) error
20+
CheckSession(ctx context.Context) error
1921
}
2022

2123
// NewCollected creates a new Collected instance

redfish/mock.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,11 @@ func (c *mockClient) Traverse(ctx context.Context, rule *CollectRule) Collected
116116
func (c *mockClient) GetVersion(ctx context.Context) (string, error) {
117117
return "1.0.0", nil
118118
}
119+
120+
func (c *mockClient) Login(ctx context.Context) error {
121+
return nil
122+
}
123+
124+
func (c *mockClient) CheckSession(ctx context.Context) error {
125+
return nil
126+
}

redfish/redfish.go

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package redfish
22

33
import (
4+
"bytes"
45
"context"
56
"crypto/tls"
67
"encoding/json"
78
"fmt"
9+
"io"
810
"net/http"
911
"net/url"
1012
"reflect"
@@ -21,6 +23,8 @@ type redfishClient struct {
2123
password string
2224
httpClient *http.Client
2325
noEscape bool
26+
token string
27+
sessionId string
2428
}
2529

2630
// ClientConfig is a set of configurations for redfishClient.
@@ -163,8 +167,9 @@ func (c *redfishClient) newRequest(ctx context.Context, path string) (*http.Requ
163167
if err != nil {
164168
return nil, err
165169
}
166-
req.SetBasicAuth(c.user, c.password)
170+
req.Header.Set("X-auth-token", c.token)
167171
req.Header.Set("Accept", "application/json")
172+
168173
req = req.WithContext(ctx)
169174

170175
return req, nil
@@ -194,3 +199,90 @@ func (c *redfishClient) follow(ctx context.Context, parsed *gabs.Container, cl C
194199
return
195200
}
196201
}
202+
203+
type BasicAuthentication struct {
204+
Username string `json:"UserName"`
205+
Password string `json:"Password"`
206+
}
207+
208+
type ResultAuthentication struct {
209+
Id string `json:"Id"`
210+
}
211+
212+
func (c *redfishClient) Login(ctx context.Context) error {
213+
if c.CheckSession(ctx) == nil {
214+
return nil
215+
}
216+
217+
bAuth := BasicAuthentication{
218+
Username: c.user,
219+
Password: c.password,
220+
}
221+
jsonBody, err := json.Marshal(bAuth)
222+
if err != nil {
223+
return fmt.Errorf("error marshaling JSON: %s", err)
224+
}
225+
226+
tr := http.DefaultTransport.(*http.Transport).Clone()
227+
tr.TLSClientConfig = &tls.Config{
228+
InsecureSkipVerify: true, // For self-signed certificates
229+
}
230+
231+
c.httpClient = &http.Client{Transport: tr, Timeout: 5 * time.Second}
232+
url := fmt.Sprintf("%s/%s", c.endpoint, "redfish/v1/SessionService/Sessions")
233+
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(jsonBody))
234+
if err != nil {
235+
return fmt.Errorf("error making NewRequest: %s", err)
236+
}
237+
req.Header.Set("Content-Type", "application/json")
238+
239+
resp, err := c.httpClient.Do(req)
240+
if err != nil {
241+
return fmt.Errorf("error accessing rest service: %s", err)
242+
}
243+
244+
byteJSON, err := io.ReadAll(resp.Body)
245+
if err != nil {
246+
return fmt.Errorf("error reading http body: %s", err)
247+
}
248+
249+
var ses ResultAuthentication
250+
json.Unmarshal(byteJSON, &ses)
251+
c.sessionId = ses.Id
252+
c.token = resp.Header.Get("X-auth-token")
253+
254+
return nil
255+
}
256+
257+
func (c *redfishClient) CheckSession(ctx context.Context) error {
258+
tr := http.DefaultTransport.(*http.Transport).Clone()
259+
tr.TLSClientConfig = &tls.Config{
260+
InsecureSkipVerify: true, // For self-signed certificates
261+
}
262+
c.httpClient = &http.Client{Transport: tr, Timeout: 5 * time.Second}
263+
264+
url := fmt.Sprintf("%s/%s/%s", c.endpoint, "redfish/v1/SessionService/Sessions", c.sessionId)
265+
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
266+
if err != nil {
267+
return fmt.Errorf("error making NewRequest: %s", err)
268+
}
269+
req.Header.Set("Content-Type", "application/json")
270+
req.Header.Set("X-auth-token", c.token)
271+
272+
resp, err := c.httpClient.Do(req)
273+
if err != nil {
274+
return fmt.Errorf("error accessing rest service: %s", err)
275+
}
276+
277+
var ses ResultAuthentication
278+
byteJSON, err := io.ReadAll(resp.Body)
279+
if err != nil {
280+
return fmt.Errorf("error reading http body: %s", err)
281+
}
282+
283+
json.Unmarshal(byteJSON, &ses)
284+
if c.sessionId == ses.Id && c.sessionId != "" {
285+
return nil
286+
}
287+
return fmt.Errorf("not exist session")
288+
}

redfish/stub_client_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,11 @@ func (c *stubClient) Traverse(ctx context.Context, rule *CollectRule) Collected
8989
func (c *stubClient) GetVersion(ctx context.Context) (string, error) {
9090
return "1.0.0", nil
9191
}
92+
93+
func (c *stubClient) Login(ctx context.Context) error {
94+
return nil
95+
}
96+
97+
func (c *stubClient) CheckSession(ctx context.Context) error {
98+
return nil
99+
}

0 commit comments

Comments
 (0)