Skip to content

Commit

Permalink
KNOX-3008 - Displaying hostname and custom banner text on the Knox Ho…
Browse files Browse the repository at this point in the history
…me page and other UIs (#842)
  • Loading branch information
smolnar82 authored Feb 27, 2024
1 parent 74ffc2a commit c41230b
Show file tree
Hide file tree
Showing 31 changed files with 182 additions and 13 deletions.
4 changes: 3 additions & 1 deletion gateway-admin-ui/admin-ui/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {ProviderConfigSelectorComponent} from './provider-config-selector/provid
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';
import {SafeHtmlPipe} from './sessionInformation/session.information.component';

@NgModule({
imports: [BrowserModule,
Expand Down Expand Up @@ -76,7 +77,8 @@ import {SessionInformationComponent} from './sessionInformation/session.informat
ProviderConfigSelectorComponent,
NewDescWizardComponent,
ProviderConfigWizardComponent,
SessionInformationComponent
SessionInformationComponent,
SafeHtmlPipe
],
providers: [TopologyService,
ServiceDefinitionService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@
limitations under the License.
-->
<div style="text-align: right; color: rgb(130, 180, 93);">Logged in as {{ getUser() }}</div>
<div style="margin-left: 15%; text-align: center; color: rgb(130,180, 93); max-width: 70%; word-wrap: break-word;" [innerHTML]="getBannerText() | safeHtml"></div>

Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,20 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {Component, OnInit} from '@angular/core';
import {Component, OnInit, Pipe, PipeTransform} from '@angular/core';
import {DomSanitizer} from '@angular/platform-browser';
import {SessionInformationService} from './session.information.service';
import {SessionInformation} from './session.information';

@Pipe({ name: 'safeHtml' })
export class SafeHtmlPipe implements PipeTransform {
constructor(private sanitizer: DomSanitizer) {}

transform(value) {
return this.sanitizer.bypassSecurityTrustHtml(value);
}
}

@Component({
selector: 'app-session-information',
templateUrl: './session.information.component.html',
Expand All @@ -42,6 +52,14 @@ export class SessionInformationComponent implements OnInit {
}
}

getBannerText() {
if (this.sessionInformation) {
console.debug('SessionInformationComponent --> getBannerHtml() --> ' + this.sessionInformation.bannerText);
return this.sessionInformation.bannerText;
}
return '';
}

ngOnInit(): void {
console.debug('SessionInformationComponent --> ngOnInit() --> ');
this.sessionInformationService.getSessionInformation()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ export class SessionInformation {
logoutUrl: string;
logoutPageUrl: string;
globalLgoutPageUrl: string;
bannerText: string;
}
3 changes: 3 additions & 0 deletions gateway-release/home/conf/topologies/manager.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@
<service>
<role>KNOX</role>
</service>
<service>
<role>KNOX-SESSION</role>
</service>
<application>
<name>admin-ui</name>
</application>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@ public class GatewayConfigImpl extends Configuration implements GatewayConfig {
private static final String GLOBAL_LOGOUT_PAGE_URL = "knox.global.logout.page.url";
private static final String KNOX_INCOMING_XFORWARDED_ENABLED = "gateway.incoming.xforwarded.enabled";

private static final String UI_BANNER_TEXT = GATEWAY_CONFIG_FILE_PREFIX + ".ui.banner.text";

//Gateway Database related properties
public static final String GATEWAY_DATABASE_TYPE = GATEWAY_CONFIG_FILE_PREFIX + ".database.type";
public static final String GATEWAY_DATABASE_CONN_URL = GATEWAY_CONFIG_FILE_PREFIX + ".database.connection.url";
Expand Down Expand Up @@ -1571,4 +1573,9 @@ public String getHttpClientCookieSpec() {
return get(HTTP_CLIENT_COOKIE_SPEC);
}

@Override
public String getBannerText() {
return get(UI_BANNER_TEXT, "");
}

}
4 changes: 4 additions & 0 deletions gateway-service-metadata/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,9 @@
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.kstruct</groupId>
<artifactId>gethostname4j</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public class GeneralProxyInformation {
@ApiModelProperty(value = "The version of this Knox Gateway")
private String version;

@XmlElement
@ApiModelProperty(value = "The name of the host where this Knox Gateway is running")
private String hostname;

@XmlElement
@ApiModelProperty(value = "The Admin UI URL")
private String adminUiUrl;
Expand Down Expand Up @@ -59,6 +63,14 @@ public void setVersion(String version) {
this.version = version;
}

public String getHostname() {
return hostname;
}

public void setHostname(String hostname) {
this.hostname = hostname;
}

public String getAdminUiUrl() {
return adminUiUrl;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
import org.apache.knox.gateway.util.JsonUtils;
import org.apache.knox.gateway.util.X509CertificateUtil;

import com.kstruct.gethostname4j.Hostname;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

Expand Down Expand Up @@ -101,6 +103,7 @@ public GeneralProxyInformation getGeneralProxyInformation() {
final ServerInfoService serviceInfoService = gatewayServices.getService(ServiceType.SERVER_INFO_SERVICE);
final String versionInfo = serviceInfoService.getBuildVersion() + " (hash=" + serviceInfoService.getBuildHash() + ")";
proxyInfo.setVersion(versionInfo);
proxyInfo.setHostname(Hostname.getHostname());
proxyInfo.setAdminApiBookUrl(
String.format(Locale.ROOT, "https://knox.apache.org/books/knox-%s/user-guide.html#Admin+API", getAdminApiBookVersion(serviceInfoService.getBuildVersion())));
final GatewayConfig config = (GatewayConfig) request.getServletContext().getAttribute(GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public class SessionInformation {
@XmlElement
private String currentKnoxSsoCookieTokenId;

@XmlElement
private String bannerText;

public String getUser() {
return user;
}
Expand Down Expand Up @@ -88,4 +91,12 @@ public void setCurrentKnoxSsoCookieTokenId(String currentKnoxSsoCookieTokenId) {
this.currentKnoxSsoCookieTokenId = currentKnoxSsoCookieTokenId;
}

public String getBannerText() {
return bannerText;
}

public void setBannerText(String bannerText) {
this.bannerText = bannerText;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public SessionInformation getSessionInformation(@QueryParam("logoutPageProfile")
}
sessionInfo.setCanSeeAllTokens(config != null ? config.canSeeAllTokens(user) : false);
sessionInfo.setCurrentKnoxSsoCookieTokenId((String) this.request.getAttribute(TokenUtils.ATTR_CURRENT_KNOXSSO_COOKIE_TOKEN_ID));
sessionInfo.setBannerText(config != null ? config.getBannerText() : "");

return sessionInfo;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1111,4 +1111,9 @@ public String getHttpClientCookieSpec() {
return null;
}

@Override
public String getBannerText() {
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -938,4 +938,10 @@ public interface GatewayConfig {
* @return CookieSpec for the HTTP client used by the dispatch, see org.apache.http.client.config.CookieSpecs
*/
String getHttpClientCookieSpec();

/**
* @return a text that should be displayed on all Knox UIs within the banner on the top.
*/
String getBannerText();

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
public class HomePageProfile {
static final String GPI_PREFIX = "gpi_";
static final String GPI_VERSION = GPI_PREFIX + "version";
static final String GPI_HOSTNAME = GPI_PREFIX + "hostname";
static final String GPI_CERT = GPI_PREFIX + "cert";
static final String GPI_ADMIN_UI = GPI_PREFIX + "admin_ui";
static final String GPI_ADMIN_API = GPI_PREFIX + "admin_api";
Expand All @@ -41,6 +42,7 @@ public class HomePageProfile {

public HomePageProfile(Collection<String> profileConfiguration) {
addElement(GPI_VERSION, profileConfiguration);
addElement(GPI_HOSTNAME, profileConfiguration);
addElement(GPI_CERT, profileConfiguration);
addElement(GPI_ADMIN_UI, profileConfiguration);
addElement(GPI_ADMIN_API, profileConfiguration);
Expand Down Expand Up @@ -73,14 +75,14 @@ public Map<String, String> getProfileElements() {
}

public static Collection<String> getFullProfileElements() {
return Arrays.asList(GPI_VERSION, GPI_CERT, GPI_ADMIN_UI, GPI_ADMIN_API, GPI_METADATA_API, GPI_TOKENS, GPI_WEBSHELL);
return Arrays.asList(GPI_VERSION, GPI_HOSTNAME, GPI_CERT, GPI_ADMIN_UI, GPI_ADMIN_API, GPI_METADATA_API, GPI_TOKENS, GPI_WEBSHELL);
}

public static Collection<String> getThinProfileElemens() {
return Arrays.asList(GPI_VERSION, GPI_CERT);
return Arrays.asList(GPI_VERSION, GPI_HOSTNAME, GPI_CERT);
}

public static Collection<String> getTokenProfileElements() {
return Arrays.asList(GPI_VERSION, GPI_CERT, GPI_TOKENS);
return Arrays.asList(GPI_VERSION, GPI_HOSTNAME, GPI_CERT, GPI_TOKENS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class HomePageProfileTest {
@Test
public void testEmptyConfiguration() throws Exception {
final HomePageProfile profile = new HomePageProfile(Collections.emptySet());
assertEquals(8, profile.getProfileElements().size());
assertEquals(9, profile.getProfileElements().size());
profile.getProfileElements().forEach((key, value) -> {
if (key.startsWith(HomePageProfile.GPI_PREFIX)) {
assertFalse(Boolean.parseBoolean(value));
Expand Down Expand Up @@ -75,7 +75,7 @@ public void testGeneralProxyInformationTokens() throws Exception {
@Test
public void testTokenProfileElements() throws Exception {
final Collection<String> tokenProfileElements = HomePageProfile.getTokenProfileElements();
assertEquals(3, tokenProfileElements.size());
assertEquals(4, tokenProfileElements.size());
assertTrue(tokenProfileElements.containsAll(Arrays.asList(HomePageProfile.GPI_VERSION, HomePageProfile.GPI_CERT, HomePageProfile.GPI_TOKENS)));
}

Expand Down
4 changes: 3 additions & 1 deletion knox-homepage-ui/home/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {APP_BASE_HREF} from '@angular/common';
import {GeneralProxyInformationComponent} from './generalProxyInformation/general.proxy.information.component';
import {TopologyInformationsComponent} from './topologies/topology.information.component';
import {SessionInformationComponent} from './sessionInformation/session.information.component';
import {SafeHtmlPipe} from './sessionInformation/session.information.component';
import {HomepageService} from './homepage.service';

@NgModule({
Expand All @@ -39,7 +40,8 @@ import {HomepageService} from './homepage.service';
],
declarations: [GeneralProxyInformationComponent,
TopologyInformationsComponent,
SessionInformationComponent
SessionInformationComponent,
SafeHtmlPipe
],
providers: [HomepageService,
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ <h4 (click)="toggleBoolean('showGeneralProxyInformation')"><span [class]="'click
<td>Knox Version</td>
<td>{{ getVersion() }}</td>
</tr>
<tr *ngIf="this['showKnoxHostname']">
<td>Hostname</td>
<td>{{ getHostname() }}</td>
</tr>
<tr *ngIf="this['showPublicCerts']">
<td>TLS Public Certificate</td>
<td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class GeneralProxyInformationComponent implements OnInit {
constructor(private homepageService: HomepageService, private route: ActivatedRoute) {
this['showGeneralProxyInformation'] = false;
this['showKnoxVersion'] = true;
this['showKnoxHostname'] = true;
this['showPublicCerts'] = true;
this['showAdminUI'] = true;
this['showAdminAPI'] = true;
Expand All @@ -48,6 +49,13 @@ export class GeneralProxyInformationComponent implements OnInit {
return '';
}

getHostname() {
if (this.generalProxyInformation) {
return this.generalProxyInformation.hostname;
}
return '';
}

getAdminUiUrl() {
if (this.generalProxyInformation) {
return this.generalProxyInformation.adminUiUrl;
Expand Down Expand Up @@ -118,6 +126,7 @@ export class GeneralProxyInformationComponent implements OnInit {
setProfileFlags(profile: JSON) {
console.debug('Setting GPI profile flags...');
this['showKnoxVersion'] = (profile['gpi_version'] === 'true');
this['showKnoxHostname'] = (profile['gpi_hostname'] === 'true');
this['showPublicCerts'] = (profile['gpi_cert'] === 'true');
this['showAdminUI'] = (profile['gpi_admin_ui'] === 'true');
this['showAdminAPI'] = (profile['gpi_admin_api'] === 'true');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

export class GeneralProxyInformation {
version: string;
hostname: string;
adminUiUrl: string;
webShellUrl: string;
adminApiBookUrl: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
-->
<div style="text-align: right; color: rgb(130, 180, 93);">Welcome {{ getUser() }}</div>
<div *ngIf="logoutSupported" style="text-align: right;"><a class="btn btn-primary" (click)="logout()">logout</a></div>
<div style="margin-left: 15%; text-align: center; color: rgb(130,180, 93); max-width: 70%; word-wrap: break-word;" [innerHTML]="getBannerText() | safeHtml"></div>
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,20 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {Component, OnInit} from '@angular/core';
import {Component, OnInit, Pipe, PipeTransform} from '@angular/core';
import {DomSanitizer} from '@angular/platform-browser';
import {HomepageService} from '../homepage.service';
import {SessionInformation} from './session.information';

@Pipe({ name: 'safeHtml' })
export class SafeHtmlPipe implements PipeTransform {
constructor(private sanitizer: DomSanitizer) {}

transform(value) {
return this.sanitizer.bypassSecurityTrustHtml(value);
}
}

@Component({
selector: 'app-session-information',
templateUrl: './session.information.component.html',
Expand Down Expand Up @@ -59,6 +69,14 @@ export class SessionInformationComponent implements OnInit {
return null;
}

getBannerText() {
if (this.sessionInformation) {
console.debug('SessionInformationComponent --> getBannerHtml() --> ' + this.sessionInformation.bannerText);
return this.sessionInformation.bannerText;
}
return '';
}

logout() {
console.debug('SessionInformationComponent --> attempting logout() --> ');
if (this.sessionInformation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ export class SessionInformation {
logoutUrl: string;
logoutPageUrl: string;
globalLgoutPageUrl: string;
bannerText: string;
}
3 changes: 2 additions & 1 deletion knox-token-generation-ui/token-generation/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ import { TokenGenerationComponent } from './token-generation.component';
import { ReactiveFormsModule } from '@angular/forms';
import { TokenGenService } from './token-generation.service';
import { SessionInformationComponent } from './session.information.component';
import { SafeHtmlPipe } from './session.information.component';

@NgModule({
imports: [BrowserModule,
HttpClientModule,
ReactiveFormsModule
],
declarations: [TokenGenerationComponent, SessionInformationComponent],
declarations: [TokenGenerationComponent, SessionInformationComponent, SafeHtmlPipe],
providers: [TokenGenService],
bootstrap: [TokenGenerationComponent, SessionInformationComponent]
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
-->
<!-- 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>
<div style="margin-left: 15%; text-align: center; color: rgb(130,180, 93); max-width: 70%; word-wrap: break-word;" [innerHTML]="getBannerText() | safeHtml"></div>
Loading

0 comments on commit c41230b

Please sign in to comment.