Skip to content

Commit d93dc1f

Browse files
henrybarretogustavosbarreto
authored andcommitted
feat(pkg): add configurable cloud and api services addresses
1 parent cbbe049 commit d93dc1f

File tree

9 files changed

+70
-47
lines changed

9 files changed

+70
-47
lines changed

api/services/device.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -408,12 +408,12 @@ func (s *service) mergeDevice(ctx context.Context, tenantID string, oldDevice *m
408408
// handleCloudBilling processes billing-related operations for Cloud environment.
409409
// This function has side effects: it may delete removed devices and report to billing.
410410
func (s *service) handleCloudBilling(ctx context.Context, namespace *models.Namespace) error {
411-
if namespace.Billing.IsActive() {
412-
if err := s.BillingReport(ctx, s.client, namespace.TenantID, ReportDeviceAccept); err != nil {
413-
return NewErrBillingReportNamespaceDelete(err)
414-
}
415-
} else {
416-
ok, err := s.BillingEvaluate(ctx, s.client, namespace.TenantID)
411+
if namespace.Billing.IsActive() {
412+
if err := s.BillingReport(ctx, s.client, namespace.TenantID, ReportDeviceAccept); err != nil {
413+
return NewErrBillingReportNamespaceDelete(err)
414+
}
415+
} else {
416+
ok, err := s.BillingEvaluate(ctx, s.client, namespace.TenantID)
417417
switch {
418418
case err != nil:
419419
return NewErrBillingEvaluate(err)

pkg/api/internalclient/billing.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func (c *client) BillingReport(ctx context.Context, tenant string, action string
2424
SetContext(ctx).
2525
SetHeader("X-Tenant-ID", tenant).
2626
SetQueryParam("action", action).
27-
Post("http://cloud:8080/internal/billing/report")
27+
Post(c.Config.EnterpriseBaseURL + "/internal/billing/report")
2828
if err != nil {
2929
return http.StatusInternalServerError, err
3030
}
@@ -40,7 +40,7 @@ func (c *client) BillingEvaluate(ctx context.Context, tenantID string) (*models.
4040
SetContext(ctx).
4141
SetHeader("X-Tenant-ID", tenantID).
4242
SetResult(&eval).
43-
Post("http://cloud:8080/internal/billing/evaluate")
43+
Post(c.Config.EnterpriseBaseURL + "/internal/billing/evaluate")
4444
if err != nil {
4545
return eval, resp.StatusCode(), err
4646
}

pkg/api/internalclient/client.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ type Config struct {
3030
// RetryMaxWaitTime defines the maximum wait time between retries.
3131
RetryMaxWaitTime time.Duration
3232

33-
// BaseURL defines the base URL for the API.
34-
BaseURL string
33+
// APIBaseURL defines the base URL for the API service.
34+
APIBaseURL string
35+
36+
// EnterpriseBaseURL defines the base URL for enterprise endpoints (cloud component).
37+
EnterpriseBaseURL string
3538
}
3639

3740
type client struct {
@@ -60,10 +63,11 @@ func NewClient(opts ...clientOption) (Client, error) {
6063
http: httpClient,
6164
Config: &Config{
6265
// NOTE: Default values can be overridden using the WithConfig option.
63-
RetryCount: 3,
64-
RetryWaitTime: 5 * time.Second,
65-
RetryMaxWaitTime: 20 * time.Second,
66-
BaseURL: "http://api:8080",
66+
RetryCount: 3,
67+
RetryWaitTime: 5 * time.Second,
68+
RetryMaxWaitTime: 20 * time.Second,
69+
APIBaseURL: "http://api:8080",
70+
EnterpriseBaseURL: "http://cloud:8080",
6771
},
6872
}
6973

@@ -77,7 +81,8 @@ func NewClient(opts ...clientOption) (Client, error) {
7781
httpClient.SetLogger(&LeveledLogger{c.logger})
7882
}
7983

80-
httpClient.SetBaseURL(c.Config.BaseURL)
84+
// NOTE: Avoid setting a global base URL on the Resty client. Calls to enterprise endpoints
85+
// will use c.Config.EnterpriseBaseURL explicitly when needed.
8186
httpClient.SetRetryCount(c.Config.RetryCount)
8287
httpClient.SetRetryWaitTime(c.Config.RetryWaitTime)
8388
httpClient.SetRetryMaxWaitTime(c.Config.RetryMaxWaitTime)

pkg/api/internalclient/device.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package internalclient
33
import (
44
"context"
55
"errors"
6-
"fmt"
76
"net/http"
87
"time"
98

@@ -38,7 +37,8 @@ func (c *client) DevicesOffline(ctx context.Context, uid string) error {
3837
_, err := c.http.
3938
R().
4039
SetContext(ctx).
41-
Post(fmt.Sprintf("/internal/devices/%s/offline", uid))
40+
SetPathParam("uid", uid).
41+
Post(c.Config.APIBaseURL + "/internal/devices/{uid}/offline")
4242
if err != nil {
4343
return err
4444
}
@@ -60,7 +60,7 @@ func (c *client) Lookup(ctx context.Context, lookup map[string]string) (string,
6060
SetContext(ctx).
6161
SetQueryParams(lookup).
6262
SetResult(&device).
63-
Get("/internal/lookup")
63+
Get(c.Config.APIBaseURL + "/internal/lookup")
6464

6565
if resp.StatusCode() != http.StatusOK {
6666
return "", []error{errors.New("lookup failed")}
@@ -77,7 +77,7 @@ func (c *client) DeviceLookup(ctx context.Context, tenantID, name string) (*mode
7777
SetQueryParam("tenant_id", tenantID).
7878
SetQueryParam("name", name).
7979
SetResult(&device).
80-
Get("/internal/device/lookup")
80+
Get(c.Config.APIBaseURL + "/internal/device/lookup")
8181
if err != nil {
8282
return nil, ErrConnectionFailed
8383
}
@@ -101,7 +101,7 @@ func (c *client) ListDevices(ctx context.Context) ([]models.Device, error) {
101101
R().
102102
SetContext(ctx).
103103
SetResult(list).
104-
Get("/api/devices")
104+
Get(c.Config.APIBaseURL + "/api/devices")
105105

106106
return list, err
107107
}
@@ -112,7 +112,7 @@ func (c *client) GetDevice(ctx context.Context, uid string) (*models.Device, err
112112
R().
113113
SetContext(ctx).
114114
SetResult(&device).
115-
Get(fmt.Sprintf("/api/devices/%s", uid))
115+
Get(c.Config.APIBaseURL + "/api/devices/{uid}")
116116
if err != nil {
117117
return nil, ErrConnectionFailed
118118
}
@@ -144,8 +144,9 @@ func (c *client) LookupWebEndpoints(ctx context.Context, address string) (*WebEn
144144
resp, err := c.http.
145145
R().
146146
SetContext(ctx).
147+
SetPathParam("address", address).
147148
SetResult(&tunnel).
148-
Get(fmt.Sprintf("http://cloud:8080/internal/web-endpoints/%s", address))
149+
Get(c.Config.EnterpriseBaseURL + "/internal/web-endpoints/{address}")
149150
if err != nil {
150151
return nil, ErrConnectionFailed
151152
}

pkg/api/internalclient/firewall.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func (c *client) FirewallEvaluate(ctx context.Context, lookup map[string]string)
2323
R().
2424
SetContext(ctx).
2525
SetQueryParams(lookup).
26-
Get("http://cloud:8080/internal/firewall/rules/evaluate")
26+
Get(c.Config.EnterpriseBaseURL + "/internal/firewall/rules/evaluate")
2727
if err != nil {
2828
return ErrFirewallConnection
2929
}

pkg/api/internalclient/mocks/internalclient.go

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/api/internalclient/namespace.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ func (c *client) NamespaceLookup(ctx context.Context, tenant string) (*models.Na
2424
res, err := c.http.
2525
R().
2626
SetContext(ctx).
27+
SetPathParam("tenant", tenant).
2728
SetResult(namespace).
28-
Get("/api/namespaces/" + tenant)
29+
Get(c.Config.APIBaseURL + "/api/namespaces/{tenant}")
2930
if err != nil {
3031
return nil, []error{err}
3132
}

pkg/api/internalclient/session.go

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package internalclient
33
import (
44
"context"
55
"errors"
6-
"fmt"
76
"net/http"
7+
"strconv"
8+
"strings"
89

910
"github.com/gorilla/websocket"
1011
"github.com/shellhub-io/shellhub/pkg/api/requests"
@@ -46,7 +47,7 @@ func (c *client) SessionCreate(ctx context.Context, session requests.SessionCrea
4647
R().
4748
SetContext(ctx).
4849
SetBody(session).
49-
Post("/internal/sessions")
50+
Post(c.Config.APIBaseURL + "/internal/sessions")
5051

5152
return err
5253
}
@@ -56,10 +57,11 @@ func (c *client) SessionAsAuthenticated(ctx context.Context, uid string) []error
5657
_, err := c.http.
5758
R().
5859
SetContext(ctx).
60+
SetPathParam("uid", uid).
5961
SetBody(&models.Status{
6062
Authenticated: true,
6163
}).
62-
Patch(fmt.Sprintf("/internal/sessions/%s", uid))
64+
Patch(c.Config.APIBaseURL + "/internal/sessions/{uid}")
6365
if err != nil {
6466
errors = append(errors, err)
6567
}
@@ -72,7 +74,8 @@ func (c *client) FinishSession(ctx context.Context, uid string) []error {
7274
_, err := c.http.
7375
R().
7476
SetContext(ctx).
75-
Post(fmt.Sprintf("/internal/sessions/%s/finish", uid))
77+
SetPathParam("uid", uid).
78+
Post(c.Config.APIBaseURL + "/internal/sessions/{uid}/finish")
7679
if err != nil {
7780
errors = append(errors, err)
7881
}
@@ -85,7 +88,8 @@ func (c *client) KeepAliveSession(ctx context.Context, uid string) []error {
8588
_, err := c.http.
8689
R().
8790
SetContext(ctx).
88-
Post(fmt.Sprintf("/internal/sessions/%s/keepalive", uid))
91+
SetPathParam("uid", uid).
92+
Post(c.Config.APIBaseURL + "/internal/sessions/{uid}/keepalive")
8993
if err != nil {
9094
errors = append(errors, err)
9195
}
@@ -101,7 +105,7 @@ func (c *client) UpdateSession(ctx context.Context, uid string, model *models.Se
101105
"tenant": uid,
102106
}).
103107
SetBody(model).
104-
Patch("/internal/sessions/{tenant}")
108+
Patch(c.Config.APIBaseURL + "/internal/sessions/{tenant}")
105109
if err != nil {
106110
return errors.Join(errors.New("failed to update the session due error"), err)
107111
}
@@ -114,14 +118,19 @@ func (c *client) UpdateSession(ctx context.Context, uid string, model *models.Se
114118
}
115119

116120
func (c *client) EventSessionStream(ctx context.Context, uid string) (*websocket.Conn, error) {
117-
connection, _, err := websocket.
118-
DefaultDialer.
119-
DialContext(
120-
ctx,
121-
fmt.Sprintf("ws://api:8080/internal/sessions/%s/events",
122-
uid,
123-
),
124-
nil)
121+
// Dial the enterprise events websocket. Convert configured enterprise HTTP scheme to ws(s).
122+
scheme := "ws"
123+
if strings.HasPrefix(c.Config.APIBaseURL, "https") {
124+
scheme = "wss"
125+
}
126+
127+
host := strings.TrimPrefix(strings.TrimPrefix(c.Config.APIBaseURL, "http://"), "https://")
128+
129+
connection, _, err := websocket.DefaultDialer.DialContext(
130+
ctx,
131+
scheme+"://"+host+"/internal/sessions/"+uid+"/events",
132+
nil,
133+
)
125134
if err != nil {
126135
return nil, err
127136
}
@@ -135,9 +144,9 @@ func (c *client) SaveSession(ctx context.Context, uid string, seat int) error {
135144
SetContext(ctx).
136145
SetPathParams(map[string]string{
137146
"uid": uid,
138-
"seat": fmt.Sprintf("%d", seat),
147+
"seat": strconv.Itoa(seat),
139148
}).
140-
Post("http://cloud:8080/internal/sessions/{uid}/records/{seat}")
149+
Post(c.Config.EnterpriseBaseURL + "/internal/sessions/{uid}/records/{seat}")
141150
if err != nil {
142151
return errors.Join(errors.New("failed to save the Asciinema file on Object Storage"), err)
143152
}
@@ -150,7 +159,7 @@ func (c *client) SaveSession(ctx context.Context, uid string, seat int) error {
150159
// represent an error.
151160
logrus.WithFields(logrus.Fields{
152161
"uid": uid,
153-
"seat": fmt.Sprintf("%d", seat),
162+
"seat": strconv.Itoa(seat),
154163
}).Debug("save session not acceptable")
155164

156165
return nil

pkg/api/internalclient/sshkey.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package internalclient
22

33
import (
44
"context"
5-
"fmt"
6-
75
"net/http"
86

97
"github.com/shellhub-io/shellhub/pkg/models"
@@ -27,8 +25,12 @@ func (c *client) GetPublicKey(ctx context.Context, fingerprint, tenant string) (
2725
resp, err := c.http.
2826
R().
2927
SetContext(ctx).
28+
SetPathParams(map[string]string{
29+
"fingerprint": fingerprint,
30+
"tenant": tenant,
31+
}).
3032
SetResult(&pubKey).
31-
Get(fmt.Sprintf("/internal/sshkeys/public-keys/%s/%s", fingerprint, tenant))
33+
Get(c.Config.APIBaseURL + "/internal/sshkeys/public-keys/{fingerprint}/{tenant}")
3234
if err != nil {
3335
return nil, err
3436
}
@@ -46,9 +48,13 @@ func (c *client) EvaluateKey(ctx context.Context, fingerprint string, dev *model
4648
resp, err := c.http.
4749
R().
4850
SetContext(ctx).
51+
SetPathParams(map[string]string{
52+
"fingerprint": fingerprint,
53+
"username": username,
54+
}).
4955
SetBody(dev).
5056
SetResult(&evaluate).
51-
Post(fmt.Sprintf("/internal/sshkeys/public-keys/evaluate/%s/%s", fingerprint, username))
57+
Post(c.Config.APIBaseURL + "/internal/sshkeys/public-keys/evaluate/{fingerprint}/{username}")
5258
if err != nil {
5359
return false, err
5460
}
@@ -67,7 +73,7 @@ func (c *client) CreatePrivateKey(ctx context.Context) (*models.PrivateKey, erro
6773
R().
6874
SetContext(ctx).
6975
SetResult(&privKey).
70-
Post("/internal/sshkeys/private-keys")
76+
Post(c.Config.APIBaseURL + "/internal/sshkeys/private-keys")
7177
if err != nil {
7278
return nil, err
7379
}

0 commit comments

Comments
 (0)