Skip to content
Open
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
7 changes: 4 additions & 3 deletions cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package cmd

import (
"context"
"fmt"
"os"
"strconv"
Expand Down Expand Up @@ -65,7 +66,7 @@ func NewImportCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command
// Create client with server address.
mc = connectors.NewMicrocksClient(globalClientOpts.ServerAddr)

keycloakURL, err := mc.GetKeycloakURL()
keycloakURL, err := mc.GetKeycloakURL(context.Background())
if err != nil {
fmt.Printf("Got error when invoking Microcks client retrieving config: %s", err)
os.Exit(1)
Expand All @@ -76,7 +77,7 @@ func NewImportCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command
// If Keycloak is enabled, retrieve an OAuth token using Keycloak Client.
kc := connectors.NewKeycloakClient(keycloakURL, globalClientOpts.ClientId, globalClientOpts.ClientSecret)

oauthToken, err = kc.ConnectAndGetToken()
oauthToken, err = kc.ConnectAndGetToken(context.Background())
if err != nil {
fmt.Printf("Got error when invoking Keycloack client: %s", err)
os.Exit(1)
Expand Down Expand Up @@ -132,7 +133,7 @@ func NewImportCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command
}

// Try uploading this artifact.
msg, err := mc.UploadArtifact(f, mainArtifact)
msg, err := mc.UploadArtifact(context.Background(), f, mainArtifact)
if err != nil {
fmt.Printf("Got error when invoking Microcks client importing Artifact: %s", err)
os.Exit(1)
Expand Down
9 changes: 5 additions & 4 deletions cmd/importDir.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package cmd

import (
"context"
"fmt"
"os"
"path/filepath"
Expand All @@ -28,7 +29,7 @@ import (

// MicrocksClient interface for dependency injection
type MicrocksClient interface {
UploadArtifact(file string, main bool) (string, error)
UploadArtifact(ctx context.Context, file string, main bool) (string, error)
}

type FileType struct {
Expand Down Expand Up @@ -159,7 +160,7 @@ func NewImportDirCommand(globalClientOpts *connectors.ClientOptions) *cobra.Comm
}

// Execute business logic
result, err := ImportDirectory(mc, fs, dirPath, importConfig)
result, err := ImportDirectory(context.Background(), mc, fs, dirPath, importConfig)
if err != nil {
if validationErr, ok := err.(*ValidationError); ok {
fmt.Println(validationErr.Message)
Expand Down Expand Up @@ -207,7 +208,7 @@ func NewImportDirCommand(globalClientOpts *connectors.ClientOptions) *cobra.Comm
return importDirCmd
}

func ImportDirectory(client MicrocksClient, fs FileSystem, dirPath string, config ImportConfig) (ImportResult, error) {
func ImportDirectory(ctx context.Context, client MicrocksClient, fs FileSystem, dirPath string, config ImportConfig) (ImportResult, error) {
if err := validateDirectory(fs, dirPath); err != nil {
return ImportResult{}, err
}
Expand All @@ -231,7 +232,7 @@ func ImportDirectory(client MicrocksClient, fs FileSystem, dirPath string, confi
for _, file := range files {
fileType := detectFileType(file)

msg, err := client.UploadArtifact(file, fileType.IsPrimary)
msg, err := client.UploadArtifact(ctx, file, fileType.IsPrimary)
if err != nil {
result.FailedCount++
result.FailedFiles = append(result.FailedFiles, file)
Expand Down
7 changes: 4 additions & 3 deletions cmd/importDir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package cmd

import (
"context"
"fmt"
"os"
"path/filepath"
Expand All @@ -33,7 +34,7 @@ type MockMicrocksClient struct {
UploadCalls int
}

func (m *MockMicrocksClient) UploadArtifact(file string, main bool) (string, error) {
func (m *MockMicrocksClient) UploadArtifact(ctx context.Context, file string, main bool) (string, error) {
m.UploadCalls++
m.Uploaded = append(m.Uploaded, file)

Expand Down Expand Up @@ -189,7 +190,7 @@ func TestImportDirectory(t *testing.T) {
}

// Execute
result, err := ImportDirectory(mockClient, mockFS, "/test", tt.config)
result, err := ImportDirectory(context.Background(), mockClient, mockFS, "/test", tt.config)

// Assertions
if tt.expectError {
Expand Down Expand Up @@ -417,7 +418,7 @@ func BenchmarkImportDirectory(b *testing.B) {

b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := ImportDirectory(mockClient, mockFS, "/test", config)
_, err := ImportDirectory(context.Background(), mockClient, mockFS, "/test", config)
require.NoError(b, err)
}
}
7 changes: 4 additions & 3 deletions cmd/importURL.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package cmd

import (
"context"
"fmt"
"os"
"strconv"
Expand Down Expand Up @@ -51,7 +52,7 @@ func NewImportURLCommand(globalClientOpts *connectors.ClientOptions) *cobra.Comm
// create client with server address
mc = connectors.NewMicrocksClient(globalClientOpts.ServerAddr)

keycloakURL, err := mc.GetKeycloakURL()
keycloakURL, err := mc.GetKeycloakURL(context.Background())
if err != nil {
fmt.Printf("Got error when invoking Microcks client retrieving config: %s", err)
os.Exit(1)
Expand All @@ -62,7 +63,7 @@ func NewImportURLCommand(globalClientOpts *connectors.ClientOptions) *cobra.Comm
// If Keycloak is enabled, retrieve an OAuth token using Keycloak Client.
kc := connectors.NewKeycloakClient(keycloakURL, globalClientOpts.ClientId, globalClientOpts.ClientSecret)

oauthToken, err = kc.ConnectAndGetToken()
oauthToken, err = kc.ConnectAndGetToken(context.Background())
if err != nil {
fmt.Printf("Got error when invoking Keycloack client: %s", err)
os.Exit(1)
Expand Down Expand Up @@ -118,7 +119,7 @@ func NewImportURLCommand(globalClientOpts *connectors.ClientOptions) *cobra.Comm
}

// Try downloading the artifcat
msg, err := mc.DownloadArtifact(f, mainArtifact, secret)
msg, err := mc.DownloadArtifact(context.Background(), f, mainArtifact, secret)
if err != nil {
fmt.Printf("Got error when invoking Microcks client importing Artifact: %s", err)
os.Exit(1)
Expand Down
10 changes: 5 additions & 5 deletions cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ microcks login http://localhost:8080 --sso --sso-launch-browser=false

server = args[0]
mc := connectors.NewMicrocksClient(server)
keycloakUrl, err := mc.GetKeycloakURL()
keycloakUrl, err := mc.GetKeycloakURL(ctx)

if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -120,14 +120,14 @@ microcks login http://localhost:8080 --sso --sso-launch-browser=false
os.Exit(1)
}
//Perform login and retrive tokens
authToken, refreshToken = passwordLogin(keycloakUrl, clientID, clientSecret, username, password)
authToken, refreshToken = passwordLogin(ctx, keycloakUrl, clientID, clientSecret, username, password)
authCfg.ClientId = clientID
authCfg.ClientSecret = clientSecret
} else {
httpClient := mc.HttpClient()
ctx = oidc.ClientContext(ctx, httpClient)
kc := connectors.NewKeycloakClient(keycloakUrl, "", "")
oauth2conf, err := kc.GetOIDCConfig()
oauth2conf, err := kc.GetOIDCConfig(ctx)
errors.CheckError(err)
authToken, refreshToken = oauth2login(ctx, ssoProt, oauth2conf, ssoLaunchBrowser)
authCfg.ClientId = "microcks-app-js"
Expand Down Expand Up @@ -308,11 +308,11 @@ func ssoAuthFlow(url string, ssoLaunchBrowser bool) {
}
}

func passwordLogin(keycloakURL, clientId, clientSecret, Username, Password string) (string, string) {
func passwordLogin(ctx context.Context, keycloakURL, clientId, clientSecret, Username, Password string) (string, string) {
kc := connectors.NewKeycloakClient(keycloakURL, clientId, clientSecret)
username, password := promptCredentials(Username, Password)

authToken, refreshToken, err := kc.ConnectAndGetTokenAndRefreshToken(username, password)
authToken, refreshToken, err := kc.ConnectAndGetTokenAndRefreshToken(ctx, username, password)

if err != nil {
panic(err)
Expand Down
9 changes: 5 additions & 4 deletions cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package cmd

import (
"context"
"fmt"
"os"
"strconv"
Expand Down Expand Up @@ -105,7 +106,7 @@ func NewTestCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command {
serverAddr = globalClientOpts.ServerAddr
mc = connectors.NewMicrocksClient(serverAddr)

keycloakURL, err := mc.GetKeycloakURL()
keycloakURL, err := mc.GetKeycloakURL(context.Background())
if err != nil {
fmt.Printf("Got error when invoking Microcks client retrieving config: %s", err)
os.Exit(1)
Expand All @@ -116,7 +117,7 @@ func NewTestCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command {
// If Keycloak is enabled, retrieve an OAuth token using Keycloak Client.
kc := connectors.NewKeycloakClient(keycloakURL, globalClientOpts.ClientId, globalClientOpts.ClientSecret)

oauthToken, err = kc.ConnectAndGetToken()
oauthToken, err = kc.ConnectAndGetToken(context.Background())
if err != nil {
fmt.Printf("Got error when invoking Keycloack client: %s", err)
os.Exit(1)
Expand Down Expand Up @@ -156,7 +157,7 @@ func NewTestCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command {
}

var testResultID string
testResultID, err := mc.CreateTestResult(serviceRef, testEndpoint, runnerType, secretName, waitForMilliseconds, filteredOperations, operationsHeaders, oAuth2Context)
testResultID, err := mc.CreateTestResult(context.Background(), serviceRef, testEndpoint, runnerType, secretName, waitForMilliseconds, filteredOperations, operationsHeaders, oAuth2Context)
if err != nil {
fmt.Printf("Got error when invoking Microcks client creating Test: %s", err)
os.Exit(1)
Expand All @@ -172,7 +173,7 @@ func NewTestCommand(globalClientOpts *connectors.ClientOptions) *cobra.Command {

var success = false
for nowInMilliseconds() < future {
testResultSummary, err := mc.GetTestResult(testResultID)
testResultSummary, err := mc.GetTestResult(context.Background(), testResultID)
if err != nil {
fmt.Printf("Got error when invoking Microcks client check TestResult: %s", err)
os.Exit(1)
Expand Down
19 changes: 10 additions & 9 deletions pkg/connectors/keycloak_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package connectors

import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
Expand All @@ -31,9 +32,9 @@ import (

// KeycloakClient defines methods for cinteracting with Keycloak
type KeycloakClient interface {
ConnectAndGetToken() (string, error)
ConnectAndGetTokenAndRefreshToken(string, string) (string, string, error)
GetOIDCConfig() (*oauth2.Config, error)
ConnectAndGetToken(ctx context.Context) (string, error)
ConnectAndGetTokenAndRefreshToken(ctx context.Context, username string, password string) (string, string, error)
GetOIDCConfig(ctx context.Context) (*oauth2.Config, error)
}

type keycloakClient struct {
Expand Down Expand Up @@ -69,11 +70,11 @@ func NewKeycloakClient(realmURL string, username string, password string) Keyclo
}

// ConnectAndGetToken implementation on keycloakClient structure
func (c *keycloakClient) ConnectAndGetToken() (string, error) {
func (c *keycloakClient) ConnectAndGetToken(ctx context.Context) (string, error) {
rel := &url.URL{Path: "protocol/openid-connect/token"}
u := c.BaseURL.ResolveReference(rel)

req, err := http.NewRequest("POST", u.String(), strings.NewReader(url.Values{"grant_type": {"client_credentials"}}.Encode()))
req, err := http.NewRequestWithContext(ctx, "POST", u.String(), strings.NewReader(url.Values{"grant_type": {"client_credentials"}}.Encode()))
if err != nil {
return "", err
}
Expand Down Expand Up @@ -109,12 +110,12 @@ func (c *keycloakClient) ConnectAndGetToken() (string, error) {
return accessToken, err
}

func (c *keycloakClient) GetOIDCConfig() (*oauth2.Config, error) {
func (c *keycloakClient) GetOIDCConfig(ctx context.Context) (*oauth2.Config, error) {
rel := &url.URL{Path: ".well-known/openid-configuration"}
u := c.BaseURL.ResolveReference(rel)

// Create HTTP request
req, err := http.NewRequest("GET", u.String(), nil)
req, err := http.NewRequestWithContext(ctx, "GET", u.String(), nil)
if err != nil {
fmt.Println("Error creating request:", err)
}
Expand Down Expand Up @@ -146,7 +147,7 @@ func (c *keycloakClient) GetOIDCConfig() (*oauth2.Config, error) {
}, nil
}

func (c *keycloakClient) ConnectAndGetTokenAndRefreshToken(username, password string) (string, string, error) {
func (c *keycloakClient) ConnectAndGetTokenAndRefreshToken(ctx context.Context, username, password string) (string, string, error) {

rel := &url.URL{Path: "protocol/openid-connect/token"}
u := c.BaseURL.ResolveReference(rel)
Expand All @@ -158,7 +159,7 @@ func (c *keycloakClient) ConnectAndGetTokenAndRefreshToken(username, password st
data.Set("password", password)
data.Set("grant_type", "password")
// Create HTTP request
req, err := http.NewRequest("POST", u.String(), bytes.NewBufferString(data.Encode()))
req, err := http.NewRequestWithContext(ctx, "POST", u.String(), bytes.NewBufferString(data.Encode()))
if err != nil {
fmt.Println("Error creating request:", err)
}
Expand Down
Loading