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

PMM-12251 Service accounts. #2516

Merged
merged 171 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
171 commits
Select commit Hold shift + click to select a range
d51556f
Merge branch 'main' into PMM-12251-service-accounts
JiriCtvrtka Sep 19, 2023
6ef0886
Merge branch 'main' into PMM-12251-service-accounts
JiriCtvrtka Oct 2, 2023
3c4ab02
PMM-12251 Service accounts.
JiriCtvrtka Oct 2, 2023
f4fec78
PMM-12251Create service account and token.
JiriCtvrtka Oct 4, 2023
b35d984
Merge branch 'main' into PMM-12251-service-accounts
JiriCtvrtka Oct 4, 2023
1612b8c
PMM-12251 Another changes.
JiriCtvrtka Oct 4, 2023
22d879f
Merge branch 'main' into PMM-12251-service-accounts
JiriCtvrtka Oct 4, 2023
979b1be
PMM-12251 Fix for http client status code, creating of service token.
JiriCtvrtka Oct 4, 2023
9343f12
PMM-12251 Set orgId to 1 to avoid problem with default (-1).
JiriCtvrtka Oct 4, 2023
9a2f09c
PMM-12251 Mock regen.
JiriCtvrtka Oct 5, 2023
5d467fe
PMM-12251 Setup, tokens.
JiriCtvrtka Oct 5, 2023
b18c2d5
PMM-12251 Basic/Bearer auth.
JiriCtvrtka Oct 5, 2023
9deca51
PMM-12251 Set transport, check token/basic.
JiriCtvrtka Oct 7, 2023
df6d920
Merge branch 'main' into PMM-12251-service-accounts
JiriCtvrtka Oct 7, 2023
a8a86e3
PMM-12251 Tidy.
JiriCtvrtka Oct 7, 2023
f3f21b0
PMM-12251 Lint, small changes.
JiriCtvrtka Oct 9, 2023
49d0453
Merge branch 'main' into PMM-12251-service-accounts
JiriCtvrtka Oct 9, 2023
9245d44
PMM-12251 Add test, logic changes.
JiriCtvrtka Oct 9, 2023
c3999d1
PMM-12251 Test, tokens funcs.
JiriCtvrtka Oct 10, 2023
8e93cf9
PMM-12251 Not needed, set on transport.
JiriCtvrtka Oct 10, 2023
d0abd62
PMM-12251 Mock gen.
JiriCtvrtka Oct 10, 2023
ca5c3eb
PMM-12251 Node changes.
JiriCtvrtka Oct 10, 2023
112d6b7
PMM-12251 Add existedServiceToken to NodeRegister.
JiriCtvrtka Oct 10, 2023
e80e9c4
PMM-12251 Typo.
JiriCtvrtka Oct 10, 2023
4c5d452
Merge branch 'main' into PMM-12251-service-accounts
JiriCtvrtka Oct 10, 2023
af9f6e5
PMM-12251 Small logic changes. Validation.
JiriCtvrtka Oct 11, 2023
73cd6a0
PMM-12251 Add API tests.
JiriCtvrtka Oct 11, 2023
8926653
Merge remote-tracking branch 'origin/PMM-12251-service-accounts' into…
JiriCtvrtka Oct 11, 2023
af32e2d
Merge branch 'main' into PMM-12251-service-accounts
JiriCtvrtka Oct 11, 2023
88199cd
Merge branch 'main' into PMM-12251-service-accounts
JiriCtvrtka Oct 11, 2023
82b5826
Merge remote-tracking branch 'origin/PMM-12251-service-accounts' into…
JiriCtvrtka Oct 11, 2023
b097149
PMM-12251 Align mock with changes.
JiriCtvrtka Oct 11, 2023
2b6d2e5
Revert "Merge remote-tracking branch 'origin/PMM-12251-service-accoun…
JiriCtvrtka Oct 11, 2023
fc5e581
PMM-12251 Tidy.
JiriCtvrtka Oct 11, 2023
dd3026b
PMM-12251 Small refactor.
JiriCtvrtka Oct 11, 2023
f9b4339
PMM-12251 Fix.
JiriCtvrtka Oct 11, 2023
0064059
PMM-12251 Unregister.
JiriCtvrtka Oct 11, 2023
c99246e
PMM-12251 Tidy.
JiriCtvrtka Oct 11, 2023
8de6288
PMM-12251 Unregister complete.
JiriCtvrtka Oct 12, 2023
945b769
Merge branch 'main' into PMM-12251-service-accounts
JiriCtvrtka Oct 12, 2023
41de6fc
PMM-12251 Unregister.
JiriCtvrtka Oct 12, 2023
fda1e01
PMM-12251 Fix.
JiriCtvrtka Oct 12, 2023
d8da652
PMM-12251 Remove print.
JiriCtvrtka Oct 12, 2023
4bcd1e0
PMM-12251 Refactor ID int64 to int.
JiriCtvrtka Oct 12, 2023
a5a64af
PMM-12251 Modify description in md to be aligned with Service acc.
JiriCtvrtka Oct 12, 2023
1a11ac5
PMM-12251 Int64 to int in tests.
JiriCtvrtka Oct 12, 2023
f2ead0e
Merge branch 'main' into PMM-12251-service-accounts
JiriCtvrtka Oct 12, 2023
9fbcd56
PMM-12251 Lint.
JiriCtvrtka Oct 12, 2023
92c8f2f
PMM-12251 Service token ID to int.
JiriCtvrtka Oct 12, 2023
511ad4a
PMM-12251 Lint.
JiriCtvrtka Oct 12, 2023
443b7d9
Merge branch 'main' into PMM-12251-service-accounts
JiriCtvrtka Oct 16, 2023
2ea3164
Merge branch 'main' into PMM-12251-service-accounts
JiriCtvrtka Oct 25, 2023
6362936
Merge branch 'main' into PMM-12251-service-accounts
JiriCtvrtka Oct 25, 2023
4da1f43
Merge branch 'main' into PMM-12251-service-accounts
JiriCtvrtka Oct 25, 2023
1d307d5
Merge branch 'main' into PMM-12251-service-accounts
JiriCtvrtka Nov 1, 2023
ea88aa1
PMM-12251 Refactor of IF.
JiriCtvrtka Nov 1, 2023
dea0e71
PMM-12251 Warning instead error.
JiriCtvrtka Nov 1, 2023
873747c
PMM-12251 Auth tools, existed token got from headers.
JiriCtvrtka Nov 1, 2023
5e0940e
Merge branch 'main' into PMM-12251-service-accounts
JiriCtvrtka Nov 1, 2023
68a17ac
Merge branch 'main' into PMM-12251-service-accounts
JiriCtvrtka Nov 1, 2023
e1b69b4
Merge branch 'main' into PMM-12251-service-accounts
JiriCtvrtka Nov 7, 2023
4fa9b6a
PMM-12251 Fix in doc.
JiriCtvrtka Nov 7, 2023
cb5ab7f
PMM-12251 Remove token prefix check.
JiriCtvrtka Nov 7, 2023
9841225
Merge branch 'main' into PMM-12251-service-accounts
JiriCtvrtka Nov 7, 2023
4d33f64
Merge branch 'v3' into PMM-12251-service-accounts
JiriCtvrtka Nov 8, 2023
d71b26d
Revert "Bump @typescript-eslint/parser from 6.9.0 to 6.10.0 in /cli-t…
JiriCtvrtka Nov 8, 2023
89ecdcd
Revert "Bump @typescript-eslint/eslint-plugin from 6.9.0 to 6.10.0 in…
JiriCtvrtka Nov 8, 2023
ef09821
Revert "Bump golang.org/x/text from 0.13.0 to 0.14.0 (#2593)"
JiriCtvrtka Nov 8, 2023
58000e9
Revert "Bump golang.org/x/sys from 0.13.0 to 0.14.0 (#2594)"
JiriCtvrtka Nov 8, 2023
e0990ad
Revert "Bump eslint from 8.52.0 to 8.53.0 in /cli-tests (#2596)"
JiriCtvrtka Nov 8, 2023
16cbe9e
PMM-12251 Remove not used code.
JiriCtvrtka Nov 13, 2023
b979363
Revert "PMM-12251 Remove not used code."
JiriCtvrtka Nov 13, 2023
dd3be16
PMM-12251 Token/headers fix.
JiriCtvrtka Nov 13, 2023
b2ef715
Revert "Revert "PMM-12251 Remove not used code.""
JiriCtvrtka Nov 13, 2023
a108afc
PMM-12251 Doc update.
JiriCtvrtka Nov 13, 2023
801bc1d
Merge branch 'v3' into PMM-12251-service-accounts
JiriCtvrtka Nov 13, 2023
ea4bca5
PMM-12251 Remove APIKey permissions tests.
JiriCtvrtka Nov 14, 2023
9fbce02
PMM-12251 Better error message.
JiriCtvrtka Nov 14, 2023
2cffa68
PMM-12251 Remove API key methods. Refactor.
JiriCtvrtka Nov 14, 2023
d914c39
PMM-12251 Fix when token is empty.
JiriCtvrtka Nov 14, 2023
5249ff1
PMM-12251 Basic auth tests.
JiriCtvrtka Nov 21, 2023
3da6687
PMM-12251 Small refactor.
JiriCtvrtka Nov 21, 2023
b1ff21a
PMM-12251 API Tests clean.
JiriCtvrtka Nov 14, 2023
d9e4b2f
PMM-12251 Typo.
JiriCtvrtka Nov 21, 2023
1cf54a8
Merge branch 'v3' into PMM-12251-service-accounts
JiriCtvrtka Nov 21, 2023
8b9d492
PMM-12251 Start tests in parallel.
JiriCtvrtka Nov 21, 2023
4c1d5f5
Merge branch 'v3' into PMM-12251-service-accounts
JiriCtvrtka Nov 22, 2023
8720a66
PMM-12251 Force in test.
JiriCtvrtka Nov 22, 2023
56fa461
PMM-12251 Merge CreateServiceAccount and CreateServiceToken together.
JiriCtvrtka Nov 23, 2023
f7fd703
PMM-12251 Mock.
JiriCtvrtka Nov 23, 2023
71583b3
Merge branch 'v3' into PMM-12251-service-accounts
JiriCtvrtka Nov 23, 2023
e470641
PMM-12251 Gen.
JiriCtvrtka Nov 23, 2023
f0303c6
PMM-12251 Refactor, node name.
JiriCtvrtka Nov 23, 2023
1d43d28
PMM-12251 Delete, mock and nodeName.
JiriCtvrtka Nov 23, 2023
b1a88af
PMM-12251 Fix node test after changes.
JiriCtvrtka Nov 23, 2023
96aa1f1
PMM-12251 Fix test.
JiriCtvrtka Nov 27, 2023
ad4247a
PMM-12251 nodeName in Service Account name.
JiriCtvrtka Nov 27, 2023
a10c16c
PMM-12251 Change in naming.
JiriCtvrtka Nov 27, 2023
a7af92b
PMM-12251 Modify API test. Data race.
JiriCtvrtka Nov 27, 2023
a08169c
PMM-12251 Changes.
JiriCtvrtka Nov 28, 2023
d6bc927
PMM-12251 Fix V3 failing tests.
JiriCtvrtka Nov 28, 2023
4e19268
PMM-12251 Tests.
JiriCtvrtka Nov 28, 2023
bba7d76
PMM-12251 Double paralel.
JiriCtvrtka Nov 28, 2023
15c2154
PMM-12251 Test.
JiriCtvrtka Nov 28, 2023
34d6cfd
PMM-12251 Changes.
JiriCtvrtka Nov 28, 2023
30ee6c3
PMM-12251 Changes in API test.
JiriCtvrtka Nov 28, 2023
c232311
PMM-12251 Modify test back.
JiriCtvrtka Nov 28, 2023
263b701
PMM-12251 Fix tests.
JiriCtvrtka Nov 28, 2023
f2e07f7
PMM-12251 Fix version test.
JiriCtvrtka Nov 28, 2023
f661d7c
PMM-12251 Fix another cleanup.
JiriCtvrtka Nov 28, 2023
78783fc
PMM-12251 Fix another tests.
JiriCtvrtka Nov 28, 2023
cb9d359
PMM-12251 Another fix of tests.
JiriCtvrtka Nov 28, 2023
d263e69
PMM-12251 URL query escape.
JiriCtvrtka Nov 29, 2023
36ac96a
PMM-12251 Fix annotation API test.
JiriCtvrtka Nov 29, 2023
4effd2f
PMM-12251 Fix external API test.
JiriCtvrtka Nov 29, 2023
53522fa
PMM-12251 Fix remove external API test.
JiriCtvrtka Nov 29, 2023
7be63a0
PMM-12251 Fix HAProxy API test.
JiriCtvrtka Nov 29, 2023
f954584
PMM-12251 Fix another API tests.
JiriCtvrtka Nov 29, 2023
0dcb6b1
PMM-12251 Node test.
JiriCtvrtka Nov 30, 2023
23d53c6
Merge branch 'v3' into PMM-12251-service-accounts
JiriCtvrtka Dec 1, 2023
c3c6e72
Merge branch 'v3' into PMM-12251-service-accounts
JiriCtvrtka Dec 5, 2023
07cbb2a
Merge branch 'v3' into PMM-12251-service-accounts
JiriCtvrtka Dec 5, 2023
1ba2762
Merge branch 'v3' into PMM-12251-service-accounts
JiriCtvrtka Jan 11, 2024
e2ba1a5
PMM-12251 Tidy.
JiriCtvrtka Jan 11, 2024
f8c3638
PMM-12251 Comment out for now.
JiriCtvrtka Jan 11, 2024
ad9ffa0
PMM-12251 TODO.
JiriCtvrtka Jan 12, 2024
5774901
PMM-12251 Temp, TODO lefts.
JiriCtvrtka Jan 16, 2024
d5af1ae
Merge branch 'v3' into PMM-12251-service-accounts
JiriCtvrtka Jan 16, 2024
e02d9c7
PMM-12251 Cleanup for migrated API keys in API tests.
JiriCtvrtka Jan 17, 2024
703e6e2
Merge remote-tracking branch 'origin/PMM-12251-service-accounts' into…
JiriCtvrtka Jan 17, 2024
a80a686
PMM-12251 Cleanup for tests.
JiriCtvrtka Jan 17, 2024
1ae7ec8
PMM-12251 Add log error.
JiriCtvrtka Jan 17, 2024
6f1387d
PMM-12251 Lint.
JiriCtvrtka Jan 17, 2024
63428b7
PMM-12251 Another fix for very long service accounts names.
JiriCtvrtka Jan 17, 2024
1bf2d46
PMM-12251 Lint.
JiriCtvrtka Jan 17, 2024
f4fc6c3
PMM-12251 Required changes in mock.
JiriCtvrtka Jan 18, 2024
f43f9f4
PMM-12251 Lint.
JiriCtvrtka Jan 18, 2024
5309788
PMM-12251 Remove comment.
JiriCtvrtka Jan 18, 2024
4c3a651
PMM-12251 Remove another duplicate test.
JiriCtvrtka Jan 18, 2024
71a7bd3
PMM-12251 Seeds in generator, ctx.
JiriCtvrtka Jan 18, 2024
62ea7cb
PMM-12251 Cleanup.
JiriCtvrtka Jan 18, 2024
69c1c6d
PMM-12251 Make test names/SA names shorter due to limit.
JiriCtvrtka Jan 18, 2024
f5364e0
PMM-12251 Lint, formatting.
JiriCtvrtka Jan 19, 2024
8d73a6e
PMM-12251 Remove leftover code, fix node tests.
JiriCtvrtka Jan 23, 2024
40684b4
Merge branch 'v3' into PMM-12251-service-accounts
JiriCtvrtka Jan 29, 2024
c7b526e
Merge branch 'v3' into PMM-12251-service-accounts
JiriCtvrtka Jan 30, 2024
774c67f
Update docs/api/welcome/authentication.md
JiriCtvrtka Feb 1, 2024
cc63dcb
Update managed/services/grafana/client.go
JiriCtvrtka Feb 1, 2024
9c01dd6
PMM-12251 Add debug error message.
JiriCtvrtka Feb 1, 2024
77c058d
Merge branch 'v3' into PMM-12251-service-accounts
JiriCtvrtka Feb 7, 2024
51dd142
PMM-12251 Handle of non migrated API keys.
JiriCtvrtka Feb 8, 2024
8c12fa8
PMM-12251 Fix test.
JiriCtvrtka Feb 8, 2024
003103a
PMM-12251 Small refactor.
JiriCtvrtka Feb 8, 2024
422d542
Merge branch 'v3' into PMM-12251-service-accounts
JiriCtvrtka Feb 8, 2024
e623939
PMM-12251 Lint.
JiriCtvrtka Feb 8, 2024
528bc41
Update api-tests/management/mongodb_test.go
JiriCtvrtka Feb 14, 2024
c7d7b9f
Update api-tests/management/mongodb_test.go
JiriCtvrtka Feb 14, 2024
cc64d3b
Update api/managementpb/node.proto
JiriCtvrtka Feb 14, 2024
dc3f5bb
PMM-12251 Gen after suggestions.
JiriCtvrtka Feb 14, 2024
2195145
Merge branch 'v3' into PMM-12251-service-accounts
JiriCtvrtka Feb 26, 2024
4af52d4
PMM-12251 Gen after merge.
JiriCtvrtka Feb 26, 2024
d588176
Merge branch 'v3' into PMM-12251-service-accounts
JiriCtvrtka Feb 28, 2024
0090b7a
Merge branch 'v3' into PMM-12251-service-accounts
ademidoff Mar 6, 2024
452b350
Merge branch 'v3' into PMM-12251-service-accounts
ademidoff Mar 7, 2024
12f805e
PMM-12251 Another reverted changes.
JiriCtvrtka Mar 11, 2024
0410772
PMM-12686 Years in licence.
JiriCtvrtka Mar 12, 2024
f86273f
Merge branch 'v3' into PMM-12251-service-accounts
JiriCtvrtka Mar 12, 2024
4be8fa0
Merge branch 'v3' into PMM-12251-service-accounts
JiriCtvrtka Mar 18, 2024
088d461
Merge branch 'v3' into PMM-12251-service-accounts
JiriCtvrtka Mar 20, 2024
01a14f0
Merge branch 'v3' into PMM-12251-service-accounts
BupycHuk Mar 27, 2024
3ac6d7f
PMM-12686 Basic/Token auth between server and client. (#2852)
JiriCtvrtka Apr 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion agent/commands/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"net/http"
"os"
"strings"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -160,7 +161,11 @@ func register(cfg *config.Config, l *logrus.Entry) {
}
cfg.ID = agentID
if token != "" {
cfg.Server.Username = "api_key"
if strings.HasPrefix(token, "glsa_") {
cfg.Server.Username = "service_token"
} else {
cfg.Server.Username = "api_key"
}
cfg.Server.Password = token
} else {
l.Info("PMM Server responded with an empty api key token. Consider upgrading PMM Server to the latest version.")
Expand Down
124 changes: 117 additions & 7 deletions managed/services/grafana/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"encoding/json"
"fmt"
"io"
"math/rand"
"net"
"net/http"
"net/url"
Expand All @@ -47,7 +48,10 @@
// ErrFailedToGetToken means it failed to get user's token. Most likely due to the fact user is not logged in using Percona Account.
var ErrFailedToGetToken = errors.New("failed to get token")

const defaultEvaluationInterval = time.Minute
const (
defaultEvaluationInterval = time.Minute
pmmServiceTokenName = "pmm-agent-service-token"

Check failure on line 53 in managed/services/grafana/client.go

View workflow job for this annotation

GitHub Actions / Checks

G101: Potential hardcoded credentials (gosec)
JiriCtvrtka marked this conversation as resolved.
Show resolved Hide resolved
JiriCtvrtka marked this conversation as resolved.
Show resolved Hide resolved
)

// Client represents a client for Grafana API.
type Client struct {
Expand Down Expand Up @@ -138,7 +142,7 @@
if err != nil {
return errors.WithStack(err)
}
if resp.StatusCode != 200 && resp.StatusCode != 202 {
if resp.StatusCode != 200 && resp.StatusCode != 201 && resp.StatusCode != 202 {
JiriCtvrtka marked this conversation as resolved.
Show resolved Hide resolved
cErr := &clientError{
Method: req.Method,
URL: req.URL.String(),
Expand Down Expand Up @@ -218,9 +222,23 @@
// Otherwise, it returns a role in the default organization (with ID 1).
// Ctx is used only for cancelation.
func (c *Client) getAuthUser(ctx context.Context, authHeaders http.Header) (authUser, error) {
// Check if it's API Key
if c.isAPIKeyAuth(authHeaders.Get("Authorization")) {
role, err := c.getRoleForAPIKey(ctx, authHeaders)
// Check if it's API Key or Service Token
auth := authHeaders.Get("Authorization")
if c.isBearerTokenAuth(auth) {
h := strings.TrimPrefix(auth, "Basic ")
d, err := base64.StdEncoding.DecodeString(strings.TrimSpace(h))
if err != nil {
return authUser{}, err
}
if strings.HasPrefix(string(d), "api_key") {
role, err := c.getRoleForAPIKey(ctx, authHeaders)
return authUser{
role: role,
userID: 0,
}, err
}

role, err := c.getRoleForServiceToken(ctx, authHeaders)
return authUser{
role: role,
userID: 0,
Expand Down Expand Up @@ -277,7 +295,7 @@
}, nil
}

func (c *Client) isAPIKeyAuth(authHeader string) bool {
func (c *Client) isBearerTokenAuth(authHeader string) bool {
switch {
case strings.HasPrefix(authHeader, "Bearer"):
return true
Expand All @@ -287,7 +305,7 @@
if err != nil {
return false
}
return strings.HasPrefix(string(d), "api_key:")
return strings.HasPrefix(string(d), "api_key:") || strings.HasPrefix(string(d), "service_token:")
}
return false
}
Expand Down Expand Up @@ -327,6 +345,20 @@
return c.convertRole(role), nil
}

func (c *Client) getRoleForServiceToken(ctx context.Context, authHeaders http.Header) (role, error) {
var k map[string]interface{}
if err := c.do(ctx, http.MethodGet, "/api/auth/serviceaccount", "", authHeaders, nil, &k); err != nil {
return none, err
}

if id, _ := k["orgId"].(float64); id != 1 {
return none, nil
}

role, _ := k["role"].(string)
return c.convertRole(role), nil
}

func (c *Client) testCreateUser(ctx context.Context, login string, role role, authHeaders http.Header) (int, error) {
// https://grafana.com/docs/http_api/admin/#global-users
b, err := json.Marshal(map[string]string{
Expand Down Expand Up @@ -376,6 +408,15 @@
return c.createAPIKey(ctx, name, admin, authHeaders)
}

// CreateServiceAccountAndToken creates service account and token with Admin role and provided name.
func (c *Client) CreateServiceAccountAndToken(ctx context.Context, name string) (int64, string, error) {
authHeaders, err := c.authHeadersFromContext(ctx)
if err != nil {
return 0, "", err
}
return c.createServiceAccountAndToken(ctx, name, admin, authHeaders)
}

// DeleteAPIKeysWithPrefix deletes all API keys with provided prefix. If there is no api key with provided prefix just ignores it.
func (c *Client) DeleteAPIKeysWithPrefix(ctx context.Context, prefix string) error {
authHeaders, err := c.authHeadersFromContext(ctx)
Expand Down Expand Up @@ -578,6 +619,75 @@
return c.do(ctx, "DELETE", "/api/auth/keys/"+strconv.FormatInt(apiKeyID, 10), "", authHeaders, nil, nil)
}

type serviceAccount struct {
Name string `json:"name"`
Role string `json:"role"`
OrgID int64 `json:"orgId"`
}
type serviceToken struct {
ID int64 `json:"id"`
Name string `json:"name"`
Role string `json:"role"`
Expiration *time.Time `json:"expiration,omitempty"`
}

func (c *Client) createServiceAccountAndToken(ctx context.Context, name string, role role, authHeaders http.Header) (int64, string, error) {
b, err := json.Marshal(serviceAccount{Name: name, Role: role.String()})
if err != nil {
return 0, "", errors.WithStack(err)
}

var m map[string]interface{}
if err = c.do(ctx, "POST", "/api/serviceaccounts", "", authHeaders, b, &m); err != nil {
return 0, "", err
}

serviceAccountID := int64(m["id"].(float64)) //nolint:forcetypeassert

// orgId is ignored during creating service account and default is -1
// orgId should be setup to 1
JiriCtvrtka marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return 0, "", errors.WithStack(err)
}
if err = c.do(ctx, "PATCH", fmt.Sprintf("/api/serviceaccounts/%d", serviceAccountID), "", authHeaders, []byte("{\"orgId\": 1}"), &m); err != nil {
return 0, "", err
}

// due to reregister of node PMM agent related tokens should be deleted first
err = c.deletePMMAgentRelatedServiceTokens(ctx, serviceAccountID, authHeaders)
if err != nil {
return 0, "", errors.WithStack(err)
}

serviceTokenName := fmt.Sprintf("%s-%s-%d", pmmServiceTokenName, name, rand.Int63())

Check failure on line 662 in managed/services/grafana/client.go

View workflow job for this annotation

GitHub Actions / Checks

G404: Use of weak random number generator (math/rand instead of crypto/rand) (gosec)
JiriCtvrtka marked this conversation as resolved.
Show resolved Hide resolved
b, err = json.Marshal(serviceToken{Name: serviceTokenName})
if err != nil {
return 0, "", errors.WithStack(err)
}
if err = c.do(ctx, "POST", fmt.Sprintf("/api/serviceaccounts/%d/tokens", serviceAccountID), "", authHeaders, b, &m); err != nil {
return 0, "", errors.WithStack(err)
}
serviceTokenID := int64(m["id"].(float64)) //nolint:forcetypeassert
serviceTokenKey := m["key"].(string) //nolint:forcetypeassert

return serviceTokenID, serviceTokenKey, nil
}

func (c *Client) deletePMMAgentRelatedServiceTokens(ctx context.Context, serviceAccountID int64, authHeaders http.Header) error {
var tokens []serviceToken
if err := c.do(ctx, "GET", fmt.Sprintf("/api/serviceaccounts/%d/tokens", serviceAccountID), "", authHeaders, nil, &tokens); err != nil {
return err
}

for _, token := range tokens {
if strings.HasPrefix(token.Name, pmmServiceTokenName) {
c.do(ctx, "DELETE", fmt.Sprintf("/api/serviceaccounts/%d/tokens/%d", serviceAccountID, token.ID), "", authHeaders, nil, nil)
}
}

return nil
}

// Annotation contains grafana annotation response.
type annotation struct {
Time time.Time `json:"-"`
Expand Down
60 changes: 0 additions & 60 deletions managed/services/management/mock_api_key_provider_test.go

This file was deleted.

91 changes: 91 additions & 0 deletions managed/services/management/mock_auth_provider_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading