Skip to content

Commit c56a0d4

Browse files
committed
proper logout
1 parent b10d416 commit c56a0d4

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

api/api.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ func (a *Api) getSession(host string) *Session {
6262
return session
6363
}
6464

65+
func (a *Api) deleteSession(host string) {
66+
delete(a.sessions, host)
67+
}
68+
6569
func (a *Api) postRequest(host string, path string, data string) (*http.Response, error) {
6670
// get session
6771
session := a.getSession(host)

api/login.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,53 @@ func (a *Api) Login(host string, user string, password string) (*Session, error)
9696

9797
return nil, &BMCAPIError{StatusCode: resp.StatusCode, Message: "failed to login"}
9898
}
99+
100+
// Logout makes a request to the BMC to logout the user
101+
func (a *Api) Logout(host string) error {
102+
/*
103+
curl 'https://endor-bmc.lan/api/session' -X DELETE -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:128.0) Gecko/20100101 Firefox/128.0' -H 'Accept: application/json, text/javascript, */ /*; q=0.01' -H 'Accept-Language: en-US,en;q=0.5' -H 'Accept-Encoding: gzip, deflate, br, zstd' -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' -H 'X-CSRFTOKEN: null' -H 'X-Requested-With: XMLHttpRequest' -H 'Origin: https://endor-bmc.lan' -H 'Connection: keep-alive' -H 'Referer: https://endor-bmc.lan/' -H 'Cookie: lang=en-us; i18next=en-us' -H 'Sec-Fetch-Dest: empty' -H 'Sec-Fetch-Mode: cors' -H 'Sec-Fetch-Site: same-origin' -H 'Priority: u=0' --data-raw ''
104+
*/
105+
106+
// get session
107+
session := a.getSession(host)
108+
if session == nil {
109+
return &NoSessionError{"no session found"}
110+
}
111+
112+
// call api
113+
var domain string
114+
if a.options.UseSsl {
115+
domain = "https://" + host
116+
} else {
117+
domain = "http://" + host
118+
}
119+
resource := "/api/session"
120+
121+
u, _ := url.ParseRequestURI(domain)
122+
u.Path = resource
123+
urlStr := u.String()
124+
125+
r, err := http.NewRequest(http.MethodDelete, urlStr, nil)
126+
r.Header.Add("X-CSRFTOKEN", session.CSRFToken)
127+
r.Header.Add("Cookie", "QSESSIONID="+session.QSessionID)
128+
129+
if err != nil {
130+
return fmt.Errorf("failed to logout: %v", err)
131+
}
132+
133+
resp, err := a.httpClient.Do(r)
134+
if err != nil {
135+
return fmt.Errorf("failed to logout: %v", err)
136+
}
137+
138+
defer resp.Body.Close()
139+
switch resp.StatusCode {
140+
case 200:
141+
// success
142+
// remove session from cache
143+
a.deleteSession(host)
144+
return nil
145+
}
146+
147+
return &BMCAPIError{StatusCode: resp.StatusCode, Message: "failed to logout"}
148+
}

cmd/power.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ func PowerOn(c *cli.Context) error {
2929
return cli.Exit(fmt.Sprintf("FAIL: Failed to login to BMC host %s: %v", profile.Host, err), 1)
3030
}
3131

32+
defer func() {
33+
err = srv.Logout(profile.Host)
34+
if err != nil {
35+
lgr.Logger.Logf("[WARN] Failed to logout from BMC host %s: %v", profile.Host, err)
36+
}
37+
}()
38+
3239
err = srv.PowerOn(profile.Host)
3340
if err != nil {
3441
return cli.Exit(fmt.Sprintf("FAIL: Failed to power on BMC host %s: %v", profile.Host, err), 1)
@@ -74,6 +81,13 @@ func PowerOff(c *cli.Context) error {
7481
return cli.Exit(fmt.Sprintf("FAIL: Failed to login to BMC host %s: %v", profile.Host, err), 1)
7582
}
7683

84+
defer func() {
85+
err = srv.Logout(profile.Host)
86+
if err != nil {
87+
lgr.Logger.Logf("[WARN] Failed to logout from BMC host %s: %v", profile.Host, err)
88+
}
89+
}()
90+
7791
err = srv.PowerOff(profile.Host)
7892
if err != nil {
7993
return cli.Exit(fmt.Sprintf("FAIL: Failed to power off BMC host %s: %v", profile.Host, err), 1)
@@ -119,6 +133,13 @@ func PowerStatus(c *cli.Context) error {
119133
return cli.Exit(fmt.Sprintf("FAIL: Failed to login to BMC host %s: %v", profile.Host, err), 1)
120134
}
121135

136+
defer func() {
137+
err = srv.Logout(profile.Host)
138+
if err != nil {
139+
lgr.Logger.Logf("[WARN] Failed to logout from BMC host %s: %v", profile.Host, err)
140+
}
141+
}()
142+
122143
status, err := srv.ChassisStatus(profile.Host)
123144
if err != nil {
124145
return cli.Exit(fmt.Sprintf("FAIL: Failed to get power status for BMC host %s: %v", profile.Host, err), 1)

cmd/sensor.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ func SensorList(c *cli.Context) error {
4141
return cli.Exit(fmt.Sprintf("FAIL: Failed to login to BMC host %s: %v", profile.Host, err), 1)
4242
}
4343

44+
defer func() {
45+
err = srv.Logout(profile.Host)
46+
if err != nil {
47+
lgr.Logger.Logf("[WARN] Failed to logout from BMC host %s: %v", profile.Host, err)
48+
}
49+
}()
50+
4451
sensors, err := srv.GetSensorsList(profile.Host)
4552
if err != nil {
4653
return cli.Exit(fmt.Sprintf("FAIL: Failed to get sensor list from BMC host %s: %v", profile.Host, err), 1)

0 commit comments

Comments
 (0)