Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v12] feat: adds motd to the ui #27923

Merged
merged 6 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/client/webclient/webconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,6 @@ type WebConfigAuthSettings struct {
LocalConnectorName string `json:"localConnectorName,omitempty"`
// PrivateKeyPolicy is the configured private key policy for the cluster.
PrivateKeyPolicy keys.PrivateKeyPolicy `json:"privateKeyPolicy,omitempty"`
// MOTD is message of the day. MOTD is displayed to users before login.
MOTD string `json:"motd"`
}
1 change: 1 addition & 0 deletions lib/web/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,7 @@ func (h *Handler) getWebConfig(w http.ResponseWriter, r *http.Request, p httprou
PreferredLocalMFA: cap.GetPreferredLocalMFA(),
LocalConnectorName: localConnectorName,
PrivateKeyPolicy: cap.GetPrivateKeyPolicy(),
MOTD: cap.GetMessageOfTheDay(),
}
}

Expand Down
3 changes: 3 additions & 0 deletions lib/web/apiserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4067,13 +4067,15 @@ func TestGetWebConfig(t *testing.T) {
env := newWebPack(t, 1)

// Set auth preference with passwordless.
const MOTD = "Welcome to cluster, your activity will be recorded."
ap, err := types.NewAuthPreference(types.AuthPreferenceSpecV2{
Type: constants.Local,
SecondFactor: constants.SecondFactorOptional,
ConnectorName: constants.PasswordlessConnector,
Webauthn: &types.Webauthn{
RPID: "localhost",
},
MessageOfTheDay: MOTD,
})
require.NoError(t, err)
err = env.server.Auth().SetAuthPreference(ctx, ap)
Expand Down Expand Up @@ -4107,6 +4109,7 @@ func TestGetWebConfig(t *testing.T) {
PreferredLocalMFA: constants.SecondFactorWebauthn,
LocalConnectorName: constants.PasswordlessConnector,
PrivateKeyPolicy: keys.PrivateKeyPolicyNone,
MOTD: MOTD,
},
CanJoinSessions: true,
ProxyClusterName: env.server.ClusterName(),
Expand Down
3 changes: 3 additions & 0 deletions web/packages/teleport/src/Login/Login.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,7 @@ const sample: State = {
isPasswordlessEnabled: false,
primaryAuthType: 'local',
privateKeyPolicyEnabled: false,
motd: '',
showMotd: false,
acknowledgeMotd: () => null,
};
39 changes: 39 additions & 0 deletions web/packages/teleport/src/Login/Login.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,42 @@ test('login with private key policy enabled through role setting', async () => {
expect(screen.queryByPlaceholderText(/username/i)).not.toBeInTheDocument();
expect(screen.getByText(/login disabled/i)).toBeInTheDocument();
});

test('show motd only if motd is set', async () => {
// default login form
const { unmount } = render(<Login />);
expect(screen.getByPlaceholderText(/username/i)).toBeInTheDocument();
expect(
screen.queryByText('Welcome to cluster, your activity will be recorded.')
).not.toBeInTheDocument();
unmount();

// now set motd
jest
.spyOn(cfg, 'getMotd')
.mockImplementation(
() => 'Welcome to cluster, your activity will be recorded.'
);

render(<Login />);

expect(
screen.getByText('Welcome to cluster, your activity will be recorded.')
).toBeInTheDocument();
expect(screen.queryByPlaceholderText(/username/i)).not.toBeInTheDocument();
});

test('show login form after modt acknowledge', async () => {
jest
.spyOn(cfg, 'getMotd')
.mockImplementation(
() => 'Welcome to cluster, your activity will be recorded.'
);
render(<Login />);
expect(
screen.getByText('Welcome to cluster, your activity will be recorded.')
).toBeInTheDocument();

fireEvent.click(screen.getByText('Acknowledge'));
expect(screen.getByPlaceholderText(/username/i)).toBeInTheDocument();
});
38 changes: 23 additions & 15 deletions web/packages/teleport/src/Login/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import FormLogin from 'teleport/components/FormLogin';
import Logo from 'teleport/components/LogoHero';

import useLogin, { State } from './useLogin';
import Motd from './Motd';

const logoSrc = require('design/assets/images/teleport-medallion.svg');

Expand All @@ -41,25 +42,32 @@ export function Login({
isPasswordlessEnabled,
primaryAuthType,
privateKeyPolicyEnabled,
motd,
showMotd,
acknowledgeMotd,
}: State) {
return (
<>
<Logo src={logoSrc} />
<FormLogin
title={'Sign in to Teleport'}
authProviders={authProviders}
auth2faType={auth2faType}
preferredMfaType={preferredMfaType}
isLocalAuthEnabled={isLocalAuthEnabled}
onLoginWithSso={onLoginWithSso}
onLoginWithWebauthn={onLoginWithWebauthn}
onLogin={onLogin}
attempt={attempt}
clearAttempt={clearAttempt}
isPasswordlessEnabled={isPasswordlessEnabled}
primaryAuthType={primaryAuthType}
privateKeyPolicyEnabled={privateKeyPolicyEnabled}
/>
{showMotd ? (
<Motd message={motd} onClick={acknowledgeMotd} />
) : (
<FormLogin
title={'Sign in to Teleport'}
authProviders={authProviders}
auth2faType={auth2faType}
preferredMfaType={preferredMfaType}
isLocalAuthEnabled={isLocalAuthEnabled}
onLoginWithSso={onLoginWithSso}
onLoginWithWebauthn={onLoginWithWebauthn}
onLogin={onLogin}
attempt={attempt}
clearAttempt={clearAttempt}
isPasswordlessEnabled={isPasswordlessEnabled}
primaryAuthType={primaryAuthType}
privateKeyPolicyEnabled={privateKeyPolicyEnabled}
/>
)}
</>
);
}
38 changes: 38 additions & 0 deletions web/packages/teleport/src/Login/Motd/Motd.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright 2023 Gravitational, Inc.

Licensed 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 { Card, Box, Text, ButtonPrimary } from 'design';

export function Motd({ message, onClick }: Props) {
return (
<Card bg="levels.surface" my={6} mx="auto" width="464px">
<Box p={6}>
<Text typography="h5" mb={3} textAlign="center">
{message}
</Text>
<ButtonPrimary width="100%" mt={3} size="large" onClick={onClick}>
Acknowledge
</ButtonPrimary>
</Box>
</Card>
);
}

type Props = {
message: string;
onClick(): void;
};
17 changes: 17 additions & 0 deletions web/packages/teleport/src/Login/Motd/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
Copyright 2023 Gravitational, Inc.

Licensed 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.
*/

export { Motd as default } from './Motd';
9 changes: 9 additions & 0 deletions web/packages/teleport/src/Login/useLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ export default function useLogin() {
const authProviders = cfg.getAuthProviders();
const auth2faType = cfg.getAuth2faType();
const isLocalAuthEnabled = cfg.getLocalAuthFlag();
const motd = cfg.getMotd();
const [showMotd, setShowMotd] = useState(!!motd);

function acknowledgeMotd() {
setShowMotd(false);
}

function onLogin(email, password, token) {
attemptActions.start();
Expand Down Expand Up @@ -87,6 +93,9 @@ export default function useLogin() {
isPasswordlessEnabled: cfg.isPasswordlessEnabled(),
primaryAuthType: cfg.getPrimaryAuthType(),
privateKeyPolicyEnabled,
motd,
showMotd,
acknowledgeMotd,
};
}

Expand Down
6 changes: 6 additions & 0 deletions web/packages/teleport/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ const cfg = {
authType: 'local' as AuthType,
preferredLocalMfa: '' as PreferredMfaType,
privateKeyPolicy: 'none' as PrivateKeyPolicy,
// motd is message of the day, displayed to users before login.
motd: '',
},

proxyCluster: 'localhost',
Expand Down Expand Up @@ -275,6 +277,10 @@ const cfg = {
return cfg.auth ? cfg.auth.preferredLocalMfa : null;
},

getMotd() {
return cfg.auth.motd;
},

getLocalAuthFlag() {
return cfg.auth.localAuthEnabled;
},
Expand Down