Skip to content

Commit bb2b308

Browse files
authored
Merge pull request #692 from KxSystems/ee-keycloak
Fix for IE 1.17 Keycloak url change
2 parents dcccbc8 + 0bc5eb4 commit bb2b308

File tree

8 files changed

+155
-108
lines changed

8 files changed

+155
-108
lines changed

package.json

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
},
108108
"kdb.linting": {
109109
"type": "boolean",
110-
"description": "Enable linting for q and quke files",
110+
"description": "Enable linting",
111111
"default": false,
112112
"scope": "resource"
113113
},
@@ -188,7 +188,7 @@
188188
{
189189
"category": "KX",
190190
"command": "kdb.datasource.create",
191-
"title": "New KX Datasource",
191+
"title": "New Datasource",
192192
"icon": "$(add)"
193193
},
194194
{
@@ -201,7 +201,7 @@
201201
{
202202
"category": "KX",
203203
"command": "kdb.scratchpad.create",
204-
"title": "New KX Workbook (q)",
204+
"title": "New Workbook (q)",
205205
"icon": {
206206
"dark": "./resources/dark/add-scratchpad.svg",
207207
"light": "./resources/light/add-scratchpad.svg"
@@ -210,7 +210,7 @@
210210
{
211211
"category": "KX",
212212
"command": "kdb.scratchpad.python.create",
213-
"title": "New KX Workbook (Python)",
213+
"title": "New Workbook (Python)",
214214
"icon": {
215215
"dark": "./resources/dark/add-scratchpad-python.svg",
216216
"light": "./resources/light/add-scratchpad-python.svg"
@@ -242,7 +242,7 @@
242242
{
243243
"category": "KX",
244244
"command": "kdb.file.pickTarget",
245-
"title": "Choose Target",
245+
"title": "Choose Execution Target",
246246
"shortTitle": "Target",
247247
"icon": "$(target)"
248248
},
@@ -265,17 +265,17 @@
265265
{
266266
"category": "KX",
267267
"command": "kdb.scratchpad.reset",
268-
"title": "Reset scratchpad"
268+
"title": "Reset Scratchpad"
269269
},
270270
{
271271
"category": "KX",
272272
"command": "kdb.scratchpad.editor.reset",
273-
"title": "Reset editor connection"
273+
"title": "Reset Scratchpad"
274274
},
275275
{
276276
"category": "KX",
277277
"command": "kdb.connections.add",
278-
"title": "New KX Connection",
278+
"title": "New Connection",
279279
"icon": "$(add)"
280280
},
281281
{
@@ -423,7 +423,7 @@
423423
{
424424
"category": "KX",
425425
"command": "kdb.toggleParameterCache",
426-
"title": "Toggle Parameter Cache"
426+
"title": "KX: Toggle Parameter Cache"
427427
},
428428
{
429429
"category": "KX",
@@ -463,7 +463,7 @@
463463
{
464464
"category": "KX",
465465
"command": "kdb.createNotebook",
466-
"title": "New KX Notebook"
466+
"title": "New Notebook"
467467
}
468468
],
469469
"keybindings": [
@@ -513,13 +513,13 @@
513513
"command": "kdb.toggleParameterCache",
514514
"key": "ctrl+shift+y",
515515
"mac": "cmd+shift+y",
516-
"when": "resourceFilename =~ /\\.(?:q|quke)$/i"
516+
"when": "resourceFilename =~ /\\.(?:q)$/i"
517517
},
518518
{
519519
"command": "kdb.file.pickTarget",
520520
"key": "ctrl+alt+t",
521521
"mac": "cmd+alt+t",
522-
"when": "kdb.connected.active && resourceFilename =~ /\\.(?:q|py)$/i"
522+
"when": "kdb.connected.active && resourceFilename =~ /\\.(?:q|py|sql)$/i"
523523
}
524524
],
525525
"snippets": [
@@ -843,7 +843,7 @@
843843
},
844844
{
845845
"command": "kdb.toggleParameterCache",
846-
"when": "true"
846+
"when": "false"
847847
},
848848
{
849849
"command": "kdb.connections.labels.rename",
@@ -1102,6 +1102,11 @@
11021102
"group": "q2@0",
11031103
"when": "resourceFilename =~ /\\.(?:q|py|sql)$/i"
11041104
},
1105+
{
1106+
"command": "kdb.toggleParameterCache",
1107+
"group": "q2@1",
1108+
"when": "resourceFilename =~ /\\.(?:q)$/i"
1109+
},
11051110
{
11061111
"command": "kdb.scratchpad.python.run.file",
11071112
"group": "p@0",

src/classes/insightsConnection.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,13 @@ export class InsightsConnection {
7676

7777
public async connect(): Promise<boolean> {
7878
ext.context.secrets.delete(this.node.details.alias);
79-
await this.getTokens().then(async (token) => {
80-
this.connected = token ? true : false;
81-
if (token) {
82-
await this.getConfig();
83-
await this.getApiConfig();
84-
await this.getMeta();
85-
}
86-
});
79+
const token = await this.getTokens();
80+
if (token) {
81+
this.connected = true;
82+
await this.getConfig();
83+
await this.getApiConfig();
84+
await this.getMeta();
85+
} else this.connected = false;
8786
return this.connected;
8887
}
8988

src/classes/localConnection.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,9 @@ export class LocalConnection {
9090
nodeq.connect(options, (err, conn) => {
9191
if (err || !conn) {
9292
ext.serverProvider.reload();
93-
notify(
94-
`Connection to server ${this.options.host}:${this.options.port} failed.`,
95-
MessageKind.ERROR,
96-
{ logger, params: err },
97-
);
93+
this.connection = undefined;
94+
this.connected = false;
95+
callback(err, this);
9896
return;
9997
}
10098

src/services/connectionManagerService.ts

Lines changed: 61 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -125,69 +125,77 @@ export class ConnectionManagementService {
125125

126126
public async connect(connLabel: string): Promise<void> {
127127
/* c8 ignore start */
128-
const connection = this.retrieveConnection(connLabel);
129-
if (!connection) {
130-
return;
131-
}
132-
if (connection instanceof KdbNode) {
133-
const connectionString = this.retrieveLocalConnectionString(connection);
134-
const authCredentials = connection.details.auth
135-
? await ext.secretSettings.getAuthData(connection.children[0])
136-
: undefined;
137-
const localConnection = new LocalConnection(
138-
connectionString,
139-
connLabel,
140-
retrieveConnLabelsNames(connection),
141-
authCredentials ? authCredentials.split(":") : undefined,
142-
connection.details.tls,
143-
);
144-
await localConnection.connect((err, conn) => {
145-
if (err) {
146-
this.connectFailBehaviour(connLabel);
147-
return;
148-
}
149-
if (conn) {
128+
try {
129+
const connection = this.retrieveConnection(connLabel);
130+
if (!connection) {
131+
this.connectFailBehaviour(
132+
connLabel,
133+
new Error(`Connection ${connLabel} not found.`),
134+
);
135+
return;
136+
}
137+
if (connection instanceof KdbNode) {
138+
const connectionString = this.retrieveLocalConnectionString(connection);
139+
const authCredentials = connection.details.auth
140+
? await ext.secretSettings.getAuthData(connection.children[0])
141+
: undefined;
142+
const localConnection = new LocalConnection(
143+
connectionString,
144+
connLabel,
145+
retrieveConnLabelsNames(connection),
146+
authCredentials ? authCredentials.split(":") : undefined,
147+
connection.details.tls,
148+
);
149+
await localConnection.connect((err, conn) => {
150+
if (err) {
151+
this.connectFailBehaviour(connLabel, err);
152+
return;
153+
}
154+
if (conn) {
155+
notify(
156+
`Connection established successfully to: ${connLabel}`,
157+
MessageKind.DEBUG,
158+
{
159+
logger,
160+
telemetry:
161+
"Connection.Connected" +
162+
this.getTelemetryConnectionType(connLabel),
163+
},
164+
);
165+
166+
ext.connectedConnectionList.push(localConnection);
167+
168+
this.connectSuccessBehaviour(connection);
169+
}
170+
});
171+
} else {
172+
ext.context.secrets.delete(connection.details.alias);
173+
const insightsConn: InsightsConnection = new InsightsConnection(
174+
connLabel,
175+
connection,
176+
);
177+
await insightsConn.connect();
178+
if (insightsConn.connected) {
150179
notify(
151180
`Connection established successfully to: ${connLabel}`,
152181
MessageKind.DEBUG,
153182
{
154183
logger,
184+
params: { insightsVersion: insightsConn.insightsVersion },
155185
telemetry:
156186
"Connection.Connected" +
157187
this.getTelemetryConnectionType(connLabel),
158188
},
159189
);
160-
161-
ext.connectedConnectionList.push(localConnection);
162-
190+
ext.connectedConnectionList.push(insightsConn);
163191
this.connectSuccessBehaviour(connection);
192+
} else {
193+
this.connectFailBehaviour(connLabel);
164194
}
165-
});
166-
} else {
167-
ext.context.secrets.delete(connection.details.alias);
168-
const insightsConn: InsightsConnection = new InsightsConnection(
169-
connLabel,
170-
connection,
171-
);
172-
await insightsConn.connect();
173-
if (insightsConn.connected) {
174-
notify(
175-
`Connection established successfully to: ${connLabel}`,
176-
MessageKind.DEBUG,
177-
{
178-
logger,
179-
params: { insightsVersion: insightsConn.insightsVersion },
180-
telemetry:
181-
"Connection.Connected" +
182-
this.getTelemetryConnectionType(connLabel),
183-
},
184-
);
185-
ext.connectedConnectionList.push(insightsConn);
186-
this.connectSuccessBehaviour(connection);
187-
} else {
188-
this.connectFailBehaviour(connLabel);
195+
refreshDataSourcesPanel();
189196
}
190-
refreshDataSourcesPanel();
197+
} catch (error) {
198+
this.connectFailBehaviour(connLabel, error);
191199
}
192200
/* c8 ignore stop */
193201
}
@@ -292,9 +300,10 @@ export class ConnectionManagementService {
292300
ext.serverProvider.reload();
293301
}
294302

295-
public connectFailBehaviour(connLabel: string): void {
296-
notify(`Connection failed to: ${connLabel}`, MessageKind.ERROR, {
303+
public connectFailBehaviour(connLabel: string, error?: unknown): void {
304+
notify(`Connection to ${connLabel} failed.`, MessageKind.ERROR, {
297305
logger,
306+
params: error,
298307
telemetry:
299308
"Connection.Failed" + this.getTelemetryConnectionType(connLabel),
300309
});

src/services/kdbInsights/codeFlowLogin.ts

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,56 @@ interface IDeferred<T> {
3030
reject: (reason: any) => void;
3131
}
3232

33-
function getAuthUrl(insightsUrl: string, realm: string) {
33+
const prefixMap = new Map<string, string>([
34+
["https://insights.example.com", "auth/"],
35+
]);
36+
37+
async function getAuthPrefix(
38+
insightsUrl: string,
39+
realm: string,
40+
): Promise<string> {
41+
let prefix = prefixMap.get(insightsUrl);
42+
if (prefix === undefined) {
43+
/* c8 ignore start */
44+
const res = await axios.get(
45+
new url.URL(
46+
`${`realms/${realm}/.well-known/openid-configuration`}`,
47+
insightsUrl,
48+
).toString(),
49+
{
50+
maxRedirects: 0,
51+
validateStatus: () => true,
52+
transformResponse: (res) => res,
53+
responseType: "json",
54+
},
55+
);
56+
prefix = res.status === 200 ? "" : "auth/";
57+
prefixMap.set(insightsUrl, prefix);
58+
/* c8 ignore stop */
59+
}
60+
return prefix;
61+
}
62+
63+
async function getAuthUrl(insightsUrl: string, realm: string) {
64+
const auth = await getAuthPrefix(insightsUrl, realm);
3465
return new url.URL(
35-
`auth/realms/${realm}/protocol/openid-connect/auth`,
66+
`${auth}realms/${realm}/protocol/openid-connect/auth`,
3667
insightsUrl,
3768
);
3869
}
3970

40-
function getTokenUrl(insightsUrl: string, realm: string) {
71+
async function getTokenUrl(insightsUrl: string, realm: string) {
72+
const auth = await getAuthPrefix(insightsUrl, realm);
4173
return new url.URL(
42-
`auth/realms/${realm}/protocol/openid-connect/token`,
74+
`${auth}realms/${realm}/protocol/openid-connect/token`,
4375
insightsUrl,
4476
);
4577
}
4678

47-
function getRevokeUrl(insightsUrl: string, realm: string) {
79+
async function getRevokeUrl(insightsUrl: string, realm: string) {
80+
const auth = await getAuthPrefix(insightsUrl, realm);
4881
return new url.URL(
49-
`auth/realms/${realm}/protocol/openid-connect/revoke`,
82+
`${auth}realms/${realm}/protocol/openid-connect/revoke`,
5083
insightsUrl,
5184
);
5285
}
@@ -85,7 +118,7 @@ export async function signIn(
85118
state: crypto.randomBytes(20).toString("hex"),
86119
};
87120

88-
const authorizationUrl = getAuthUrl(insightsUrl, realm);
121+
const authorizationUrl = await getAuthUrl(insightsUrl, realm);
89122

90123
authorizationUrl.search = queryString(authParams);
91124

@@ -115,7 +148,8 @@ export async function signOut(
115148
headers: { "Content-Type": "application/x-www-form-urlencoded" },
116149
httpsAgent: getHttpsAgent(insecure),
117150
};
118-
const requestUrl = getRevokeUrl(insightsUrl, realm);
151+
152+
const requestUrl = await getRevokeUrl(insightsUrl, realm);
119153

120154
await axios.post(requestUrl.toString(), body, headers).then((res) => {
121155
return res.data;
@@ -204,7 +238,7 @@ async function tokenRequest(
204238
httpsAgent: getHttpsAgent(insecure),
205239
};
206240

207-
const requestUrl = getTokenUrl(insightsUrl, realm);
241+
const requestUrl = await getTokenUrl(insightsUrl, realm);
208242

209243
let response;
210244
if (params.grant_type === "refresh_token") {

0 commit comments

Comments
 (0)