Skip to content

Commit 5f1e541

Browse files
authored
Add unique parameter to openapp for forcing unique engine session. (#211)
Add `unique` parameter to `openapp` for forcing unique engine session not re-using previous session state for user.
1 parent fcbd485 commit 5f1e541

File tree

9 files changed

+49
-14
lines changed

9 files changed

+49
-14
lines changed

connection/connection.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,16 @@ func (connectionSettings *ConnectionSettings) Validate() error {
120120
}
121121

122122
// GetConnectFunc Get function for connecting to sense
123-
func (connectionSettings *ConnectionSettings) GetConnectFunc(state *session.State, appGUID string) (func() (string, error), error) {
123+
func (connectionSettings *ConnectionSettings) GetConnectFunc(state *session.State, appGUID string, customHeaders http.Header) (func() (string, error), error) {
124124
header, err := connectionSettings.GetHeaders(state)
125125
if err != nil {
126126
return nil, errors.WithStack(err)
127127
}
128128
switch connectionSettings.Mode {
129129
case JWT:
130-
return connectionSettings.JwtSettings.GetConnectFunc(state, connectionSettings, appGUID, header), nil
130+
return connectionSettings.JwtSettings.GetConnectFunc(state, connectionSettings, appGUID, header, customHeaders), nil
131131
case WS:
132-
return connectionSettings.WsSettings.GetConnectFunc(state, connectionSettings, appGUID, header), nil
132+
return connectionSettings.WsSettings.GetConnectFunc(state, connectionSettings, appGUID, header, customHeaders), nil
133133
default:
134134
return nil, errors.Errorf("Unknown connection mode <%d>", connectionSettings.Mode)
135135
}

connection/jwtconnection.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type (
5151
)
5252

5353
// GetConnectFunc which establishes a connection to Qlik Sense
54-
func (connectJWT *ConnectJWTSettings) GetConnectFunc(sessionState *session.State, connection *ConnectionSettings, appGUID string, headers http.Header) func() (string, error) {
54+
func (connectJWT *ConnectJWTSettings) GetConnectFunc(sessionState *session.State, connection *ConnectionSettings, appGUID string, headers, customHeaders http.Header) func() (string, error) {
5555
connectFunc := func() (string, error) {
5656
url, err := connection.GetURL(appGUID)
5757

@@ -80,7 +80,16 @@ func (connectJWT *ConnectJWTSettings) GetConnectFunc(sessionState *session.State
8080
return appGUID, errors.Wrap(err, "failed creating cookie jar")
8181
}
8282
}
83-
if err = sense.Connect(ctx, url, headers, sessionState.Cookies, connection.Allowuntrusted, sessionState.Timeout); err != nil {
83+
84+
// combine headers for connection
85+
connectHeaders := make(http.Header)
86+
for k, v := range headers {
87+
connectHeaders[k] = v
88+
}
89+
for k, v := range customHeaders {
90+
connectHeaders[k] = v
91+
}
92+
if err = sense.Connect(ctx, url, connectHeaders, sessionState.Cookies, connection.Allowuntrusted, sessionState.Timeout); err != nil {
8493
return appGUID, errors.WithStack(err)
8594
}
8695

connection/wsconnection.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type (
1515
)
1616

1717
// GetConnectFunc get ws connect function
18-
func (connectWs *ConnectWsSettings) GetConnectFunc(sessionState *session.State, connectionSettings *ConnectionSettings, appGUID string, header http.Header) func() (string, error) {
18+
func (connectWs *ConnectWsSettings) GetConnectFunc(sessionState *session.State, connectionSettings *ConnectionSettings, appGUID string, headers, customHeaders http.Header) func() (string, error) {
1919
return func() (string, error) {
2020
if sessionState == nil {
2121
return appGUID, errors.New("Session state is nil")
@@ -47,7 +47,16 @@ func (connectWs *ConnectWsSettings) GetConnectFunc(sessionState *session.State,
4747
ctx, cancel := sessionState.ContextWithTimeout(sessionState.BaseContext())
4848
defer cancel()
4949

50-
if err := sense.Connect(ctx, url, header, sessionState.Cookies, connectionSettings.Allowuntrusted, sessionState.Timeout); err != nil {
50+
// combine headers for connection
51+
connectHeaders := make(http.Header)
52+
for k, v := range headers {
53+
connectHeaders[k] = v
54+
}
55+
for k, v := range customHeaders {
56+
connectHeaders[k] = v
57+
}
58+
59+
if err := sense.Connect(ctx, url, connectHeaders, sessionState.Cookies, connectionSettings.Allowuntrusted, sessionState.Timeout); err != nil {
5160
return appGUID, errors.Wrap(err, "Failed connecting to sense server")
5261
}
5362

docs/settingup.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,7 @@ Open an app.
886886
* `app`: App name or app GUID (supports the use of [session variables](#session_variables)). Used with `appmode` set to `guid` or `name`.
887887
* `list`: List of apps. Used with `appmode` set to `randomnamefromlist`, `randomguidfromlist`, `roundnamefromlist` or `roundguidfromlist`.
888888
* `filename`: Path to a file in which each line represents an app. Used with `appmode` set to `randomnamefromfile`, `randomguidfromfile`, `roundnamefromfile` or `roundguidfromfile`.
889+
* `unique`: Create unqiue engine session not re-using session from previous connection with same user. Defaults to false.
889890

890891
### Examples
891892

generatedocs/data/params.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,9 @@
486486
"listboxselect.accept": [
487487
"Accept or abort selection after selection (only used with `wrap`) (`true` / `false`)."
488488
],
489+
"openapp.unique" : [
490+
"Create unqiue engine session not re-using session from previous connection with same user. Defaults to false."
491+
],
489492
"productversion.log": [
490493
"Save the product version to the log (`true` / `false`). Defaults to `false`, if omitted."
491494
],

generatedocs/generated/documentation.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ var (
353353
"listboxselect.id": {"ID of the listbox in which to select values."},
354354
"listboxselect.type": {"Selection type.", "`all`: Select all values.", "`alternative`: Select alternative values.", "`excluded`: Select excluded values.", "`possible`: Select possible values."},
355355
"listboxselect.wrap": {"Wrap selection with Begin / End selection requests (`true` / `false`)."},
356+
"openapp.unique": {"Create unqiue engine session not re-using session from previous connection with same user. Defaults to false."},
356357
"productversion.log": {"Save the product version to the log (`true` / `false`). Defaults to `false`, if omitted."},
357358
"publishsheet.mode": {"", "`allsheets`: Publish all sheets in the app.", "`sheetids`: Only publish the sheets specified by the `sheetIds` array."},
358359
"publishsheet.sheetIds": {"(optional) Array of sheet IDs for the `sheetids` mode."},

scenario/connfunchandler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func GetConnTestFuncs() []func(*connection.ConnectionSettings, *session.State, *
4242
}
4343

4444
func defaultGuidWsConnectTest(connectionSettings *connection.ConnectionSettings, sessionState *session.State, actionState *action.State) error {
45-
connectFunc, err := connectionSettings.GetConnectFunc(sessionState, "00000000-0000-0000-0000-000000000000")
45+
connectFunc, err := connectionSettings.GetConnectFunc(sessionState, "00000000-0000-0000-0000-000000000000", nil)
4646
if err != nil {
4747
return errors.Wrapf(err, "failed to get connect function")
4848
}

scenario/openapp.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package scenario
33
import (
44
"context"
55
"fmt"
6+
"net/http"
67

8+
"github.com/google/uuid"
79
"github.com/pkg/errors"
810
"github.com/qlik-oss/gopherciser/action"
911
"github.com/qlik-oss/gopherciser/appstructure"
@@ -14,9 +16,13 @@ import (
1416
)
1517

1618
type (
19+
OpenAppSettingsCore struct {
20+
UniqueSession bool `json:"unique" displayname:"Make session unique" doc-key:"openapp.unique"`
21+
}
1722
// OpenAppSettings app and server settings
1823
OpenAppSettings struct {
1924
session.AppSelection
25+
OpenAppSettingsCore
2026
}
2127

2228
connectWsSettings struct {
@@ -37,11 +43,13 @@ func (openApp *OpenAppSettings) UnmarshalJSON(arg []byte) error {
3743
return errors.Errorf("%s %s, please remove from script", ActionOpenApp, err.Error())
3844
}
3945

40-
var appSelection session.AppSelection
41-
if err := jsonit.Unmarshal(arg, &appSelection); err != nil {
46+
if err := jsonit.Unmarshal(arg, &openApp.OpenAppSettingsCore); err != nil {
47+
return errors.Wrapf(err, "failed to unmarshal action<%s>", ActionOpenApp)
48+
}
49+
50+
if err := jsonit.Unmarshal(arg, &openApp.AppSelection); err != nil {
4251
return errors.Wrapf(err, "failed to unmarshal action<%s>", ActionOpenApp)
4352
}
44-
*openApp = OpenAppSettings{appSelection}
4553
return nil
4654
}
4755

@@ -54,8 +62,12 @@ func (openApp OpenAppSettings) Execute(sessionState *session.State, actionState
5462
}
5563

5664
actionState.Details = sessionState.LogEntry.Session.AppName
57-
58-
connectFunc, err := connectionSettings.GetConnectFunc(sessionState, appEntry.ID)
65+
var headers http.Header
66+
if openApp.UniqueSession {
67+
headers = make(http.Header, 1)
68+
headers.Add("X-Qlik-Session", uuid.NewString())
69+
}
70+
connectFunc, err := connectionSettings.GetConnectFunc(sessionState, appEntry.ID, headers)
5971
if err != nil {
6072
actionState.AddErrors(errors.Wrapf(err, "Failed to get connect function"))
6173
return

scenario/openhub.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (openHub OpenHubSettings) Validate() error {
2525
func (openHub OpenHubSettings) Execute(sessionState *session.State, actionState *action.State, connectionSettings *connection.ConnectionSettings, label string, setHubStart func()) {
2626
actionState.Details = "SenseEfW"
2727

28-
connectFunc, err := connectionSettings.GetConnectFunc(sessionState, "")
28+
connectFunc, err := connectionSettings.GetConnectFunc(sessionState, "", nil)
2929
if err != nil {
3030
actionState.AddErrors(errors.Wrapf(err, "failed to get connect function"))
3131
return

0 commit comments

Comments
 (0)