Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 13a4fd0

Browse files
committedMar 18, 2025··
fix team API key seeding
1 parent 6fada30 commit 13a4fd0

File tree

9 files changed

+32
-17
lines changed

9 files changed

+32
-17
lines changed
 

‎packages/api/internal/auth/constants.go

-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,3 @@ package auth
22

33
const TeamContextKey string = "team"
44
const UserIDContextKey string = "user_id"
5-
6-
const ApiKeyPrefix = "e2b_"
7-
const AccessTokenPrefix = "sk_e2b_"

‎packages/api/internal/handlers/accesstoken.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"github.com/google/uuid"
1010

1111
"github.com/e2b-dev/infra/packages/api/internal/api"
12-
"github.com/e2b-dev/infra/packages/api/internal/auth"
1312
"github.com/e2b-dev/infra/packages/api/internal/utils"
13+
"github.com/e2b-dev/infra/packages/shared/pkg/keys"
1414
"github.com/e2b-dev/infra/packages/shared/pkg/models/accesstoken"
1515
"github.com/e2b-dev/infra/packages/shared/pkg/telemetry"
1616
)
@@ -30,7 +30,7 @@ func (a *APIStore) PostAccesstokens(c *gin.Context) {
3030
return
3131
}
3232

33-
accessToken, err := auth.GenerateKey(auth.AccessTokenPrefix)
33+
accessToken, err := keys.GenerateKey(keys.AccessTokenPrefix)
3434
if err != nil {
3535
a.sendAPIStoreError(c, http.StatusInternalServerError, fmt.Sprintf("Error when generating access token: %s", err))
3636

‎packages/api/internal/handlers/apikey.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import (
1111
"github.com/google/uuid"
1212

1313
"github.com/e2b-dev/infra/packages/api/internal/api"
14-
"github.com/e2b-dev/infra/packages/api/internal/auth"
1514
"github.com/e2b-dev/infra/packages/api/internal/team"
1615
"github.com/e2b-dev/infra/packages/api/internal/utils"
16+
"github.com/e2b-dev/infra/packages/shared/pkg/keys"
1717
"github.com/e2b-dev/infra/packages/shared/pkg/models/teamapikey"
1818
"github.com/e2b-dev/infra/packages/shared/pkg/telemetry"
1919
)
@@ -78,10 +78,10 @@ func (a *APIStore) GetApikeys(c *gin.Context) {
7878
}
7979
}
8080

81-
keyValue := strings.Split(apiKey.APIKey, auth.ApiKeyPrefix)[1]
81+
keyValue := strings.Split(apiKey.APIKey, keys.ApiKeyPrefix)[1]
8282

8383
// TODO: remove this once we migrate to hashed API keys
84-
KeyMask, err := auth.MaskKey(auth.ApiKeyPrefix, keyValue)
84+
KeyMask, err := keys.MaskKey(keys.ApiKeyPrefix, keyValue)
8585
if err != nil {
8686
fmt.Printf("masking API key failed %d: %v", apiKey.ID, err)
8787
continue

‎packages/api/internal/team/apikeys.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import (
77

88
"github.com/google/uuid"
99

10-
"github.com/e2b-dev/infra/packages/api/internal/auth"
1110
"github.com/e2b-dev/infra/packages/shared/pkg/db"
11+
"github.com/e2b-dev/infra/packages/shared/pkg/keys"
1212
"github.com/e2b-dev/infra/packages/shared/pkg/models"
1313
"github.com/e2b-dev/infra/packages/shared/pkg/telemetry"
1414
)
1515

1616
func CreateAPIKey(ctx context.Context, db *db.DB, teamID uuid.UUID, userID uuid.UUID, name string) (*models.TeamAPIKey, error) {
17-
teamApiKey, err := auth.GenerateKey(auth.ApiKeyPrefix)
17+
teamApiKey, err := keys.GenerateKey(keys.ApiKeyPrefix)
1818
if err != nil {
1919
errMsg := fmt.Errorf("error when generating team API key: %w", err)
2020
telemetry.ReportCriticalError(ctx, errMsg)

‎packages/shared/pkg/keys/constants.go

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package keys
2+
3+
const ApiKeyPrefix = "e2b_"
4+
const AccessTokenPrefix = "sk_e2b_"

‎packages/api/internal/auth/hashing.go ‎packages/shared/pkg/keys/hashing.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package auth
1+
package keys
22

33
type Hasher interface {
44
Hash(key []byte) string

‎packages/api/internal/auth/key.go ‎packages/shared/pkg/keys/key.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package auth
1+
package keys
22

33
import (
44
"crypto/rand"

‎packages/api/internal/auth/sha256.go ‎packages/shared/pkg/keys/sha256.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
package auth
1+
package keys
22

33
import (
44
"crypto/sha256"
55
"encoding/base64"
66
"fmt"
77
)
88

9-
type sha256Hashing struct {
9+
type Sha256Hashing struct {
1010
}
1111

12-
func NewSHA256Hashing() *sha256Hashing {
13-
return &sha256Hashing{}
12+
func NewSHA256Hashing() *Sha256Hashing {
13+
return &Sha256Hashing{}
1414
}
1515

16-
func (h *sha256Hashing) Hash(key []byte) string {
16+
func (h *Sha256Hashing) Hash(key []byte) string {
1717
hashBytes := sha256.Sum256(key)
1818

1919
hash64 := base64.RawStdEncoding.EncodeToString(hashBytes[:])

‎tests/integration/seed.go

+14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"encoding/hex"
56
"fmt"
67
"log"
78
"os"
@@ -10,6 +11,7 @@ import (
1011
"github.com/google/uuid"
1112

1213
"github.com/e2b-dev/infra/packages/shared/pkg/db"
14+
"github.com/e2b-dev/infra/packages/shared/pkg/keys"
1315
"github.com/e2b-dev/infra/packages/shared/pkg/models/envbuild"
1416
)
1517

@@ -83,9 +85,21 @@ func seed(db *db.DB, data SeedData) error {
8385
}
8486

8587
// Team API Key
88+
hasher := keys.NewSHA256Hashing()
89+
apiKeyBytes, err := hex.DecodeString(data.APIKey)
90+
if err != nil {
91+
return fmt.Errorf("failed to decode api key: %w", err)
92+
}
93+
hash := hasher.Hash(apiKeyBytes)
94+
mask, err := keys.MaskKey(keys.ApiKeyPrefix, data.APIKey)
95+
if err != nil {
96+
return fmt.Errorf("failed to mask api key: %w", err)
97+
}
8698
_, err = db.Client.TeamAPIKey.Create().
8799
SetTeam(t).
88100
SetAPIKey(data.APIKey).
101+
SetAPIKeyHash(hash).
102+
SetAPIKeyMask(mask).
89103
SetName("Integration Tests API Key").
90104
Save(ctx)
91105
if err != nil {

0 commit comments

Comments
 (0)
Please sign in to comment.