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

fix: cache the instance config and api key #1775

Merged
merged 2 commits into from
Nov 14, 2023
Merged
Changes from all commits
Commits
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
52 changes: 42 additions & 10 deletions enclave-manager/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"net/url"
"strings"
"sync"
"time"

"connectrpc.com/connect"
Expand Down Expand Up @@ -38,8 +39,12 @@ type Authentication struct {
}

type WebServer struct {
instanceConfigMutex *sync.RWMutex
apiKeyMutex *sync.RWMutex
engineServiceClient *kurtosis_engine_rpc_api_bindingsconnect.EngineServiceClient
enforceAuth bool
instanceConfig *kurtosis_backend_server_rpc_api_bindings.GetCloudInstanceConfigResponse
apiKey *string
}

func NewWebserver(enforceAuth bool) (*WebServer, error) {
Expand All @@ -50,6 +55,8 @@ func NewWebserver(enforceAuth bool) (*WebServer, error) {
return &WebServer{
engineServiceClient: &engineServiceClient,
enforceAuth: enforceAuth,
instanceConfigMutex: &sync.RWMutex{},
apiKeyMutex: &sync.RWMutex{},
}, nil
}

Expand Down Expand Up @@ -408,6 +415,15 @@ func (c *WebServer) GetCloudInstanceConfig(
ctx context.Context,
apiKey string,
) (*kurtosis_backend_server_rpc_api_bindings.GetCloudInstanceConfigResponse, error) {
// Check if we have already fetched the instance config, if so return the cache
if c.instanceConfig != nil {
return c.instanceConfig, nil
}

// We have not yet fetched the instance configuration, so we write lock, make the external call and cache the result
c.instanceConfigMutex.Lock()
defer c.instanceConfigMutex.Unlock()

client, err := c.createKurtosisCloudBackendClient(
kurtosisCloudApiHost,
kurtosisCloudApiPort,
Expand All @@ -420,6 +436,7 @@ func (c *WebServer) GetCloudInstanceConfig(
ApiKey: apiKey,
},
}

getInstanceResponse, err := (*client).GetOrCreateInstance(ctx, getInstanceRequest)
if err != nil {
return nil, stacktrace.Propagate(err, "Failed to get the instance")
Expand All @@ -435,6 +452,8 @@ func (c *WebServer) GetCloudInstanceConfig(
return nil, stacktrace.Propagate(err, "Failed to get the instance config")
}

c.instanceConfig = getInstanceConfigResponse.Msg

return getInstanceConfigResponse.Msg, nil
}

Expand All @@ -454,20 +473,33 @@ func (c *WebServer) ConvertJwtTokenToApiKey(
AccessToken: jwtToken,
},
}
result, err := (*client).GetOrCreateApiKey(ctx, request)
if err != nil {
return nil, stacktrace.Propagate(err, "Failed to get the API key")
}

if result == nil {
// User does not have an API key (unlikely if valid jwt token)
return nil, stacktrace.NewError("User does not have an API key assigned")
}
if len(result.Msg.ApiKey) > 0 {
if c.apiKey != nil {
return &Authentication{
ApiKey: result.Msg.ApiKey,
ApiKey: *c.apiKey,
JwtToken: jwtToken,
}, nil
} else {
c.apiKeyMutex.Lock()
defer c.apiKeyMutex.Unlock()

result, err := (*client).GetOrCreateApiKey(ctx, request)
if err != nil {
return nil, stacktrace.Propagate(err, "Failed to get the API key")
}

if result == nil {
// User does not have an API key (not really possible if they have a valid jwt token)
return nil, stacktrace.NewError("User does not have an API key assigned")
}

if len(result.Msg.ApiKey) > 0 {
c.apiKey = &result.Msg.ApiKey
return &Authentication{
ApiKey: result.Msg.ApiKey,
JwtToken: jwtToken,
}, nil
}
}

return nil, stacktrace.NewError("an empty API key was returned from Kurtosis Cloud Backend")
Expand Down