Skip to content

Commit

Permalink
Improve logging in docker reverse proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubno committed Oct 24, 2024
1 parent c6779bc commit b5cd1c6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
17 changes: 14 additions & 3 deletions packages/docker-reverse-proxy/internal/cache/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package cache

import (
"fmt"
"log"
"time"

"github.com/jellydator/ttlcache/v3"

"github.com/e2b-dev/infra/packages/docker-reverse-proxy/internal/utils"
)

const (
Expand Down Expand Up @@ -40,11 +43,19 @@ func (c *AuthCache) Get(e2bToken string) (*AccessTokenData, error) {
}

// Create creates a new auth token for the given templateID and accessToken and returns e2bToken
func (c *AuthCache) Create(userAccessToken, templateID, encodedDockerRegistryAccessToken string) {
func (c *AuthCache) Create(templateID, token string, expiresIn int) string {
// Get docker token from the actual registry for the scope
// Create a new e2b token for the user and store it in the cache
userToken := utils.GenerateRandomString(128)
jsonResponse := fmt.Sprintf(`{"token": "%s", "expires_in": %d}`, userToken, expiresIn)

data := &AccessTokenData{
AccessToken: encodedDockerRegistryAccessToken,
AccessToken: token,
TemplateID: templateID,
}

c.cache.Set(userAccessToken, data, authInfoExpiration)
c.cache.Set(userToken, data, authInfoExpiration)

log.Printf("Created new auth token for '%s' expiring in '%d'\n", templateID, expiresIn)
return jsonResponse
}
2 changes: 1 addition & 1 deletion packages/docker-reverse-proxy/internal/handlers/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (a *APIStore) Login(w http.ResponseWriter, r *http.Request) error {
w.Header().Set("Www-Authenticate", fmt.Sprintf("Bearer realm=\"https://docker.%s/v2/token\"", consts.Domain))
w.WriteHeader(http.StatusUnauthorized)

return fmt.Errorf("no authorization header")
return nil
}

authHeader := r.Header.Get("Authorization")
Expand Down
2 changes: 1 addition & 1 deletion packages/docker-reverse-proxy/internal/handlers/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (a *APIStore) Proxy(w http.ResponseWriter, req *http.Request) {

token, err := a.AuthCache.Get(e2bToken)
if err != nil {
log.Printf("Error while getting token for %s: %s\n", path, err)
log.Printf("Error while getting token for %s: %s, header: %s\n", path, err, authHeader)
w.WriteHeader(http.StatusUnauthorized)

return
Expand Down
7 changes: 1 addition & 6 deletions packages/docker-reverse-proxy/internal/handlers/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strings"

"github.com/e2b-dev/infra/packages/docker-reverse-proxy/internal/auth"
"github.com/e2b-dev/infra/packages/docker-reverse-proxy/internal/utils"
"github.com/e2b-dev/infra/packages/shared/pkg/consts"
)

Expand Down Expand Up @@ -103,11 +102,7 @@ func (a *APIStore) GetToken(w http.ResponseWriter, r *http.Request) error {
return fmt.Errorf("error while getting docker token: %s", err)
}

// Create a new e2b token for the user and store it in the cache
userToken := utils.GenerateRandomString(128)
jsonResponse := fmt.Sprintf(`{"token": "%s", "expires_in": %d}`, userToken, dockerToken.ExpiresIn)

a.AuthCache.Create(userToken, templateID, dockerToken.Token)
jsonResponse := a.AuthCache.Create(templateID, dockerToken.Token, dockerToken.ExpiresIn)

w.Header().Set("Content-Type", "application/json")
w.Write([]byte(jsonResponse))
Expand Down

0 comments on commit b5cd1c6

Please sign in to comment.