This is an automated email from the ASF dual-hosted git repository. smolnar pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/knox.git
The following commit(s) were added to refs/heads/master by this push: new e888ec0cb KNOX-2929 - Logged in user is shown on Knox UIs (#819) e888ec0cb is described below commit e888ec0cb71bd3ab5c02293639fa858025f9059a Author: Sandor Molnar <smol...@apache.org> AuthorDate: Thu Nov 16 15:29:23 2023 +0100 KNOX-2929 - Logged in user is shown on Knox UIs (#819) --- gateway-admin-ui/admin-ui/app/app.module.ts | 7 ++- .../session.information.component.html | 19 +----- .../session.information.component.ts | 55 +++++++++++++++++ .../session.information.service.ts | 69 ++++++++++++++++++++++ .../app/sessionInformation/session.information.ts | 21 ++----- gateway-admin-ui/admin-ui/index.html | 1 + .../token-generation/app/app.module.ts | 5 +- .../session.information.component.html} | 20 +------ .../app/session.information.component.ts | 56 ++++++++++++++++++ .../app/token-generation.models.ts | 9 +++ .../app/token-generation.service.ts | 20 ++++++- .../token-generation/index.html | 6 ++ .../token-management/app/app.module.ts | 5 +- .../app/session.information.component.html | 20 +------ .../app/session.information.component.ts | 56 ++++++++++++++++++ 15 files changed, 292 insertions(+), 77 deletions(-) diff --git a/gateway-admin-ui/admin-ui/app/app.module.ts b/gateway-admin-ui/admin-ui/app/app.module.ts index c0fa8b96e..02c10ef0b 100644 --- a/gateway-admin-ui/admin-ui/app/app.module.ts +++ b/gateway-admin-ui/admin-ui/app/app.module.ts @@ -47,6 +47,7 @@ import {ResourceDetailComponent} from './resource-detail/resource-detail.compone import {ProviderConfigSelectorComponent} from './provider-config-selector/provider-config-selector.component'; import {NewDescWizardComponent} from './new-desc-wizard/new-desc-wizard.component'; import {ProviderConfigWizardComponent} from './provider-config-wizard/provider-config-wizard.component'; +import {SessionInformationComponent} from './sessionInformation/session.information.component'; @NgModule({ imports: [BrowserModule, @@ -74,7 +75,8 @@ import {ProviderConfigWizardComponent} from './provider-config-wizard/provider-c ResourceDetailComponent, ProviderConfigSelectorComponent, NewDescWizardComponent, - ProviderConfigWizardComponent + ProviderConfigWizardComponent, + SessionInformationComponent ], providers: [TopologyService, ServiceDefinitionService, @@ -85,7 +87,8 @@ import {ProviderConfigWizardComponent} from './provider-config-wizard/provider-c {provide: APP_BASE_HREF, useValue: '/'} ], bootstrap: [AppComponent, - GatewayVersionComponent + GatewayVersionComponent, + SessionInformationComponent ] }) export class AppModule { diff --git a/knox-token-generation-ui/token-generation/index.html b/gateway-admin-ui/admin-ui/app/sessionInformation/session.information.component.html similarity index 57% copy from knox-token-generation-ui/token-generation/index.html copy to gateway-admin-ui/admin-ui/app/sessionInformation/session.information.component.html index 6b8d4c565..77da6559e 100644 --- a/knox-token-generation-ui/token-generation/index.html +++ b/gateway-admin-ui/admin-ui/app/sessionInformation/session.information.component.html @@ -12,21 +12,4 @@ See the License for the specific language governing permissions and limitations under the License. --> -<!doctype html> -<html> -<head> - <meta charset="utf-8"> - <title>Apache Knox Token Generation</title> - <meta name="viewport" content="width=device-width, initial-scale=1"> - <link rel="icon" type="image/x-icon" href="favicon.ico"> - <meta name="viewport" content="width=device-width, initial-scale=1"> - <!-- Custom styles for this template --> - <link href="assets/sticky-footer.css" rel="stylesheet"> - <script> - window['base-href'] = window.location.pathname; - </script> -</head> -<body class="login"> - <app-token-generation></app-token-generation> -</body> -</html> +<div style="text-align: right; color: rgb(130, 180, 93);">Logged in as {{ getUser() }}</div> diff --git a/gateway-admin-ui/admin-ui/app/sessionInformation/session.information.component.ts b/gateway-admin-ui/admin-ui/app/sessionInformation/session.information.component.ts new file mode 100644 index 000000000..59ae3e549 --- /dev/null +++ b/gateway-admin-ui/admin-ui/app/sessionInformation/session.information.component.ts @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import {Component, OnInit} from '@angular/core'; +import {SessionInformationService} from './session.information.service'; +import {SessionInformation} from './session.information'; + +@Component({ + selector: 'app-session-information', + templateUrl: './session.information.component.html', + providers: [SessionInformationService] +}) + +export class SessionInformationComponent implements OnInit { + + sessionInformation: SessionInformation; + logoutSupported = true; + + constructor(private sessionInformationService: SessionInformationService) { + this['showSessionInformation'] = true; + } + + getUser() { + if (this.sessionInformation) { + return this.sessionInformation.user; + } else { + console.debug('SessionInformationComponent --> getUser() --> dr.who'); + return 'dr.who'; + } + } + + ngOnInit(): void { + console.debug('SessionInformationComponent --> ngOnInit() --> '); + this.sessionInformationService.getSessionInformation() + .then(sessionInformation => this.setSessonInformation(sessionInformation)); + } + + setSessonInformation(sessionInformation: SessionInformation) { + this.sessionInformation = sessionInformation; + console.debug('SessionInformationComponent --> setSessonInformation() --> ' + this.sessionInformation.user); + } +} diff --git a/gateway-admin-ui/admin-ui/app/sessionInformation/session.information.service.ts b/gateway-admin-ui/admin-ui/app/sessionInformation/session.information.service.ts new file mode 100644 index 000000000..95a3cdc8f --- /dev/null +++ b/gateway-admin-ui/admin-ui/app/sessionInformation/session.information.service.ts @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import {Injectable} from '@angular/core'; +import {HttpClient, HttpErrorResponse, HttpHeaders} from '@angular/common/http'; +import Swal from 'sweetalert2'; + +import 'rxjs/add/operator/toPromise'; +import {SessionInformation} from './session.information'; + +@Injectable() +export class SessionInformationService { + pathParts = window.location.pathname.split('/'); + topologyContext = '/' + this.pathParts[1] + '/' + this.pathParts[2] + '/'; + sessionUrl = this.topologyContext + 'session/api/v1/sessioninfo'; + + constructor(private http: HttpClient) {} + + getSessionInformation(): Promise<SessionInformation> { + let headers = new HttpHeaders(); + headers = this.addJsonHeaders(headers); + return this.http.get(this.sessionUrl, { headers: headers}) + .toPromise() + .then(response => response['sessioninfo'] as SessionInformation) + .catch((err: HttpErrorResponse) => { + console.debug('HomepageService --> getSessionInformation() --> ' + this.sessionUrl + '\n error: ' + err.message); + if (err.status === 401) { + window.location.assign(document.location.pathname); + } else { + return this.handleError(err); + } + }); + } + + addJsonHeaders(headers: HttpHeaders): HttpHeaders { + return this.addCsrfHeaders(headers.append('Accept', 'application/json').append('Content-Type', 'application/json')); + } + + addCsrfHeaders(headers: HttpHeaders): HttpHeaders { + return this.addXHRHeaders(headers.append('X-XSRF-Header', 'homepage')); + } + + addXHRHeaders(headers: HttpHeaders): HttpHeaders { + return headers.append('X-Requested-With', 'XMLHttpRequest'); + } + + private handleError(error: HttpErrorResponse): Promise<any> { + Swal.fire({ + icon: 'error', + title: 'Oops!', + text: 'Something went wrong!\n' + (error.error ? error.error : error.statusText), + confirmButtonColor: '#7cd1f9' + }); + return Promise.reject(error.message || error); + } +} diff --git a/knox-token-generation-ui/token-generation/app/app.module.ts b/gateway-admin-ui/admin-ui/app/sessionInformation/session.information.ts similarity index 57% copy from knox-token-generation-ui/token-generation/app/app.module.ts copy to gateway-admin-ui/admin-ui/app/sessionInformation/session.information.ts index 7a63bb5d8..549bb09dd 100644 --- a/knox-token-generation-ui/token-generation/app/app.module.ts +++ b/gateway-admin-ui/admin-ui/app/sessionInformation/session.information.ts @@ -14,21 +14,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { NgModule } from '@angular/core'; -import { BrowserModule } from '@angular/platform-browser'; -import { HttpClientModule } from '@angular/common/http'; -import { TokenGenerationComponent } from './token-generation.component'; -import { ReactiveFormsModule } from '@angular/forms'; -import { TokenGenService } from './token-generation.service'; -@NgModule({ - imports: [BrowserModule, - HttpClientModule, - ReactiveFormsModule - ], - declarations: [TokenGenerationComponent], - providers: [TokenGenService], - bootstrap: [TokenGenerationComponent] -}) -export class AppModule { +export class SessionInformation { + user: string; + logoutUrl: string; + logoutPageUrl: string; + globalLgoutPageUrl: string; } diff --git a/gateway-admin-ui/admin-ui/index.html b/gateway-admin-ui/admin-ui/index.html index 0a24f6816..ab2d882cf 100644 --- a/gateway-admin-ui/admin-ui/index.html +++ b/gateway-admin-ui/admin-ui/index.html @@ -42,6 +42,7 @@ style="max-width:200px; margin-top: -9px;" src="assets/knox-logo-transparent.gif" alt="Apache Knox Manager"> </a> </div> + <app-session-information></app-session-information> </div> </nav> diff --git a/knox-token-generation-ui/token-generation/app/app.module.ts b/knox-token-generation-ui/token-generation/app/app.module.ts index 7a63bb5d8..a5962de1d 100644 --- a/knox-token-generation-ui/token-generation/app/app.module.ts +++ b/knox-token-generation-ui/token-generation/app/app.module.ts @@ -20,15 +20,16 @@ import { HttpClientModule } from '@angular/common/http'; import { TokenGenerationComponent } from './token-generation.component'; import { ReactiveFormsModule } from '@angular/forms'; import { TokenGenService } from './token-generation.service'; +import { SessionInformationComponent } from './session.information.component'; @NgModule({ imports: [BrowserModule, HttpClientModule, ReactiveFormsModule ], - declarations: [TokenGenerationComponent], + declarations: [TokenGenerationComponent, SessionInformationComponent], providers: [TokenGenService], - bootstrap: [TokenGenerationComponent] + bootstrap: [TokenGenerationComponent, SessionInformationComponent] }) export class AppModule { } diff --git a/knox-token-generation-ui/token-generation/index.html b/knox-token-generation-ui/token-generation/app/session.information.component.html similarity index 57% copy from knox-token-generation-ui/token-generation/index.html copy to knox-token-generation-ui/token-generation/app/session.information.component.html index 6b8d4c565..c81cdcd1d 100644 --- a/knox-token-generation-ui/token-generation/index.html +++ b/knox-token-generation-ui/token-generation/app/session.information.component.html @@ -12,21 +12,5 @@ See the License for the specific language governing permissions and limitations under the License. --> -<!doctype html> -<html> -<head> - <meta charset="utf-8"> - <title>Apache Knox Token Generation</title> - <meta name="viewport" content="width=device-width, initial-scale=1"> - <link rel="icon" type="image/x-icon" href="favicon.ico"> - <meta name="viewport" content="width=device-width, initial-scale=1"> - <!-- Custom styles for this template --> - <link href="assets/sticky-footer.css" rel="stylesheet"> - <script> - window['base-href'] = window.location.pathname; - </script> -</head> -<body class="login"> - <app-token-generation></app-token-generation> -</body> -</html> +<!-- The text color is the same as the Cloudera logo's color --> +<div style="text-align: right; color: rgb(130, 180, 93);">Logged in as {{ getUser() }}</div> diff --git a/knox-token-generation-ui/token-generation/app/session.information.component.ts b/knox-token-generation-ui/token-generation/app/session.information.component.ts new file mode 100644 index 000000000..01d472804 --- /dev/null +++ b/knox-token-generation-ui/token-generation/app/session.information.component.ts @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import {Component, OnInit} from '@angular/core'; +import {TokenGenService} from './token-generation.service'; +import {SessionInformation} from './token-generation.models'; + +@Component({ + selector: 'app-session-information', + templateUrl: './session.information.component.html', + providers: [TokenGenService] +}) + +export class SessionInformationComponent implements OnInit { + + sessionInformation: SessionInformation; + logoutSupported = true; + + constructor(private tokenGenerationService: TokenGenService) { + this['showSessionInformation'] = true; + } + + getUser() { + if (this.sessionInformation) { + return this.sessionInformation.user; + } else { + console.debug('SessionInformationComponent --> getUser() --> dr.who'); + return 'dr.who'; + } + } + + ngOnInit(): void { + console.debug('SessionInformationComponent --> ngOnInit() --> '); + this.tokenGenerationService.getSessionInformation() + .then(sessionInformation => this.setSessionInformation(sessionInformation)); + } + + private setSessionInformation(sessionInformation: SessionInformation) { + this.sessionInformation = sessionInformation; + console.debug('SessionInformationComponent --> setSessionInformation() --> ' + this.sessionInformation.user); + } + +} diff --git a/knox-token-generation-ui/token-generation/app/token-generation.models.ts b/knox-token-generation-ui/token-generation/app/token-generation.models.ts index b0825207d..f419c800a 100644 --- a/knox-token-generation-ui/token-generation/app/token-generation.models.ts +++ b/knox-token-generation-ui/token-generation/app/token-generation.models.ts @@ -50,3 +50,12 @@ export interface TokenData { lifespanHours: number; lifespanMins: number; } + +export class SessionInformation { + user: string; + logoutUrl: string; + logoutPageUrl: string; + globalLgoutPageUrl: string; + canSeeAllTokens: boolean; + currentKnoxSsoCookieTokenId: string; +} diff --git a/knox-token-generation-ui/token-generation/app/token-generation.service.ts b/knox-token-generation-ui/token-generation/app/token-generation.service.ts index b20933594..19c651b18 100644 --- a/knox-token-generation-ui/token-generation/app/token-generation.service.ts +++ b/knox-token-generation-ui/token-generation/app/token-generation.service.ts @@ -17,13 +17,14 @@ import { Injectable } from '@angular/core'; import { HttpClient, HttpErrorResponse, HttpHeaders, HttpParams } from '@angular/common/http'; import Swal from 'sweetalert2'; -import { TokenData, TokenRequestParams, TokenResultData, TssStatusData } from './token-generation.models'; +import { TokenData, TokenRequestParams, TokenResultData, TssStatusData, SessionInformation } from './token-generation.models'; @Injectable() export class TokenGenService { readonly baseURL: string; readonly tokenURL: string; readonly tssStatusRequestURL: string; + readonly sessionUrl: string; constructor(private http: HttpClient) { const knoxtokenURL = 'knoxtoken/api/v2/token'; @@ -35,6 +36,7 @@ export class TokenGenService { this.baseURL = temporaryURL.substring(0, temporaryURL.lastIndexOf('/') + 1); this.tokenURL = topologyContext + knoxtokenURL; this.tssStatusRequestURL = topologyContext + tssStatusURL; + this.sessionUrl = topologyContext + 'session/api/v1/sessioninfo'; } getTokenStateServiceStatus(): Promise<TssStatusData> { @@ -70,6 +72,22 @@ export class TokenGenService { }); } + getSessionInformation(): Promise<SessionInformation> { + let headers = new HttpHeaders(); + headers = this.addHeaders(headers); + return this.http.get(this.sessionUrl, { headers: headers}) + .toPromise() + .then(response => response['sessioninfo'] as SessionInformation) + .catch((err: HttpErrorResponse) => { + console.debug('TokenManagementService --> getSessionInformation() --> ' + this.sessionUrl + '\n error: ' + err.message); + if (err.status === 401) { + window.location.assign(document.location.pathname); + } else { + return this.handleError(err); + } + }); + } + getGeneratedTokenData(params: TokenRequestParams): Promise<TokenResultData> { let headers = new HttpHeaders(); headers = this.addHeaders(headers); diff --git a/knox-token-generation-ui/token-generation/index.html b/knox-token-generation-ui/token-generation/index.html index 6b8d4c565..c26d66dea 100644 --- a/knox-token-generation-ui/token-generation/index.html +++ b/knox-token-generation-ui/token-generation/index.html @@ -27,6 +27,12 @@ </script> </head> <body class="login"> + <div style="background: #222; padding-right: 15px; padding-left: 15px; margin-right: 1.75%; margin-left: 1.75%; position: relative; min-height: 110px; font-size:14px;"> + <app-session-information></app-session-information> + <a class="navbar-brand" href="#"> <img style="max-width:200px;" src="assets/knox-logo-transparent.gif" alt="Apache Knox Home"></a> + </div> + <app-token-generation></app-token-generation> +</div> </body> </html> diff --git a/knox-token-management-ui/token-management/app/app.module.ts b/knox-token-management-ui/token-management/app/app.module.ts index f49275980..7ec5e55fe 100644 --- a/knox-token-management-ui/token-management/app/app.module.ts +++ b/knox-token-management-ui/token-management/app/app.module.ts @@ -32,6 +32,7 @@ import {FormsModule, ReactiveFormsModule} from '@angular/forms'; import {TokenManagementComponent} from './token.management.component'; import {TokenManagementService} from './token.management.service'; +import {SessionInformationComponent} from './session.information.component'; @NgModule({ imports: [BrowserModule, @@ -52,9 +53,9 @@ import {TokenManagementService} from './token.management.service'; MatSlideToggleModule, MatCheckboxModule ], - declarations: [TokenManagementComponent], + declarations: [TokenManagementComponent, SessionInformationComponent], providers: [TokenManagementService], - bootstrap: [TokenManagementComponent] + bootstrap: [TokenManagementComponent, SessionInformationComponent] }) export class AppModule { } diff --git a/knox-token-generation-ui/token-generation/index.html b/knox-token-management-ui/token-management/app/session.information.component.html similarity index 57% copy from knox-token-generation-ui/token-generation/index.html copy to knox-token-management-ui/token-management/app/session.information.component.html index 6b8d4c565..c81cdcd1d 100644 --- a/knox-token-generation-ui/token-generation/index.html +++ b/knox-token-management-ui/token-management/app/session.information.component.html @@ -12,21 +12,5 @@ See the License for the specific language governing permissions and limitations under the License. --> -<!doctype html> -<html> -<head> - <meta charset="utf-8"> - <title>Apache Knox Token Generation</title> - <meta name="viewport" content="width=device-width, initial-scale=1"> - <link rel="icon" type="image/x-icon" href="favicon.ico"> - <meta name="viewport" content="width=device-width, initial-scale=1"> - <!-- Custom styles for this template --> - <link href="assets/sticky-footer.css" rel="stylesheet"> - <script> - window['base-href'] = window.location.pathname; - </script> -</head> -<body class="login"> - <app-token-generation></app-token-generation> -</body> -</html> +<!-- The text color is the same as the Cloudera logo's color --> +<div style="text-align: right; color: rgb(130, 180, 93);">Logged in as {{ getUser() }}</div> diff --git a/knox-token-management-ui/token-management/app/session.information.component.ts b/knox-token-management-ui/token-management/app/session.information.component.ts new file mode 100644 index 000000000..9c2038daa --- /dev/null +++ b/knox-token-management-ui/token-management/app/session.information.component.ts @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import {Component, OnInit} from '@angular/core'; +import {TokenManagementService} from './token.management.service'; +import {SessionInformation} from './session.information'; + +@Component({ + selector: 'app-session-information', + templateUrl: './session.information.component.html', + providers: [TokenManagementService] +}) + +export class SessionInformationComponent implements OnInit { + + sessionInformation: SessionInformation; + logoutSupported = true; + + constructor(private tokenManagementService: TokenManagementService) { + this['showSessionInformation'] = true; + } + + getUser() { + if (this.sessionInformation) { + return this.sessionInformation.user; + } else { + console.debug('SessionInformationComponent --> getUser() --> dr.who'); + return 'dr.who'; + } + } + + ngOnInit(): void { + console.debug('SessionInformationComponent --> ngOnInit() --> '); + this.tokenManagementService.getSessionInformation() + .then(sessionInformation => this.setSessionInformation(sessionInformation)); + } + + private setSessionInformation(sessionInformation: SessionInformation) { + this.sessionInformation = sessionInformation; + console.debug('SessionInformationComponent --> setSessionInformation() --> ' + this.sessionInformation.user); + } + +}