Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #113 from galasa-dev/mcobbett-auth-conditional-on-…
Browse files Browse the repository at this point in the history
…bootstrap-prop

maven plugin deploys test catalog with or without a galasa.token
  • Loading branch information
techcobweb authored Mar 8, 2024
2 parents 8ddf07c + 27102a3 commit 86b6c88
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 53 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ Phase: `deploy`

Input Parameters/Properties:
- `galasa.test.stream` required. A string.
- `galasa.token` required. An access token for the galasa ecosystem.
- `galasa.token` optional. An access token for the galasa ecosystem, if that ecosytem is using authentication.
- `galasa.bootstrap` required. A URL to the ecosystem.
- `galasa.skip.bundletestcatalog` optional. A boolean.
- `galasa.skip.deploytestcatalog` optional. A boolean.
- `galasa.skip.bundletestcatalog` optional. A boolean. If set to true, the test catalog is not deployed to the Galasa server.
- `galasa.skip.deploytestcatalog` optional. A boolean. If set to true, the test catalog is not deployed to the Galasa server.

For example:
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ public class DeployTestCatalog extends AbstractMojo {
@Parameter(defaultValue = "${galasa.skip.bundletestcatatlog}", readonly = true, required = false)
public boolean skipBundleTestCatalogOldSpelling;

@Parameter(defaultValue = "${galasa.skip.bundletestcatalog}", name="skip" , readonly = true, required = false)
@Parameter(defaultValue = "${galasa.skip.bundletestcatalog}", readonly = true, required = false)
public boolean skipBundleTestCatalog;

// This spelling of the property is old/wrong/deprecated.
@Parameter(defaultValue = "${galasa.skip.deploytestcatatlog}" , readonly = true, required = false)
public boolean skipDeployTestCatalogOldSpelling;

@Parameter(defaultValue = "${galasa.skip.deploytestcatalog}", name="skipDeploy" , readonly = true, required = false)
@Parameter(defaultValue = "${galasa.skip.deploytestcatalog}", readonly = true, required = false)
public boolean skipDeployTestCatalog;

// A protected variable so we can inject a mock factory if needed during unit testing.
Expand Down Expand Up @@ -100,23 +100,16 @@ public void execute() throws MojoExecutionException, MojoFailureException {

URL testcatalogUrl = calculateTestCatalogUrl(bootstrapProperties, this.testStream, this.bootstrapUrl);

checkGalasaAccessTokenIsValid(this.galasaAccessToken);

String jwt = getAuthenticatedJwt(this.authFactory, this.galasaAccessToken, this.bootstrapUrl) ;
String jwt = null ;
// For now, if no galasa token is supplied, that's ok. It's optional.
// If no galasa access token supplied by the user, the jwt will stay as null.
if ( (this.galasaAccessToken!=null) && (!this.galasaAccessToken.isEmpty()) ) {
jwt = getAuthenticatedJwt(this.authFactory, this.galasaAccessToken, this.bootstrapUrl) ;
}

publishTestCatalogToGalasaServer(testcatalogUrl,jwt, testCatalogArtifact);
}

private void checkGalasaAccessTokenIsValid(String galasaAccessToken) throws MojoExecutionException {
if (galasaAccessToken==null || galasaAccessToken.isEmpty()) {
String msg = "No Galasa authentication token supplied. Set the galasa.token property."+
" The token is required to communicate with the Galasa server."+
" Obtain a personal access token using the Galasa web user interface for the ecosystem you are trying to publish the test catalog to.";
getLog().error(msg);
throw new MojoExecutionException(msg);
}
}

private Artifact getTestCatalogArtifact() {
Artifact artifact = null;
for (Artifact a : project.getAttachedArtifacts()) {
Expand Down Expand Up @@ -164,7 +157,13 @@ private void postTestCatalogToGalasaServer(HttpURLConnection conn, URL testCatal
conn.addRequestProperty("Content-Type", "application/json");
conn.addRequestProperty("Accept", "application/json");

conn.addRequestProperty("Authorization", "Bearer "+jwt);
// Only add the jwt header if we have a jwt value.
if (jwt == null) {
getLog().info("Not sending a JWT bearer token to the server, as the galasa.token property was not supplied.");
} else {
conn.addRequestProperty("Authorization", "Bearer "+jwt);
}

conn.addRequestProperty("ClientApiVersion","0.32.0"); // The version of the API we coded this against.

FileUtils.copyFile(testCatalogArtifact.getFile(), conn.getOutputStream());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import static org.assertj.core.api.Assertions.*;
import org.junit.Test;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;

public class DeployTestCatalogTest {
Expand Down Expand Up @@ -254,39 +253,7 @@ public void TestCalculateTestCatalogUrlSpotsBootstrapUrlWhichDoesntEndInBootstra
assertThat(ex).hasMessageContaining("Unable to calculate the test catalog url, the bootstrap url does not end with /bootstrap, need a framework.testcatalog.url property in the bootstrap");
}

@SuppressWarnings("deprecation")
@Test
public void TestFailsIfNoGalasaAccessToken() throws Exception {
DeployTestCatalog command = new DeployTestCatalog();
MockLog mockLog = new MockLog();
command.setLog(mockLog);

command.testStream = "myTestStream";
command.bootstrapUrl = new URL("http://myBootstrapUrl/bootstrap");

MavenProject project = new MavenProject();
project.setPackaging("galasa-obr");
command.project = project;

MockArtifact testCatalogArtifact = new MockArtifact();
project.addAttachedArtifact(testCatalogArtifact);
testCatalogArtifact.type = "json";
testCatalogArtifact.classifier = "testcatalog";

// Set a mock boostrap loader...
command.bootstrapLoader = new BootstrapLoader() {
@Override
public Properties getBootstrapProperties(URL bootstrapUrl, Log log) throws MojoExecutionException {
return new Properties();
}
};

Exception ex = catchException( ()-> command.execute() );
assertThat(ex).isInstanceOf(MojoExecutionException.class);
assertThat(ex).hasMessageContaining("No Galasa authentication token supplied. Set the galasa.token property. The token is required to communicate with the Galasa server");

}


// This is my exploration unit test.
//
// The unit tests are not yet complete, as they don't test the last piece where the test catalog file is
Expand Down

0 comments on commit 86b6c88

Please sign in to comment.