Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7ed462c

Browse files
committed
fix: replace moby/moby namesgenerator with internal implementation
The moby/moby/pkg/namesgenerator package has ~25k unique name combinations. With the retry parameter, which is awkwardly an integer that only differs in behavior when the value is > 0, it only adds a single digit (0-9), giving ~250k total possibilities. It also uses the math/rand package internally with the default seed which is deterministically random. In parallel tests, this combination has experienced collisions.This replaces the external dependency with an internal implementation that uses a monotonically increasing atomic counter instead of random digits, guaranteeing uniqueness within a process. Names continue to be truncated to 32 characters (including atomic counter suffix) to fit common name length limits for test purposes.
1 parent61d7d29 commit7ed462c

File tree

16 files changed

+511
-92
lines changed

16 files changed

+511
-92
lines changed

‎coderd/apikey.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
"github.com/go-chi/chi/v5"
1111
"github.com/google/uuid"
12-
"github.com/moby/moby/pkg/namesgenerator"
1312
"golang.org/x/xerrors"
1413

1514
"cdr.dev/slog"
@@ -23,6 +22,7 @@ import (
2322
"github.com/coder/coder/v2/coderd/rbac"
2423
"github.com/coder/coder/v2/coderd/rbac/policy"
2524
"github.com/coder/coder/v2/coderd/telemetry"
25+
"github.com/coder/coder/v2/coderd/util/namesgenerator"
2626
"github.com/coder/coder/v2/codersdk"
2727
)
2828

@@ -102,7 +102,7 @@ func (api *API) postToken(rw http.ResponseWriter, r *http.Request) {
102102
}
103103
}
104104

105-
tokenName:=namesgenerator.GetRandomName(1)
105+
tokenName:=namesgenerator.GetRandomName()
106106

107107
iflen(createToken.TokenName)!=0 {
108108
tokenName=createToken.TokenName

‎coderd/coderdtest/authorize.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"testing"
1212

1313
"github.com/google/uuid"
14-
"github.com/moby/moby/pkg/namesgenerator"
1514
"github.com/stretchr/testify/assert"
1615
"github.com/stretchr/testify/require"
1716
"golang.org/x/xerrors"
@@ -22,6 +21,7 @@ import (
2221
"github.com/coder/coder/v2/coderd/rbac"
2322
"github.com/coder/coder/v2/coderd/rbac/policy"
2423
"github.com/coder/coder/v2/coderd/rbac/regosql"
24+
"github.com/coder/coder/v2/coderd/util/namesgenerator"
2525
"github.com/coder/coder/v2/codersdk"
2626
"github.com/coder/coder/v2/cryptorand"
2727
)
@@ -439,10 +439,10 @@ func RandomRBACObject() rbac.Object {
439439
OrgID:uuid.NewString(),
440440
Type:randomRBACType(),
441441
ACLUserList:map[string][]policy.Action{
442-
namesgenerator.GetRandomName(1): {RandomRBACAction()},
442+
namesgenerator.GetRandomName(): {RandomRBACAction()},
443443
},
444444
ACLGroupList:map[string][]policy.Action{
445-
namesgenerator.GetRandomName(1): {RandomRBACAction()},
445+
namesgenerator.GetRandomName(): {RandomRBACAction()},
446446
},
447447
}
448448
}
@@ -471,7 +471,7 @@ func RandomRBACSubject() rbac.Subject {
471471
return rbac.Subject{
472472
ID:uuid.NewString(),
473473
Roles: rbac.RoleIdentifiers{rbac.RoleMember()},
474-
Groups: []string{namesgenerator.GetRandomName(1)},
474+
Groups: []string{namesgenerator.GetRandomName()},
475475
Scope:rbac.ScopeAll,
476476
}
477477
}

‎coderd/coderdtest/coderdtest.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import (
3737
"github.com/go-chi/chi/v5"
3838
"github.com/golang-jwt/jwt/v4"
3939
"github.com/google/uuid"
40-
"github.com/moby/moby/pkg/namesgenerator"
4140
"github.com/prometheus/client_golang/prometheus"
4241
"github.com/stretchr/testify/assert"
4342
"github.com/stretchr/testify/require"
@@ -83,6 +82,7 @@ import (
8382
"github.com/coder/coder/v2/coderd/schedule"
8483
"github.com/coder/coder/v2/coderd/telemetry"
8584
"github.com/coder/coder/v2/coderd/updatecheck"
85+
"github.com/coder/coder/v2/coderd/util/namesgenerator"
8686
"github.com/coder/coder/v2/coderd/util/ptr"
8787
"github.com/coder/coder/v2/coderd/webpush"
8888
"github.com/coder/coder/v2/coderd/workspaceapps"
@@ -793,7 +793,7 @@ func AuthzUserSubject(user codersdk.User, orgID uuid.UUID) rbac.Subject {
793793

794794
funccreateAnotherUserRetry(t testing.TB,client*codersdk.Client,organizationIDs []uuid.UUID,retriesint,roles []rbac.RoleIdentifier,mutators...func(r*codersdk.CreateUserRequestWithOrgs)) (*codersdk.Client, codersdk.User) {
795795
req:= codersdk.CreateUserRequestWithOrgs{
796-
Email:namesgenerator.GetRandomName(10)+"@coder.com",
796+
Email:namesgenerator.GetRandomName()+"@coder.com",
797797
Username:RandomUsername(t),
798798
Name:RandomName(t),
799799
Password:"SomeSecurePassword!",
@@ -1561,7 +1561,7 @@ func RandomUsername(t testing.TB) string {
15611561
suffix,err:=cryptorand.String(3)
15621562
require.NoError(t,err)
15631563
suffix="-"+suffix
1564-
n:=strings.ReplaceAll(namesgenerator.GetRandomName(10),"_","-")+suffix
1564+
n:=namesgenerator.GetRandomNameHyphenated()+suffix
15651565
iflen(n)>32 {
15661566
n=n[:32-len(suffix)]+suffix
15671567
}
@@ -1571,7 +1571,7 @@ func RandomUsername(t testing.TB) string {
15711571
funcRandomName(t testing.TB)string {
15721572
varsb strings.Builder
15731573
varerrerror
1574-
ss:=strings.Split(namesgenerator.GetRandomName(10),"_")
1574+
ss:=strings.Split(namesgenerator.GetRandomName(),"_")
15751575
forsi,s:=rangess {
15761576
forri,r:=ranges {
15771577
ifri==0 {

‎coderd/taskname/taskname.go‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import (
1414

1515
"github.com/anthropics/anthropic-sdk-go"
1616
anthropicoption"github.com/anthropics/anthropic-sdk-go/option"
17-
"github.com/moby/moby/pkg/namesgenerator"
1817
"golang.org/x/xerrors"
1918

2019
"github.com/coder/aisdk-go"
20+
"github.com/coder/coder/v2/coderd/util/namesgenerator"
2121
strutil"github.com/coder/coder/v2/coderd/util/strings"
2222
"github.com/coder/coder/v2/codersdk"
2323
)
@@ -126,10 +126,7 @@ func generateFallback() TaskName {
126126
// We have a 32 character limit for the name.
127127
// We have a 5 character suffix `-ffff`.
128128
// This leaves us with 27 characters for the name.
129-
//
130-
// `namesgenerator.GetRandomName(0)` can generate names
131-
// up to 27 characters, but we truncate defensively.
132-
name:=strings.ReplaceAll(namesgenerator.GetRandomName(0),"_","-")
129+
name:=namesgenerator.GenerateHyphenated()
133130
name=name[:min(len(name),27)]
134131
name=strings.TrimSuffix(name,"-")
135132

‎coderd/templateversions.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616

1717
"github.com/go-chi/chi/v5"
1818
"github.com/google/uuid"
19-
"github.com/moby/moby/pkg/namesgenerator"
2019
"github.com/sqlc-dev/pqtype"
2120
"github.com/zclconf/go-cty/cty"
2221
"golang.org/x/xerrors"
@@ -39,6 +38,7 @@ import (
3938
"github.com/coder/coder/v2/coderd/rbac"
4039
"github.com/coder/coder/v2/coderd/rbac/policy"
4140
"github.com/coder/coder/v2/coderd/tracing"
41+
"github.com/coder/coder/v2/coderd/util/namesgenerator"
4242
"github.com/coder/coder/v2/coderd/util/ptr"
4343
"github.com/coder/coder/v2/codersdk"
4444
"github.com/coder/coder/v2/examples"
@@ -1700,7 +1700,7 @@ func (api *API) postTemplateVersionsByOrganization(rw http.ResponseWriter, r *ht
17001700
}
17011701

17021702
ifreq.Name=="" {
1703-
req.Name=namesgenerator.GetRandomName(1)
1703+
req.Name=namesgenerator.GetRandomName()
17041704
}
17051705

17061706
err=tx.InsertTemplateVersion(ctx, database.InsertTemplateVersionParams{

‎coderd/userauth.go‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,30 @@ import (
1919
"github.com/go-jose/go-jose/v4/jwt"
2020
"github.com/google/go-github/v43/github"
2121
"github.com/google/uuid"
22-
"github.com/moby/moby/pkg/namesgenerator"
2322
"golang.org/x/oauth2"
2423
"golang.org/x/xerrors"
2524

2625
"cdr.dev/slog"
2726

28-
"github.com/coder/coder/v2/coderd/cryptokeys"
29-
"github.com/coder/coder/v2/coderd/idpsync"
30-
"github.com/coder/coder/v2/coderd/jwtutils"
31-
"github.com/coder/coder/v2/coderd/telemetry"
32-
"github.com/coder/coder/v2/coderd/util/ptr"
33-
3427
"github.com/coder/coder/v2/coderd/apikey"
3528
"github.com/coder/coder/v2/coderd/audit"
29+
"github.com/coder/coder/v2/coderd/cryptokeys"
3630
"github.com/coder/coder/v2/coderd/database"
3731
"github.com/coder/coder/v2/coderd/database/dbauthz"
3832
"github.com/coder/coder/v2/coderd/database/dbtime"
3933
"github.com/coder/coder/v2/coderd/externalauth"
4034
"github.com/coder/coder/v2/coderd/httpapi"
4135
"github.com/coder/coder/v2/coderd/httpmw"
36+
"github.com/coder/coder/v2/coderd/idpsync"
37+
"github.com/coder/coder/v2/coderd/jwtutils"
4238
"github.com/coder/coder/v2/coderd/notifications"
4339
"github.com/coder/coder/v2/coderd/promoauth"
4440
"github.com/coder/coder/v2/coderd/rbac"
4541
"github.com/coder/coder/v2/coderd/render"
42+
"github.com/coder/coder/v2/coderd/telemetry"
4643
"github.com/coder/coder/v2/coderd/userpassword"
44+
"github.com/coder/coder/v2/coderd/util/namesgenerator"
45+
"github.com/coder/coder/v2/coderd/util/ptr"
4746
"github.com/coder/coder/v2/codersdk"
4847
"github.com/coder/coder/v2/cryptorand"
4948
)
@@ -1725,7 +1724,7 @@ func (api *API) oauthLogin(r *http.Request, params *oauthLoginParams) ([]*http.C
17251724
validUsernamebool
17261725
)
17271726
fori:=0;i<10;i++ {
1728-
alternate:=fmt.Sprintf("%s-%s",original,namesgenerator.GetRandomName(1))
1727+
alternate:=fmt.Sprintf("%s-%s",original,namesgenerator.GetRandomName())
17291728

17301729
params.Username=codersdk.UsernameFrom(alternate)
17311730

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp