Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit 29d156b

Browse files
authored
feat/enterpiseportal: add checks bypass toggle as escape hatch (#64517)
The existing handler has a similar escape hatch - when enabled, all checks return healthy. ## Test plan unit tests
1 parent f129d66 commit 29d156b

File tree

6 files changed

+175
-18
lines changed

6 files changed

+175
-18
lines changed

cmd/enterprise-portal/internal/subscriptionlicensechecksservice/mocks_test.go

Lines changed: 117 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/enterprise-portal/internal/subscriptionlicensechecksservice/v1.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,19 @@ func (h *handlerV1) CheckLicenseKey(ctx context.Context, req *connect.Request[su
6060
errors.New("instance_id is required"))
6161
}
6262

63+
tr := trace.FromContext(ctx)
6364
logger := trace.Logger(ctx, h.logger).With(
6465
log.String("instanceID", instanceID))
6566

66-
tr := trace.FromContext(ctx)
67+
if h.store.BypassAllLicenseChecks() {
68+
logger.Warn("bypassing license check")
69+
tr.SetAttributes(attribute.Bool("bypass", true))
70+
return &connect.Response[subscriptionlicensechecksv1.CheckLicenseKeyResponse]{
71+
Msg: &subscriptionlicensechecksv1.CheckLicenseKeyResponse{
72+
Valid: true,
73+
},
74+
}, nil
75+
}
6776

6877
// HACK: For back-compat with old license check, try to look for a format
6978
// that looks like a license key hash token. Remove in Sourcegraph 5.8

cmd/enterprise-portal/internal/subscriptionlicensechecksservice/v1_store.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ import (
2222
type StoreV1 interface {
2323
Now() time.Time
2424

25+
// BypassAllLicenseChecks, if true, indicates that all license checks should
26+
// return valid. It is an escape hatch to ensure nobody is bricked in an
27+
// incident.
28+
BypassAllLicenseChecks() bool
29+
2530
// GetByLicenseKey returns the SubscriptionLicense with the given license key.
2631
// If no such SubscriptionLicense exists, it returns (nil, nil).
2732
//
@@ -48,29 +53,37 @@ type NewStoreV1Options struct {
4853
// LicenseKeySigner is the SSH signer to use for signing license keys. It is
4954
// used here for validation only.
5055
LicenseKeySigner ssh.Signer
56+
// BypassAllLicenseChecks, if true, indicates that all license checks should
57+
// return valid. It is an escape hatch to ensure nobody is bricked in an
58+
// incident.
59+
BypassAllLicenseChecks bool
5160
}
5261

5362
// NewStoreV1 returns a new StoreV1 using the given resource handles.
5463
func NewStoreV1(logger log.Logger, opts NewStoreV1Options) StoreV1 {
5564
return &storeV1{
56-
logger: logger,
57-
licenses: opts.DB.Subscriptions().Licenses(),
58-
subscriptions: opts.DB.Subscriptions(),
59-
slackWebhookURL: opts.SlackWebhookURL,
60-
licensePublicKey: opts.LicenseKeySigner.PublicKey(),
65+
logger: logger,
66+
licenses: opts.DB.Subscriptions().Licenses(),
67+
subscriptions: opts.DB.Subscriptions(),
68+
slackWebhookURL: opts.SlackWebhookURL,
69+
licensePublicKey: opts.LicenseKeySigner.PublicKey(),
70+
bypassAllLicenseChecks: opts.BypassAllLicenseChecks,
6171
}
6272
}
6373

6474
type storeV1 struct {
65-
logger log.Logger
66-
licenses *subscriptions.LicensesStore
67-
subscriptions *subscriptions.Store
68-
slackWebhookURL *string
69-
licensePublicKey ssh.PublicKey
75+
logger log.Logger
76+
licenses *subscriptions.LicensesStore
77+
subscriptions *subscriptions.Store
78+
slackWebhookURL *string
79+
licensePublicKey ssh.PublicKey
80+
bypassAllLicenseChecks bool
7081
}
7182

7283
func (s *storeV1) Now() time.Time { return time.Now() }
7384

85+
func (s *storeV1) BypassAllLicenseChecks() bool { return s.bypassAllLicenseChecks }
86+
7487
var errInvalidLicensekey = errors.New("key is invalid")
7588

7689
func (s *storeV1) GetByLicenseKey(ctx context.Context, licenseKey string) (*subscriptions.SubscriptionLicense, error) {

cmd/enterprise-portal/internal/subscriptionlicensechecksservice/v1_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,24 @@ func TestCheckLicenseKey(t *testing.T) {
100100
})
101101

102102
for _, tc := range []struct {
103-
name string
104-
req *subscriptionlicensechecksv1.CheckLicenseKeyRequest
103+
name string
104+
req *subscriptionlicensechecksv1.CheckLicenseKeyRequest
105+
bypass bool
106+
105107
wantResult autogold.Value
106108
wantErr autogold.Value
107109

108110
wantSetDetectedInstance autogold.Value
109111
wantPostToSlack autogold.Value
110112
}{{
113+
name: "bypass enabled",
114+
req: &subscriptionlicensechecksv1.CheckLicenseKeyRequest{
115+
InstanceId: "instance-id",
116+
LicenseKey: "license-key",
117+
},
118+
bypass: true,
119+
wantResult: autogold.Expect(map[string]interface{}{"reason": "", "valid": true}),
120+
}, {
111121
name: "instance_id required",
112122
req: &subscriptionlicensechecksv1.CheckLicenseKeyRequest{
113123
InstanceId: "",
@@ -174,6 +184,7 @@ func TestCheckLicenseKey(t *testing.T) {
174184
// Clone the underlying mock store, to avoid polluting other test
175185
// cases
176186
store := NewMockStoreV1From(store)
187+
store.BypassAllLicenseChecksFunc.SetDefaultReturn(tc.bypass)
177188

178189
h := &handlerV1{
179190
logger: logtest.Scoped(t),

cmd/enterprise-portal/service/config.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ type Config struct {
4343
RequiredTags []string
4444
}
4545

46-
SubscriptionLicenseChecksSlackWebhookURL *string
46+
SubscriptionLicenseChecks struct {
47+
BypassAllChecks bool
48+
SlackWebhookURL *string
49+
}
4750

4851
LicenseExpirationChecker licenseexpiration.Config
4952

@@ -117,7 +120,10 @@ func (c *Config) Load(env *runtime.Env) {
117120
return strings.Split(*tags, ",")
118121
}()
119122

120-
c.SubscriptionLicenseChecksSlackWebhookURL = env.GetOptional(
123+
c.SubscriptionLicenseChecks.BypassAllChecks = env.GetBool(
124+
"SUBSCRIPTION_LICENSE_CHECKS_BYPASS_ALL_CHECKS", "false",
125+
"Set to true to bypass all checks for subscription licenses.")
126+
c.SubscriptionLicenseChecks.SlackWebhookURL = env.GetOptional(
121127
"SUBSCRIPTION_LICENSE_CHECKS_SLACK_WEBHOOK_URL",
122128
"Destination webhook for subscription license check messages. If not set, messages are logged.")
123129

cmd/enterprise-portal/service/service.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,10 @@ func (Service) Initialize(ctx context.Context, logger log.Logger, contract runti
139139
subscriptionlicensechecksservice.NewStoreV1(
140140
logger,
141141
subscriptionlicensechecksservice.NewStoreV1Options{
142-
DB: dbHandle,
143-
SlackWebhookURL: config.SubscriptionLicenseChecksSlackWebhookURL,
144-
LicenseKeySigner: config.LicenseKeys.Signer,
142+
DB: dbHandle,
143+
SlackWebhookURL: config.SubscriptionLicenseChecks.SlackWebhookURL,
144+
LicenseKeySigner: config.LicenseKeys.Signer,
145+
BypassAllLicenseChecks: config.SubscriptionLicenseChecks.BypassAllChecks,
145146
},
146147
),
147148
connect.WithInterceptors(otelConnectInterceptor),

0 commit comments

Comments
 (0)