Skip to content

Commit

Permalink
fix: look up apikey by JWT token to avoid any random jwt token to acc…
Browse files Browse the repository at this point in the history
…ess the API key
  • Loading branch information
Anders Schwartz committed Dec 1, 2023
1 parent f4e87ec commit e0bb1f1
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions enclave-manager/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type WebServer struct {
engineServiceClient *kurtosis_engine_rpc_api_bindingsconnect.EngineServiceClient
enforceAuth bool
instanceConfig *kurtosis_backend_server_rpc_api_bindings.GetCloudInstanceConfigResponse
apiKey *string
apiKeyMap map[string]*string
}

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

Expand Down Expand Up @@ -468,21 +469,21 @@ func (c *WebServer) ConvertJwtTokenToApiKey(
if err != nil {
return nil, stacktrace.Propagate(err, "Failed to create the Cloud backend client")
}
request := &connect.Request[kurtosis_backend_server_rpc_api_bindings.GetOrCreateApiKeyRequest]{
Msg: &kurtosis_backend_server_rpc_api_bindings.GetOrCreateApiKeyRequest{
AccessToken: jwtToken,
},
}

if c.apiKey != nil {
if c.apiKeyMap[jwtToken] != nil {
return &Authentication{
ApiKey: *c.apiKey,
ApiKey: *c.apiKeyMap[jwtToken],
JwtToken: jwtToken,
}, nil
} else {
c.apiKeyMutex.Lock()
defer c.apiKeyMutex.Unlock()

request := &connect.Request[kurtosis_backend_server_rpc_api_bindings.GetOrCreateApiKeyRequest]{
Msg: &kurtosis_backend_server_rpc_api_bindings.GetOrCreateApiKeyRequest{
AccessToken: jwtToken,
},
}
result, err := (*client).GetOrCreateApiKey(ctx, request)
if err != nil {
return nil, stacktrace.Propagate(err, "Failed to get the API key")
Expand All @@ -494,7 +495,7 @@ func (c *WebServer) ConvertJwtTokenToApiKey(
}

if len(result.Msg.ApiKey) > 0 {
c.apiKey = &result.Msg.ApiKey
c.apiKeyMap[jwtToken] = &result.Msg.ApiKey
return &Authentication{
ApiKey: result.Msg.ApiKey,
JwtToken: jwtToken,
Expand Down

0 comments on commit e0bb1f1

Please sign in to comment.