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

[Hackathon] implement kontext client authorization #3892

Merged
merged 1 commit into from
Aug 8, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion js/modules/k6/experimental/kontext/kontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
// ErrKontextWrongType is the error returned when the type of the value in the kontext is not the expected one.
var ErrKontextWrongType = errors.New("wrong type")

const k6ServiceURLEnvironmentVariable = "K6_KONTEXT_SERVICE_URL"

Check failure on line 29 in js/modules/k6/experimental/kontext/kontext.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
const k6ServiceAuthEnvironmentVariable = "K6_KONTEXT_AUTH_TOKEN"
const secureEnvironmentVariable = "K6_KONTEXT_SECURE"
const testRunIDHeader = "X-Test-Run-ID"

Expand Down Expand Up @@ -339,7 +340,7 @@
return poppedValue, nil
}

func (lk *LocalKontext) Size(key string) (int64, error) {

Check warning on line 343 in js/modules/k6/experimental/kontext/kontext.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method LocalKontext.Size should have comment or be unexported (revive)
var size int64

err := lk.db.handle.View(func(tx *bolt.Tx) error {
Expand Down Expand Up @@ -372,11 +373,11 @@
return size, nil
}

func (lk *LocalKontext) Incr(key string) (int64, error) {

Check warning on line 376 in js/modules/k6/experimental/kontext/kontext.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method LocalKontext.Incr should have comment or be unexported (revive)
return lk.incrBy(key, 1)
}

func (lk *LocalKontext) Decr(key string) (int64, error) {

Check warning on line 380 in js/modules/k6/experimental/kontext/kontext.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method LocalKontext.Decr should have comment or be unexported (revive)
return lk.incrBy(key, -1)
}

Expand Down Expand Up @@ -447,8 +448,24 @@
// Ensure that CloudKontext implements the Kontexter interface.
var _ Kontexter = &CloudKontext{}

// kontextTokenAuth implements the credentials.PerRPCCredentials interface
// to send the authorization token to the k6-cloud-kontext server.
type kontextTokenAuth struct {
Token string
}

func (t *kontextTokenAuth) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {

Check warning on line 457 in js/modules/k6/experimental/kontext/kontext.go

View workflow job for this annotation

GitHub Actions / lint

unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
return map[string]string{
"authorization": "Bearer " + t.Token,
}, nil
}

func (t *kontextTokenAuth) RequireTransportSecurity() bool {
return true
}

// NewCloudKontext creates a new CloudKontext instance.
func NewCloudKontext(vu modules.VU, serviceURL string, secure bool, testRunID string) (*CloudKontext, error) {
func NewCloudKontext(vu modules.VU, serviceURL string, secure bool, testRunID string, authToken string) (*CloudKontext, error) {

Check failure on line 468 in js/modules/k6/experimental/kontext/kontext.go

View workflow job for this annotation

GitHub Actions / lint

line is 128 characters (lll)
// create a gRPC connection to the server
opts := []grpc.DialOption{}
if secure {
Expand All @@ -459,6 +476,10 @@
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
}

opts = append(opts, grpc.WithPerRPCCredentials(&kontextTokenAuth{
Token: authToken,
}))

conn, err := grpc.NewClient(serviceURL, opts...)
if err != nil {
return nil, fmt.Errorf("failed to dial gRPC server: %w", err)
Expand Down Expand Up @@ -615,7 +636,7 @@
return value, nil
}

func (c CloudKontext) Size(key string) (int64, error) {

Check warning on line 639 in js/modules/k6/experimental/kontext/kontext.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method CloudKontext.Size should have comment or be unexported (revive)
ctx := c.context()
response, err := c.client.Size(ctx, &proto.SizeRequest{Key: key})

Expand All @@ -630,14 +651,14 @@
}

return response.Count, nil

Check failure on line 654 in js/modules/k6/experimental/kontext/kontext.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
}

Check failure on line 655 in js/modules/k6/experimental/kontext/kontext.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary trailing newline (whitespace)

func (c CloudKontext) Incr(key string) (int64, error) {

Check warning on line 657 in js/modules/k6/experimental/kontext/kontext.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method CloudKontext.Incr should have comment or be unexported (revive)
return c.incrBy(key, 1)
}

func (c CloudKontext) Decr(key string) (int64, error) {

Check warning on line 661 in js/modules/k6/experimental/kontext/kontext.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported method CloudKontext.Decr should have comment or be unexported (revive)
return c.incrBy(key, -1)
}

Expand Down
14 changes: 9 additions & 5 deletions js/modules/k6/experimental/kontext/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,17 @@
common.Throw(mi.vu.Runtime(), fmt.Errorf("kontext instances can only be created in the init context"))
}

serviceURL, hasServiceURL := os.LookupEnv(k6ServiceURLEnvironmentVariable)
secure := strings.ToLower(os.Getenv(secureEnvironmentVariable)) != "false"

var kv Kontexter
var err error
if hasServiceURL {
kv, err = NewCloudKontext(mi.vu, serviceURL, secure, mi.rm.testRunID)

if serviceURL, hasServiceURL := os.LookupEnv(k6ServiceURLEnvironmentVariable); hasServiceURL {

Check failure on line 101 in js/modules/k6/experimental/kontext/module.go

View workflow job for this annotation

GitHub Actions / lint

use of `os.LookupEnv` forbidden because "Using anything except Signal and SyscallError from the os package is forbidden" (forbidigo)
kontextAuthToken := os.Getenv(k6ServiceAuthEnvironmentVariable)

Check failure on line 102 in js/modules/k6/experimental/kontext/module.go

View workflow job for this annotation

GitHub Actions / lint

use of `os.Getenv` forbidden because "Using anything except Signal and SyscallError from the os package is forbidden" (forbidigo)
if kontextAuthToken == "" {
common.Throw(mi.vu.Runtime(), fmt.Errorf("Kontext module is missing the kontext authToken"))
}
secure := strings.ToLower(os.Getenv(secureEnvironmentVariable)) != "false"

Check failure on line 106 in js/modules/k6/experimental/kontext/module.go

View workflow job for this annotation

GitHub Actions / lint

use of `os.Getenv` forbidden because "Using anything except Signal and SyscallError from the os package is forbidden" (forbidigo)

kv, err = NewCloudKontext(mi.vu, serviceURL, secure, mi.rm.testRunID, kontextAuthToken)
if err != nil {
common.Throw(mi.vu.Runtime(), fmt.Errorf("failed to create new Kontext instance: %w", err))
}
Expand Down
Loading