-
Notifications
You must be signed in to change notification settings - Fork 124
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
PMM-13315 Service account max length. #3221
Open
JiriCtvrtka
wants to merge
12
commits into
v3
Choose a base branch
from
PMM-13315-service-account-max-length
base: v3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+85
−12
Open
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d812c2a
PMM-13315 Remove comment about SA max length.
JiriCtvrtka 36dcfd0
PMM-13315 Long postfix for tests.
JiriCtvrtka 2b58b35
trigger
JiriCtvrtka f600dc8
Revert "PMM-13315 Remove comment about SA max length."
JiriCtvrtka f0c93c5
PMM-13315 Long SA names hashing.
JiriCtvrtka 15e11f9
PMM-13315 Lint.
JiriCtvrtka 1a70691
PMM-11315 Changes, refactor.
JiriCtvrtka 278417a
PMM-13315 Format.
JiriCtvrtka eac0660
PMM-13315 Remove forgotten prints.
JiriCtvrtka ac7ea22
PMM-13315 Remove another print.
JiriCtvrtka 5c45652
PMM-13315 Fix problem with big orgIDs.
JiriCtvrtka b14fef8
Merge branch 'v3' into PMM-13315-service-account-max-length
JiriCtvrtka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ package grafana | |
import ( | ||
"bytes" | ||
"context" | ||
"crypto/md5" //nolint:gosec | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
|
@@ -350,7 +351,7 @@ type serviceAccountSearch struct { | |
|
||
func (c *Client) getServiceAccountIDFromName(ctx context.Context, nodeName string, authHeaders http.Header) (int, error) { | ||
var res serviceAccountSearch | ||
serviceAccountName := fmt.Sprintf("%s-%s", pmmServiceAccountName, nodeName) | ||
serviceAccountName := sanitizeSAName(fmt.Sprintf("%s-%s", pmmServiceAccountName, nodeName)) | ||
if err := c.do(ctx, http.MethodGet, "/api/serviceaccounts/search", fmt.Sprintf("query=%s", serviceAccountName), authHeaders, nil, &res); err != nil { | ||
return 0, err | ||
} | ||
|
@@ -383,7 +384,7 @@ func (c *Client) getNotPMMAgentTokenCountForServiceAccount(ctx context.Context, | |
count := 0 | ||
for _, token := range tokens { | ||
serviceTokenName := fmt.Sprintf("%s-%s", pmmServiceTokenName, nodeName) | ||
if !strings.HasPrefix(token.Name, serviceTokenName) { | ||
if !strings.HasPrefix(token.Name, sanitizeSAName(serviceTokenName)) { | ||
count++ | ||
} | ||
} | ||
|
@@ -672,15 +673,27 @@ type serviceToken struct { | |
Role string `json:"role"` | ||
} | ||
|
||
// 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't that a utility method? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if it belongs to utils when its related only to service accounts, which are part of Grafana. |
||
if len(name) <= 180 { | ||
return name | ||
} | ||
|
||
return fmt.Sprintf("%s%x", name[:148], md5.Sum([]byte(name[148:]))) //nolint:gosec | ||
} | ||
|
||
func (c *Client) createServiceAccount(ctx context.Context, role role, nodeName string, reregister bool, authHeaders http.Header) (int, error) { | ||
if role == none { | ||
return 0, errors.New("you cannot create service account with empty role") | ||
} | ||
|
||
// Max length of service account name is 190 chars (default limit in Grafana). In reality it is 187. | ||
// Some tests could fail if you pass a name longer than 187 chars. | ||
serviceAccountName := fmt.Sprintf("%s-%s", pmmServiceAccountName, nodeName) | ||
b, err := json.Marshal(serviceAccount{Name: serviceAccountName, Role: role.String(), Force: reregister}) | ||
b, err := json.Marshal(serviceAccount{Name: sanitizeSAName(serviceAccountName), Role: role.String(), Force: reregister}) | ||
if err != nil { | ||
return 0, errors.WithStack(err) | ||
} | ||
|
@@ -714,7 +727,7 @@ func (c *Client) createServiceToken(ctx context.Context, serviceAccountID int, n | |
} | ||
} | ||
|
||
b, err := json.Marshal(serviceToken{Name: serviceTokenName, Role: admin.String()}) | ||
b, err := json.Marshal(serviceToken{Name: sanitizeSAName(serviceTokenName), Role: admin.String()}) | ||
if err != nil { | ||
return 0, "", errors.WithStack(err) | ||
} | ||
|
@@ -737,7 +750,7 @@ func (c *Client) serviceTokenExists(ctx context.Context, serviceAccountID int, n | |
|
||
serviceTokenName := fmt.Sprintf("%s-%s", pmmServiceTokenName, nodeName) | ||
for _, token := range tokens { | ||
if !strings.HasPrefix(token.Name, serviceTokenName) { | ||
if !strings.HasPrefix(token.Name, sanitizeSAName(serviceTokenName)) { | ||
continue | ||
} | ||
|
||
|
@@ -755,7 +768,7 @@ func (c *Client) deletePMMAgentServiceToken(ctx context.Context, serviceAccountI | |
|
||
serviceTokenName := fmt.Sprintf("%s-%s", pmmServiceTokenName, nodeName) | ||
for _, token := range tokens { | ||
if strings.HasPrefix(token.Name, serviceTokenName) { | ||
if strings.HasPrefix(token.Name, sanitizeSAName(serviceTokenName)) { | ||
if err := c.do(ctx, "DELETE", fmt.Sprintf("/api/serviceaccounts/%d/tokens/%d", serviceAccountID, token.ID), "", authHeaders, nil, nil); err != nil { | ||
return err | ||
} | ||
|
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,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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we can't keep and then import this method from
github.com/percona/pmm/managed/utils/strings
package?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is to avoid dependencies between managed and API tests. To keep them separate. We doing this in more places. It was discussed with Nurlan.