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

Remove temporary code and return an error instead of trying to send unauthenticated requests #230

Merged
merged 9 commits into from
Apr 10, 2024
31 changes: 20 additions & 11 deletions pkg/auth/authLogin.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,25 @@ func GetAuthenticatedAPIClient(
galasaHome utils.GalasaHome,
timeService utils.TimeService,
env utils.Environment,
) *galasaapi.APIClient {
) (*galasaapi.APIClient, error) {
bearerToken, err := GetBearerToken(apiServerUrl, fileSystem, galasaHome, timeService, env)

var apiClient *galasaapi.APIClient
if err == nil {
apiClient = api.InitialiseAuthenticatedAPI(apiServerUrl, bearerToken)
}
return apiClient, err
}

// Gets a locally-stored bearer token, or attempts to log in and retrieve a new bearer token if
// one does not already exist
func GetBearerToken(
apiServerUrl string,
fileSystem files.FileSystem,
galasaHome utils.GalasaHome,
timeService utils.TimeService,
env utils.Environment,
) (string, error) {
bearerToken, err := GetBearerTokenFromTokenJsonFile(fileSystem, galasaHome, timeService)
if err != nil {
// Attempt to log in
Expand All @@ -85,14 +103,5 @@ func GetAuthenticatedAPIClient(
bearerToken, err = GetBearerTokenFromTokenJsonFile(fileSystem, galasaHome, timeService)
}
}

var apiClient *galasaapi.APIClient
if err == nil {
apiClient = api.InitialiseAuthenticatedAPI(apiServerUrl, bearerToken)
} else {
// Temporary code to allow the CLI to continue running commands while authentication is being implemented.
// Remove this once authentication has been delivered and users can authenticate against an ecosystem
apiClient = api.InitialiseAPI(apiServerUrl)
}
return apiClient
return bearerToken, err
}
44 changes: 9 additions & 35 deletions pkg/auth/authLogin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,10 @@ func TestGetAuthenticatedAPIClientWithBearerTokenFileReturnsClient(t *testing.T)
mockFileSystem.WriteTextFile(bearerTokenFilePath, fmt.Sprintf(`{"jwt":"%s"}`, mockJwt))

// When...
apiClient := GetAuthenticatedAPIClient(apiServerUrl, mockFileSystem, mockGalasaHome, mockTimeService, mockEnvironment)
apiClient, err := GetAuthenticatedAPIClient(apiServerUrl, mockFileSystem, mockGalasaHome, mockTimeService, mockEnvironment)

// Then...
assert.Nil(t, err, "No error should have been thrown")
assert.NotNil(t, apiClient, "API client should not be nil")
}

Expand All @@ -233,13 +234,11 @@ func TestGetAuthenticatedAPIClientWithMissingBearerTokenFileAttemptsLogin(t *tes

galasactlPropertiesFilePath := mockGalasaHome.GetNativeFolderPath() + "/galasactl.properties"

mockClientId := "dummyId"
mockSecret := "shhhh"
mockRefreshToken := "abcdefg"
mockFileSystem.WriteTextFile(galasactlPropertiesFilePath, fmt.Sprintf(
"GALASA_CLIENT_ID=%s\n"+
"GALASA_SECRET=%s\n"+
"GALASA_ACCESS_TOKEN=%s", mockClientId, mockSecret, mockRefreshToken))
accessTokenValue := "abc"
clientIdValue := "dummyId"
tokenPropertyValue := accessTokenValue + TOKEN_SEPARATOR + clientIdValue

mockFileSystem.WriteTextFile(galasactlPropertiesFilePath, fmt.Sprintf("GALASA_TOKEN=%s", tokenPropertyValue))

// This is a dummy JWT that expires 1 hour after the Unix epoch
mockJwt := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJleHAiOjM2MDB9._j3Fchdx5IIqgGrdEGWXHxdgVyoBEyoD2-IBvhlxF1s"
Expand All @@ -251,35 +250,10 @@ func TestGetAuthenticatedAPIClientWithMissingBearerTokenFileAttemptsLogin(t *tes
apiServerUrl := server.URL

// When...
apiClient := GetAuthenticatedAPIClient(apiServerUrl, mockFileSystem, mockGalasaHome, mockTimeService, mockEnvironment)
apiClient, err := GetAuthenticatedAPIClient(apiServerUrl, mockFileSystem, mockGalasaHome, mockTimeService, mockEnvironment)

// Then...
assert.Nil(t, err, "No error should have been thrown")
assert.NotNil(t, apiClient, "API client should not be nil if the login was successful")
}

// Temporary test - remove once authentication is enforced
func TestGetAuthenticatedAPIClientWithUnavailableAPIContinuesWithoutToken(t *testing.T) {
// Given...
mockFileSystem := files.NewMockFileSystem()
mockEnvironment := utils.NewMockEnv()
mockGalasaHome, _ := utils.NewGalasaHome(mockFileSystem, mockEnvironment, "")
mockTimeService := utils.NewMockTimeService()

galasactlPropertiesFilePath := mockGalasaHome.GetNativeFolderPath() + "/galasactl.properties"

mockClientId := "dummyId"
mockRefreshToken := "abcdefg"
tokenPropertyValue := mockRefreshToken + TOKEN_SEPARATOR + mockClientId
mockFileSystem.WriteTextFile(galasactlPropertiesFilePath, fmt.Sprintf("GALASA_TOKEN=%s", tokenPropertyValue))

server := NewAuthServletMock(t, 500, "")
defer server.Close()

apiServerUrl := server.URL

// When...
apiClient := GetAuthenticatedAPIClient(apiServerUrl, mockFileSystem, mockGalasaHome, mockTimeService, mockEnvironment)

// Then...
assert.NotNil(t, apiClient, "API client should not be nil")
}
11 changes: 7 additions & 4 deletions pkg/cmd/propertiesDelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/galasa-dev/cli/pkg/api"
"github.com/galasa-dev/cli/pkg/auth"
"github.com/galasa-dev/cli/pkg/galasaapi"
"github.com/galasa-dev/cli/pkg/properties"
"github.com/galasa-dev/cli/pkg/utils"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -119,10 +120,12 @@ func (cmd *PropertiesDeleteCommand) executePropertiesDelete(factory Factory, pro
apiServerUrl := bootstrapData.ApiServerURL
log.Printf("The API server is at '%s'\n", apiServerUrl)

apiClient := auth.GetAuthenticatedAPIClient(apiServerUrl, fileSystem, galasaHome, timeService, env)

// Call to process the command in a unit-testable way.
err = properties.DeleteProperty(propertiesCmdValues.namespace, propertiesCmdValues.propertyName, apiClient)
var apiClient *galasaapi.APIClient
apiClient, err = auth.GetAuthenticatedAPIClient(apiServerUrl, fileSystem, galasaHome, timeService, env)
if err == nil {
// Call to process the command in a unit-testable way.
err = properties.DeleteProperty(propertiesCmdValues.namespace, propertiesCmdValues.propertyName, apiClient)
}
}
}
}
Expand Down
30 changes: 17 additions & 13 deletions pkg/cmd/propertiesGet.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/galasa-dev/cli/pkg/api"
"github.com/galasa-dev/cli/pkg/auth"
"github.com/galasa-dev/cli/pkg/galasaapi"
"github.com/galasa-dev/cli/pkg/properties"
"github.com/galasa-dev/cli/pkg/utils"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -165,19 +166,22 @@ func (cmd *PropertiesGetCommand) executePropertiesGet(
apiServerUrl := bootstrapData.ApiServerURL
log.Printf("The API server is at '%s'\n", apiServerUrl)

apiClient := auth.GetAuthenticatedAPIClient(apiServerUrl, fileSystem, galasaHome, timeService, env)

// Call to process the command in a unit-testable way.
err = properties.GetProperties(
propertiesCmdValues.namespace,
propertiesCmdValues.propertyName,
cmd.values.propertiesPrefix,
cmd.values.propertiesSuffix,
cmd.values.propertiesInfix,
apiClient,
cmd.values.propertiesOutputFormat,
console,
)
var apiClient *galasaapi.APIClient
apiClient, err = auth.GetAuthenticatedAPIClient(apiServerUrl, fileSystem, galasaHome, timeService, env)

if err == nil {
// Call to process the command in a unit-testable way.
err = properties.GetProperties(
propertiesCmdValues.namespace,
propertiesCmdValues.propertyName,
cmd.values.propertiesPrefix,
cmd.values.propertiesSuffix,
cmd.values.propertiesInfix,
apiClient,
cmd.values.propertiesOutputFormat,
console,
)
}
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions pkg/cmd/propertiesNamespaceGet.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/galasa-dev/cli/pkg/api"
"github.com/galasa-dev/cli/pkg/auth"
"github.com/galasa-dev/cli/pkg/galasaapi"
"github.com/galasa-dev/cli/pkg/properties"
"github.com/galasa-dev/cli/pkg/utils"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -136,10 +137,13 @@ func (cmd *PropertiesNamespaceGetCommand) executePropertiesNamespaceGet(
apiServerUrl := bootstrapData.ApiServerURL
log.Printf("The API server is at '%s'\n", apiServerUrl)

apiClient := auth.GetAuthenticatedAPIClient(apiServerUrl, fileSystem, galasaHome, timeService, env)
var apiClient *galasaapi.APIClient
apiClient, err = auth.GetAuthenticatedAPIClient(apiServerUrl, fileSystem, galasaHome, timeService, env)

// Call to process the command in a unit-testable way.
err = properties.GetPropertiesNamespaces(apiClient, cmd.values.namespaceOutputFormat, console)
if err == nil {
// Call to process the command in a unit-testable way.
err = properties.GetPropertiesNamespaces(apiClient, cmd.values.namespaceOutputFormat, console)
}
}
}
}
Expand Down
20 changes: 12 additions & 8 deletions pkg/cmd/propertiesSet.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/galasa-dev/cli/pkg/api"
"github.com/galasa-dev/cli/pkg/auth"
"github.com/galasa-dev/cli/pkg/galasaapi"
"github.com/galasa-dev/cli/pkg/properties"
"github.com/galasa-dev/cli/pkg/utils"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -138,14 +139,17 @@ func (cmd *PropertiesSetCommand) executePropertiesSet(
apiServerUrl := bootstrapData.ApiServerURL
log.Printf("The API server is at '%s'\n", apiServerUrl)

apiClient := auth.GetAuthenticatedAPIClient(apiServerUrl, fileSystem, galasaHome, timeService, env)

// Call to process the command in a unit-testable way.
err = properties.SetProperty(
propertiesCmdValues.namespace,
propertiesCmdValues.propertyName,
cmd.values.propertyValue,
apiClient)
var apiClient *galasaapi.APIClient
apiClient, err = auth.GetAuthenticatedAPIClient(apiServerUrl, fileSystem, galasaHome, timeService, env)

if err == nil {
// Call to process the command in a unit-testable way.
err = properties.SetProperty(
propertiesCmdValues.namespace,
propertiesCmdValues.propertyName,
cmd.values.propertyValue,
apiClient)
}
}
}
}
Expand Down
21 changes: 15 additions & 6 deletions pkg/cmd/resourcesApply.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"log"

"github.com/galasa-dev/cli/pkg/api"
"github.com/galasa-dev/cli/pkg/auth"
"github.com/galasa-dev/cli/pkg/resources"
"github.com/galasa-dev/cli/pkg/utils"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -126,12 +127,20 @@ func loadAndPassDataIntoResourcesApi(action string, factory Factory, resourcesCm
apiServerUrl := bootstrapData.ApiServerURL
log.Printf("The API server is at '%s'\n", apiServerUrl)

err = resources.ApplyResources(
action,
resourcesCmdValues.filePath,
fileSystem,
apiServerUrl,
)
timeService := factory.GetTimeService()

var bearerToken string
bearerToken, err = auth.GetBearerToken(apiServerUrl, fileSystem, galasaHome, timeService, env)

if err == nil {
err = resources.ApplyResources(
action,
resourcesCmdValues.filePath,
fileSystem,
apiServerUrl,
bearerToken,
)
}
}

}
Expand Down
24 changes: 14 additions & 10 deletions pkg/cmd/runsCancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/galasa-dev/cli/pkg/api"
"github.com/galasa-dev/cli/pkg/auth"
"github.com/galasa-dev/cli/pkg/galasaapi"
"github.com/galasa-dev/cli/pkg/runs"
"github.com/galasa-dev/cli/pkg/utils"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -130,16 +131,19 @@ func (cmd *RunsCancelCommand) executeCancel(
apiServerUrl := bootstrapData.ApiServerURL
log.Printf("The API Server is at '%s'\n", apiServerUrl)

apiClient := auth.GetAuthenticatedAPIClient(apiServerUrl, fileSystem, galasaHome, timeService, env)

// Call to process command in unit-testable way.
err = runs.CancelRun(
cmd.values.runName,
timeService,
console,
apiServerUrl,
apiClient,
)
var apiClient *galasaapi.APIClient
apiClient, err = auth.GetAuthenticatedAPIClient(apiServerUrl, fileSystem, galasaHome, timeService, env)

if err == nil {
// Call to process command in unit-testable way.
err = runs.CancelRun(
cmd.values.runName,
timeService,
console,
apiServerUrl,
apiClient,
)
}
}
}
}
Expand Down
28 changes: 16 additions & 12 deletions pkg/cmd/runsDownload.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/galasa-dev/cli/pkg/api"
"github.com/galasa-dev/cli/pkg/auth"
"github.com/galasa-dev/cli/pkg/galasaapi"
"github.com/galasa-dev/cli/pkg/runs"
"github.com/galasa-dev/cli/pkg/utils"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -138,18 +139,21 @@ func (cmd *RunsDownloadCommand) executeRunsDownload(
apiServerUrl := bootstrapData.ApiServerURL
log.Printf("The API server is at '%s'\n", apiServerUrl)

apiClient := auth.GetAuthenticatedAPIClient(apiServerUrl, fileSystem, galasaHome, timeService, env)

// Call to process the command in a unit-testable way.
err = runs.DownloadArtifacts(
cmd.values.runNameDownload,
cmd.values.runForceDownload,
fileSystem,
timeService,
console,
apiClient,
cmd.values.runDownloadTargetFolder,
)
var apiClient *galasaapi.APIClient
apiClient, err = auth.GetAuthenticatedAPIClient(apiServerUrl, fileSystem, galasaHome, timeService, env)

if err == nil {
// Call to process the command in a unit-testable way.
err = runs.DownloadArtifacts(
cmd.values.runNameDownload,
cmd.values.runForceDownload,
fileSystem,
timeService,
console,
apiClient,
cmd.values.runDownloadTargetFolder,
)
}
}
}
}
Expand Down
Loading