Skip to content

Commit

Permalink
instance config also needs to be a map
Browse files Browse the repository at this point in the history
  • Loading branch information
Anders Schwartz committed Dec 1, 2023
1 parent e0bb1f1 commit ab18f0c
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions enclave-manager/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type WebServer struct {
engineServiceClient *kurtosis_engine_rpc_api_bindingsconnect.EngineServiceClient
enforceAuth bool
instanceConfig *kurtosis_backend_server_rpc_api_bindings.GetCloudInstanceConfigResponse
instanceConfigMap map[string]*kurtosis_backend_server_rpc_api_bindings.GetCloudInstanceConfigResponse
apiKeyMap map[string]*string
}

Expand All @@ -58,6 +59,7 @@ func NewWebserver(enforceAuth bool) (*WebServer, error) {
instanceConfigMutex: &sync.RWMutex{},
apiKeyMutex: &sync.RWMutex{},
apiKeyMap: map[string]*string{},
instanceConfigMap: map[string]*kurtosis_backend_server_rpc_api_bindings.GetCloudInstanceConfigResponse{},
}, nil
}

Expand Down Expand Up @@ -93,7 +95,7 @@ func (c *WebServer) ValidateRequestAuthorization(
return false, stacktrace.NewError("An internal error has occurred. An empty API key was found")
}

instanceConfig, err := c.GetCloudInstanceConfig(ctx, auth.ApiKey)
instanceConfig, err := c.GetCloudInstanceConfig(ctx, reqToken, auth.ApiKey)
if err != nil {
return false, stacktrace.Propagate(err, "Failed to retrieve the instance config")
}
Expand All @@ -104,7 +106,9 @@ func (c *WebServer) ValidateRequestAuthorization(
}
reqHost = splitHost[0]
if instanceConfig.LaunchResult.PublicDns != reqHost {
return false, stacktrace.NewError("Instance config public dns '%s' does not match the request host '%s'", instanceConfig.LaunchResult.PublicDns, reqHost)
delete(c.apiKeyMap, reqToken)
delete(c.instanceConfigMap, reqToken)
return false, stacktrace.NewError("either the requested instance does not exist or the user is not authorized to access the resource")
}

return true, nil
Expand Down Expand Up @@ -414,11 +418,12 @@ func (c *WebServer) createKurtosisCloudBackendClient(

func (c *WebServer) GetCloudInstanceConfig(
ctx context.Context,
jwtToken string,
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
if c.instanceConfigMap[jwtToken] != nil {
return c.instanceConfigMap[jwtToken], nil
}

// We have not yet fetched the instance configuration, so we write lock, make the external call and cache the result
Expand Down Expand Up @@ -453,7 +458,7 @@ func (c *WebServer) GetCloudInstanceConfig(
return nil, stacktrace.Propagate(err, "Failed to get the instance config")
}

c.instanceConfig = getInstanceConfigResponse.Msg
c.instanceConfigMap[jwtToken] = getInstanceConfigResponse.Msg

return getInstanceConfigResponse.Msg, nil
}
Expand Down

0 comments on commit ab18f0c

Please sign in to comment.