forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'v3' into PMM-13608-dismiss-modal
- Loading branch information
Showing
23 changed files
with
332 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package nameutil | ||
|
||
import ( | ||
"crypto/md5" | ||
"fmt" | ||
) | ||
|
||
// SanitizeSAName is used for sanitize name and it's length for service accounts. | ||
// Max length of service account name is 190 chars (limit in Grafana Postgres DB). | ||
// However, prefix added by grafana is counted too. Prefix is sa-{orgID}-. | ||
// Bare minimum is 5 chars reserved (orgID is <10, like sa-1-) and could be more depends | ||
// on orgID number. Let's reserve 10 chars. It will cover almost one million orgIDs. | ||
// Sanitizing, ensure its length by hashing postfix when length is exceeded. | ||
// MD5 is used because it has fixed length 32 chars. | ||
// | ||
// Be aware that the same method is implemented in the PMM repo, and all changes should be reflected there as well! | ||
func SanitizeSAName(name string) string { | ||
if len(name) <= 180 { | ||
return name | ||
} | ||
|
||
return fmt.Sprintf("%s%x", name[:148], md5.Sum([]byte(name[148:]))) //nolint:gosec | ||
} |
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,32 @@ | ||
package nameutil | ||
|
||
import ( | ||
"crypto/rand" | ||
"encoding/base64" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSanitizeSAName(t *testing.T) { | ||
// max possible length without hashing | ||
len180, err := generateRandomString(180) | ||
require.NoError(t, err) | ||
require.Equal(t, len180, SanitizeSAName(len180)) | ||
|
||
// too long length - postfix hashed | ||
len200, err := generateRandomString(200) | ||
require.NoError(t, err) | ||
len200sanitized := SanitizeSAName(len200) | ||
require.Equal(t, fmt.Sprintf("%s%s", len200[:148], len200sanitized[148:]), len200sanitized) | ||
} | ||
|
||
func generateRandomString(length int) (string, error) { | ||
buffer := make([]byte, length) | ||
_, err := rand.Read(buffer) | ||
if err != nil { | ||
return "", err | ||
} | ||
return base64.URLEncoding.EncodeToString(buffer)[:length], nil | ||
} |
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
19 changes: 19 additions & 0 deletions
19
public/app/percona/shared/components/PerconaBootstrapper/PerconaMigrator/PerconaMigrator.tsx
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,19 @@ | ||
import React, { FC } from 'react'; | ||
|
||
import { useMigrator } from 'app/percona/shared/core/hooks/migrator'; | ||
import { useSelector } from 'app/types'; | ||
|
||
import MigrationSummary from './components/MigrationSummary'; | ||
|
||
const PerconaMigrator: FC = () => { | ||
const migrationResult = useSelector((state) => state.apiKeys.migrationResult); | ||
const { migrationSummaryVisible, dismissSummary } = useMigrator(); | ||
|
||
if (!migrationResult || !migrationSummaryVisible) { | ||
return; | ||
} | ||
|
||
return <MigrationSummary data={migrationResult} visible={migrationSummaryVisible} onDismiss={dismissSummary} />; | ||
}; | ||
|
||
export default PerconaMigrator; |
5 changes: 5 additions & 0 deletions
5
.../components/PerconaBootstrapper/PerconaMigrator/components/FailedMigrationRow.messages.ts
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,5 @@ | ||
export const Messages = { | ||
id: (id: number) => `API Key ID ${id}`, | ||
name: 'Name: ', | ||
error: 'Error: ', | ||
}; |
35 changes: 35 additions & 0 deletions
35
...a/shared/components/PerconaBootstrapper/PerconaMigrator/components/FailedMigrationRow.tsx
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,35 @@ | ||
import React, { FC } from 'react'; | ||
|
||
import { Text } from '@grafana/ui'; | ||
|
||
import { Messages } from './FailedMigrationRow.messages'; | ||
import { FailedMigrationRowProps } from './FailedMigrationRow.types'; | ||
import { parseDetail } from './FailedMigrationRow.utils'; | ||
|
||
const FailedMigrationRow: FC<FailedMigrationRowProps> = ({ id, details }) => { | ||
const { name, error } = parseDetail(details); | ||
|
||
return ( | ||
<li> | ||
<strong>{Messages.id(id)}</strong> | ||
<ul> | ||
{!!name && ( | ||
<li> | ||
<Text> | ||
<strong>{Messages.name}</strong> | ||
{name} | ||
</Text> | ||
</li> | ||
)} | ||
<li> | ||
<Text> | ||
<strong>{Messages.error}</strong> | ||
{error} | ||
</Text> | ||
</li> | ||
</ul> | ||
</li> | ||
); | ||
}; | ||
|
||
export default FailedMigrationRow; |
4 changes: 4 additions & 0 deletions
4
...red/components/PerconaBootstrapper/PerconaMigrator/components/FailedMigrationRow.types.ts
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,4 @@ | ||
export interface FailedMigrationRowProps { | ||
id: number; | ||
details: string; | ||
} |
17 changes: 17 additions & 0 deletions
17
...red/components/PerconaBootstrapper/PerconaMigrator/components/FailedMigrationRow.utils.ts
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,17 @@ | ||
import { capitalizeText } from 'app/percona/shared/helpers/capitalizeText'; | ||
|
||
export const parseDetail = (detail: string) => { | ||
if (!detail.includes('- Error: ')) { | ||
return { | ||
name: '', | ||
error: detail, | ||
}; | ||
} | ||
|
||
const [name, error] = detail.split('- Error: '); | ||
|
||
return { | ||
name: name.replace('API key name: ', ''), | ||
error: capitalizeText(error), | ||
}; | ||
}; |
6 changes: 6 additions & 0 deletions
6
...d/components/PerconaBootstrapper/PerconaMigrator/components/MigrationSummary.constants.ts
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,6 @@ | ||
// todo: use a shortened link | ||
export const SERVICE_ACCOUNTS_DOCS_LINK = | ||
'https://docs.percona.com/percona-monitoring-and-management/3/api/authentication.html?h=service+accounts'; | ||
|
||
// todo: use a shortened link | ||
export const CONTACT_SUPPORT_LINK = 'https://www.percona.com/about/contact'; |
12 changes: 12 additions & 0 deletions
12
...ed/components/PerconaBootstrapper/PerconaMigrator/components/MigrationSummary.messages.ts
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,12 @@ | ||
export const Messages = { | ||
title: 'API migration status', | ||
description: | ||
'Migration of API keys complete. The following keys could not be converted to service accounts but remain fully functional:', | ||
failed: (failed: number, total: number) => `Failed conversions (${failed} of ${total} total)`, | ||
needHelp: 'Need help? See our ', | ||
documentation: 'documentation', | ||
or: ' or ', | ||
contactSupport: 'contact support', | ||
dot: '.', | ||
close: 'Close', | ||
}; |
31 changes: 31 additions & 0 deletions
31
...ared/components/PerconaBootstrapper/PerconaMigrator/components/MigrationSummary.styles.ts
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,31 @@ | ||
import { css } from '@emotion/css'; | ||
|
||
import { GrafanaTheme2 } from '@grafana/data'; | ||
|
||
export const getStyles = (theme: GrafanaTheme2) => ({ | ||
modal: css` | ||
width: auto; | ||
min-width: 750px; | ||
max-width: 75vw; | ||
`, | ||
migrationSummary: css` | ||
padding: ${theme.spacing(2)}; | ||
padding-top: 0; | ||
`, | ||
list: css` | ||
margin-top: ${theme.spacing(0.5)}; | ||
margin-bottom: ${theme.spacing(2)}; | ||
ol, | ||
ul { | ||
margin-left: ${theme.spacing(2)}; | ||
li > * { | ||
display: block; | ||
white-space: nowrap; | ||
overflow: hidden; | ||
text-overflow: ellipsis; | ||
} | ||
} | ||
`, | ||
}); |
51 changes: 51 additions & 0 deletions
51
...ona/shared/components/PerconaBootstrapper/PerconaMigrator/components/MigrationSummary.tsx
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,51 @@ | ||
import React, { FC } from 'react'; | ||
|
||
import { Button, Modal, TextLink, useStyles2 } from '@grafana/ui'; | ||
import { MigrationSummaryProps } from 'app/features/api-keys/ApiKeysPage'; | ||
|
||
import FailedMigrationRow from './FailedMigrationRow'; | ||
import { CONTACT_SUPPORT_LINK, SERVICE_ACCOUNTS_DOCS_LINK } from './MigrationSummary.constants'; | ||
import { Messages } from './MigrationSummary.messages'; | ||
import { getStyles } from './MigrationSummary.styles'; | ||
|
||
const MigrationSummary: FC<MigrationSummaryProps> = ({ visible, data, onDismiss }) => { | ||
const styles = useStyles2(getStyles); | ||
|
||
return ( | ||
<Modal | ||
title={Messages.title} | ||
isOpen={visible} | ||
closeOnBackdropClick={true} | ||
onDismiss={onDismiss} | ||
className={styles.modal} | ||
> | ||
<p>{Messages.description}</p> | ||
<strong>{Messages.failed(data.failed, data.total)}</strong> | ||
<div className={styles.list}> | ||
<ol> | ||
{data.failedApikeyIDs.map((id, idx) => ( | ||
<FailedMigrationRow key={id} id={id} details={data.failedDetails[idx]} /> | ||
))} | ||
</ol> | ||
</div> | ||
<p> | ||
{Messages.needHelp} | ||
<TextLink href={SERVICE_ACCOUNTS_DOCS_LINK} external> | ||
{Messages.documentation} | ||
</TextLink> | ||
{Messages.or} | ||
<TextLink href={CONTACT_SUPPORT_LINK} external> | ||
{Messages.contactSupport} | ||
</TextLink> | ||
{Messages.dot} | ||
</p> | ||
<Modal.ButtonRow> | ||
<Button variant="secondary" onClick={onDismiss}> | ||
{Messages.close} | ||
</Button> | ||
</Modal.ButtonRow> | ||
</Modal> | ||
); | ||
}; | ||
|
||
export default MigrationSummary; |
3 changes: 3 additions & 0 deletions
3
public/app/percona/shared/components/PerconaBootstrapper/PerconaMigrator/index.ts
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,3 @@ | ||
import PerconaMigrator from './PerconaMigrator'; | ||
|
||
export default PerconaMigrator; |
Oops, something went wrong.