-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* PMM-13315 Remove comment about SA max length. * PMM-13315 Long postfix for tests. * trigger * Revert "PMM-13315 Remove comment about SA max length." This reverts commit d812c2a. * PMM-13315 Long SA names hashing. * PMM-13315 Lint. * PMM-11315 Changes, refactor. * PMM-13315 Format. * PMM-13315 Remove forgotten prints. * PMM-13315 Remove another print. * PMM-13315 Fix problem with big orgIDs. * PMM-13315 Move SanitizeSAName to global utils. * PMM-13315 Headers. * PMM-13315 Licence year. * PMM-13315 Lint.
- Loading branch information
1 parent
14242e8
commit 84c8b60
Showing
6 changed files
with
124 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,37 @@ | ||
// Copyright (C) 2023 Percona LLC | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
// Package grafana provides util functions related to grafana functionality. | ||
package grafana | ||
|
||
import ( | ||
"crypto/md5" //nolint:gosec | ||
"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. | ||
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,37 @@ | ||
// Copyright (C) 2023 Percona LLC | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
package grafana | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
stringsgen "github.com/percona/pmm/utils/strings" | ||
) | ||
|
||
func Test_sanitizeSAName(t *testing.T) { | ||
// max possible length without hashing | ||
len180, err := stringsgen.GenerateRandomString(180) | ||
require.NoError(t, err) | ||
require.Equal(t, len180, SanitizeSAName(len180)) | ||
|
||
// too long length - postfix hashed | ||
len200, err := stringsgen.GenerateRandomString(200) | ||
require.NoError(t, err) | ||
len200sanitized := SanitizeSAName(len200) | ||
require.Equal(t, fmt.Sprintf("%s%s", len200[:148], len200sanitized[148:]), len200sanitized) | ||
} |
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 @@ | ||
// Copyright (C) 2023 Percona LLC | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
// Package stringsgen provides functions to generate random strings. | ||
package stringsgen | ||
|
||
import ( | ||
"crypto/rand" | ||
"encoding/base64" | ||
) | ||
|
||
// GenerateRandomString returns random string with given length. | ||
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 | ||
} |