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

PUT and POST APIs for tenant creation #779

Merged
merged 4 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
80 changes: 42 additions & 38 deletions internal/pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,44 +253,48 @@ func SetupAPIServer(lifecycle fx.Lifecycle, config config.Config, logger *zerolo
Handler: mux,
}

// initialize google profiler
profilerConfigMap := make(map[string]string, 0)
profilerConfigMap["type"] = config.GetString("ears.profiler.type")
profilerConfigMap["project_id"] = config.GetString("ears.profiler.project_id")
profilerConfigMap["private_key_id"] = config.GetString("ears.profiler.private_key_id")
profilerConfigMap["private_key"] = config.GetString("ears.profiler.private_key")
profilerConfigMap["client_email"] = config.GetString("ears.profiler.client_email")
profilerConfigMap["client_id"] = config.GetString("ears.profiler.client_id")
profilerConfigMap["auth_uri"] = config.GetString("ears.profiler.auth_uri")
profilerConfigMap["token_uri"] = config.GetString("ears.profiler.token_uri")
profilerConfigMap["auth_provider_x509_cert_url"] = config.GetString("ears.profiler.auth_provider_x509_cert_url")
profilerConfigMap["client_x509_cert_url"] = config.GetString("ears.profiler.client_x509_cert_url")
profilerConfigMap["universe_domain"] = config.GetString("ears.profiler.universe_domain")
buf, err := json.MarshalIndent(profilerConfigMap, "", "\t")
if err != nil {
logger.Error().Str("op", "SetupAPIServer.StartProfiler").Msg(err.Error())
}
err = os.WriteFile("google_profiler_config.json", buf, 0644)
if err != nil {
logger.Error().Str("op", "SetupAPIServer.StartProfiler").Msg(err.Error())
}
workingDir, err := os.Getwd()
if err != nil {
logger.Error().Str("op", "SetupAPIServer.StartProfiler").Msg(err.Error())
}
os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", filepath.Join(workingDir, "google_profiler_config.json"))
taskArn, err := GetTaskArn()
if err != nil {
logger.Error().Str("op", "SetupAPIServer.StartProfiler").Msg(err.Error())
}
cfg := profiler.Config{
Service: "ears",
ServiceVersion: app.Version,
ProjectID: "ears-project",
Zone: config.GetString("ears.env") + "-" + taskArn,
}
if err := profiler.Start(cfg); err != nil {
logger.Error().Str("op", "SetupAPIServer.StartProfiler").Msg(err.Error())
if config.GetString("ears.profiler.active") == "yes" {
// initialize google profiler
profilerConfigMap := make(map[string]string, 0)
profilerConfigMap["type"] = config.GetString("ears.profiler.type")
profilerConfigMap["project_id"] = config.GetString("ears.profiler.project_id")
profilerConfigMap["private_key_id"] = config.GetString("ears.profiler.private_key_id")
profilerConfigMap["private_key"] = config.GetString("ears.profiler.private_key")
profilerConfigMap["client_email"] = config.GetString("ears.profiler.client_email")
profilerConfigMap["client_id"] = config.GetString("ears.profiler.client_id")
profilerConfigMap["auth_uri"] = config.GetString("ears.profiler.auth_uri")
profilerConfigMap["token_uri"] = config.GetString("ears.profiler.token_uri")
profilerConfigMap["auth_provider_x509_cert_url"] = config.GetString("ears.profiler.auth_provider_x509_cert_url")
profilerConfigMap["client_x509_cert_url"] = config.GetString("ears.profiler.client_x509_cert_url")
profilerConfigMap["universe_domain"] = config.GetString("ears.profiler.universe_domain")
buf, err := json.MarshalIndent(profilerConfigMap, "", "\t")
if err != nil {
logger.Error().Str("op", "SetupAPIServer.StartProfiler").Msg(err.Error())
}
err = os.WriteFile("google_profiler_config.json", buf, 0644)
if err != nil {
logger.Error().Str("op", "SetupAPIServer.StartProfiler").Msg(err.Error())
}
workingDir, err := os.Getwd()
if err != nil {
logger.Error().Str("op", "SetupAPIServer.StartProfiler").Msg(err.Error())
}
os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", filepath.Join(workingDir, "google_profiler_config.json"))
taskArn, err := GetTaskArn()
if err != nil {
logger.Error().Str("op", "SetupAPIServer.StartProfiler").Msg(err.Error())
}
cfg := profiler.Config{
Service: "ears",
ServiceVersion: app.Version,
ProjectID: "ears-project",
Zone: config.GetString("ears.env") + "-" + taskArn,
}
if err := profiler.Start(cfg); err != nil {
logger.Error().Str("op", "SetupAPIServer.StartProfiler").Msg(err.Error())
}
} else {
logger.Info().Msg("Google profiler disabled")
}

// initialize event logger
Expand Down
67 changes: 59 additions & 8 deletions internal/pkg/app/handlers_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/global"
"go.opentelemetry.io/otel/trace"
"io/ioutil"
"io"
"net/http"
"regexp"
"strings"
Expand Down Expand Up @@ -175,12 +175,16 @@ func NewAPIManager(routingMgr tablemgr.RoutingTableManager, tenantStorer tenant.
api.muxRouter.HandleFunc("/ears/v1/orgs/{orgId}/applications/{appId}/fragments/{fragmentId}", api.getFragmentHandler).Methods(http.MethodGet)
api.muxRouter.HandleFunc("/ears/v1/orgs/{orgId}/applications/{appId}/fragments", api.getAllTenantFragmentsHandler).Methods(http.MethodGet)

// old tenant APIs for backward compatibility

api.muxRouter.HandleFunc("/ears/v1/orgs/{orgId}/applications/{appId}/config", api.getTenantConfigHandler).Methods(http.MethodGet)
api.muxRouter.HandleFunc("/ears/v1/orgs/{orgId}/applications/{appId}/config", api.addTenantConfigHandler).Methods(http.MethodPut)
api.muxRouter.HandleFunc("/ears/v1/orgs/{orgId}/applications/{appId}/config", api.updateTenantConfigHandler).Methods(http.MethodPut)
api.muxRouter.HandleFunc("/ears/v1/orgs/{orgId}/applications/{appId}/config", api.addTenantConfigHandler).Methods(http.MethodPost)
api.muxRouter.HandleFunc("/ears/v1/orgs/{orgId}/applications/{appId}/config", api.deleteTenantConfigHandler).Methods(http.MethodDelete)

api.muxRouter.HandleFunc("/ears/v1/orgs/{orgId}/applications/{appId}", api.getTenantConfigHandler).Methods(http.MethodGet)
api.muxRouter.HandleFunc("/ears/v1/orgs/{orgId}/applications/{appId}", api.addTenantConfigHandler).Methods(http.MethodPut)
api.muxRouter.HandleFunc("/ears/v1/orgs/{orgId}/applications/{appId}", api.updateTenantConfigHandler).Methods(http.MethodPut)
api.muxRouter.HandleFunc("/ears/v1/orgs/{orgId}/applications/{appId}", api.addTenantConfigHandler).Methods(http.MethodPost)
api.muxRouter.HandleFunc("/ears/v1/orgs/{orgId}/applications/{appId}", api.deleteTenantConfigHandler).Methods(http.MethodDelete)

api.muxRouter.HandleFunc("/ears/v1/routes", api.getAllRoutesHandler).Methods(http.MethodGet)
Expand Down Expand Up @@ -327,7 +331,7 @@ func (a *APIManager) webhookHandler(w http.ResponseWriter, r *http.Request) {
resp.Respond(ctx, w, doYaml(r))
return
}
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
log.Ctx(ctx).Error().Str("op", "webhookHandler").Msg(err.Error())
resp := ErrorResponse(&InternalServerError{err})
Expand Down Expand Up @@ -387,7 +391,7 @@ func (a *APIManager) sendEventHandler(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, maxEventSize)
defer r.Body.Close()
}
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
log.Ctx(ctx).Error().Str("op", "sendEventHandler").Msg(err.Error())
resp := ErrorResponse(&InternalServerError{err})
Expand Down Expand Up @@ -506,7 +510,7 @@ func (a *APIManager) addRouteHandler(w http.ResponseWriter, r *http.Request) {
return
}
routeId := vars["routeId"]
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
log.Ctx(ctx).Error().Str("op", "addRouteHandler").Msg(err.Error())
a.addRouteFailureRecorder.Add(ctx, 1.0)
Expand Down Expand Up @@ -872,7 +876,7 @@ func (a *APIManager) addFragmentHandler(w http.ResponseWriter, r *http.Request)
return
}
fragmentId := vars["fragmentId"]
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
log.Ctx(ctx).Error().Str("op", "addFragmentHandler").Msg(err.Error())
a.addRouteFailureRecorder.Add(ctx, 1.0)
Expand Down Expand Up @@ -964,7 +968,15 @@ func (a *APIManager) addTenantConfigHandler(w http.ResponseWriter, r *http.Reque
resp.Respond(ctx, w, doYaml(r))
return
}
body, err := ioutil.ReadAll(r.Body)
config, err := a.tenantStorer.GetConfig(ctx, *tid)
if err == nil && config != nil {
err = errors.New("cannot update existing tenant " + tid.AppId)
log.Ctx(ctx).Error().Str("op", "addTenantConfigHandler").Str("error", err.Error()).Msg("tenant " + tid.AppId + "already exists")
resp := ErrorResponse(convertToApiError(ctx, err))
resp.Respond(ctx, w, doYaml(r))
return
}
body, err := io.ReadAll(r.Body)
if err != nil {
log.Ctx(ctx).Error().Str("op", "addTenantConfigHandler").Str("error", err.Error()).Msg("error reading request body")
resp := ErrorResponse(&InternalServerError{err})
Expand Down Expand Up @@ -993,6 +1005,45 @@ func (a *APIManager) addTenantConfigHandler(w http.ResponseWriter, r *http.Reque
resp.Respond(ctx, w, doYaml(r))
}

func (a *APIManager) updateTenantConfigHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
vars := mux.Vars(r)
tid, apiErr := getTenant(ctx, vars)
if apiErr != nil {
log.Ctx(ctx).Error().Str("op", "updateTenantConfigHandler").Str("error", apiErr.Error()).Msg("orgId or appId empty")
resp := ErrorResponse(apiErr)
resp.Respond(ctx, w, doYaml(r))
return
}
body, err := io.ReadAll(r.Body)
if err != nil {
log.Ctx(ctx).Error().Str("op", "updateTenantConfigHandler").Str("error", err.Error()).Msg("error reading request body")
resp := ErrorResponse(&InternalServerError{err})
resp.Respond(ctx, w, doYaml(r))
return
}
var tenantConfig tenant.Config
err = yaml.Unmarshal(body, &tenantConfig)
if err != nil {
log.Ctx(ctx).Error().Str("op", "updateTenantConfigHandler").Str("error", err.Error()).Msg("error unmarshal request body")
resp := ErrorResponse(&BadRequestError{"Cannot unmarshal request body", err})
resp.Respond(ctx, w, doYaml(r))
return
}
tenantConfig.Tenant = *tid
err = a.tenantStorer.SetConfig(ctx, tenantConfig)
if err != nil {
log.Ctx(ctx).Error().Str("op", "updateTenantConfigHandler").Str("error", err.Error()).Msg("error setting tenant config")
resp := ErrorResponse(convertToApiError(ctx, err))
resp.Respond(ctx, w, doYaml(r))
return
}
a.quotaManager.PublishQuota(ctx, *tid)
log.Ctx(ctx).Info().Str("op", "updateTenantConfigHandler").Msg("success")
resp := ItemResponse(tenantConfig)
resp.Respond(ctx, w, doYaml(r))
}

func (a *APIManager) deleteTenantConfigHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
vars := mux.Vars(r)
Expand Down
3 changes: 3 additions & 0 deletions internal/pkg/app/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ var eventUrlValidator = regexp.MustCompile(`^\/ears\/v1\/orgs\/[a-zA-Z0-9][a-zA-
func NewMiddleware(logger *zerolog.Logger, jwtManager jwt.JWTConsumer) []func(next http.Handler) http.Handler {
middlewareLogger = logger
jwtMgr = jwtManager
// for this extraction to work, X-B3-TraceId header must be present and exactly 32 byte hex and X-B3-SpanId
// must be present and exactly 16 byte hex
otelMiddleware := otelmux.Middleware("ears", otelmux.WithPropagators(b3.New()))
//otelMiddleware := otelmux.Middleware("ears", otelmux.WithPropagators(otel.GetTextMapPropagator()))
return []func(next http.Handler) http.Handler{
authenticateMiddleware,
otelMiddleware,
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/app/middlewares_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestAuthMiddleware(t *testing.T) {

w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/", nil)
r.Header.Set("X-B3-TraceId", "123456")
r.Header.Set(HeaderTraceId, "123456")

m.ServeHTTP(w, r.WithContext(subCtx))
}
12 changes: 9 additions & 3 deletions internal/pkg/appsecret/config_vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"time"
)

//ConfigVault provides secrets from ears app configuration
// ConfigVault provides secrets from ears app configuration
type ConfigVault struct {
config config.Config
}
Expand Down Expand Up @@ -139,13 +139,19 @@ func (v *TenantConfigVault) getSatBearerToken(ctx context.Context) (string, erro
if err != nil {
return "", err
}
if len(tenantConfig.ClientIds) == 0 {
clientId := ""
if tenantConfig.ClientId != "" {
clientId = tenantConfig.ClientId
} else if len(tenantConfig.ClientIds) > 0 {
clientId = tenantConfig.ClientIds[0]
}
if clientId == "" {
return "", errors.New("tenant has no client IDs")
}
if tenantConfig.ClientSecret == "" {
return "", errors.New("tenant has no client secret")
}
req.Header.Add("X-Client-Id", tenantConfig.ClientIds[0])
req.Header.Add("X-Client-Id", clientId)
req.Header.Add("X-Client-Secret", tenantConfig.ClientSecret)
req.Header.Add("Cache-Control", "no-cache")
resp, err := v.httpClient.Do(req)
Expand Down
9 changes: 4 additions & 5 deletions internal/pkg/tablemgr/routing_table_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,8 @@ func (r *DefaultRoutingTableManager) RouteEvent(ctx context.Context, tid tenant.
}
var wg sync.WaitGroup
wg.Add(1)
//sctx, cancel := context.WithTimeout(ctx, 5*time.Second)
//sctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
// no need to cancel context here because RouteEvent is only used synchronously via API call
//sctx, cancel := context.WithTimeout(ctx, 5*time.Second)
e, err := event.New(ctx, payload, event.WithAck(
func(evt event.Event) {
r.logger.Info().Str("op", "routeEventWebhook").Str("tid", tid.ToString()).Msg("success")
Expand All @@ -246,10 +245,10 @@ func (r *DefaultRoutingTableManager) RouteEvent(ctx context.Context, tid tenant.
event.WithTracePayloadOnNack(false),
)
if err != nil {
return "", errors.New("bad test event for route " + routeId)
return "", errors.New("invalid event for route " + routeId)
}
traceId, _, _ := e.GetPathValue("trace.id")
traceIdStr, _ := traceId.(string)
actualTraceId, _, _ := e.GetPathValue("trace.id")
traceIdStr, _ := actualTraceId.(string)
lrw.Receiver.Trigger(e)
wg.Wait()
return traceIdStr, nil
Expand Down
20 changes: 20 additions & 0 deletions pkg/app/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2021 Comcast Cable Communications Management, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package app

const (
HeaderTraceId = "X-B3-TraceId"
HeaderTenantId = "Application-Id"
)