-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: entry for embedded dashboard (#17529)
* create entry for embedded dashboard in webpack * add cookies * lint * token message handshake * guestTokenHeaderName * use setupClient instead of calling configure * rename the webpack chunk * simplified handshake * embedded entrypoint: render a proper app * make the embedded page accept anonymous connections * format * lint * fix test # Conflicts: # superset-frontend/src/embedded/index.tsx # superset/views/core.py * lint * Update superset-frontend/src/embedded/index.tsx Co-authored-by: David Aaron Suddjian <[email protected]> * comment out origins checks * move embedded for core to dashboard * pylint * isort Co-authored-by: David Aaron Suddjian <[email protected]> Co-authored-by: David Aaron Suddjian <[email protected]>
- Loading branch information
1 parent
4c74e53
commit fe63435
Showing
13 changed files
with
252 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/** | ||
* 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 React, { lazy, Suspense } from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { BrowserRouter as Router, Route } from 'react-router-dom'; | ||
import { bootstrapData } from 'src/preamble'; | ||
import setupClient from 'src/setup/setupClient'; | ||
import { RootContextProviders } from 'src/views/RootContextProviders'; | ||
import ErrorBoundary from 'src/components/ErrorBoundary'; | ||
import Loading from 'src/components/Loading'; | ||
|
||
const LazyDashboardPage = lazy( | ||
() => | ||
import( | ||
/* webpackChunkName: "DashboardPage" */ 'src/dashboard/containers/DashboardPage' | ||
), | ||
); | ||
|
||
const EmbeddedApp = () => ( | ||
<Router> | ||
<Route path="/superset/dashboard/:idOrSlug/embedded"> | ||
<Suspense fallback={<Loading />}> | ||
<RootContextProviders> | ||
<ErrorBoundary> | ||
<LazyDashboardPage /> | ||
</ErrorBoundary> | ||
</RootContextProviders> | ||
</Suspense> | ||
</Route> | ||
</Router> | ||
); | ||
|
||
const appMountPoint = document.getElementById('app')!; | ||
|
||
const MESSAGE_TYPE = '__embedded_comms__'; | ||
|
||
if (!window.parent) { | ||
appMountPoint.innerHTML = | ||
'This page is intended to be embedded in an iframe, but no window.parent was found.'; | ||
} | ||
|
||
// if the page is embedded in an origin that hasn't | ||
// been authorized by the curator, we forbid access entirely. | ||
// todo: check the referrer on the route serving this page instead | ||
// const ALLOW_ORIGINS = ['http://127.0.0.1:9001', 'http://localhost:9001']; | ||
// const parentOrigin = new URL(document.referrer).origin; | ||
// if (!ALLOW_ORIGINS.includes(parentOrigin)) { | ||
// throw new Error( | ||
// `[superset] iframe parent ${parentOrigin} is not in the list of allowed origins`, | ||
// ); | ||
// } | ||
|
||
async function start(guestToken: string) { | ||
// the preamble configures a client, but we need to configure a new one | ||
// now that we have the guest token | ||
setupClient({ | ||
guestToken, | ||
guestTokenHeaderName: bootstrapData.config?.GUEST_TOKEN_HEADER_NAME, | ||
}); | ||
ReactDOM.render(<EmbeddedApp />, appMountPoint); | ||
} | ||
|
||
function validateMessageEvent(event: MessageEvent) { | ||
if ( | ||
event.data?.type === 'webpackClose' || | ||
event.data?.source === '@devtools-page' | ||
) { | ||
// sometimes devtools use the messaging api and we want to ignore those | ||
throw new Error("Sir, this is a Wendy's"); | ||
} | ||
|
||
// if (!ALLOW_ORIGINS.includes(event.origin)) { | ||
// throw new Error('Message origin is not in the allowed list'); | ||
// } | ||
|
||
if (typeof event.data !== 'object' || event.data.type !== MESSAGE_TYPE) { | ||
throw new Error(`Message type does not match type used for embedded comms`); | ||
} | ||
} | ||
|
||
window.addEventListener('message', function (event) { | ||
try { | ||
validateMessageEvent(event); | ||
} catch (err) { | ||
console.info('[superset] ignoring message', err, event); | ||
return; | ||
} | ||
|
||
console.info('[superset] received message', event); | ||
const hostAppPort = event.ports?.[0]; | ||
if (hostAppPort) { | ||
hostAppPort.onmessage = function receiveMessage(event) { | ||
console.info('[superset] received message event', event.data); | ||
if (event.data.guestToken) { | ||
start(event.data.guestToken); | ||
} | ||
}; | ||
} | ||
}); | ||
|
||
console.info('[superset] embed page is ready to receive messages'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 React from 'react'; | ||
import { Route } from 'react-router-dom'; | ||
import { ThemeProvider } from '@superset-ui/core'; | ||
import { Provider as ReduxProvider } from 'react-redux'; | ||
import { QueryParamProvider } from 'use-query-params'; | ||
import { DndProvider } from 'react-dnd'; | ||
import { HTML5Backend } from 'react-dnd-html5-backend'; | ||
|
||
import { store } from './store'; | ||
import FlashProvider from '../components/FlashProvider'; | ||
import { bootstrapData, theme } from '../preamble'; | ||
import { EmbeddedUiConfigProvider } from '../components/UiConfigContext'; | ||
import { DynamicPluginProvider } from '../components/DynamicPlugins'; | ||
|
||
const common = { ...bootstrapData.common }; | ||
|
||
export const RootContextProviders: React.FC = ({ children }) => ( | ||
<ThemeProvider theme={theme}> | ||
<ReduxProvider store={store}> | ||
<DndProvider backend={HTML5Backend}> | ||
<FlashProvider messages={common.flash_messages}> | ||
<EmbeddedUiConfigProvider> | ||
<DynamicPluginProvider> | ||
<QueryParamProvider | ||
ReactRouterRoute={Route} | ||
stringifyOptions={{ encode: false }} | ||
> | ||
{children} | ||
</QueryParamProvider> | ||
</DynamicPluginProvider> | ||
</EmbeddedUiConfigProvider> | ||
</FlashProvider> | ||
</DndProvider> | ||
</ReduxProvider> | ||
</ThemeProvider> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.