Skip to content

Commit 3d5cb5f

Browse files
committed
fix: make Platform configuration parameters optional (#204)
1 parent 7b2c323 commit 3d5cb5f

File tree

5 files changed

+46
-27
lines changed

5 files changed

+46
-27
lines changed

cmd/database-lab/main.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ func main() {
7777
log.Fatalf(err)
7878
}
7979

80-
// Create a platform service to verify Platform tokens.
81-
platformSvc := platform.New(cfg.Platform)
82-
if err := platformSvc.Init(ctx); err != nil {
80+
// Create a platform service to make requests to Platform.
81+
platformSvc, err := platform.New(ctx, cfg.Platform)
82+
if err != nil {
8383
log.Fatalf(errors.WithMessage(err, "failed to create a new platform service"))
8484
}
8585

@@ -98,7 +98,7 @@ func main() {
9898
for range c {
9999
log.Msg("Reloading configuration")
100100

101-
if err := reloadConfig(instanceID, provisionSvc, retrievalSvc, cloningSvc, platformSvc, server); err != nil {
101+
if err := reloadConfig(ctx, instanceID, provisionSvc, retrievalSvc, cloningSvc, platformSvc, server); err != nil {
102102
log.Err("Failed to reload configuration", err)
103103
}
104104

@@ -130,8 +130,8 @@ func loadConfiguration(instanceID string) (*config.Config, error) {
130130
return cfg, nil
131131
}
132132

133-
func reloadConfig(instanceID string, provisionSvc provision.Provision, retrievalSvc *retrieval.Retrieval, cloningSvc cloning.Cloning,
134-
platformSvc *platform.Service, server *srv.Server) error {
133+
func reloadConfig(ctx context.Context, instanceID string, provisionSvc provision.Provision, retrievalSvc *retrieval.Retrieval,
134+
cloningSvc cloning.Cloning, platformSvc *platform.Service, server *srv.Server) error {
135135
cfg, err := loadConfiguration(instanceID)
136136
if err != nil {
137137
return err
@@ -145,10 +145,15 @@ func reloadConfig(instanceID string, provisionSvc provision.Provision, retrieval
145145
return err
146146
}
147147

148+
newPlatformSvc, err := platform.New(ctx, cfg.Platform)
149+
if err != nil {
150+
return err
151+
}
152+
148153
provisionSvc.Reload(cfg.Provision)
149154
retrievalSvc.Reload(cfg)
150155
cloningSvc.Reload(cfg.Cloning)
151-
platformSvc.Reload(cfg.Platform)
156+
platformSvc.Reload(newPlatformSvc)
152157
server.Reload(cfg.Server)
153158

154159
return nil

pkg/client/platform/client.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ const (
2525
accessToken = "Access-Token"
2626
)
2727

28+
// ConfigValidationError represents a config validation error.
29+
type ConfigValidationError error
30+
2831
// APIResponse represents common fields of an API response.
2932
type APIResponse struct {
3033
Hint string `json:"hint"`
@@ -72,7 +75,7 @@ func NewClient(platformCfg ClientConfig) (*Client, error) {
7275

7376
func validateConfig(config ClientConfig) error {
7477
if config.URL == "" || config.AccessToken == "" {
75-
return errors.New("invalid config of Platform Client given: URL and AccessToken must not be empty")
78+
return ConfigValidationError(errors.New("invalid config of Platform Client given: URL and AccessToken must not be empty"))
7679
}
7780

7881
return nil

pkg/services/platform/platform.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/pkg/errors"
1212

1313
"gitlab.com/postgres-ai/database-lab/pkg/client/platform"
14+
"gitlab.com/postgres-ai/database-lab/pkg/log"
1415
)
1516

1617
// PersonalTokenVerifier declares an interface of a struct for Platform Personal Token verification.
@@ -34,45 +35,45 @@ type Service struct {
3435
}
3536

3637
// New creates a new platform service.
37-
func New(cfg Config) *Service {
38-
return &Service{
39-
cfg: cfg,
40-
}
41-
}
42-
43-
// Reload reloads service configuration.
44-
func (s *Service) Reload(cfg Config) {
45-
s.cfg = cfg
46-
}
38+
func New(ctx context.Context, cfg Config) (*Service, error) {
39+
s := &Service{cfg: cfg}
4740

48-
// Init initialize a Platform service instance.
49-
func (s *Service) Init(ctx context.Context) error {
5041
client, err := platform.NewClient(platform.ClientConfig{
5142
URL: s.cfg.URL,
5243
AccessToken: s.cfg.AccessToken,
5344
})
5445
if err != nil {
55-
return errors.Wrap(err, "failed to create a new Platform Client")
46+
if _, ok := err.(platform.ConfigValidationError); ok {
47+
log.Msg("Warning: ", err)
48+
return s, nil
49+
}
50+
51+
return nil, errors.Wrap(err, "failed to create a new Platform Client")
5652
}
5753

5854
s.Client = client
5955

6056
if !s.IsPersonalTokenEnabled() {
61-
return nil
57+
return s, nil
6258
}
6359

6460
platformToken, err := client.CheckPlatformToken(ctx, platform.TokenCheckRequest{Token: s.cfg.AccessToken})
6561
if err != nil {
66-
return err
62+
return nil, err
6763
}
6864

6965
if platformToken.OrganizationID == 0 {
70-
return errors.New("invalid organization ID associated with the given Platform Access Token")
66+
return nil, errors.New("invalid organization ID associated with the given Platform Access Token")
7167
}
7268

7369
s.organizationID = platformToken.OrganizationID
7470

75-
return nil
71+
return s, nil
72+
}
73+
74+
// Reload reloads service configuration.
75+
func (s *Service) Reload(newService *Service) {
76+
*s = *newService
7677
}
7778

7879
// IsAllowedToken checks if the Platform Personal Token is allowed.

pkg/srv/middlewares.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ func (a *authMW) isAccessAllowed(ctx context.Context, token string) bool {
4949
return true
5050
}
5151

52-
if a.personalTokenVerifier.IsPersonalTokenEnabled() && a.personalTokenVerifier.IsAllowedToken(ctx, token) {
52+
if a.personalTokenVerifier != nil && a.personalTokenVerifier.IsPersonalTokenEnabled() &&
53+
a.personalTokenVerifier.IsAllowedToken(ctx, token) {
5354
return true
5455
}
5556

pkg/srv/routes.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,14 @@ func (s *Server) resetClone(w http.ResponseWriter, r *http.Request) {
156156
}
157157

158158
func (s *Server) startObservation(w http.ResponseWriter, r *http.Request) {
159+
if s.Platform.Client == nil {
160+
sendBadRequestError(w, r, "cannot start the session observation because a Platform client is not configured")
161+
return
162+
}
163+
159164
var observationRequest *types.StartObservationRequest
160165
if err := readJSON(r, &observationRequest); err != nil {
161166
sendBadRequestError(w, r, err.Error())
162-
163167
return
164168
}
165169

@@ -207,6 +211,11 @@ func (s *Server) startObservation(w http.ResponseWriter, r *http.Request) {
207211
}
208212

209213
func (s *Server) stopObservation(w http.ResponseWriter, r *http.Request) {
214+
if s.Platform.Client == nil {
215+
sendBadRequestError(w, r, "cannot stop the session observation because a Platform client is not configured")
216+
return
217+
}
218+
210219
var observationRequest *types.StopObservationRequest
211220

212221
if err := readJSON(r, &observationRequest); err != nil {

0 commit comments

Comments
 (0)