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

Running local test creates excessive image rendering. #222

Merged
merged 5 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
68 changes: 26 additions & 42 deletions build-locally.sh
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ function launch_test_on_ecosystem {
cmd="${BASEDIR}/bin/${galasactl_command} runs submit \
--bootstrap $GALASA_BOOTSTRAP \
--class dev.galasa.example.banking.account/dev.galasa.example.banking.account.TestAccountExtended \
--class dev.galasa.example.banking.account/dev.galasa.example.banking.account.TestAccount \
--log -"

info "Command is: $cmd"
Expand Down Expand Up @@ -483,19 +484,33 @@ function generate_galasactl_documentation {


#--------------------------------------------------------------------------
# Run test using the galasactl locally in a JVM
function submit_local_test {
function check_artifact_saved_in_ras {
# The extended type of test saves an artifact into the RAS store
# The artifact has "Hello Galasa !" inside.
# Lets check the RAS to make sure that file is present.
expected_string_in_test_artifact="Hello Galasa \!"
grep -R "$expected_string_in_test_artifact" $GALASA_HOME/ras > /dev/null
rc=$?
if [[ "${rc}" != "0" ]]; then
error "Failed to find the string \'$expected_string_in_test_artifact\" in RAS. Test case should have generated it."
exit 1
fi
success "Confirmed that test case saved a test artifact to RAS"
}

function run_test_locally_using_galasactl {
export LOG_FILE=$1

h2 "Submitting a local test using galasactl in a local JVM"
h2 "Submitting 2 local tests using galasactl in a local JVM"

cd ${BASEDIR}/temp/*banking

BUNDLE=$1
JAVA_CLASS=$2
OBR_GROUP_ID=$3
OBR_ARTIFACT_ID=$4
OBR_VERSION=$5
LOG_FILE=$6
BUNDLE=dev.galasa.example.banking.payee
JAVA_CLASS=dev.galasa.example.banking.payee.TestPayeeExtended
JAVA_CLASS_2=dev.galasa.example.banking.payee.TestPayee
OBR_GROUP_ID=dev.galasa.example.banking
OBR_ARTIFACT_ID=dev.galasa.example.banking.obr
OBR_VERSION=0.0.1-SNAPSHOT

# Could get this bootjar from https://development.galasa.dev/main/maven-repo/obr/dev/galasa/galasa-boot/
read_boot_jar_version
Expand All @@ -516,6 +531,7 @@ function submit_local_test {
cmd="${BASEDIR}/bin/${galasactl_command} runs submit local \
--obr mvn:${OBR_GROUP_ID}/${OBR_ARTIFACT_ID}/${OBR_VERSION}/obr \
--class ${BUNDLE}/${JAVA_CLASS} \
--class ${BUNDLE}/${JAVA_CLASS_2} \
--remoteMaven ${REMOTE_MAVEN} \
--throttle 1 \
--requesttype MikeCLI \
Expand All @@ -532,40 +548,8 @@ function submit_local_test {

info "Command is ${cmd}"
$cmd
rc=$?
if [[ "${rc}" != "0" ]]; then
error "Failed to run the test. See details in log file ${LOG_FILE}"
exit 1
fi
rc=$? ; if [[ "${rc}" != "0" ]]; then error "Failed to run the test. See details in log file ${LOG_FILE}" ; exit 1 ; fi
success "Test ran OK"
}

function check_artifact_saved_in_ras {
# The extended type of test saves an artifact into the RAS store
# The artifact has "Hello Galasa !" inside.
# Lets check the RAS to make sure that file is present.
expected_string_in_test_artifact="Hello Galasa \!"
grep -R "$expected_string_in_test_artifact" $GALASA_HOME/ras > /dev/null
rc=$?
if [[ "${rc}" != "0" ]]; then
error "Failed to find the string \'$expected_string_in_test_artifact\" in RAS. Test case should have generated it."
exit 1
fi
success "Confirmed that test case saved a test artifact to RAS"
}

function run_test_locally_using_galasactl {
export LOG_FILE=$1

# Run the Payee tests.
export TEST_BUNDLE=dev.galasa.example.banking.payee
export TEST_JAVA_CLASS=dev.galasa.example.banking.payee.TestPayeeExtended
export TEST_OBR_GROUP_ID=dev.galasa.example.banking
export TEST_OBR_ARTIFACT_ID=dev.galasa.example.banking.obr
export TEST_OBR_VERSION=0.0.1-SNAPSHOT


submit_local_test $TEST_BUNDLE $TEST_JAVA_CLASS $TEST_OBR_GROUP_ID $TEST_OBR_ARTIFACT_ID $TEST_OBR_VERSION $LOG_FILE

check_artifact_saved_in_ras
}
Expand Down
1 change: 1 addition & 0 deletions docs/generated/errors-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ The `galasactl` tool can generate the following errors:
- GAL1140E: The namespace, '{}', provided does not match formatting requirements. The namespace must start with a character in the 'a-z' range, followed by characters in the 'a'-'z' or '0'-'9' ranges only.
- GAL1141E: Unable to compile the regex pattern for Galasa Property field '{}'. Reason: '{}'
- GAL1142E: The {} field value, '{}', provided does not match formatting requirements. The {} field value must start with a character in the 'a-z' or 'A-Z' range, followed by any characters in the 'a'-'z', 'A'-'Z', '0'-'9', '.' (period), '-' (dash) or '_' (underscore) ranges only.
- GAL1143E: Could not query run results. Server returned a non-200 code ({})
- GAL1225E: Failed to open file '{}' cause: {}. Check that this file exists, and that you have read permissions.
- GAL1226E: Internal failure. Contents of gzip could be read, but not decoded. New gzip reader failed: file: {} error: {}
- GAL1227E: Internal failure. Contents of gzip could not be decoded. {} error: {}
Expand Down
6 changes: 3 additions & 3 deletions pkg/auth/authProperties.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

const (
TOKEN_PROPERTY = "GALASA_TOKEN"
TOKEN_PROPERTY = "GALASA_TOKEN"
TOKEN_SEPARATOR = ":"
)

Expand All @@ -32,7 +32,7 @@ func GetAuthProperties(fileSystem files.FileSystem, galasaHome utils.GalasaHome,
galasactlPropertiesFilePath := filepath.Join(galasaHome.GetNativeFolderPath(), "galasactl.properties")

// Get the file-based token property if we can
tokenProperty, fileAccessErr := getPropertyFromFile(fileSystem, galasactlPropertiesFilePath, env, TOKEN_PROPERTY)
tokenProperty, fileAccessErr := getPropertyFromFile(fileSystem, galasactlPropertiesFilePath, TOKEN_PROPERTY)

// Over-write the token property value if there is an environment variable set to do that.
tokenProperty = getPropertyWithOverride(env, tokenProperty, galasactlPropertiesFilePath, TOKEN_PROPERTY)
Expand Down Expand Up @@ -85,7 +85,7 @@ func getPropertyWithOverride(env utils.Environment, valueFromFile string, filePa
}

// Gets a property from the user's galasactl.properties file
func getPropertyFromFile(fileSystem files.FileSystem, galasactlPropertiesFilePath string, env utils.Environment, propertyName string) (string, error) {
func getPropertyFromFile(fileSystem files.FileSystem, galasactlPropertiesFilePath string, propertyName string) (string, error) {
var err error = nil
var galasactlProperties props.JavaProperties
galasactlProperties, err = props.ReadPropertiesFile(fileSystem, galasactlPropertiesFilePath)
Expand Down
117 changes: 59 additions & 58 deletions pkg/auth/bearerTokenFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,84 +19,85 @@ import (
)

type BearerTokenJson struct {
Jwt string `json:"jwt"`
Jwt string `json:"jwt"`
}

const (
TOKEN_EXPIRY_BUFFER_MINUTES = 10
TOKEN_EXPIRY_BUFFER_MINUTES = 10
)

// Writes a new bearer-token.json file containing a JWT in the following format:
// {
// "jwt": "<bearer-token-here>"
// }
//
// {
// "jwt": "<bearer-token-here>"
// }
func WriteBearerTokenJsonFile(fileSystem files.FileSystem, galasaHome utils.GalasaHome, jwt string) error {
bearerTokenFilePath := filepath.Join(galasaHome.GetNativeFolderPath(), "bearer-token.json")
bearerTokenFilePath := filepath.Join(galasaHome.GetNativeFolderPath(), "bearer-token.json")

log.Printf("Writing bearer token to file '%s'", bearerTokenFilePath)
err := fileSystem.WriteTextFile(bearerTokenFilePath, jwt)
log.Printf("Writing bearer token to file '%s'", bearerTokenFilePath)
err := fileSystem.WriteTextFile(bearerTokenFilePath, jwt)

if err == nil {
log.Printf("Written bearer token to file '%s' OK", bearerTokenFilePath)
} else {
log.Printf("Failed to write bearer token file '%s'", bearerTokenFilePath)
err = galasaErrors.NewGalasaError(galasaErrors.GALASA_ERROR_FAILED_TO_WRITE_FILE, bearerTokenFilePath, err.Error())
}
return err
if err == nil {
log.Printf("Written bearer token to file '%s' OK", bearerTokenFilePath)
} else {
log.Printf("Failed to write bearer token file '%s'", bearerTokenFilePath)
err = galasaErrors.NewGalasaError(galasaErrors.GALASA_ERROR_FAILED_TO_WRITE_FILE, bearerTokenFilePath, err.Error())
}
return err
}

// Gets the JWT from the bearer-token.json file if it exists, errors if the file does not exist or if the token is invalid
func GetBearerTokenFromTokenJsonFile(fileSystem files.FileSystem, galasaHome utils.GalasaHome, timeService utils.TimeService) (string, error) {
var err error = nil
var bearerToken string = ""
var bearerTokenJsonContents string = ""
var err error = nil
var bearerToken string = ""
var bearerTokenJsonContents string = ""

bearerTokenFilePath := filepath.Join(galasaHome.GetNativeFolderPath(), "bearer-token.json")
bearerTokenFilePath := filepath.Join(galasaHome.GetNativeFolderPath(), "bearer-token.json")

log.Printf("Retrieving bearer token from file '%s'", bearerTokenFilePath)
bearerTokenJsonContents, err = fileSystem.ReadTextFile(bearerTokenFilePath)
if err == nil {
var bearerTokenJson BearerTokenJson
err = json.Unmarshal([]byte(bearerTokenJsonContents), &bearerTokenJson)
if err == nil {
bearerToken = bearerTokenJson.Jwt
log.Printf("Retrieved bearer token from file '%s' OK", bearerTokenFilePath)
}
}
log.Printf("Retrieving bearer token from file '%s'", bearerTokenFilePath)
bearerTokenJsonContents, err = fileSystem.ReadTextFile(bearerTokenFilePath)
if err == nil {
var bearerTokenJson BearerTokenJson
err = json.Unmarshal([]byte(bearerTokenJsonContents), &bearerTokenJson)
if err == nil {
bearerToken = bearerTokenJson.Jwt
log.Printf("Retrieved bearer token from file '%s' OK", bearerTokenFilePath)
}
}

if err != nil {
log.Printf("Could not retrieve bearer token from file '%s'", bearerTokenFilePath)
err = galasaErrors.NewGalasaError(galasaErrors.GALASA_ERROR_RETRIEVING_BEARER_TOKEN_FROM_FILE, bearerTokenFilePath, err.Error())
} else {
log.Printf("Validating bearer token retrieved from file '%s'", bearerTokenFilePath)
if (!IsBearerTokenValid(bearerToken, timeService)) {
err = galasaErrors.NewGalasaError(galasaErrors.GALASA_ERROR_INVALID_BEARER_TOKEN)
} else {
log.Printf("Validated bearer token retrieved from file '%s' OK", bearerTokenFilePath)
}
}
if err != nil {
log.Printf("Could not retrieve bearer token from file '%s'", bearerTokenFilePath)
err = galasaErrors.NewGalasaError(galasaErrors.GALASA_ERROR_RETRIEVING_BEARER_TOKEN_FROM_FILE, bearerTokenFilePath, err.Error())
} else {
log.Printf("Validating bearer token retrieved from file '%s'", bearerTokenFilePath)
if !IsBearerTokenValid(bearerToken, timeService) {
err = galasaErrors.NewGalasaError(galasaErrors.GALASA_ERROR_INVALID_BEARER_TOKEN)
} else {
log.Printf("Validated bearer token retrieved from file '%s' OK", bearerTokenFilePath)
}
}

return bearerToken, err
return bearerToken, err
}

// Checks whether a given bearer token is valid or not, returning true if it is valid and false otherwise
func IsBearerTokenValid(bearerTokenString string, timeService utils.TimeService) bool {
var err error = nil
var bearerToken *jwt.Token
var err error = nil
var bearerToken *jwt.Token

// Decode the bearer token without verifying its signature
bearerToken, _, err = jwt.NewParser().ParseUnverified(bearerTokenString, jwt.MapClaims{})
if err == nil {
var tokenExpiry *jwt.NumericDate
tokenExpiry, err = bearerToken.Claims.GetExpirationTime()
if err == nil {
// Add a buffer to the current time to make sure the bearer token does not expire within
// this buffer (e.g. if the buffer is 10 mins, make sure the token doesn't expire within 10 mins)
acceptableExpiryTime := timeService.Now().Add(time.Duration(TOKEN_EXPIRY_BUFFER_MINUTES) * time.Minute)
if ((tokenExpiry.Time).After(acceptableExpiryTime)) {
return true
}
}
}
return false
// Decode the bearer token without verifying its signature
bearerToken, _, err = jwt.NewParser().ParseUnverified(bearerTokenString, jwt.MapClaims{})
if err == nil {
var tokenExpiry *jwt.NumericDate
tokenExpiry, err = bearerToken.Claims.GetExpirationTime()
if err == nil {
// Add a buffer to the current time to make sure the bearer token does not expire within
// this buffer (e.g. if the buffer is 10 mins, make sure the token doesn't expire within 10 mins)
acceptableExpiryTime := timeService.Now().Add(time.Duration(TOKEN_EXPIRY_BUFFER_MINUTES) * time.Minute)
if (tokenExpiry.Time).After(acceptableExpiryTime) {
return true
}
}
}
return false
}
Loading