Skip to content

Commit

Permalink
implement kontext client authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
d14c committed Aug 8, 2024
1 parent a2e0aeb commit 8b4f70d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
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 @@ -27,6 +27,7 @@ var ErrKontextKeyNotFound = errors.New("key not found")
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 @@ -447,8 +448,24 @@ type CloudKontext struct {
// 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 @@ func NewCloudKontext(vu modules.VU, serviceURL string, secure bool, testRunID st
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
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 @@ func (mi *ModuleInstance) NewKontext(_ sobek.ConstructorCall) *sobek.Object {
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

0 comments on commit 8b4f70d

Please sign in to comment.